Scala, Ruby, Go の REPL 環境を Docker コンテナですぐに試せる Rod (REPLs on Docker) を作った

Scala の学習用に Ammonite 環境AWS EC2 上に構築していて、Scala Exercises の例を試せるように紹介されている cats や shapeless のライブラリを追加していた。ただ、個人的に Ruby を使う機会が圧倒的に多いので pry は必須だし、Go もやってみたいと考えているうちに、「REPL 環境をまとめた Docker コンテナを作成してしまえば楽なのでは?」と思って Rod (REPLs on Docker) というツールを作成した。

github.com

Github と Dockerhub を連携させて Automated Build 設定しているので、Github が更新されると自動的に Dockerhub 内部で docker builddocker push してくれる。

https://hub.docker.com/r/kyagi/rod/

簡単な使い方は以下の通り。例えば Scala Exercises の Cats | Semigroup の紹介されているコード例をそのまま打ち込めば試せるようになっている。自分自身が sbt console の使い勝手にあまり満足していないのと、Scala の開発環境構築に手間取ったところがあるので、こういったツールが誰かの役に立ってくれれば嬉しい。(^.^)

docker コンテナをたちあげる。

$ docker run -it kyagi/rod
 ____  _____ ____  _                         ____             _
|  _ \| ____|  _ \| |    ___    ___  _ __   |  _ \  ___   ___| | _____ _ __
| |_) |  _| | |_) | |   / __|  / _ \| '_ \  | | | |/ _ \ / __| |/ / _ \ '__|
|  _ <| |___|  __/| |___\__ \ | (_) | | | | | |_| | (_) | (__|   <  __/ |
|_| \_\_____|_|   |_____|___/  \___/|_| |_| |____/ \___/ \___|_|\_\___|_|
root@e8dcf015f1b5:~# 

Scala の REPL を使いたい時は rod scala で amm が起動する。

root@e8dcf015f1b5:~# rod scala
Loading...
Compiling (synthetic)/ammonite/predef/interpBridge.sc
Compiling (synthetic)/ammonite/predef/replBridge.sc
Compiling (synthetic)/ammonite/predef/DefaultPredef.sc
Compiling /root/.ammonite/predef.sc
Welcome to the Ammonite Repl 1.0.3
(Scala 2.12.4 Java 1.8.0_151)
If you like Ammonite, please support our development at www.patreon.com/lihaoyi
@ import cats.Semigroup
import cats.Semigroup

@ import cats.implicits._
import cats.implicits._

@ Semigroup[Int => Int].combine({ (x: Int) => x + 1 }, { (x: Int) => x * 10 }).apply(6)
res2: Int = 67

@ exit
Bye!
root@e8dcf015f1b5:~#

Ruby の場合は pry が立ち上げる。

root@e8dcf015f1b5:~# rod ruby

[1] pry(main)> ri open
(... snip ...) いっぱいあるけど Tempfile.open が知りたい
[2] pry(main)> ri Tempfile.open
(... snip ...)
[3] pry(main)> ri Array
(... snip ...) Array クラスのドキュメント
[4] pry(main)> ri Array#
(... snip ...) Array クラスのインスタンスメソッド一覧

Go の場合は gore が立ち上げる。

root@e8dcf015f1b5:~# rod go
gore version 0.2.6  :help for help
gore> :help
    :import <package>     import a package
    :print                print current source
    :write [<file>]       write out current source
    :doc <expr or pkg>    show documentation
    :help                 show this help
    :quit                 quit the session
gore> :quit
root@e8dcf015f1b5:~#

いろいろいれたらイメージサイズがふくらんでしまった(1.43GB)。特定の言語の REPL があればいい場合は、Dockerfile を削ることで軽量化できるけど、その逆にこの REPL も入れたい(例えば gore じゃなくて go-pry を使いたいなど) とかもあるかもしれない。

ちなみに .rod-prompt を読み込むことでプロンプトが変わって、Docker コンテナ内の環境にいることがわかりやすくなる(おまけ)。(๑˃̵ᴗ˂̵)و

https://camo.githubusercontent.com/4982b60d784c7eea80e15e3a47de26ab4e4fd01f/687474703a2f2f63646e2d616b2e662e73742d686174656e612e636f6d2f696d616765732f666f746f6c6966652f6b2f6b796167692f32303138303230342f32303138303230343132323935322e706e67