Skip to content

Commit d594f7e

Browse files
authored
Merge pull request #21 from PatchMixolydic/master
Add `--toolchain-version` argument
2 parents 6f8c393 + c5eb8fb commit d594f7e

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-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
812+
.toolchain_version
813+
.clone()
814+
.or_else(|| match args.build_kind {
815+
BuildKind::Bench => Some("nightly".into()),
816+
_ => None,
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,
@@ -1127,14 +1155,14 @@ Constructs a Cargo command that runs on the script package.
11271155
fn cargo(
11281156
cmd_name: &str,
11291157
manifest: &str,
1130-
nightly: bool,
1158+
maybe_toolchain_version: Option<&str>,
11311159
meta: &PackageMetadata,
11321160
script_args: &[String],
11331161
run_quietly: bool,
11341162
) -> MainResult<Command> {
11351163
let mut cmd = Command::new("cargo");
1136-
if nightly {
1137-
cmd.arg("+nightly");
1164+
if let Some(toolchain_version) = maybe_toolchain_version {
1165+
cmd.arg(format!("+{}", toolchain_version));
11381166
}
11391167
cmd.arg(cmd_name);
11401168

tests/data/script-unstable-feature.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![feature(lang_items)]
2+
3+
fn main() {
4+
println!("--output--");
5+
println!("`#![feature]` *may* be used!");
6+
}

tests/tests/script.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,20 @@ fn test_whitespace_before_main() {
179179
)
180180
.unwrap()
181181
}
182+
183+
#[test]
184+
fn test_stable_toolchain() {
185+
let out = rust_script!("--toolchain-version", "stable", "tests/data/script-unstable-feature.rs").unwrap();
186+
assert!(out.stderr.contains("`#![feature]` may not be used"));
187+
assert!(!out.success());
188+
}
189+
190+
#[test]
191+
fn test_nightly_toolchain() {
192+
let out = rust_script!("--toolchain-version", "nightly", "tests/data/script-unstable-feature.rs").unwrap();
193+
scan!(out.stdout_output();
194+
("`#![feature]` *may* be used!") => ()
195+
)
196+
.unwrap();
197+
assert!(out.success());
198+
}

0 commit comments

Comments
 (0)