cargo-run
A CLI tool to run custom scripts in Rust, defined in Scripts.toml.
Features
- Run scripts defined in
Scripts.toml. - Specify interpreters for scripts (e.g., bash, zsh, PowerShell).
- Initialize a
Scripts.tomlfile with default content. - Chain multiple scripts together using the
includefeature. - Set global environment variables and script-specific environment variables with precedence handling.
Installation
To install cargo-run, use the following command:
Usage
When cargo-run crate is installed it provides a binary cargo-script or cgs to run custom scripts. Commands can start with cargo-script or cgs.
The examples below use cgs as the command prefix for simplicity.
Initialize Scripts.toml
The init command initializes a Scripts.toml file in the root of your project directory with default content. This file is used to define and manage your custom scripts.
To initialize a Scripts.toml file, use the following command:
Default Scripts.toml content:
[]
[]
= "cargo run"
= { = "cargo build", = { = "info" } }
= "cargo build --release"
= { = "cargo test", = { = "warn" } }
= "cargo doc --no-deps --open"
Run a Script
To run a script, use the following command:
Understanding Scripts.toml
The Scripts.toml file is used to define scripts. The file is located in the root of the project directory. The following is an example of a Scripts.toml file:
Simple Script
A simple script that runs a command directly.
[]
= "echo 'build'"
Script with Interpreter
You can specify an interpreter for the script.
[]
= { = "bash", = "echo 'test'", = "Script to test" }
Chain of Scripts
You can chain multiple scripts together using the include feature.
[]
= { = ["i_am_shell", "build"] }
Detailed Script
A detailed script can include interpreter, command, info, and other scripts to run.
[]
= { = "bash", = "./.scripts/i_am_shell.sh", = "Detect shell script" }
Add info to a script
You can add info to a script to provide more details about the script.
[]
= { = "cargo build", = "Build the project" }
Global Environment Variables
You can define global environment variables that will be available to all scripts. Script-specific environment variables can override these global variables.
[]
= "1"
= "example_value"
Script-Specific Environment Variables
You can define script-specific environment variables that will override global environment variables.
[]
= { = "echo $EXAMPLE_VAR", = { = "change_value" } }
= { = "echo ${RUST_LOG:-unset} ${COMMON_VAR:-unset}", = { = "warn" } }
= { = "echo ${EXAMPLE_VAR:-unset} ${RUST_LOG:-unset} ${COMMON_VAR:-unset}", = { = "change_value_again", = "info" } }
Environment Variables Precedence
The precedence order for environment variables is as follows:
- Command-line overrides: Environment variables passed through the command line when running a script.
- Script-specific environment variables: Variables defined in the env section of a script.
- Global environment variables: Variables defined in the [global_env] section.
This order ensures that command-line overrides have the highest precedence, followed by script-specific variables, and finally global variables.
Running a Script with Environment Variables
To run a script and override environment variables from the command line, use the following format:
Explanation
- Features: Summarizes the main features of the tool.
- Installation: Provides the command to install the tool.
- Usage: Explains how to run scripts using
cargo-scriptorcgs. - Initializing
Scripts.toml: Explains the purpose of theinitcommand and provides the command to initialize the file. - Default
Scripts.tomlContent: Shows the default content created by theinitcommand. - Understanding
Scripts.toml: Details different configurations possible in theScripts.tomlfile, including simple scripts, scripts with interpreters, chained scripts, and detailed scripts. - Example
Scripts.tomlFile: Provides a complete example of aScripts.tomlfile. - Example Usage: Shows how to run scripts and initialize the
Scripts.tomlfile. - Global Environment Variables: Explains how to define global environment variables.
- Script-Specific Environment Variables: Explains how to define script-specific environment variables.
- Environment Variables Precedence: Explains the order of precedence for environment variables.