Opening a Scala Play console and executing commands programmatically - scala

I have a Scala script that accesses models from my Play project and rather than open up a console and run it manually, I would like it to be run automatically after a Git hook. This could be achieved via a small bash script but there doesn't seem to be a way to execute the ":load nameOfMyScript.scala" command after the Play console has opened. Is this possible or am I going about this the wrong way?
I also considered running this command via a plain Scala console but I cannot figure out how to load my Play project definition inside of the Scala console.
Any help would be greatly appreciated.

From the sbt console of your Play app, run the console command. This will start the Scala REPL and give you access to everything on your app's classpath.

Related

Scala Console in IntelliJIDEA neither acccepts input nor prints output, comand works from temrinal

I have a "Scala/Simple module with attached Scala SDK" type project in IntelliJIDEA with which I was easily able to build and setup a couple of application configurations. A Scala workspace also works fine, but when I try to create and run a Scala Console configuration the terminal comes up, the Scala Repl appears to run fine, in that it gives me the Welcome to Scala message and prints an initial "scala>". However, although autocompletion works, the Repl otherwise appears dead to the world: nothing is returned or printed, and when I restarted and entered a one-liner to write some text to a file to test if it was just an ouptut-capture issue, that file did not appear.
I am running IntelliJIDEA 15.0.2 / idea-IU-143.1184.17 on Debian.
When I copy the command being run by IntelliJIDEA and run from a console, it works fine; command copied from IntelliJIDEA Scala Console follows:
/usr/lib/jvm/java-7-openjdk-amd64/bin/java -Djline.terminal=NONE -Didea.launcher.port=7543 -Didea.launcher.bin.path=/usr/local/idea-IU-143.1184.17/bin -Dfile.encoding=UTF-8 -classpath /home/pacs/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.7.jar:/home/pacs/.ivy2/cache/org.scala-lang/scala-compiler/jars/scala-compiler-2.11.7.jar:/home/pacs/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.11.7.jar:/home/pacs/.IntelliJIdea15/config/plugins/Scala/lib/scala-plugin-runners.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/charsets.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/compilefontconfig.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/ext/icedtea-sound.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/ext/java-atk-wrapper.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/javazic.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/jce.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/jsse.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/management-agent.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/resources.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/rhino.jar:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/rt.jar:/home/pacs/IdeaProjects/scap/out/production/scap:/usr/local/idea-IU-143.1184.17/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain org.jetbrains.plugins.scala.compiler.rt.ConsoleRunner -usejavacp
I'm rather new to the JetBrains family of IDEs so I may be missing something obvious; does anyone have any ideas?
EDIT: I tried this on IntelliJIDEA 15.0.0 on OS X with scala 2.10.6 and JDK 1.8 with the same result so I am inclined to believe this is not a JDK, Scala, or OS-specific problem.
OK, finally figured this out: it's not a normal console where you press enter; instead you use whatever keymap goes to Plug-ins/Scala/Execute Console Statement-- probably Ctrl+Enter or ⌘+Enter.
Other people seem to have been stumped also:
Scala console won't work, IntelliJ
The IntelliJIDEA console is very nice once you know this; with autocompletion, auto-formatting, and no issues with multiline code. If I had one request though, it would be to ask the JetBrains team to please add something to the Scala Console welcome message which indicates that this is not a normal console and maybe a quick hint on how to use it.

eclipse: how to debug a Scala program called from a shell script

I have a Scala program that is triggered from a shell script. I'd like to be able to run the program in eclipse in debug mode. Anybody knows how that can be done?
Thanks.
I'm not sure if there is a way to debug both together, but what you can do is run your script with the option -xv. So...
user#mypc$: bash -xv myscript other_args
That will show you the commands that are executed along with their parameters.
Then in Eclipse you can debug your Scala program normally and pass those parameters to it through the main method or run configuration.
Typically debuggers are language specific and won't be able to do both bash scripts and code in another language, but with this method, you should be able to figure out what's going on.

Call ./.../bin/spark-submit pythonfile.py in eclipse (Pydev) console when running program

