Skip to content

Commit

Permalink
Add -b/--base-path option
Browse files Browse the repository at this point in the history
Fixes #109.
  • Loading branch information
fornwall committed Jul 27, 2023
1 parent ce7fb0d commit 1cab0a5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Args {
pub expr: bool,
pub loop_: bool,
pub count: bool,
pub base_path: Option<String>,
pub pkg_path: Option<String>,
pub gen_pkg_only: bool,
pub cargo_output: bool,
Expand Down Expand Up @@ -70,6 +71,12 @@ impl Args {
)

// Options that impact the script being executed.
.arg(Arg::new("base-path")
.help("Base path for resolving dependencies")
.short('b')
.long("base-path")
.num_args(0..=1)
)
.arg(Arg::new("cargo-output")
.help("Show output from cargo when building")
.short('c')
Expand Down Expand Up @@ -208,6 +215,7 @@ impl Args {
loop_: m.get_flag("loop"),
count: m.get_flag("count"),

base_path: m.get_one::<String>("base-path").map(Into::into),
pkg_path: m.get_one::<String>("pkg_path").map(Into::into),
gen_pkg_only: m.get_flag("gen_pkg_only"),
cargo_output: m.get_flag("cargo-output"),
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,14 @@ fn decide_action_for(

let script_name = format!("{}.rs", input.safe_name());

let base_path = match &args.base_path {
Some(path) => Path::new(path).into(),
None => input.base_path(),
};

let (mani_str, script_path, script_str) = manifest::split_input(
input,
&base_path,
&deps,
&prelude,
&pkg_path,
Expand Down
31 changes: 17 additions & 14 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ Splits input into a complete Cargo manifest and unadultered Rust source.
Unless we have prelude items to inject, in which case it will be *slightly* adulterated.
*/
#[allow(clippy::too_many_arguments)]
pub fn split_input(
input: &Input,
base_path: &Path,
deps: &[(String, String)],
prelude_items: &[String],
package_path: impl AsRef<Path>,
Expand Down Expand Up @@ -105,14 +107,14 @@ pub fn split_input(
};

// It's-a mergin' time!
let def_mani = default_manifest(input, bin_name, source_path_from_package, toolchain);
let def_mani = default_manifest(bin_name, source_path_from_package, toolchain);
let dep_mani = deps_manifest(deps)?;

let mani = merge_manifest(def_mani, part_mani)?;
let mani = merge_manifest(mani, dep_mani)?;

// Fix up relative paths.
let mani = fix_manifest_paths(mani, &input.base_path())?;
let mani = fix_manifest_paths(mani, base_path)?;

let mani_str = format!("{}", mani);
info!("manifest: {}", mani_str);
Expand All @@ -136,6 +138,7 @@ fn test_split_input() {
($i:expr) => {
split_input(
&$i,
&$i.base_path(),
&[],
&[],
"/package",
Expand Down Expand Up @@ -172,7 +175,7 @@ path = "/dummy/main.rs"
[package]
authors = ["Anonymous"]
edition = "2021"
name = "n"
name = "binary-name"
version = "0.1.0""#,
STRIP_SECTION
),
Expand All @@ -196,7 +199,7 @@ path = "/dummy/main.rs"
[package]
authors = ["Anonymous"]
edition = "2021"
name = "n"
name = "binary-name"
version = "0.1.0""#,
STRIP_SECTION
),
Expand All @@ -220,7 +223,7 @@ path = "/dummy/main.rs"
[package]
authors = ["Anonymous"]
edition = "2021"
name = "n"
name = "binary-name"
version = "0.1.0""#,
STRIP_SECTION
),
Expand All @@ -232,6 +235,7 @@ version = "0.1.0""#,
assert_eq!(
split_input(
&f(r#"fn main() {}"#),
&f(r#"fn main() {}"#).base_path(),
&[],
&[],
"",
Expand All @@ -252,7 +256,7 @@ path = "/dummy/main.rs"
[package]
authors = ["Anonymous"]
edition = "2021"
name = "n"
name = "binary-name"
version = "0.1.0"
[package.metadata.rustscript]
Expand Down Expand Up @@ -282,7 +286,7 @@ path = "/dummy/main.rs"
[package]
authors = ["Anonymous"]
edition = "2021"
name = "n"
name = "binary-name"
version = "0.1.0""#,
STRIP_SECTION
),
Expand All @@ -309,7 +313,7 @@ path = "/dummy/main.rs"
[package]
authors = ["Anonymous"]
edition = "2021"
name = "n"
name = "binary-name"
version = "0.1.0""#,
STRIP_SECTION
),
Expand All @@ -336,7 +340,7 @@ time = "0.1.25"
[package]
authors = ["Anonymous"]
edition = "2021"
name = "n"
name = "binary-name"
version = "0.1.0""#,
STRIP_SECTION
),
Expand Down Expand Up @@ -364,7 +368,7 @@ time = "0.1.25"
[package]
authors = ["Anonymous"]
edition = "2021"
name = "n"
name = "binary-name"
version = "0.1.0""#,
STRIP_SECTION
),
Expand Down Expand Up @@ -398,7 +402,7 @@ time = "0.1.25"
[package]
authors = ["Anonymous"]
edition = "2021"
name = "n"
name = "binary-name"
version = "0.1.0""#,
STRIP_SECTION
),
Expand All @@ -422,7 +426,7 @@ path = "main.rs"
[package]
authors = ["Anonymous"]
edition = "2021"
name = "n"
name = "binary-name"
version = "0.1.0""#,
STRIP_SECTION
),
Expand Down Expand Up @@ -1136,15 +1140,14 @@ time = "*"
Generates a default Cargo manifest for the given input.
*/
fn default_manifest(
input: &Input,
bin_name: &str,
bin_source_path: &str,
toolchain: Option<String>,
) -> toml::value::Table {
let mut package_map = toml::map::Map::new();
package_map.insert(
"name".to_string(),
toml::value::Value::String(input.package_name()),
toml::value::Value::String(bin_name.to_owned()),
);
package_map.insert(
"version".to_string(),
Expand Down

0 comments on commit 1cab0a5

Please sign in to comment.