Skip to content

Commit

Permalink
Added some env. vars. to be given to scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielKeep committed Sep 17, 2017
1 parent 896653e commit a03053a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

# v0.2.7

**New:**

* Several environment variables are now given to scripts by `cargo-script`.

# v0.2.6

**Fixed:**
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ $ cat now.crs | cargo script --count --loop \
5: }
```

### Environment Variables

The following environment variables are provided to scripts by `cargo-script`:

- `CARGO_SCRIPT_BASE_PATH`: the base path used by `cargo-script` to resolve relative dependency paths. Note that this is *not* necessarily the same as either the working directory, or the directory in which the script is being compiled.

- `CARGO_SCRIPT_PKG_NAME`: the generated package name of the script.

- `CARGO_SCRIPT_SAFE_NAME`: the file name of the script (sans file extension) being run. For scripts, this is derived from the script's filename. May also be `"expr"` or `"loop"` for those invocations.

- `CARGO_SCRIPT_SCRIPT_PATH`: absolute path to the script being run, assuming one exists. Set to the empty string for expressions.

### Templates

You can use templates to avoid having to re-specify common code and dependencies. You can view a list of your templates by running `cargo-script templates list` (note the hyphen), or show the folder in which they should be stored by running `cargo-script templates show`. You can dump the contents of a template using `cargo-script templates dump NAME`.
Expand Down
33 changes: 30 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,19 +624,33 @@ fn try_main() -> Result<i32> {

// Run it!
if action.execute {
fn hint<F: FnOnce(&mut Command) -> &mut Command>(f: F) -> F { f }
let add_env = hint(move |cmd| {
cmd.env("CARGO_SCRIPT_SCRIPT_PATH", input.path().unwrap_or(Path::new("")));
cmd.env("CARGO_SCRIPT_SAFE_NAME", input.safe_name());
cmd.env("CARGO_SCRIPT_PKG_NAME", input.package_name());
cmd.env("CARGO_SCRIPT_BASE_PATH", input.base_path());
cmd
});

if action.build_kind.can_exec_directly() {
let exe_path = try!(get_exe_path(action.build_kind, &action.pkg_path));
info!("executing {:?}", exe_path);
match try!(Command::new(exe_path).args(&args.args).status()
.map(|st| st.code().unwrap_or(1)))
{
match try!({
Command::new(exe_path)
.args(&args.args)
.chain_map(add_env)
.status()
.map(|st| st.code().unwrap_or(1))
}) {
0 => (),
n => return Ok(n)
}
} else {
let cmd_name = action.build_kind.exec_command();
info!("running `cargo {}`", cmd_name);
let mut cmd = try!(action.cargo(cmd_name));
add_env(&mut cmd);
match try!(cmd.status().map(|st| st.code().unwrap_or(1))) {
0 => (),
n => return Ok(n)
Expand Down Expand Up @@ -1277,6 +1291,19 @@ pub enum Input<'a> {
}

impl<'a> Input<'a> {
/**
Return the path to the script, if it has one.
*/
pub fn path(&self) -> Option<&Path> {
use Input::*;

match *self {
File(_, path, _, _) => Some(path),
Expr(..) => None,
Loop(..) => None,
}
}

/**
Return the "safe name" for the input. This should be filename-safe.
Expand Down
12 changes: 12 additions & 0 deletions tests/data/script-cs-env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::env;

fn main() {
println!("--output--");
let path = env::var("CARGO_SCRIPT_SCRIPT_PATH").expect("CSSP wasn't set");
assert!(path.ends_with("script-cs-env.rs"));
assert_eq!(env::var("CARGO_SCRIPT_SAFE_NAME"), Ok("script-cs-env".into()));
assert_eq!(env::var("CARGO_SCRIPT_PKG_NAME"), Ok("script-cs-env".into()));
let base_path = env::var("CARGO_SCRIPT_BASE_PATH").expect("CSBP wasn't set");
assert!(base_path.ends_with("data"));
println!("Ok");
}
10 changes: 10 additions & 0 deletions tests/tests/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,13 @@ fn test_script_slow_output() {
("Ok") => ()
).unwrap()
}

#[test]
fn test_script_cs_env() {
let out = cargo_script!(
"tests/data/script-cs-env.rs"
).unwrap();
scan!(out.stdout_output();
("Ok") => ()
).unwrap()
}

0 comments on commit a03053a

Please sign in to comment.