Skip to content

Commit ef1d3eb

Browse files
Add --toolchain-version argument
This allows the user to specify which toolchain to use to build their script. This allows for the use of nightly features, features only stabilized in beta, and even the use of custom-built toolchains if the need ever arises.
1 parent 88c57c3 commit ef1d3eb

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

src/main.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ struct Args {
5656
build_kind: BuildKind,
5757
template: Option<String>,
5858
list_templates: bool,
59+
// This is a String instead of an
60+
// enum since one can have custom
61+
// toolchains (ex. a rustc developer
62+
// will probably have `stage1`).
63+
toolchain_version: Option<String>,
5964

6065
#[cfg(windows)]
6166
install_file_association: bool,
@@ -235,6 +240,15 @@ fn parse_args() -> Args {
235240
.takes_value(true)
236241
.requires("expr")
237242
)
243+
.arg(Arg::new("toolchain-version")
244+
.about("Build the script using the given toolchain version.")
245+
.long("toolchain-version")
246+
// "channel"
247+
.short('c')
248+
.takes_value(true)
249+
// FIXME: remove if benchmarking is stabilized
250+
.conflicts_with("bench")
251+
)
238252
.arg(Arg::new("list-templates")
239253
.about("List the available templates.")
240254
.long("list-templates")
@@ -289,6 +303,7 @@ fn parse_args() -> Args {
289303
build_kind: BuildKind::from_flags(m.is_present("test"), m.is_present("bench")),
290304
template: m.value_of("template").map(Into::into),
291305
list_templates: m.is_present("list-templates"),
306+
toolchain_version: m.value_of("toolchain-version").map(Into::into),
292307
#[cfg(windows)]
293308
install_file_association: m.is_present("install-file-association"),
294309
#[cfg(windows)]
@@ -658,7 +673,12 @@ struct InputAction {
658673
*/
659674
using_cache: bool,
660675

661-
use_nightly: bool,
676+
/**
677+
Which toolchain the script should be built with.
678+
679+
`None` indicates that the script should be built with a stable toolchain.
680+
*/
681+
toolchain_version: Option<String>,
662682

663683
/// The package metadata structure for the current invocation.
664684
metadata: PackageMetadata,
@@ -685,7 +705,7 @@ impl InputAction {
685705
cargo(
686706
cmd,
687707
&*self.manifest_path().to_string_lossy(),
688-
self.use_nightly,
708+
self.toolchain_version.as_ref().map(|s| s.as_str()),
689709
&self.metadata,
690710
script_args,
691711
run_quietly,
@@ -788,14 +808,22 @@ fn decide_action_for(
788808
};
789809
info!("input_meta: {:?}", input_meta);
790810

811+
let toolchain_version = args.toolchain_version.clone().or_else(|| {
812+
if matches!(args.build_kind, BuildKind::Bench) {
813+
Some("nightly".into())
814+
} else {
815+
None
816+
}
817+
});
818+
791819
let mut action = InputAction {
792820
cargo_output: args.cargo_output,
793821
force_compile: force,
794822
emit_metadata: true,
795823
execute: true,
796824
pkg_path,
797825
using_cache,
798-
use_nightly: matches!(args.build_kind, BuildKind::Bench),
826+
toolchain_version,
799827
metadata: input_meta,
800828
old_metadata: None,
801829
manifest: mani_str,
@@ -1123,14 +1151,14 @@ Constructs a Cargo command that runs on the script package.
11231151
fn cargo(
11241152
cmd_name: &str,
11251153
manifest: &str,
1126-
nightly: bool,
1154+
maybe_toolchain_version: Option<&str>,
11271155
meta: &PackageMetadata,
11281156
script_args: &[String],
11291157
run_quietly: bool,
11301158
) -> MainResult<Command> {
11311159
let mut cmd = Command::new("cargo");
1132-
if nightly {
1133-
cmd.arg("+nightly");
1160+
if let Some(toolchain_version) = maybe_toolchain_version {
1161+
cmd.arg(format!("+{}", toolchain_version));
11341162
}
11351163
cmd.arg(cmd_name);
11361164

0 commit comments

Comments
 (0)