How can I change Scala script's directory? - scala

I want use Scala like Python, so I install REPL in Sublime Text(Os is win8)
Everytime in REPL, I have to
scala> :load <my file>
, so I think it's inconvenient.
And I can't change
scala> :settings -d <路径名>
in Chinese directory.
I'm confused whether I can't change Scala script's directory with non-english language.
Thanks a lot!

If you use sbt then you can define initial commands when you launch the console.
yourproject/build.sbt:
// build.sbt
name := "initial-commands-example"
initialCommands := "import Foo._"
yourproject/script.scala:
// script.scala
object Foo {
def hello(name: String) = s"hello $name"
val msg = hello("world")
}
Inside yourproject, run sbt console, and you will have everything in Foo available inside that repl. See sbt initialCommands docs for more information.

Related

Ammonite: how to use another script from an Ivy dependency?

I have an Ammonite Script that I want to deliver in a JAR.
In another project I want to use this Script - but so far with no success.
I tried according to the documentation (sol_local_build.sc):
import $ivy.`mycompany:myproject_2.12:2.1.0-SNAPSHOT`, local_build
#main
def doit():Unit =
println(local_build.curl("http://localhost:8080"))
local_build.sc is in the Script I want to use.
This is the exception I get:
sol_local_build.sc:2: '.' expected but eof found.
^
The script must be compiled on the fly.
Put your script in a standard sbt project
inside a directory, example directory name: "test1"
Put your external script (example name: "script.sc")
// script.sc
println("Hello world!")
into the resource directory ("test1\src\main\resources\script.sc") of the test1 project
Publish the projekt local, i.e. sbt publishLocal
It is published to ".ivy2\local\default\test1_2.12\0.1-SNAPSHOT\ ... " directory.
Now you can use the following ammonite script "test.sc".
It reads the "script.sc" from the jar in the local ivy repository
and writes it to the local directory (must have read/write access) and then executes an external process,
which calls the scala "interpreter" and executes the script.
// test.sc
import $ivy.`default:test1_2.12:0.1-SNAPSHOT`
val scriptCode = scala.util.Try {scala.io.Source.fromResource("script.sc").mkString} getOrElse """Println("Script-file not found!")"""
println("*" * 30)
println(scriptCode)
println("*" * 30)
println()
java.nio.file.Files.write(java.nio.file.Paths.get("script.sc"), scriptCode.getBytes(java.nio.charset.StandardCharsets.UTF_8))
val cmd = Seq("cmd.exe", "/c", "scala", "script.sc")
val output = sys.process.Process(cmd).!!
println(output)
Executing the script the Ammonite REPL, you get:
******************************
// script.sc
println("Hello world!")
******************************
Hello world!
The script has no error handling and leaves the file in the running directory.
You can speed up the execution with the "-savecompiled" compiler switch, i.e
val cmd = Seq("cmd.exe", "/c", "scala", "-savecompiled", "script.sc")
An additional .jar file is created then in the running directory.
Scala Scripts are not really interpreted, but are compiled "under the hood"
as every normal Scala programm.
Therefor all code must be reachable during compile time
and you cannot call a function inside the other script from the jar-file!
But Ammonite has a buid in multi-stage feature.
It compiles one part, executes it and then compiles the next part!
Little improved ammonite-script.
It's not error free but runs.
Maybe there is better way to get the script out of the jar.
You should ask Li Haoyi!
// test_ammo.sc
// using ammonite ops
// in subdirectoy /test1
// Ammonite REPL:
// import $exec.test1.test_ammo
// # Ammonite-multi-stage
import $ivy.`default::test1:0.1-SNAPSHOT`
//import scala.util.Properties
import scala.sys.process.Process
val scriptFileName = "script.sc"
write.over(pwd/"test1"/scriptFileName, read(resource(getClass.getClassLoader)/scriptFileName))
val cmd = Seq("cmd.exe", "/c", "scala", scriptFileName)
val output = Process(cmd).!!
println(output)
#
import $exec.script // no .sc suffix
ppp() // is a function inside script.sc
script.sc inside resources folder of project
published local with "sbt publishLocal":
// script.sc
println("Hello world!")
def ppp() = println("Hello world from ppp!")
For completeness, I could solve my problem as follows:
Just create a Scala File in this project.
Copy the Script content in
an Object.
package mycompany.myproject
object LocalBuild {
def curl(..)...
}
Add the dependencies to your sbt file (e.g. ammonite.ops)
Use it like:
$ivy.`mycompany:myproject_2.12:2.1.0-SNAPSHOT`, mycompany.myproject.LocalBuild
#main
def doit():Unit =
println(LocalBuild.curl("http://localhost:8080"))

Different output depending on compilation with intellij or sbt

