Skip to content

Commit 1cab0a5

Browse files
committed
Add -b/--base-path option
Fixes #109.
1 parent ce7fb0d commit 1cab0a5

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

src/arguments.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub struct Args {
99
pub expr: bool,
1010
pub loop_: bool,
1111
pub count: bool,
12+
pub base_path: Option<String>,
1213
pub pkg_path: Option<String>,
1314
pub gen_pkg_only: bool,
1415
pub cargo_output: bool,
@@ -70,6 +71,12 @@ impl Args {
7071
)
7172

7273
// Options that impact the script being executed.
74+
.arg(Arg::new("base-path")
75+
.help("Base path for resolving dependencies")
76+
.short('b')
77+
.long("base-path")
78+
.num_args(0..=1)
79+
)
7380
.arg(Arg::new("cargo-output")
7481
.help("Show output from cargo when building")
7582
.short('c')
@@ -208,6 +215,7 @@ impl Args {
208215
loop_: m.get_flag("loop"),
209216
count: m.get_flag("count"),
210217

218+
base_path: m.get_one::<String>("base-path").map(Into::into),
211219
pkg_path: m.get_one::<String>("pkg_path").map(Into::into),
212220
gen_pkg_only: m.get_flag("gen_pkg_only"),
213221
cargo_output: m.get_flag("cargo-output"),

src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,14 @@ fn decide_action_for(
472472

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

475+
let base_path = match &args.base_path {
476+
Some(path) => Path::new(path).into(),
477+
None => input.base_path(),
478+
};
479+
475480
let (mani_str, script_path, script_str) = manifest::split_input(
476481
input,
482+
&base_path,
477483
&deps,
478484
&prelude,
479485
&pkg_path,

src/manifest.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ Splits input into a complete Cargo manifest and unadultered Rust source.
1919
2020
Unless we have prelude items to inject, in which case it will be *slightly* adulterated.
2121
*/
22+
#[allow(clippy::too_many_arguments)]
2223
pub fn split_input(
2324
input: &Input,
25+
base_path: &Path,
2426
deps: &[(String, String)],
2527
prelude_items: &[String],
2628
package_path: impl AsRef<Path>,
@@ -105,14 +107,14 @@ pub fn split_input(
105107
};
106108

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

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

114116
// Fix up relative paths.
115-
let mani = fix_manifest_paths(mani, &input.base_path())?;
117+
let mani = fix_manifest_paths(mani, base_path)?;
116118

117119
let mani_str = format!("{}", mani);
118120
info!("manifest: {}", mani_str);
@@ -136,6 +138,7 @@ fn test_split_input() {
136138
($i:expr) => {
137139
split_input(
138140
&$i,
141+
&$i.base_path(),
139142
&[],
140143
&[],
141144
"/package",
@@ -172,7 +175,7 @@ path = "/dummy/main.rs"
172175
[package]
173176
authors = ["Anonymous"]
174177
edition = "2021"
175-
name = "n"
178+
name = "binary-name"
176179
version = "0.1.0""#,
177180
STRIP_SECTION
178181
),
@@ -196,7 +199,7 @@ path = "/dummy/main.rs"
196199
[package]
197200
authors = ["Anonymous"]
198201
edition = "2021"
199-
name = "n"
202+
name = "binary-name"
200203
version = "0.1.0""#,
201204
STRIP_SECTION
202205
),
@@ -220,7 +223,7 @@ path = "/dummy/main.rs"
220223
[package]
221224
authors = ["Anonymous"]
222225
edition = "2021"
223-
name = "n"
226+
name = "binary-name"
224227
version = "0.1.0""#,
225228
STRIP_SECTION
226229
),
@@ -232,6 +235,7 @@ version = "0.1.0""#,
232235
assert_eq!(
233236
split_input(
234237
&f(r#"fn main() {}"#),
238+
&f(r#"fn main() {}"#).base_path(),
235239
&[],
236240
&[],
237241
"",
@@ -252,7 +256,7 @@ path = "/dummy/main.rs"
252256
[package]
253257
authors = ["Anonymous"]
254258
edition = "2021"
255-
name = "n"
259+
name = "binary-name"
256260
version = "0.1.0"
257261
258262
[package.metadata.rustscript]
@@ -282,7 +286,7 @@ path = "/dummy/main.rs"
282286
[package]
283287
authors = ["Anonymous"]
284288
edition = "2021"
285-
name = "n"
289+
name = "binary-name"
286290
version = "0.1.0""#,
287291
STRIP_SECTION
288292
),
@@ -309,7 +313,7 @@ path = "/dummy/main.rs"
309313
[package]
310314
authors = ["Anonymous"]
311315
edition = "2021"
312-
name = "n"
316+
name = "binary-name"
313317
version = "0.1.0""#,
314318
STRIP_SECTION
315319
),
@@ -336,7 +340,7 @@ time = "0.1.25"
336340
[package]
337341
authors = ["Anonymous"]
338342
edition = "2021"
339-
name = "n"
343+
name = "binary-name"
340344
version = "0.1.0""#,
341345
STRIP_SECTION
342346
),
@@ -364,7 +368,7 @@ time = "0.1.25"
364368
[package]
365369
authors = ["Anonymous"]
366370
edition = "2021"
367-
name = "n"
371+
name = "binary-name"
368372
version = "0.1.0""#,
369373
STRIP_SECTION
370374
),
@@ -398,7 +402,7 @@ time = "0.1.25"
398402
[package]
399403
authors = ["Anonymous"]
400404
edition = "2021"
401-
name = "n"
405+
name = "binary-name"
402406
version = "0.1.0""#,
403407
STRIP_SECTION
404408
),
@@ -422,7 +426,7 @@ path = "main.rs"
422426
[package]
423427
authors = ["Anonymous"]
424428
edition = "2021"
425-
name = "n"
429+
name = "binary-name"
426430
version = "0.1.0""#,
427431
STRIP_SECTION
428432
),
@@ -1136,15 +1140,14 @@ time = "*"
11361140
Generates a default Cargo manifest for the given input.
11371141
*/
11381142
fn default_manifest(
1139-
input: &Input,
11401143
bin_name: &str,
11411144
bin_source_path: &str,
11421145
toolchain: Option<String>,
11431146
) -> toml::value::Table {
11441147
let mut package_map = toml::map::Map::new();
11451148
package_map.insert(
11461149
"name".to_string(),
1147-
toml::value::Value::String(input.package_name()),
1150+
toml::value::Value::String(bin_name.to_owned()),
11481151
);
11491152
package_map.insert(
11501153
"version".to_string(),

0 commit comments

Comments
 (0)