Current situation which works fine:
I'm currently programming my Pyspark files in eclipse with the Pydev plugin
I manually execute these files in the ubuntu shell with the following command:
./.../bin/spark-submit pythonfile.py
Desired situation:
When I press the "run" button in eclipse, eclipse will call the command mentioned above and show the console output in the eclipse console.
Is this possible and if yes, could someone give me a clue on how to do it?
Thank you!
I would recommend an external launch.
From the Run menu, choose External Tools | External Tools Configurations... and then create a launch configuration specifying the command you want to run.
You can generalize the command a little by using Variables For example:
Location: ${workspace_loc:/myprojectname}/../bin/spark-submit
Working Directory: ${workspace_loc:/myprojectname}
Arguments: ${workspace_loc:/myprojectname/myscript.py}
This is what a screenshot of the configuration looks like.
Additionally, in the Common tab you have a control over whether the output is captured in a console (the default) and/or redirected to a file. In this example screenshot, the output comes to a console and is written to a file back in my project:
Output file: ${workspace_loc:/myprojectname/build_output}

i really would like sbt and its console to work under cygwin any way you think it can be done?

i have this issue (https://github.com/sbt/sbt/issues/562)
basically when I try to get a console it says:
[ERROR] Failed to construct terminal; falling back to unsupportedjava.lang.IllegalArgumentException: Invalid terminal type: jline.UnixTerminal
also you cant use backspace
you basically cannot use sbt in cygwin (in dos is fine but cygwin is a much nicer environment)
and have voiced my concern there
i have tried several workaround i found on the net but they are all for old releases and no use now
was just wondering if you know of any workaround?
thanks
The following works for me (mostly, see note at bottom):
Use the mintty shell. I believe this is the default shell for new cygwin installs but has been included as an alternative for a while. If mintty.exe exists in your <cygwin home>\bin folder then it's ready to use, else it can be installed through the typical cygwin package selection from the setup.exe.
Open a mintty window, right click anywhere, go to Options... -> Keys, and make sure Send Backspace as ^H is checked. This will allow the REPL to correctly interpret backspaces.
For just running the Scala REPL that should be all you need, but attempting to run sbt console can still produce that exception. To get past that, run sbt without any arguments to get to the sbt prompt. From there execute:
eval System.setProperty("jline.terminal", "scala.tools.jline.UnixTerminal")
then
console
or, as a single command (with both semi-colons being important):
; eval System.setProperty("jline.terminal", "scala.tools.jline.UnixTerminal") ; console
From what I can tell, this is caused at least in part by the Scala REPL and the sbt prompt using incompatible versions of JLine. In particular, it looks like the Scala REPL created their own wrappers around the library and are using that while sbt is using the JLine library directly.
Note
One limitation that I continue to run into is that the REPL wraps at column 80 even if the shell window has more horizontal space. Not only that, but when the REPL wraps like this it overwrites the same line rather than advancing to the next, and pulling long lines from history ends up pushing the cursor above the line you're actually editing.

eclipse plugin: run command in console, get stdin

I'm making an eclipse plugin where I want to run a system command, and have the output of the command go to the eclipse console. I know how to do this via http://wiki.eclipse.org/FAQ_How_do_I_write_to_the_console_from_a_plug-in%3F
The problem I don't know how to solve, is to allow interactive input from the console for this command.
In essence, I want to kick off a command which runs in the console. The user can interact with this command (via stdin). I then want my plugin code to get a callback when the command has finished (or user has manually stopped/terminated the command in the console). Getting the return code of the completed program would be nice as well.
For simplicity sake, lets use the unix 'passwd' command as the example (this is the first common cmd I could think of that both has output and prompts for input). I don't need to hide the stdin however.
For reference, the command I'm trying to call is the CakePHP bake shell.
I assume that you got yourself a MessageConsole, like in the FAQ. Then it should be possible to use messageConsole.getInputStream().read() in a loop to get your input (but I have not tested this). See also the documentation of the IOConsoleInputStream for more options.
On the other hand I'm not sure why you are creating your own plugin to do that. The standard console implementation in Eclipse does exactly what you want. You can see that by running a small Java application, where you can use that console for input and output as well.