Friday, December 28, 2012

Interactive Scala Console (REPL) for Play Application

Since I know Ruby/Rails so well, I constantly compare that technology stack with Scala/Play.

Ruby has IRB (interactive ruby shell).  Scala has REPL.

When developing a Rails application, I frequently use the Rails Console to debug Rails project code.  Execute the following command from the OS shell, e.g., Bash, in your Rails project:

rails c

The default environment is development, but you can also open the rails console for test or production (or any custom environment you create)

Rake is the Ruby-based build tool for Rails apps.

Similarly, SBT is the scala-based build tool used for Play applications.

SBT is typically used to compile and execute a Scala scripts containing dependency declarations or other sbt settings.

I created a script to start SBT for my project that looks like:


sbt.sh


java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=512m -Xmx2048M -noverify -jar -Dcom.company.project.common.util.devmode=yes $HOME/opt/sbt/bin/sbt-launch.jar "$@"

That allows me to run commands like:

update
gen-idea
project project_name
clean
compile

But I wanted to actually run scala commands and exercise application APIs.

I'd get errors like the following:

SBT Console

$ sbt-no-repl.sh
Listening for transport dt_socket at address: 8000
[info] Loading project definition from /hs/dev/company/project
[info] Set current project to ap (in build file:/hs/dev/company/)
project:master:5.0-SNAPSHOT> 
project:master:5.0-SNAPSHOT> var allProducts = new ListBuffer[String]()
[error] Not a valid key: var (similar: start, run, target)
[error] var allProducts = new ListBuffer[String]()
[error]    ^
project:master:5.0-SNAPSHOT> import collection.mutable.ListBuffer
[error] Not a valid key: import (similar: port)
[error] import collection.mutable.ListBuffer
[error]       ^


... until I learned how to startup the Scala REPL, defining the dependencies that should be on the classpath, and adding that to my SBT script:

sbt.sh

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=512m -Xmx2048M -noverify -jar -Dnet.tstllc.common.util.devmode=yes  -Dsbt.main.class=sbt.ConsoleMain -Dsbt.boot.directory=/Volumes/HoneyBadger1TB/Users/lex/.sbt/boot $HOME/opt/sbt/bin/sbt-launch.jar "$@"


SBT (with REPL) Console

$ sbt
Listening for transport dt_socket at address: 8000
[info] Set current project to default-3bc954 (in build file:/Volumes/HoneyBadger1TB/Users/lex/.sbt/boot/ivy-console/)
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).
Type in expressions to have them evaluated.
Type :help for more information.

scala> var allProducts = new ListBuffer[String]()
<console>:7: error: not found: type ListBuffer
       var allProducts = new ListBuffer[String]()
                             ^

scala> import collection.mutable.ListBuffer
import collection.mutable.ListBuffer

scala> var allProducts = new ListBuffer[String]()
allProducts: scala.collection.mutable.ListBuffer[String] = ListBuffer()

scala> allProducts += "1"
res0: scala.collection.mutable.ListBuffer[String] = ListBuffer(1)

scala> allProducts += "2"
res1: scala.collection.mutable.ListBuffer[String] = ListBuffer(1, 2)

scala> 


References

http://www.scala-sbt.org/
http://www.scala-sbt.org/release/docs/Detailed-Topics/Scripts.html
http://www.playframework.org/
http://rake.rubyforge.org/

Special thanks to Kyle Unverferth!

Sponsor Ads


18 comments:

  1. Just found out that the above really only applies to non-play sbt projects.

    For play apps, similar to Rails apps, you can just type "console" at the sbt prompt :-)

    ReplyDelete
  2. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it. Python Training in Chennai

    ReplyDelete
  3. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
    Click here:
    angularjs training in sholinganallur
    Click here:
    angularjs training in btm
    Click here:
    angularjs training in rajajinagar

    ReplyDelete
  4. Have you been thinking about the power sources and the tiles whom use blocks I wanted to thank you for this great read!! I definitely enjoyed every little bit of it and I have you bookmarked to check out the new stuff you post
    Click here:
    Microsoft azure training in annanagar
    Click here:
    Microsoft azure training in velarchery

    ReplyDelete
  5. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Blueprism training in Chennai

    Blueprism training in Bangalore

    Blueprism training in Pune

    Blueprism online training

    Blueprism training in tambaram

    ReplyDelete
  6. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.Devops training in sholinganallur

    ReplyDelete
  7. Awesome! Education is the extreme motivation that open the new doors of data and material. So we always need to study around the things and the new part of educations with that we are not mindful.

    Data Science Training in Chennai | Data Science course in anna nagar
    Data Science course in chennai | Data science course in Bangalore
    Data Science course in marathahalli | Data Science course in btm

    ReplyDelete
  8. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    java training in chennai | java training in bangalore

    java interview questions and answers | core java interview questions and answers

    ReplyDelete
  9. And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.• And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most safety course in chennai
    effective I’ve had.

    ReplyDelete
  10. And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
    Data science Course Training in Chennai | No.1 Data Science Training in Chennai
    RPA Course Training in Chennai | No.1 RPA Training in Chennai

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete