lingua-franca 0.10.1
Lingua Franca code generator
Loading...
Searching...
No Matches
org.lflang.util.LFCommand Class Reference

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

Detailed Description

An abstraction for an external command.

This is a wrapper around ProcessBuilder which allows for a more convenient usage in our code base.

Constructor & Destructor Documentation

◆ LFCommand()

org.lflang.util.LFCommand.LFCommand ( ProcessBuilder pb,
boolean quiet )
protected

Construct an LFCommand that executes the command carried by pb.

Member Function Documentation

◆ command()

List< String > org.lflang.util.LFCommand.command ( )

Get this command's program and arguments.

◆ directory()

File org.lflang.util.LFCommand.directory ( )

Get this command's working directory.

◆ get() [1/3]

LFCommand org.lflang.util.LFCommand.get ( final String cmd,
final List< String > args )
static

Create a LFCommand instance from a given command and argument list in the current working directory.

See also
get

◆ get() [2/3]

LFCommand org.lflang.util.LFCommand.get ( final String cmd,
final List< String > args,
boolean quiet )
static

Create a LFCommand instance from a given command and argument list in the current working directory.

See also
get

◆ get() [3/3]

LFCommand org.lflang.util.LFCommand.get ( final String cmd,
final List< String > args,
boolean quiet,
Path dir )
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.

Parameters
cmdThe command
argsA list of arguments to pass to the command
quietIf true, the commands stdout and stderr will be suppressed
dirThe directory in which the command should be executed
Returns
Returns an LFCommand if the given command could be found or null otherwise.

◆ getErrors()

String org.lflang.util.LFCommand.getErrors ( )

Get the error output collected during command execution.

◆ getOutput()

String org.lflang.util.LFCommand.getOutput ( )

Get the output collected during command execution.

◆ replaceEnvironmentVariable()

void org.lflang.util.LFCommand.replaceEnvironmentVariable ( String variableName,
String value )

Replace the given variable and its value in the command's environment.

Parameters
variableNamename of the variable to add
valuethe variable's value

◆ run() [1/2]

int org.lflang.util.LFCommand.run ( )

Execute the command.

Do not allow user cancellation.

Returns
the process' return code

◆ run() [2/2]

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.

Parameters
cancelIndicatorThe indicator of whether the underlying process should be terminated.
Returns
the process' return code
Author
Christian Menard

◆ setEnvironmentVariable()

void org.lflang.util.LFCommand.setEnvironmentVariable ( String variableName,
String value )

Add the given variable and its value to the command's environment.

Parameters
variableNamename of the variable to add
valuethe variable's value

◆ setEnvironmentVariables()

void org.lflang.util.LFCommand.setEnvironmentVariables ( Map< String, String > variables)

Add the given variables and their values to the command's environment.

Parameters
variablesA map of variable names and their values

◆ setQuiet()

void org.lflang.util.LFCommand.setQuiet ( )

Require this to be quiet, overriding the verbosity specified at construction time.

◆ setVerbose()

void org.lflang.util.LFCommand.setVerbose ( )

Require this to be verbose, overriding the verbosity specified at construction time.

◆ toString()

String org.lflang.util.LFCommand.toString ( )

Get a String representation of the stored command.

Member Data Documentation

◆ didRun

boolean org.lflang.util.LFCommand.didRun = false
protected

◆ errors

ByteArrayOutputStream org.lflang.util.LFCommand.errors = new ByteArrayOutputStream()
protected

◆ output

ByteArrayOutputStream org.lflang.util.LFCommand.output = new ByteArrayOutputStream()
protected

◆ processBuilder

ProcessBuilder org.lflang.util.LFCommand.processBuilder
protected

◆ quiet

boolean org.lflang.util.LFCommand.quiet
protected

The documentation for this class was generated from the following file:
  • /Users/runner/work/lingua-franca/lingua-franca/core/src/main/java/org/lflang/util/LFCommand.java