Skip to content

Instantly share code, notes, and snippets.

@sroebuck
Created January 6, 2012 17:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sroebuck/1571464 to your computer and use it in GitHub Desktop.
Save sroebuck/1571464 to your computer and use it in GitHub Desktop.
Running Scala shell scripts with library dependencies
#!/bin/sh
exec /Users/sroebuck/local/bin/scalas $0 $@
!#
/***
libraryDependencies ++= Seq(
"com.github.scala-incubator.io" %% "scala-io-file" % "0.2.0",
"joda-time" % "joda-time" % "2.0",
"org.joda" % "joda-convert" % "1.1"
)
*/
import org.joda.time.LocalDate
import scalax.file.Path
val desktop = Path("/Users/sroebuck/Desktop")
val archive = Path("/Users/sroebuck/Archive/Desktop")
/**
* Copy all the files on the Desktop that were not modified today into the file archive.
*/
val files = desktop.children()
val filtered = files.filter(f => new LocalDate(f.lastModified).isBefore(new LocalDate))
val grouped = filtered.groupBy(f => new LocalDate(f.lastModified).withDayOfMonth(1))
grouped.foreach {
case (date, files) =>
val year = date.getYear
val month = date.getMonthOfYear
val dir = archive \ year.toString \ "%04d-%02d".format(year,month)
dir.createDirectory(failIfExists=false)
files.foreach {
file =>
println("Moving '%s' to '%s'".format(file.name, dir.path))
file.moveTo(dir \ file.name)
}
}
#!/bin/sh
java -Dsbt.main.class=sbt.ScriptMain \
-Dsbt.boot.directory=/home/myhome/.sbt/boot \
-Dsbt.log.noformat=true \
-jar /the/path/to/my/sbt/0.11.2/libexec/sbt-launch.jar "$@"
@sroebuck
Copy link
Author

sroebuck commented Jan 6, 2012

  • archiveDesktopFiles.scala is a Scala script which has dependencies on three external libraries.
  • scalas is just a shell script which runs scalas which is part of a normal install of sbt but isn't provided with a command line executable.

Further explanation of scalas can be found here: https://github.com/harrah/xsbt/wiki/Scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment