25 November 2020

1 工具安装

Clojure基于Java虚拟机,ClojureScript基于Javascript。而GraalVM1囊括了JVM和JS。因此安装GraalVM。Leiningen2用来管理项目代码。

1.1 Windows

1.1.1 准备

  • 下载 setup-x86_64.exe 3 ,点击运行,一直下一步,到 Select PackagesSearch 处查找以下包,并选择最新版本安装:
    • git
    • emacs-w32
  • 下载安装 7-zip 4C:\Program Files\7-Zip

1.1.2 安装

  • 打开 mintty.exe ,通常装好 cygwin 后,会自动添加到桌面
  • 以下安装脚本,可以复制后,以 Shift-INS 粘贴到 mintty.exe
# https://www.graalvm.org/ with JDK and Node.js
export GRAALVM_VERSION=20.3.0
export GRAALVM_PACKAGE=graalvm-ce-java11-windows-amd64-$GRAALVM_VERSION
export GRAALVM_LIB=graalvm-ce-java11-$GRAALVM_VERSION
export GRAALVM_GITHUB=https://github.com/graalvm/graalvm-ce-builds/releases/download
wget $GRAALVM_GITHUB/vm-$GRAALVM_VERSION/$GRAALVM_PACKAGE.zip
# 用7zip解压下载到的文件
"/cygdrive/C/Program Files/7-Zip/7z.exe" x $GRAALVM_PACKAGE.zip
mv $GRAALVM_LIB /usr/local/lib
echo "export PATH=/usr/local/lib/$GRAALVM_LIB/bin:$PATH" >> ~/.bash_profile
echo "export JAVA_HOME=/usr/local/lib/$GRAALVM_LIB" >> ~/.bash_profile
source ~/.bash_profile
npm.cmd config set registry https://registry.npm.taobao.org

# https://leiningen.org/
wget https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein
chmod +x lein
mv lein /usr/local/bin
lein

1.1.3 测试

$ java -version
openjdk version "11.0.9" 2020-10-20
OpenJDK Runtime Environment GraalVM CE 20.3.0 (build 11.0.9+10-jvmci-20.3-b06)
OpenJDK 64-Bit Server VM GraalVM CE 20.3.0 (build 11.0.9+10-jvmci-20.3-b06, mixed mode, sharing)

$ npm.cmd

Usage: npm <command>
...
npm@6.14.6 C:\cygwin64\usr\local\lib\graalvm-ce-java11-20.3.0\languages\js\npm

$ lein -v
Leiningen 2.9.3 on Java 11.0.9 OpenJDK 64-Bit Server VM

1.1.4 升级(非必需)

npm.cmd install -g --force npm@latest

1.2 Linux

1.2.1 准备

For Ubuntu

sudo snap install emacs --channel=latest/edge --classic

1.2.2 安装

# https://www.graalvm.org/ with JDK and Node.js
export GRAALVM_VERSION=20.3.0
export GRAALVM_PACKAGE=graalvm-ce-java11-linux-amd64-$GRAALVM_VERSION
export GRAALVM_LIB=graalvm-ce-java11-$GRAALVM_VERSION
export GRAALVM_GITHUB=https://github.com/graalvm/graalvm-ce-builds/releases/download
wget $GRAALVM_GITHUB/vm-$GRAALVM_VERSION/$GRAALVM_PACKAGE.tar.gz
tar -xvzf $GRAALVM_PACKAGE.tar.gz
sudo mv $GRAALVM_LIB /usr/local/lib
echo "export PATH=/usr/local/lib/$GRAALVM_LIB/bin:$PATH" >> ~/.bashrc
echo "export JAVA_HOME=/usr/local/lib/$GRAALVM_LIB" >> ~/.bashrc
source ~/.bashrc
npm config set registry https://registry.npm.taobao.org

# https://leiningen.org/
wget https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein
chmod +x lein
sudo mv lein /usr/local/bin
lein

1.2.3 测试

$ java --version
openjdk 11.0.9 2020-10-20
OpenJDK Runtime Environment GraalVM CE 20.3.0 (build 11.0.9+10-jvmci-20.3-b06)
OpenJDK 64-Bit Server VM GraalVM CE 20.3.0 (build 11.0.9+10-jvmci-20.3-b06, mixed mode, sharing)

$ npm
Usage: npm <command>
...
npm@6.14.6 /usr/local/lib/graalvm-ce-java11-20.3.0/languages/js/npm

$ lein -v
Leiningen 2.9.4 on Java 11.0.9 OpenJDK 64-Bit Server VM

1.2.4 升级(非必需)

npm install -g --force npm@latest

1.3 macOS

1.3.1 准备

brew cask install emacs

1.3.2 安装

# https://www.graalvm.org/ with JDK and Node.js
export GRAALVM_VERSION=20.3.0
export GRAALVM_PACKAGE=graalvm-ce-java11-darwin-amd64-$GRAALVM_VERSION
export GRAALVM_LIB=graalvm-ce-java11-$GRAALVM_VERSION
export GRAALVM_GITHUB=https://github.com/graalvm/graalvm-ce-builds/releases/download
curl -L $GRAALVM_GITHUB/vm-$GRAALVM_VERSION/$GRAALVM_PACKAGE.tar.gz \
     -o $GRAALVM_PACKAGE.tar.gz
tar -xvzf $GRAALVM_PACKAGE.tar.gz
sudo mv $GRAALVM_LIB /Library/Java/JavaVirtualMachines/
echo "export PATH=/Library/Java/JavaVirtualMachines/$GRAALVM_LIB/Contents/Home/bin:$PATH" \
     >> ~/.bash_profile
echo "export JAVA_HOME=/Library/Java/JavaVirtualMachines/$GRAALVM_LIB/Contents/Home" \
     >> ~/.bash_profile
source ~/.bash_profile
npm config set registry https://registry.npm.taobao.org

# https://leiningen.org/
wget https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein
chmod +x lein
sudo mv lein /usr/local/bin
lein

1.3.3 测试

$ java --version
openjdk version "11.0.9" 2020-10-20
OpenJDK Runtime Environment GraalVM CE 20.3.0 (build 11.0.9+10-jvmci-20.3-b06)
OpenJDK 64-Bit Server VM GraalVM CE 20.3.0 (build 11.0.9+10-jvmci-20.3-b06, mixed mode, sharing)
$ npm
Usage: npm <command>
...
npm@6.14.6 /Library/Java/JavaVirtualMachines/graalvm-ce-java11-20.3.0/Contents/Home/languages/js/npm
$ lein -v
Leiningen 2.9.4 on Java 11.0.9 OpenJDK 64-Bit Server VM

2 开发IDE

下载并安装YaheiInconsolata:https://github.com/loseblue/yaheiInconsolata.ttf

安装配置Emacs:

cd ~ && \
mv .emacs .emacs-backup && mv .emacs.d .emacs.d-backup && \
git clone https://github.com/kimim/kimim-emacs && \
cp kimim-emacs/.emacs ~
emacs

3 示例

打开 mintty.exe

mkdir workspace && cd workspace
git clone https://github.com/kimim/vorstellung
cd vorstellung
cp dev-config.edn.sample.edn dev-config.edn
lein run

再打开一个 mintty.exe

cd workspace/vorstellung
lein shadow watch app

等看到以下信息:

[:app] Build completed. (6353 files, 0 compiled, 0 warnings, 39.88s)

网页打开:http://localhost:3000 可以看到程序运行起来了。

切换到Emacs,测试clj repl:

按住Ctrl不放,再连续按x f
输入 ~/workspace/vorstellung/src/clj/vorstellung/config.clj
敲Enter
ALT+SHIFT+>
敲Enter 2次
Ctrl+c Alt+c
选择localhost,敲Enter
输入7070,敲Enter
跳出clj repl新窗口后,按F2切回到代码
输入:
(env :nrepl-port)
Ctrl-c Ctrl-c
能够看到7070,即REPL的端口
Ctrl-a Ctrl-k

继续在Emacs中,测试cljs repl:

按住Ctrl不放,再连续按x f
输入 ~/workspace/vorstellung/src/cljs/vorstellung/core.cljs
敲Enter
ALT+SHIFT+>
敲Enter 2次
Ctrl+c Alt+Shift+c
跳出cljs repl新窗口,按F2切回到代码
输入:
(js/alert "cool")
Ctrl-c Ctrl-c
能够看到浏览器中跳出一个警报框
Ctrl-a Ctrl-k

4 Well done!

至此,你已经能够运行一个基于Luminus Web框架5、Material-UI6的Web App,并在Emacs中编写Clojure和ClojureScript代码,并通过repl实时运行代码了。