Crates.io | cargo-run |
lib.rs | cargo-run |
version | 0.4.0 |
source | src |
created_at | 2024-06-26 07:31:51.862215 |
updated_at | 2024-07-03 07:30:28.013442 |
description | A CLI tool to run custom scripts in Rust, defined in Scripts.toml |
homepage | https://github.com/rsaz/cargo-script |
repository | https://github.com/rsaz/cargo-script |
max_upload_size | |
id | 1284240 |
size | 52,088 |
A CLI tool to run custom scripts in Rust, defined in Scripts.toml
.
Scripts.toml
.Scripts.toml
file with default content.include
feature.To install cargo-run
, use the following command:
cargo install cargo-run
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.
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:
cgs init
Default Scripts.toml
content:
[global_env]
[scripts]
dev = "cargo run"
build = { command = "cargo build", env = { RUST_LOG = "info" } }
release = "cargo build --release"
test = { command = "cargo test", env = { RUST_LOG = "warn" } }
doc = "cargo doc --no-deps --open"
To run a script, use the following command:
cgs run <script_name>
Scripts.toml
The Scripts.toml
file is used to define scripts. The file is located in the root of the project directory. Here are all the possible configurations for a script:
The following is an example of a Scripts.toml
file:
A simple script that runs a command directly.
[scripts]
build = "echo 'build'"
You can specify an interpreter for the script.
[scripts]
config = { interpreter = "bash", command = "echo 'test'", info = "Script to test" }
You can chain multiple scripts together using the include feature.
[scripts]
release = { include = ["i_am_shell", "build"] }
A detailed script can include interpreter, command, info, and other scripts to run.
[scripts]
i_am_shell_obj = { interpreter = "bash", command = "./.scripts/i_am_shell.sh", info = "Detect shell script" }
You can add info to a script to provide more details about the script.
[scripts]
build = { command = "cargo build", info = "Build the project" }
You can define global environment variables that will be available to all scripts. Script-specific environment variables can override these global variables.
[global_env]
RUST_BACKTRACE = "1"
EXAMPLE_VAR = "example_value"
You can define script-specific environment variables that will override global environment variables.
[scripts]
example01 = { command = "echo $EXAMPLE_VAR", env = { EXAMPLE_VAR = "change_value" } }
example02 = { command = "echo ${RUST_LOG:-unset} ${COMMON_VAR:-unset}", env = { RUST_LOG = "warn" } }
example03 = { command = "echo ${EXAMPLE_VAR:-unset} ${RUST_LOG:-unset} ${COMMON_VAR:-unset}", env = { EXAMPLE_VAR = "change_value_again", RUST_LOG = "info" } }
The precedence order for environment variables is as follows:
This order ensures that command-line overrides have the highest precedence, followed by script-specific variables, and finally global variables.
To run a script and override environment variables from the command line, use the following format:
cgs run <script_name> --env <ENV_VAR1>=<value1>
You can specify the required versions of tools and toolchains for your scripts. If the requirements are not met, the script will not run.
Inline Configuration:
[scripts]
deploy = { command = "./deploy.sh", requires = ["docker>=19.03", "kubectl>=1.18"], info = "Deployment script", env = { EXAMPLE_VAR = "deploy_value" } }
Detailed Configuration:
[scripts.build]
command = "cargo build"
info = "Run cargo build"
[scripts.test01]
command = "cargo run"
requires = ["rustup < 1.24.3"]
toolchain = "stable"
info = "Build project with nightly toolchain"
env = { EXAMPLE_VAR = "build_value" }
[scripts.build_with_python]
command = "python setup.py build"
requires = ["python >= 3.8"]
toolchain = "python:3.8"
info = "Build project with Python 3.8"
env = { EXAMPLE_VAR = "build_with_python" }
To show all the scripts and their details, use the following command:
cgs show
cargo-script
or cgs
.Scripts.toml
: Explains the purpose of the init
command and provides the command to initialize the file.Scripts.toml
Content: Shows the default content created by the init
command.Scripts.toml
: Details different configurations possible in the Scripts.toml
file, including simple scripts, scripts with interpreters, chained scripts, and detailed scripts.Scripts.toml
File: Provides a complete example of a Scripts.toml
file.Scripts.toml
file.