Title: | Execute Native Scripts |
---|---|
Description: | Run complex native scripts with a single command, similar to system commands. |
Authors: | Sagie Gur-Ari [aut, cre] |
Maintainer: | Sagie Gur-Ari <[email protected]> |
License: | Apache License 2.0 |
Version: | 0.3.1 |
Built: | 2025-03-09 03:11:00 UTC |
Source: | https://github.com/sagiegurari/scriptexec |
Builds the output structure.
build_output(output, wait)
build_output(output, wait)
output |
The invocation output |
wait |
A TRUE/FALSE parameter, indicating whether the function should wait for the command to finish, or run it asynchronously |
The script output structure
output <- c('line 1', '\n', 'line 2') attr(output, 'status') <- 15 script_output <- build_output(output)
output <- c('line 1', '\n', 'line 2') attr(output, 'status') <- 15 script_output <- build_output(output)
Creates a temporary file, writes the provided script content into it and returns the file name.
create_script_file(script = "")
create_script_file(script = "")
script |
The script text |
The temporary file name
filename <- create_script_file('echo test')
filename <- create_script_file('echo test')
Returns the system call arguments.
create_system_call_args(command, cli_args, wait, env, is_windows_os)
create_system_call_args(command, cli_args, wait, env, is_windows_os)
command |
The command to invoke |
cli_args |
Possible list of command line arguments |
wait |
A TRUE/FALSE parameter, indicating whether the function should wait for the command to finish, or run it asynchronously |
env |
Optional character vector of name=value strings to set environment variables |
is_windows_os |
True if windows based OS, false for unix based OS |
The system call arguments
filename <- './myfile.sh' arg_list <- create_system_call_args('sh', c(filename), TRUE, character(), FALSE)
filename <- './myfile.sh' arg_list <- create_system_call_args('sh', c(filename), TRUE, character(), FALSE)
Executes a script and returns the output. The stdout and stderr are captured and returned. In case of errors, the exit code will return in the status field.
execute(script = "", args = c(), env = character(), wait = TRUE, runner = NULL, print_commands = FALSE, get_runtime_script = FALSE)
execute(script = "", args = c(), env = character(), wait = TRUE, runner = NULL, print_commands = FALSE, get_runtime_script = FALSE)
script |
The script text |
args |
Optional script command line arguments (arguments are added as variables in the script named ARG1, ARG2, ...) |
env |
Optional character vector of name=value strings to set environment variables |
wait |
A TRUE/FALSE parameter, indicating whether the function should wait for the command to finish, or run it asynchronously (output status will be -1) |
runner |
The executable used to invoke the script (by default cmd.exe for windows, sh for other platforms) |
print_commands |
True if to print each command before invocation (not available for windows) |
get_runtime_script |
True to return the actual invoked script in a script output parameter |
The process output, status code (in case wait=TRUE), error message (in case of any errors) and invoked script in the form of list(status = status, output = output_text, error = error_message, script = script)
library('scriptexec') library('testthat') # execute script text output <- scriptexec::execute('echo command1\necho command2') expect_equal(output$status, 0) expect_equal(grepl('command1', output$output), TRUE) expect_equal(grepl('command2', output$output), TRUE) if (.Platform$OS.type == 'windows') { ls_command <- 'dir' } else { ls_command <- 'ls' } output <- scriptexec::execute(c('echo user home:', ls_command)) expect_equal(output$status, 0) # execute multiple commands as a script output <- scriptexec::execute(c('cd', 'echo test')) expect_equal(output$status, 0) # pass arguments (later defined as ARG1, ARG2, ...) and env vars if (.Platform$OS.type == 'windows') { command <- 'echo %ARG1% %ARG2% %MYENV%' } else { command <- 'echo $ARG1 $ARG2 $MYENV' } output <- scriptexec::execute(command, args = c('TEST1', 'TEST2'), env = c('MYENV=TEST3')) expect_equal(output$status, 0) expect_equal(grepl('TEST1 TEST2 TEST3', output$output), TRUE) # non zero status code is returned in case of errors expect_warning(output <- scriptexec::execute('exit 1')) expect_equal(output$status, 1) # do not wait for command to finish output <- scriptexec::execute('echo my really long task', wait = FALSE) expect_equal(output$status, -1)
library('scriptexec') library('testthat') # execute script text output <- scriptexec::execute('echo command1\necho command2') expect_equal(output$status, 0) expect_equal(grepl('command1', output$output), TRUE) expect_equal(grepl('command2', output$output), TRUE) if (.Platform$OS.type == 'windows') { ls_command <- 'dir' } else { ls_command <- 'ls' } output <- scriptexec::execute(c('echo user home:', ls_command)) expect_equal(output$status, 0) # execute multiple commands as a script output <- scriptexec::execute(c('cd', 'echo test')) expect_equal(output$status, 0) # pass arguments (later defined as ARG1, ARG2, ...) and env vars if (.Platform$OS.type == 'windows') { command <- 'echo %ARG1% %ARG2% %MYENV%' } else { command <- 'echo $ARG1 $ARG2 $MYENV' } output <- scriptexec::execute(command, args = c('TEST1', 'TEST2'), env = c('MYENV=TEST3')) expect_equal(output$status, 0) expect_equal(grepl('TEST1 TEST2 TEST3', output$output), TRUE) # non zero status code is returned in case of errors expect_warning(output <- scriptexec::execute('exit 1')) expect_equal(output$status, 1) # do not wait for command to finish output <- scriptexec::execute('echo my really long task', wait = FALSE) expect_equal(output$status, -1)
Generates and returns a script which sets up the env vars for the script arguments
generate_args_setup_script(args = character())
generate_args_setup_script(args = character())
args |
Optional script command line arguments |
The script text which sets up the env vars for the script arguments
script <- generate_args_setup_script(args = c('first', 'second'))
script <- generate_args_setup_script(args = c('first', 'second'))
Generates and returns a script which sets up the env vars for the script execution.
generate_env_setup_script(env = character())
generate_env_setup_script(env = character())
env |
Optional character vector of name=value strings to set environment variables |
The script text which sets up the env
script <- generate_env_setup_script(c('ENV_TEST=MYENV'))
script <- generate_env_setup_script(c('ENV_TEST=MYENV'))
Returns the command and arguments needed to execute the provided script file on the current platform.
get_command(filename, runner = NULL)
get_command(filename, runner = NULL)
filename |
The script file to execute |
runner |
The executable used to invoke the script (by default cmd.exe for windows, sh for other platforms) |
A list holding the command and arguments
command_struct <- get_command('myfile.sh') command <- command_struct$command cli_args <- command_struct$args
command_struct <- get_command('myfile.sh') command <- command_struct$command cli_args <- command_struct$args
Returns the value based on the current platform.
get_platform_value(unix_value = c(), windows_value = c(), force_windows = FALSE)
get_platform_value(unix_value = c(), windows_value = c(), force_windows = FALSE)
unix_value |
The unix platform value |
windows_value |
The windows platform value |
force_windows |
True to force windows (defaulted to OS validation) |
unix_value in case of unix system, else the windows_value
platform_value <- get_platform_value('.sh', '.bat')
platform_value <- get_platform_value('.sh', '.bat')
Returns true if windows, else false.
is_windows()
is_windows()
True if windows, else false.
windows <- is_windows()
windows <- is_windows()
Modifies the provided script text and ensures the script content is executed in the correct location.
modify_script(script, args = c(), env = character(), print_commands = FALSE, is_windows_os = FALSE)
modify_script(script, args = c(), env = character(), print_commands = FALSE, is_windows_os = FALSE)
script |
The script text |
args |
Optional script command line arguments |
env |
Optional character vector of name=value strings to set environment variables |
print_commands |
True if to print each command before invocation (not available for windows) |
is_windows_os |
True if windows based OS, false for unix based OS |
The modified script text
script <- modify_script(script = 'echo test', args = c('first', 'second'), env = c('MYENV=MYENV'))
script <- modify_script(script = 'echo test', args = c('first', 'second'), env = c('MYENV=MYENV'))
Internal error handler.
on_invoke_error(error)
on_invoke_error(error)
error |
The invocation error |
The invocation output
This package provides one main function: execute which executes the provided script and returns its output.