I have the following scala program that simply counts the words in a file:
package com.impatient
import java.util.Scanner
object Main extends App {
countWords()
def countWords(): Unit = {
val in = new Scanner(new java.io.File("C:\\tmp\\SampleText.txt"))
var wc = 0
while (in.hasNext()) {
var word = in.next()
wc += 1
println( wc + ". " + word)
}
in.close()
println("======================")
println(s"Total words: $wc")
}
def TestFunction(): Unit ={
println("Hello from test function!")
}
TestFunction()
}
When I execute this in intellij, either directly or via the sbt shell inside intellij, I get the following output:
mostly
positive:
======================
Total words: 309
Hello from test function!
So it correctly counts 309 words in the text file.
However if I try to execute the jar that I just compiled with intellij, directly, either via scala or sbt I get the following output:
λ scala .\impatientscala2_2.12-1.0.jar
======================
Total words: 0
Hello from test function!
So in this case, although the methods in Main are executed the wordcount is 0.
Why would be this the case? Is intellij referencing some other java libraries that standalone sbt or scala isn't? In that case why doesn't the Scanner fail?
Edit 1
I have cloned this repo to another PC to see what happens. And in that case I can observe the exact opposite, i.e.:
The program returns 0 word count when run from Intellij
When the jar is run directly with scala, it gives the current wordcount.
The 0 wordcount is due to not processing the while loop, as in.hasNext() returns nothing.
I found a workaround by using Paths.get from java.nio.file._.
//val in = new Scanner(new java.io.File("C:/tmp/SampleText.txt"))
val in: Scanner = new Scanner(Paths.get("C/tmp/SampleText.txt"))
This produces the correct jars on both PCs, when using Run | Sbt Task in Intellij, using SBT Shell in intellij, and using standalone SBT shell. If anyone can provide an explanation of why would java.io.File fail in certain setups that would be great.

SBT system property not being set

I'm trying to do sbt flywayMigrate -Denvi=foo but the system property envi is not being set. Pointers for debugging is greatly appreciated as I haven't been successful in identifying the cause of this issue for hours now. No question in SO or anywhere else have had this issue so far.
In build.sbt, this will be used as a variable.
lazy val envi = sys.props.getOrElse("envi", "default")
Using sys.env.get("ENVI") instead is currently not an option due to shared/team repo considerations.
sbt console -Denvi=foo
scala> sys.props.get("envi")
res0: Option[String] = None
scala> sys.props.getOrElse("envi", "default")
res1: Option[String] = default
scala, sbt installed using brew
You have to put the environment variable before the command:
sbt -Denvi=foo console
otherwise it will be passed as an argument to the main class instead of to the JVM.
Alternatively you can set the environment in the JAVA_OPTS variable before starting sbt:
export JAVA_OPTS="-Denvi=foo"
sbt console
scala> sys.props.getOrElse("envi", "default")
res0: String = foo

Test initialCommands in SBT

I have a subproject in my build.sbt with a rather long setting for initialCommands, comprising a list of imports and some definitions. I'd like to test this as part of regular CI, because otherwise I won't notice breaking changes after refactoring code. It is not clear to me how to do so.
Just running sbt console doesn't seem to cut it, because there is always a "successful" exit code even when code doesn't compile.
Moving the code out into an object defined in a special source file won't help because I need the list of imports to be present (and I don't want to cakeify my whole code base).
Moving the code out into a source file and then loading that with :load also always gives a successful exit code.
I found out about scala -e but that does strange things on my machine (see the error log below).
This is Scala 2.12.
$ scala -e '1'
cat: /release: No such file or directory
Exception in thread "main" java.net.UnknownHostException: <my-host-name-here>: <my-host-name-here>: Name or service not known
You could generate a file and run it like any other test file:
(sourceGenerators in Test) += Def.task {
val contents = """object TestRepl {
{{}}
}""".replace("{{}}", (initialCommands in console).value)
val file = (sourceManaged in Test).value / "repltest.scala"
IO.write(file, contents)
Seq(file)
}.taskValue

prompt for user input when running scala program with sbt

I have a very simple scala program:
object TakeInputs {
def main(args: Array[String]) {
val name = readLine("What is your name?")
println(name)
}
}
When I try to run this with
sbt "project myproject" "run-main TakeInput"
it doesn't wait for user input and the program just finishes with
What is your name?null
as the output.
Is there a way to make sbt wait for user input (like what happens if "readLine" is run in sbt console)? I can provide the inputs as command line parameters but I have a lot of them and I would like to make the program more user-friendly by displaying messages indicating what the user should enter next. Thanks.
Add the following to your build.sbt
connectInput in run := true
From the sbt documentation in Configuring Input