Class ExecOperation
- Since:
- 1.0
- Author:
- Erik C. Thauvin
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncommand()Returns the command and arguments to be executed.Configures the command and arguments to be executed.final ExecOperationcommand(Collection<String> args) Configures the command and arguments to be executed.env()Returns the environment variables to be set for the command.Adds an environment variable for the command.Adds environment variables for the command.voidexecute()failOnExit(boolean failOnExit) Configures whether the operation should fail if the command exit value/status is not 0.fromProject(BaseProject project) Configures an Exec operation from aBaseProject.inheritIO(boolean inheritIO) Configures whether the child process should inherit the I/O streams of the current JVM.static booleanisAix()Determines if the current operating system is AIX.static booleanisCygwin()Determines if the current operating system is Cygwin.booleanReturns whether the operation should fail if the command exit value/status is not 0.static booleanDetermines if the current operating system is FreeBSD.booleanReturns whether the child process inherits the I/O streams of the current JVM.static booleanisLinux()Determines if the operating system is Linux.static booleanisMacOS()Determines if the current operating system is macOS.static booleanisMingw()Determines if the current operating system is MinGW.static booleanDetermines if the current operating system is OpenVMS.static booleanDetermines if the current operating system is Solaris.static booleanDetermines if the current operating system is Windows.If the current OS is Linux, configures the command and arguments to be executed.onLinux(Collection<String> args) If the current OS is Linux, configures the command and arguments to be executed.If the current OS is macOS, configures the command and arguments to be executed.onMacOS(Collection<String> args) If the current OS is macOS, configures the command and arguments to be executed.If the current OS is Unix-like (Linux, macOS, FreeBSD, Solaris, AIX), configures the command and arguments to be executed.onUnix(Collection<String> args) If the current OS is Unix-like, configures the command and arguments to be executed.If the current OS is Windows, configures the command and arguments to be executed.onWindows(Collection<String> args) If the current OS is Windows, configures the command and arguments to be executed.outputConsumer(Consumer<String> outputConsumer) Sets a consumer to receive output lines when not inheriting I/O.longtimeout()Returns the command timeout.timeout(long timeout) Configure the command timeout.workDir()Returns the working directory.Configures the working directory.Configures the working directory.Configures the working directory.Methods inherited from class rife.bld.operations.AbstractOperation
executeOnce, executeOnce, silent, silent
-
Constructor Details
-
ExecOperation
public ExecOperation()
-
-
Method Details
-
execute
- Specified by:
executein classAbstractOperation<ExecOperation>- Throws:
Exception
-
isAix
public static boolean isAix()Determines if the current operating system is AIX.- Returns:
trueif the operating system is identified as AIX,falseotherwise- See Also:
-
isCygwin
public static boolean isCygwin()Determines if the current operating system is Cygwin.- Returns:
trueif the operating system is identified as Cygwin,falseotherwise- See Also:
-
isFreeBsd
public static boolean isFreeBsd()Determines if the current operating system is FreeBSD.- Returns:
trueif the operating system is FreeBSD,falseotherwise- See Also:
-
isLinux
public static boolean isLinux()Determines if the operating system is Linux.- Returns:
trueif the operating system is Linux,falseotherwise- See Also:
-
isMacOS
public static boolean isMacOS()Determines if the current operating system is macOS.- Returns:
trueif the OS is macOS,falseotherwise- See Also:
-
isMingw
public static boolean isMingw()Determines if the current operating system is MinGW.- Returns:
trueif the operating system is identified as MinGW,falseotherwise- See Also:
-
isOpenVms
public static boolean isOpenVms()Determines if the current operating system is OpenVMS.- Returns:
trueif the operating system is OpenVMS,falseotherwise- See Also:
-
isSolaris
public static boolean isSolaris()Determines if the current operating system is Solaris.- Returns:
trueif the operating system is Solaris,falseotherwise- See Also:
-
isWindows
public static boolean isWindows()Determines if the current operating system is Windows.- Returns:
trueif the operating system is Windows,falseotherwise- See Also:
-
command
Configures the command and arguments to be executed.For example:
command("cmd", "/c", "stop.bat")command("./stop.sh"
- Parameters:
args- one or more arguments, must not be null or contain null/empty elements- Returns:
- this operation instance
- Throws:
NullPointerException- if args is nullIllegalArgumentException- if args contains null or empty elements- See Also:
-
command
Returns the command and arguments to be executed.The returned list is mutable and can be modified directly before calling
execute(). This allows callers to append flags or manipulate arguments conditionally:var op = new ExecOperation().command("git", "status"); if (verbose) { op.command().add("--verbose"); } op.execute();- Returns:
- the mutable command and arguments list, never null
-
command
Configures the command and arguments to be executed.- Parameters:
args- the list of arguments, must not be null or contain null/empty elements- Returns:
- this operation instance
- Throws:
NullPointerException- if args is nullIllegalArgumentException- if args contains null or empty elements- See Also:
-
env
Adds an environment variable for the command.These variables are merged with the current process environment. Existing variables with the same name are overridden.
- Parameters:
name- the variable name, must not be nullvalue- the variable value, must not be null- Returns:
- this operation instance
- Throws:
NullPointerException- if name or value is null- See Also:
-
env
Adds environment variables for the command.These variables are merged with the current process environment. Existing variables with the same name are overridden.
- Parameters:
vars- the map of environment variables, must not be null- Returns:
- this operation instance
- Throws:
NullPointerException- if vars is null- See Also:
-
env
Returns the environment variables to be set for the command.The returned map is mutable and can be modified directly before calling
execute().- Returns:
- the mutable environment variables map, never null
-
failOnExit
Configures whether the operation should fail if the command exit value/status is not 0.Default is
TRUE- Parameters:
failOnExit- The fail on exit toggle- Returns:
- this operation instance.
-
fromProject
Configures an Exec operation from aBaseProject.The
work directoryis automatically set to the project's working directory.- Parameters:
project- the project, must not be null- Returns:
- this operation instance
- Throws:
NullPointerException- if project is null
-
inheritIO
Configures whether the child process should inherit the I/O streams of the current JVM.When
true, the child process uses the same stdin, stdout, and stderr as the current Java process. This enables interactive commands, preserves ANSI colors, and allows progress bars to display correctly. Output is not captured by the logger and cannot be asserted in tests.When
false, stdout and stderr are merged and captured through the logger. This makes output testable and keeps it in the build log, but breaks interactive prompts and ANSI formatting.Default is
TRUE- Parameters:
inheritIO-trueto inherit I/O,falseto capture output- Returns:
- this operation instance
-
isFailOnExit
public boolean isFailOnExit()Returns whether the operation should fail if the command exit value/status is not 0.- Returns:
trueorfalse
-
isInheritIO
public boolean isInheritIO()Returns whether the child process inherits the I/O streams of the current JVM.- Returns:
trueif I/O is inherited (default),falseif output is captured- See Also:
-
onLinux
If the current OS is Linux, configures the command and arguments to be executed. If not Linux, this call is ignored.- Parameters:
args- the command to use on Linux, must not be null or contain null/empty elements- Returns:
- this operation instance
- Throws:
NullPointerException- if args is nullIllegalArgumentException- if args contains null or empty elements- See Also:
-
onLinux
If the current OS is Linux, configures the command and arguments to be executed. If not Linux, this call is ignored.- Parameters:
args- the command to use on Linux, must not be null or contain null/empty elements- Returns:
- this operation instance
- Throws:
NullPointerException- if args is nullIllegalArgumentException- if args contains null or empty elements- See Also:
-
onMacOS
If the current OS is macOS, configures the command and arguments to be executed. If not macOS, this call is ignored.- Parameters:
args- the command to use on macOS, must not be null or contain null/empty elements- Returns:
- this operation instance
- Throws:
NullPointerException- if args is nullIllegalArgumentException- if args contains null or empty elements- See Also:
-
onMacOS
If the current OS is macOS, configures the command and arguments to be executed. If not macOS, this call is ignored.- Parameters:
args- the command to use on macOS, must not be null or contain null/empty elements- Returns:
- this operation instance
- Throws:
NullPointerException- if args is nullIllegalArgumentException- if args contains null or empty elements- See Also:
-
onUnix
If the current OS is Unix-like (Linux, macOS, FreeBSD, Solaris, AIX), configures the command and arguments to be executed. If Windows, this call is ignored.This is a convenience method for commands that work on all Unix-like systems.
- Parameters:
args- the command to use on Unix-like systems, must not be null or contain null/empty elements- Returns:
- this operation instance
- Throws:
NullPointerException- if args is nullIllegalArgumentException- if args contains null or empty elements- See Also:
-
onUnix
If the current OS is Unix-like, configures the command and arguments to be executed. If Windows, this call is ignored.- Parameters:
args- the command to use on Unix-like systems, must not be null or contain null/empty elements- Returns:
- this operation instance
- Throws:
NullPointerException- if args is nullIllegalArgumentException- if args contains null or empty elements- See Also:
-
onWindows
If the current OS is Windows, configures the command and arguments to be executed. If not Windows, this call is ignored.Allows platform-specific commands to be declared fluently:
new ExecOperation() .fromProject(this) .onWindows("cmd", "/c", "build.bat") .onUnix("./build.sh") .execute();Note: If multiple matching calls are made, the last one wins.
- Parameters:
args- the command to use on Windows, must not be null or contain null/empty elements- Returns:
- this operation instance
- Throws:
NullPointerException- if args is nullIllegalArgumentException- if args contains null or empty elements- See Also:
-
onWindows
If the current OS is Windows, configures the command and arguments to be executed. If not Windows, this call is ignored.- Parameters:
args- the command to use on Windows, must not be null or contain null/empty elements- Returns:
- this operation instance
- Throws:
NullPointerException- if args is nullIllegalArgumentException- if args contains null or empty elements- See Also:
-
outputConsumer
Sets a consumer to receive output lines when not inheriting I/O.Only called when
isInheritIO()isfalse. Default logs at INFO level.- Parameters:
outputConsumer- the output consumer, must not be null- Returns:
- this operation instance
- Throws:
NullPointerException- if outputConsumer is null
-
timeout
Configure the command timeout.- Parameters:
timeout- The timeout in seconds, use a negative number for no timeout- Returns:
- this operation instance
- Throws:
IllegalArgumentException- if timeout is 0
-
timeout
public long timeout()Returns the command timeout.- Returns:
- the timeout in seconds
-
workDir
Configures the working directory.- Parameters:
dir- the directory, must not be null- Returns:
- this operation instance
- Throws:
NullPointerException- if dir is null
-
workDir
Configures the working directory.- Parameters:
dir- the directory, must not be null- Returns:
- this operation instance
- Throws:
NullPointerException- if dir is null
-
workDir
Configures the working directory.- Parameters:
dir- the directory path, must not be null or empty- Returns:
- this operation instance
- Throws:
IllegalArgumentException- if dir is null or empty
-
workDir
Returns the working directory.- Returns:
- the directory, or
nullif not yet configured viafromProject(BaseProject)orworkDir(File)
-