Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'scala' command line tool takes only one source file as a parameter #5161

Closed
scabug opened this issue Nov 7, 2011 · 2 comments · Fixed by scala/scala#8349
Closed

'scala' command line tool takes only one source file as a parameter #5161

scabug opened this issue Nov 7, 2011 · 2 comments · Fixed by scala/scala#8349

Comments

@scabug
Copy link

scabug commented Nov 7, 2011

'scala' command line tool is very useful as a linux script executor. Unfortunately it supports only one file which, can be passed as argument. But I think that in more advance system scripting it is very common to want to refactor part of scripts into separate source unit. For example I have few scripts in bash for backing up my system, but calculating paths is common part and is in separate file mirror-config.sh.

As I said before I can not do it easily using Scala as I can not provide more than one scala source file.

My current (imperfect) solution is as follows:

scala-utils.scala -> contains some common utilities for scripting
test.sh -> main script in scala wrapped in bash

test.sh file:

#!/bin/bash
source /root/bin/boot-scala.sh
!#

import java.io.File
import utils.Timer

object App {
def main(args: Array[String]): Unit = {
val timer = new Timer

timer.start
println("Executing '%s' in directory '%s'".format(args(1), args(0)))
println("There are %d arguments passed to script.".format(args.length))
println(args.mkString(" "))

println("Time elapsed: " + timer.elapsed)

}
}

boot-scala file:

#!/bin/sh
SCRIPT="$(cd "${0%/}" 2>/dev/null; echo "$PWD"/"${0##/}")"
DIR=dirname "${SCRIPT}"}
CP="/tmp/scala-cp/"
if [ ! -d $CP ]; then
mkdir /tmp/scala-cp
scalac -d $CP $DIR/*.scala
fi
exec scala -savecompiled -classpath $CP $0 $DIR $SCRIPT $@

As you can see in boot-scala.sh, I am extracting SCRIPT name and DIR name and then these values are passed to scala.
Problem is that I can only provide classpath to scala tool, so I have to compile manually my scala-utils.scala file and put classes into arbitrary /tmp/scala-cp.

If scala tool could have another command line argument e.g. -sources, then I could provide other files directly without making such a strange hacks.

Also it would be nice if compiled *.jar files could be stored in some internal scala directory, so they are not cluttering my scripts directory.

@scabug
Copy link
Author

scabug commented Nov 7, 2011

Imported From: https://issues.scala-lang.org/browse/SI-5161?orig=1
Reporter: aarti_pl
Affected Versions: 2.9.1

@som-snytt
Copy link

This is a use case for -i, but it doesn't seem to work:

$ scala -howtorun:script -i t4033b.scala -- t4033.scala
t4033.scala:17: error: not found: value ng
    println("ng:" + ng.klass)
                    ^

Normally, there's no way to know this, but -i says take these "init" files (terminated by double dash), for REPL, but you can force the script runner.

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

Successfully merging a pull request may close this issue.

3 participants