(注意)この記事はもうとても古いです。スクリプトとして Clojure を使うなら https://babashka.org/ などをおすすめします。
MacでClojureを手軽に始める&スクリプト的に使う方法として、inleinとplanckを紹介します。1
また、sublime-text3から実行できるように設定します。
inlein & planck
inlein
は、Clojureの実行環境です。2
Clojureは、JVM上で動くLispライクな言語です。
Clojureのビルドツールとして普及しているleiningen
というツールでは、依存ライブラリの設定をproject.clj
などの特定の設定ファイルに記述します。
しかし、inlien
では、依存ライブラリの設定をスクリプト中にインラインで記述することができます。依存の書き方はleiningen
と互換のある構文です。
planck
は、OS-X用のClojureScriptの実行環境です。3
ClojureScriptは、JavascriptにコンパイルされるClojureライクな言語です。
ClojureScriptのビルド&実行ツールとしては、leiningen
やboot
が一般的なようです(よく知りません・・)。planck
は、inlein
のように依存をスクリプト中にインラインで書けるわけではないのですが、環境構築と実行が簡単にできるのが嬉しい点です。
ここでは一緒に紹介してますが、2つは全く別々に開発されたツールで、開発者も異なります。
inlein (clojure)
setup inlien
インストールの方法は公式サイトの通りです。
JDKのバージョン7以上が入ってる必要があります。
java -version
で確認してください。
https://github.com/hyPiRion/inlein/releases/latest からinlein
のバイナリ・ファイルをDLします。
curl -L -O https://github.com/hyPiRion/inlein/releases/download/0.1.0/inlein
実行権限を付与します
chmod a+x ./inlein
パスの通っている場所に配置します(自分は~/bin/inlein
に置いています)。
mv ./inlein ~/bin/
inlein
コマンドを叩きます。
最初に実行するタイミングは必要なファイルを自動でDLします。
少し時間がかかるかもしれません。
use inlein
使ってみます。以下のようなファイルを用意して、
#!/usr/bin/env inlein
'{:dependencies [[org.clojure/clojure "1.8.0"]]}
(require '[clojure.string :as s])
(defn greet [s]
(println "hello," s))
(if-let [args *command-line-args*]
(greet (s/join ", " args))
(greet "Clojure with inlein"))
実行します。
$ inlein script.clj world
hello, world
ファイルに実行権限を与えて、shebang(#!/bin/sh
のようなファイル先頭に書くランタイム指定)を書けば直接実行することもできます。
$ chmod a+x ./script.clj
$ ./script.clj world
hello, world
詳しい使い方はinlein --help
で確認できます。
$ inlein --help
inlein is a tool to handle Clojure scripts with dependencies
Usage: inlein [--run] file [args...]
(to run a clojure script)
or inlein --task [args...]
(to run an inlein task)
Several tasks are available:
--deps Retrieves the dependencies for a script
--help Prints this banner, or extended information about a task.
--ping Pings the inlein server, if it runs.
--restart-daemon Restarts the inlein daemon
--run Runs a clojure script with dependencies
--sh-cmd Prints the shell command a clojure script with dependencies will use
--shutdown-daemon Shuts off the Inlein daemon
--start-daemon Starts the inlein daemon
--upgrade Upgrades to the specified inlein version, or latest
--version Prints the currently running Inlein version.
planck (clojurescript)
setup planck
brew install planck
でOKです。
簡単!嬉しい!
Homebrewが入っていない場合は、先にインストールしておいてください。
既にplanck
がHomebrewでインストールされていて、最新に更新する場合は、
brew update
brew upgrade planck
use planck
引数なしで実行すると、REPLが立ち上がります。
$ planck
Planck 1.12
ClojureScript 1.8.51
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Exit: Control+D or :cljs/quit or exit or quit
Results: Stored in vars *1, *2, *3, an exception in *e
cljs.user=> "abc"
"abc"
cljs.user=> (js/Date.)
#inst "2016-05-23T14:36:27.631-00:00"
cljs.user=> (println *1 *2)
#inst "2016-05-23T14:36:27.631-00:00" abc
nil
cljs.user=> quit
第1引数にファイル名を渡してスクリプトとして実行してみます。
以下のようなファイルを用意して
#!/usr/bin/env planck
(ns example.planck
(:require [planck.core :refer [*command-line-args*]]
[clojure.string :as s]))
(defn greet [s]
(println "hello," s))
(if-let [args *command-line-args*]
(greet (s/join ", " args))
(greet "ClojureScript with planck"))
実行します。
$ planck ./script.cljs world
hello, world
inlein
同様、ファイルに実行権限を与えて、shebangを書けば直接実行することもできます。
$ chmod a+x ./script.cljs
$ ./script.cljs world
hello, world
詳しい使い方は、planck -h
で確認できます。
$ planck -h
Planck 1.12
Usage: planck [init-opt*] [main-opt] [arg*]
With no options or args, runs an interactive Read-Eval-Print Loop
init options:
-i path, --init=path Load a file or resource
-e string, --eval=string Evaluate expressions in string; print non-nil
values
-c cp, --classpath=cp Use colon-delimited cp for source directories and
JARs
-K, --auto-cache Create and use .planck_cache dir for cache
-k path, --cache=path If dir exists at path, use it for cache
-q, --quiet Quiet mode
-v, --verbose Emit verbose diagnostic output
-d, --dumb-terminal Disable line editing / VT100 terminal control
-t theme, --theme=theme Set the color theme
-n x, --socket-repl=x Enable socket REPL where x is port or IP:port
-s, --static-fns Generate static dispatch function calls
-a, --elide-asserts Set *assert* to false to remove asserts
(以下省略)
sublime-text
sublime-text3から実行できるようにしてみます。
sublime-text3で実行環境を構築できると何が嬉しいか。
これからClojure(ClojureScript)を触る人が簡単に始められるというのもありますが、
チームのみんながemacsやIntelliJを使っていなくても布教展開しやすくなるという側面もあります。
setup sublime-text for inlein
「Sublime Text -> Preferences -> Browse Packages」で開いたフォルダにClojure.sublime-build
というファイルで以下の内容を保存します。
通常は$HOME/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/Clojure.sublime-build
になると思います。
{
"cmd": "inlein --run $file",
"selector": "source.clojure",
"file_regex": ".+\\(([^:]*):([0-9]*):?([0-9]*)\\)$",
"shell": true,
"path": "$HOME/bin:$PATH",
}
さっきのscript.clj
をsublime-textで開いて、「Tools -> Build」もしくはcommand + b
ショートカットを実行します。
コンソールが開いて
hello, ClojureScript with planck
[Finished in 0.9s]
のようなのが表示されればOK。
setup sublime-text for planck
同様に「Sublime Text -> Preferences -> Browse Packages」で開いたフォルダにclojurescript.sublime-build
というファイルで以下の内容を保存します。
通常は$HOME/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/clojurescript.sublime-build
になると思います。
{
"cmd": ["planck", "$file"],
"selector": "source.clojurescript",
"file_regex": "^.* \\(([^:]*):([0-9]*):([0-9]*)\\)$",
}
さっきのscript.cljs
をsublime-textで開いて、「Tools -> Build」もしくはcommand + b
ショートカットを実行します。
コンソールが開いて
hello, Clojure with inlein
[Finished in 1.0s]
のようなのが表示されればOK。
misc
- Clojure(またはClojureScript)のファイルを開いて「
Sublime Text
->Preferences
->Settings - More
->Syntax Specific - User
」で、.cljファイル用の設定を作っておくと使いやすくなります。 - CljureScriptも同様です。
- sublime-textでREPLを利用することもできます。
Sublime Text 2でClojure REPLを使う - Qiitaの記事がとても分かりやすかったです。
まとめ
- inleinとplanckでClojureとClojureScriptが手軽に始められ、スクリプティングできます。
- sublime-text3から実行できます。
分からなかったこと
-
*.sublime-buildファイルにはfile_regex
を書いたほうが良さそうだが、どう書けば良いのか分かりません。。file_regex
を設定すると、sublimeがエラーのスタックトレースのファイル名や行番号や列番号を解釈して、リンクにしてくれます。以下を参考に追記しました。 - sublime-textから実行する場合にコマンドライン引数はどうやって渡せばよいのだろう。