だいたい Ammonite に書いてある。Magic Imports はとてもありがたい。
インストール
$ sudo curl -L -o /usr/local/bin/amm https://git.io/vdNv2 && sudo chmod +x /usr/local/bin/amm && amm
REPL として使う
$ amm
(... snip the greeting message ...)
@ "This is Ammonite REPL".split(" ").foreach(println)
This
is
Ammonite
REPL
REPL として使う + 外部ライブラリを追加する
$ amm
(... snip the greeting message ...)
@ import $ivy.`com.github.nscala-time::nscala-time:2.18.0`, com.github.nscala_time.time.Imports._
import $ivy.$ , com.github.nscala_time.time.Imports._
@ DateTime.now().toMutableDateTimeISO
res14: org.joda.time.MutableDateTime = 2018-01-18T20:53:19.347+09:00
REPL として使う + 外部ライブラリを起動時に読み込ませるように設定しておく
$ vi ~/.ammonite/predef.sc
import $ivy.`org.scalaz::scalaz-core:7.2.7`, scalaz._, Scalaz._
import $ivy.`com.github.nscala-time::nscala-time:2.18.0`, com.github.nscala_time.time.Imports._
$ amm
(... snip the greeting message ...)
@ (1.hour + 2.minutes + 34.seconds).getClass
res9: Class[T] = class com.github.nscala_time.time.DurationBuilder
コマンドとして使う(-c)
$ amm -c 'import scala.io.Source; val f = Source.fromFile("/home/kyagi/.gitconfig"); for(l <- f.getLines) { if(l.contains("checkout")) println(l); }'
Compiling /home/kyagi/bin/(console)
cb = checkout -b
co = checkout
tg = "!f() { git checkout -b "$1" "refs/tags/$1"; }; f"
yo = "!f() { git checkout -b "$1" "origin/$1"; }; f"
yp = "!f() { git checkout -b "$2" "$1/$2"; }; f"
スクリプトを実行するインタプリンタとして使う (-s はサイレントオプション)
import scala.io.Source
import ammonite.ops._
@main
def main(str: String, path: Path): Unit = {
val file = Source.fromFile(path.toString)
for (line <- file.getLines) {
if (line.contains(str)) println(line)
}
}
$ amm -s MyGrep.sc branch /home/kyagi/.gitconfig
Compiling /home/kyagi/bin/MyGrep.sc
ba = branch -ra
bd = branch -d
bD = branch -D
br = branch
bv = branch -vv
スクリプトを実行するインタプリンタとして使う + 別窓でスクリプトを修正したら自動実行
$ amm -s -w MyHello.sc
hello, wolrd
Watching for changes to 2 files... (Ctrl-C to exit) # 別窓で vi MyHello.sc して s/world/scala/ する
hello, scala
Watching for changes to 2 files... (Ctrl-C to exit)
...
-w オプションは sbt の ~run
に相当する(Ruby でいう Guard)。٩(๑´3`๑)۶