Ammonite is such a great project, especially Ammonite-REPL. It brings many great things to scala console: syntax highlighting, better editing, dynamically loading dependencies, loading scripts, etc. It comes very handy if you want to play with a some library without creating an sbt project. Or you want to try something out without necessarily loading IntelliJ. I happen to do it often and decided to start an open source project called ammonite-modules. The idea is to have pre-defined modules you can load in Ammonite Repl. Modules will have all the dependencies needed at latest versions and do some basic setup.
The structure of the project is simple:
Base.sc file contains all the versions (hopefully it will be maintained so all latest versions will be in master). Modules directory has all the modules. The idea here is that each project may have different modules and user chooses which module to load. For example this:
Here use can load
akka-stream, etc. All three are different modules. Or user may want to load only
cats (all cats projects). Or it can be a simple project with only one module, like
circe in this example.
To load a module run
loadM("<PROJECT>" -> "<MODULE>") (for example, loadM(“cats” -> “cats-core”)). To use
loadM method you need to have it in
predef.sc file. Simply run following to have the proper predefs created:
You can start
ammonite-modules directory to laod a module or have
AMMONITE_MODULES environment variable to point to the directory where you checked out
ammonite-modules. Environemt variable will give more flexibility.
Modules don’t stop by just loading needed dependencies and importing classes. Each module can also do a reasonable setup for the project. For example,
akka-actor may want to create an implicit actor system,
akka-stream may want to create implicit actor system and materializer, etc. You get the idea.
To demonstrate this let’s write an akka application right in Ammonite Repl. First things first, let’s load
Then let’s write this simple akka actor and start it:
You should see
Hello ammonite-modules message print every one second. Note that we didn’t create the actor system nor we imported
scala.concurrent.duration._ or any akka classes,
akka-actor module has it predefined for us. In a production app you would want to keep the messages and props method for the actor in it’s companion object but to just play with akka this is good enough code.
Hope others will find it helpful and use it. Also see the next section if you would like to contribute.
How to contribute
- Each now project should have it’s sub directory under
modulesfolder and at least one scala file in it. See above how modules are named.
Versionscase object in
Base.scfile should include a public val for the project with latest version.
- Add those three lines to the top of the file:
- Send pull requests, be it a bug fix, improvement, upgrading a version to the latest or adding more modules. All pull requests are welcome.