Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for -w/--wrapper #112

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ jobs:
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ matrix.rust }}
- name: Install just
uses: taiki-e/install-action@just
- name: Setup homebrew
uses: Homebrew/actions/setup-homebrew@master
- name: Install hyperfine and just
run: brew install hyperfine just
- name: Install rust-script
run: cargo install --path .
- name: Test examples
Expand Down
11 changes: 9 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ log = "0.4"
pulldown-cmark = "0.9"
regex = "1"
sha1 = "0.10"
shell-words = "1"
tempfile = "3"
toml = "0.7"

Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Useful command-line arguments:
- `--force`: Force the script to be rebuilt. Useful if you want to force a recompile with a different toolchain.
- `--package`: Generate the Cargo package and print the path to it - but don't compile or run it. Effectively "unpacks" the script into a Cargo package.
- `--test`: Compile and run tests.
- `--wrapper`: Add a wrapper around the executable. Can be used to run debugging with e.g. `rust-script --wrapper rust-lldb my-script.rs` or benchmarking with `rust-script --wrapper "hyperfine --runs 100" my-script.rs`

## Executable Scripts

Expand Down
13 changes: 13 additions & 0 deletions examples/fib.ers
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env rust-script

fn fib(n: i32) -> i32 {
match n {
1 | 2 => 1,
_ => fib(n-1) + fib(n-2)
}
}

fn main() {
assert_eq!(fib(30), 832040);
}

11 changes: 11 additions & 0 deletions examples/test-examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@ assert_equals() {
assert_equals "result: 3" "$(just -f justfile/Justfile)"
assert_equals "hello, rust" "$(./hello.ers)"
assert_equals "hello, rust" "$(./hello-without-main.ers)"

HYPERFINE_OUTPUT=$(rust-script --wrapper "hyperfine --runs 99" fib.ers)

case "$HYPERFINE_OUTPUT" in
*"99 runs"*)
;;
*)
echo "Hyperfine output: $HYPERFINE_OUTPUT"
exit 1
;;
esac
10 changes: 9 additions & 1 deletion src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Args {
pub install_file_association: bool,
#[cfg(windows)]
pub uninstall_file_association: bool,
pub wrapper: Option<String>,
}

impl Args {
Expand Down Expand Up @@ -75,7 +76,7 @@ impl Args {
.help("Base path for resolving dependencies")
.short('b')
.long("base-path")
.num_args(1..=1)
.num_args(1)
)
.arg(Arg::new("cargo-output")
.help("Show output from cargo when building")
Expand Down Expand Up @@ -163,6 +164,12 @@ impl Args {
.num_args(1)
// Benchmarking currently requires nightly:
.conflicts_with("bench")
)
.arg(Arg::new("wrapper")
.help("Wrapper injected before the command to run, e.g. 'rust-lldb' or 'hyperfine --runs 100'")
.long("wrapper")
.short('w')
.num_args(1)
);

#[cfg(windows)]
Expand Down Expand Up @@ -240,6 +247,7 @@ impl Args {
install_file_association: m.get_flag("install-file-association"),
#[cfg(windows)]
uninstall_file_association: m.get_flag("uninstall-file-association"),
wrapper: m.get_one::<String>("wrapper").map(Into::into),
}
}
}
35 changes: 27 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,14 @@ fn try_main() -> MainResult<i32> {
return Ok(0);
}

let mut cmd = action.command_to_execute(&args.script_args, args.wrapper)?;
#[cfg(unix)]
{
let mut cmd = action.cargo(&args.script_args)?;
let err = cmd.exec();
Err(MainError::from(err))
}
#[cfg(not(unix))]
{
let mut cmd = action.cargo(&args.script_args)?;
let exit_code = cmd.status().map(|st| st.code().unwrap_or(1))?;
Ok(exit_code)
}
Expand Down Expand Up @@ -332,7 +331,11 @@ impl InputAction {
self.pkg_path.join("Cargo.toml")
}

fn cargo(&self, script_args: &[String]) -> MainResult<Command> {
fn command_to_execute(
&self,
script_args: &[String],
wrapper: Option<String>,
) -> MainResult<Command> {
let release_mode = !self.debug && !matches!(self.build_kind, BuildKind::Bench);

let built_binary_path = platform::binary_cache_path()
Expand All @@ -351,9 +354,25 @@ impl InputAction {
let manifest_path = self.manifest_path();

let execute_command = || {
let mut cmd = Command::new(&built_binary_path);
cmd.args(script_args.iter());
cmd
if let Some(wrapper) = wrapper {
let wrapper_words = shell_words::split(&wrapper).unwrap();
if wrapper_words.is_empty() {
return MainResult::Err(MainError::OtherBorrowed(
"The wrapper cannot be empty",
));
}
let mut cmd = Command::new(&wrapper_words[0]);
if wrapper_words.len() > 1 {
cmd.args(wrapper_words[1..].iter());
}
cmd.arg(&built_binary_path);
cmd.args(script_args.iter());
Ok(cmd)
} else {
let mut cmd = Command::new(&built_binary_path);
cmd.args(script_args.iter());
Ok(cmd)
}
};

if matches!(self.build_kind, BuildKind::Normal) && !self.force_compile {
Expand All @@ -376,7 +395,7 @@ impl InputAction {
&& built_binary_time.cmp(&manifest_mtime).is_ge()
{
debug!("Keeping old binary");
return Ok(execute_command());
return execute_command();
} else {
debug!("Old binary too old - rebuilding");
}
Expand Down Expand Up @@ -423,7 +442,7 @@ impl InputAction {

if matches!(self.build_kind, BuildKind::Normal) {
if cmd.status()?.code() == Some(0) {
cmd = execute_command();
cmd = execute_command()?;
} else {
return Err(MainError::OtherOwned("Could not execute cargo".to_string()));
}
Expand Down