LoginSignup
4
4

More than 1 year has passed since last update.

sublime-text3, inlein, planck でclojureを手軽に始める

Last updated at Posted at 2016-05-23

(注意)この記事はもうとても古いです。スクリプトとして Clojure を使うなら https://babashka.org/ などをおすすめします。


MacでClojureを手軽に始める&スクリプト的に使う方法として、inleinplanckを紹介します。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のビルド&実行ツールとしては、leiningenbootが一般的なようです(よく知りません・・)。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

使ってみます。以下のようなファイルを用意して、

script.clj
#!/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引数にファイル名を渡してスクリプトとして実行してみます。

以下のようなファイルを用意して

script.cljs
#!/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になると思います。

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になると思います。

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から実行する場合にコマンドライン引数はどうやって渡せばよいのだろう。

  1. ここで、「スクリプト的に使う」と表現しているのは、システムのメンテやローカルの環境設定などのような、ちょっとした処理を単一ファイルで記述するような用途を指しています。 

  2. javajavacコマンドのラッパーなので、実行ツールと言ったほうが正しいのかもしれません。 

  3. こちらもMacに標準でバンドルされているJavascriptCoreというランタイムを使用しているので、実行ツールと言ったほうが正しいのかもしれません。そもそも、スクリプティングではなくREPL利用がメインです。 

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4