Skip to content

Commit a03053a

Browse files
committed
Added some env. vars. to be given to scripts.
Closes DanielKeep#49.
1 parent 896653e commit a03053a

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11

2+
# v0.2.7
3+
4+
**New:**
5+
6+
* Several environment variables are now given to scripts by `cargo-script`.
7+
28
# v0.2.6
39

410
**Fixed:**

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ $ cat now.crs | cargo script --count --loop \
155155
5: }
156156
```
157157

158+
### Environment Variables
159+
160+
The following environment variables are provided to scripts by `cargo-script`:
161+
162+
- `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.
163+
164+
- `CARGO_SCRIPT_PKG_NAME`: the generated package name of the script.
165+
166+
- `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.
167+
168+
- `CARGO_SCRIPT_SCRIPT_PATH`: absolute path to the script being run, assuming one exists. Set to the empty string for expressions.
169+
158170
### Templates
159171

160172
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`.

src/main.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,19 +624,33 @@ fn try_main() -> Result<i32> {
624624

625625
// Run it!
626626
if action.execute {
627+
fn hint<F: FnOnce(&mut Command) -> &mut Command>(f: F) -> F { f }
628+
let add_env = hint(move |cmd| {
629+
cmd.env("CARGO_SCRIPT_SCRIPT_PATH", input.path().unwrap_or(Path::new("")));
630+
cmd.env("CARGO_SCRIPT_SAFE_NAME", input.safe_name());
631+
cmd.env("CARGO_SCRIPT_PKG_NAME", input.package_name());
632+
cmd.env("CARGO_SCRIPT_BASE_PATH", input.base_path());
633+
cmd
634+
});
635+
627636
if action.build_kind.can_exec_directly() {
628637
let exe_path = try!(get_exe_path(action.build_kind, &action.pkg_path));
629638
info!("executing {:?}", exe_path);
630-
match try!(Command::new(exe_path).args(&args.args).status()
631-
.map(|st| st.code().unwrap_or(1)))
632-
{
639+
match try!({
640+
Command::new(exe_path)
641+
.args(&args.args)
642+
.chain_map(add_env)
643+
.status()
644+
.map(|st| st.code().unwrap_or(1))
645+
}) {
633646
0 => (),
634647
n => return Ok(n)
635648
}
636649
} else {
637650
let cmd_name = action.build_kind.exec_command();
638651
info!("running `cargo {}`", cmd_name);
639652
let mut cmd = try!(action.cargo(cmd_name));
653+
add_env(&mut cmd);
640654
match try!(cmd.status().map(|st| st.code().unwrap_or(1))) {
641655
0 => (),
642656
n => return Ok(n)
@@ -1277,6 +1291,19 @@ pub enum Input<'a> {
12771291
}
12781292

12791293
impl<'a> Input<'a> {
1294+
/**
1295+
Return the path to the script, if it has one.
1296+
*/
1297+
pub fn path(&self) -> Option<&Path> {
1298+
use Input::*;
1299+
1300+
match *self {
1301+
File(_, path, _, _) => Some(path),
1302+
Expr(..) => None,
1303+
Loop(..) => None,
1304+
}
1305+
}
1306+
12801307
/**
12811308
Return the "safe name" for the input. This should be filename-safe.
12821309

tests/data/script-cs-env.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::env;
2+
3+
fn main() {
4+
println!("--output--");
5+
let path = env::var("CARGO_SCRIPT_SCRIPT_PATH").expect("CSSP wasn't set");
6+
assert!(path.ends_with("script-cs-env.rs"));
7+
assert_eq!(env::var("CARGO_SCRIPT_SAFE_NAME"), Ok("script-cs-env".into()));
8+
assert_eq!(env::var("CARGO_SCRIPT_PKG_NAME"), Ok("script-cs-env".into()));
9+
let base_path = env::var("CARGO_SCRIPT_BASE_PATH").expect("CSBP wasn't set");
10+
assert!(base_path.ends_with("data"));
11+
println!("Ok");
12+
}

tests/tests/script.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,13 @@ fn test_script_slow_output() {
9292
("Ok") => ()
9393
).unwrap()
9494
}
95+
96+
#[test]
97+
fn test_script_cs_env() {
98+
let out = cargo_script!(
99+
"tests/data/script-cs-env.rs"
100+
).unwrap();
101+
scan!(out.stdout_output();
102+
("Ok") => ()
103+
).unwrap()
104+
}

0 commit comments

Comments
 (0)