Run SBT command from script

* decided to start distributing and edit information that is not needed

I would like to run a script inside the SBT console, which will run the SBT command at the end. How can I do that

I wrote a script that allows me to execute shell commands. Entering sbt , then path/to/my-script start gives me this error: /bin/sh: start command not found

But path/to/my-script sbt start works fine

The reason why sbt plugins (such as these ) or a custom task will not work in this case :

  • script not written to scala

quick edit

* I would rather run start from a script instead of using a custom task / command to run my script

Further information below.


I will explain step by step what I would like to do (what I do may seem silly to you, but please read my answer on Etan):

  • Type sbt in my console which will call the SBT console

  • Instead of typing start I would like to run a script that will execute something else that directly has nothing to do with the project, and then calls start for me when it's done.

Unaware of the script, the script may call the #!/bin/sh commands, so I assume that what I'm trying to do is call the #!/bin/sh/<*this-sbt-console*> , if possible

Even a workaround, for example, if I can get the script to simply print start on the terminal and call the enter/return key after it is completed, it will be enough

other information:

  • Do not use any specific infrastructure
  • SBT version = 0.13 +
+6
source share
2 answers

To execute the script, and then complete the script, proceed to execute another sbt command, one approach is to implement a custom sbt command that does the following:

  • Executes the external process provided as the first argument, passing any additional arguments to this process.
  • Waits for the external process to complete.
  • After the external process completes, it runs another sbt command.

This is shown in this Build.scala file:

 import sbt._ import Keys._ // imports standard command parsing functionality import complete.DefaultParsers._ object CommandExample extends Build { // Declare a project, adding new commands. lazy override val projects = Seq(root) lazy val root = Project("root", file(".")) settings( commands ++= Seq(start, customStart) ) // A fake "start" command. def start = Command.command("start") { state => println("Fake start command executed.") state } // A command that executes an external command before executing the "start" command. // The name of the external command is the first parameter. // Any additional parameters are passed along to the external command. def customStart = Command.args("customStart", "<name>") { (state, args) => if (args.length > 0) { val externalCommand = args.mkString(" ") println(s"Executing '$externalCommand'") externalCommand ! } "start" :: state } } 

This is an example of execution:

 $ sbt [info] Loading project definition from /home/fernando/work/github/fernandoacorreia/so24565469/project [info] Set current project to hello (in build file:/home/fernando/work/github/fernandoacorreia/so24565469/) > customStart ls -la Executing 'ls -la' total 40 drwxr-xr-x 6 fernando fernando 4096 Jul 8 10:52 . drwxr-xr-x 27 fernando fernando 4096 Jul 8 08:51 .. -rw-r--r-- 1 fernando fernando 95 Jul 8 10:47 build.sbt drwxr-xr-x 8 fernando fernando 4096 Jul 8 08:58 .git -rw-r--r-- 1 fernando fernando 203 Jul 8 09:04 .gitignore -rw-r--r-- 1 fernando fernando 1082 Jul 8 08:51 LICENSE drwxr-xr-x 3 fernando fernando 4096 Jul 8 10:51 project -rw-r--r-- 1 fernando fernando 111 Jul 8 08:51 README.md drwxr-xr-x 3 fernando fernando 4096 Jul 8 08:57 src drwxr-xr-x 2 fernando fernando 4096 Jul 8 10:52 target Fake start command executed. > 

The external command passed as the customStart parameter can be any executable command, for example. binary file or shell script.

For more information on creating commands, see the Documentation documentation page.

To learn more about running external processes, see the External Process Documentation page.

A complete example is available for download in this GitHub repository .

Since sbt executes the script (as a child process), the script cannot execute commands in the original (parent) sbt process.

It would be possible to write the script output to either stdout or a file, and the sbt command to execute an arbitrary command created by the script, but a bit confusing.

Another approach is to invert execution order. Instead of sbt executing the script, the script will be executed first, directly from the shell, and the script will execute the sbt command.

For example, this script:

 #!/bin/bash echo "Running custom script" # Insert commands here sbt start 

Produces this conclusion:

 $ ./script Running custom script [info] Loading project definition from /home/fernando/work/github/fernandoacorreia/so24565469/project [info] Set current project to hello (in build file:/home/fernando/work/github/fernandoacorreia/so24565469/) Fake start command executed. 
+9
source

As you say: "Is there a way to just tell the script to write something to the terminal and press the" Enter / Enter "key?

yes - a slightly hacked, but highly effective solution - a small program called wait .

http://www.thegeekstuff.com/2010/10/expect-examples/

http://www.admin-magazine.com/Articles/Automating-with-Expect-Scripts

This will allow you to do everything you can do from the keyboard, but automatically. It is similar to the macro playback engine for the UNIX command line.

+2
source

Source: https://habr.com/ru/post/971736/


All Articles