![]() |
lingua-franca 0.10.1
Lingua Franca code generator
|
An abstraction for an external command. More...
Public Member Functions | |
| List< String > | command () |
| Get this command's program and arguments. | |
| File | directory () |
| Get this command's working directory. | |
| String | getErrors () |
| Get the error output collected during command execution. | |
| String | getOutput () |
| Get the output collected during command execution. | |
| void | replaceEnvironmentVariable (String variableName, String value) |
| Replace the given variable and its value in the command's environment. | |
| int | run () |
| Execute the command. | |
| int | run (CancelIndicator cancelIndicator) |
| Execute the command. | |
| void | setEnvironmentVariable (String variableName, String value) |
| Add the given variable and its value to the command's environment. | |
| void | setEnvironmentVariables (Map< String, String > variables) |
| Add the given variables and their values to the command's environment. | |
| void | setQuiet () |
| Require this to be quiet, overriding the verbosity specified at construction time. | |
| void | setVerbose () |
| Require this to be verbose, overriding the verbosity specified at construction time. | |
| String | toString () |
| Get a String representation of the stored command. | |
Static Public Member Functions | |
| static LFCommand | get (final String cmd, final List< String > args) |
| Create a LFCommand instance from a given command and argument list in the current working directory. | |
| static LFCommand | get (final String cmd, final List< String > args, boolean quiet) |
| Create a LFCommand instance from a given command and argument list in the current working directory. | |
| static LFCommand | get (final String cmd, final List< String > args, boolean quiet, Path dir) |
| Create a LFCommand instance from a given command, an argument list and a directory. | |
Protected Member Functions | |
| LFCommand (ProcessBuilder pb, boolean quiet) | |
| Construct an LFCommand that executes the command carried by pb. | |
Protected Attributes | |
| boolean | didRun = false |
| ByteArrayOutputStream | errors = new ByteArrayOutputStream() |
| ByteArrayOutputStream | output = new ByteArrayOutputStream() |
| ProcessBuilder | processBuilder |
| boolean | quiet |
An abstraction for an external command.
This is a wrapper around ProcessBuilder which allows for a more convenient usage in our code base.
|
protected |
Construct an LFCommand that executes the command carried by pb.
| List< String > org.lflang.util.LFCommand.command | ( | ) |
Get this command's program and arguments.
| File org.lflang.util.LFCommand.directory | ( | ) |
Get this command's working directory.
|
static |
|
static |
|
static |
Create a LFCommand instance from a given command, an argument list and a directory.
This will check first if the command can actually be found and executed. If the command is not found, null is returned. In order to find the command, different methods are applied in the following order:
1. Check if the given command cmd is an executable file within dir. 2. If the above fails 'which cmd' (or 'where cmd' on Windows) is executed to see if the command is available on the PATH. 3. If both points above fail, a third attempt is started using bash to indirectly execute the command (see below for an explanation).
A bit more context: If the command cannot be found directly, then a second attempt is made using a Bash shell with the –login option, which sources the user's ~/.bash_profile, ~/.bash_login, or ~/.bashrc (whichever is first found) before running the command. This helps to ensure that the user's PATH variable is set according to their usual environment, assuming that they use a bash shell.
More information: Unfortunately, at least on a Mac if you are running within Eclipse, the PATH variable is extremely limited; supposedly, it is given by the default provided in /etc/paths, but at least on my machine, it does not even include directories in that file for some reason. One way to add a directory like /usr/local/bin to the path once-and-for-all is this:
sudo launchctl config user path /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
But asking users to do that is not ideal. Hence, we try a more hack-y approach of just trying to execute using a bash shell. Also note that while ProcessBuilder can be configured to use custom environment variables, these variables do not affect the command that is to be executed but merely the environment in which the command executes.
| cmd | The command |
| args | A list of arguments to pass to the command |
| quiet | If true, the commands stdout and stderr will be suppressed |
| dir | The directory in which the command should be executed |
| String org.lflang.util.LFCommand.getErrors | ( | ) |
Get the error output collected during command execution.
| String org.lflang.util.LFCommand.getOutput | ( | ) |
Get the output collected during command execution.
| void org.lflang.util.LFCommand.replaceEnvironmentVariable | ( | String | variableName, |
| String | value ) |
Replace the given variable and its value in the command's environment.
| variableName | name of the variable to add |
| value | the variable's value |
| int org.lflang.util.LFCommand.run | ( | ) |
Execute the command.
Do not allow user cancellation.
| int org.lflang.util.LFCommand.run | ( | CancelIndicator | cancelIndicator | ) |
Execute the command.
Executing a process directly with processBuilder.start() could lead to a deadlock as the subprocess blocks when output or error buffers are full. This method ensures that output and error messages are continuously read and forwards them to the system output and error streams as well as to the output and error streams hold in this class.
If the current operation is cancelled (as indicated by cancelIndicator), the subprocess is destroyed. Output and error streams until that point are still collected.
| cancelIndicator | The indicator of whether the underlying process should be terminated. |
| void org.lflang.util.LFCommand.setEnvironmentVariable | ( | String | variableName, |
| String | value ) |
Add the given variable and its value to the command's environment.
| variableName | name of the variable to add |
| value | the variable's value |
| void org.lflang.util.LFCommand.setEnvironmentVariables | ( | Map< String, String > | variables | ) |
Add the given variables and their values to the command's environment.
| variables | A map of variable names and their values |
| void org.lflang.util.LFCommand.setQuiet | ( | ) |
Require this to be quiet, overriding the verbosity specified at construction time.
| void org.lflang.util.LFCommand.setVerbose | ( | ) |
Require this to be verbose, overriding the verbosity specified at construction time.
| String org.lflang.util.LFCommand.toString | ( | ) |
Get a String representation of the stored command.
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |