Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Python-ealy way to run Scala scripts with dependencies?

The ways I know about so far are

  • Create an ant build.xml file, make compile and run tasks, and include appropriate jars in a classpath=

  • Make at sbt project and include dependencies with version numbers in build.sbt

  • Make a maven project and include dependencies in the xml file

  • Run from the command line setting -classpath explicitly

None of these are bad, but it feels like extra work after being babied with

import json

json.loads('[1, 2]')

and having that work right off the bat, provided I have json installed. In particular tracking down appropriate versions on Mavenhub gets a little tiresome.

Though maybe I'm just being too picky ;-)

like image 517
Owen Avatar asked Jan 19 '23 18:01

Owen


2 Answers

What you want is xsbtscript: https://github.com/paulp/xsbtscript

It allows you to create a single script file which includes both the sbt config your code requires along with the Scala code itself.

like image 180
Sean Parsons Avatar answered Jan 21 '23 15:01

Sean Parsons


I think scalas from SBT is better. Either install conscript and run this command:

cs harrah/xsbt --branch v0.10.1

Or create it by hand:

java -Dsbt.main.class=sbt.ScriptMain -Dsbt.boot.directory=/home/user/.sbt/boot -jar sbt-launch.jar "$@"

And then use it like this:

#!/usr/bin/env scalas
!#

/***
scalaVersion := "2.9.0-1"

libraryDependencies ++= Seq(
  "net.databinder" %% "dispatch-twitter" % "0.8.3",
  "net.databinder" %% "dispatch-http" % "0.8.3"
)
*/

import dispatch.{ json, Http, Request }
import dispatch.twitter.Search
import json.{ Js, JsObject }

def process(param: JsObject) = {
  val Search.text(txt)        = param
  val Search.from_user(usr)   = param
  val Search.created_at(time) = param

  "(" + time + ")" + usr + ": " + txt
}

Http.x((Search("#scala") lang "en") ~> (_ map process foreach println))

Paul's xsbtscript is basically a shell that downloads and install all necessary components to do the same thing. It usually works well, but has some limitations (won't go through authenticated proxies, for instance).

like image 21
Daniel C. Sobral Avatar answered Jan 21 '23 16:01

Daniel C. Sobral