Skip to content

Commit c23f6f4

Browse files
authored
Merge pull request #78 from fornwall/add-toolchain-to-cargo-toml
Add toolchain to cargo.toml
2 parents c343a6e + ab77a62 commit c23f6f4

File tree

2 files changed

+74
-18
lines changed

2 files changed

+74
-18
lines changed

src/main.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,15 @@ impl InputAction {
361361

362362
match fs::File::open(&built_binary_path) {
363363
Ok(built_binary_file) => {
364-
let built_binary_mtime =
365-
built_binary_file.metadata().unwrap().modified().unwrap();
364+
// Use ctime instead of mtime as cargo may copy an already
365+
// built binary (with old mtime) here:
366+
let built_binary_ctime = built_binary_file.metadata()?.created()?;
366367
match (fs::File::open(script_path), fs::File::open(manifest_path)) {
367368
(Ok(script_file), Ok(manifest_file)) => {
368-
let script_mtime = script_file.metadata().unwrap().modified().unwrap();
369-
let manifest_mtime =
370-
manifest_file.metadata().unwrap().modified().unwrap();
371-
if built_binary_mtime.cmp(&script_mtime).is_ge()
372-
&& built_binary_mtime.cmp(&manifest_mtime).is_ge()
369+
let script_mtime = script_file.metadata()?.modified()?;
370+
let manifest_mtime = manifest_file.metadata()?.modified()?;
371+
if built_binary_ctime.cmp(&script_mtime).is_ge()
372+
&& built_binary_ctime.cmp(&manifest_mtime).is_ge()
373373
{
374374
return Ok(execute_command());
375375
}
@@ -455,15 +455,6 @@ fn decide_action_for(
455455
info!("pkg_path: {:?}", pkg_path);
456456
info!("using_cache: {:?}", using_cache);
457457

458-
let (mani_str, script_str) = manifest::split_input(input, &deps, &prelude, &bin_name)?;
459-
460-
// Forcibly override some flags based on build kind.
461-
let debug = match args.build_kind {
462-
BuildKind::Normal => args.debug,
463-
BuildKind::Test => true,
464-
BuildKind::Bench => false,
465-
};
466-
467458
let toolchain_version = args
468459
.toolchain_version
469460
.clone()
@@ -472,6 +463,16 @@ fn decide_action_for(
472463
_ => None,
473464
});
474465

466+
let (mani_str, script_str) =
467+
manifest::split_input(input, &deps, &prelude, &bin_name, toolchain_version.clone())?;
468+
469+
// Forcibly override some flags based on build kind.
470+
let debug = match args.build_kind {
471+
BuildKind::Normal => args.debug,
472+
BuildKind::Test => true,
473+
BuildKind::Bench => false,
474+
};
475+
475476
Ok(InputAction {
476477
cargo_output: args.cargo_output,
477478
force_compile: args.force,

src/manifest.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub fn split_input(
4343
deps: &[(String, String)],
4444
prelude_items: &[String],
4545
bin_name: &str,
46+
toolchain: Option<String>,
4647
) -> MainResult<(String, String)> {
4748
fn contains_main_method(line: &str) -> bool {
4849
let line = line.trim_start();
@@ -111,7 +112,28 @@ pub fn split_input(
111112
let mani = merge_manifest(mani, dep_mani)?;
112113

113114
// Fix up relative paths.
114-
let mani = fix_manifest_paths(mani, &input.base_path())?;
115+
let mut mani = fix_manifest_paths(mani, &input.base_path())?;
116+
117+
if let Some(toolchain) = toolchain {
118+
let mut mani_map = toml::map::Map::new();
119+
let mut package_map = toml::map::Map::new();
120+
let mut metadata = toml::map::Map::new();
121+
let mut rustscript_metadata = toml::map::Map::new();
122+
rustscript_metadata.insert(
123+
"toolchain".to_string(),
124+
toml::value::Value::String(toolchain),
125+
);
126+
metadata.insert(
127+
"rustscript".to_string(),
128+
toml::value::Value::Table(rustscript_metadata),
129+
);
130+
package_map.insert("metadata".to_string(), toml::value::Value::Table(metadata));
131+
mani_map.insert(
132+
"package".to_string(),
133+
toml::value::Value::Table(package_map),
134+
);
135+
mani = merge_manifest(mani, mani_map)?;
136+
}
115137
info!("mani: {:?}", mani);
116138

117139
let mani_str = format!("{}", mani);
@@ -130,9 +152,10 @@ strip = true
130152
#[test]
131153
fn test_split_input() {
132154
let bin_name = "binary-name".to_string();
155+
let toolchain = None;
133156
macro_rules! si {
134157
($i:expr) => {
135-
split_input(&$i, &[], &[], &bin_name).ok()
158+
split_input(&$i, &[], &[], &bin_name, toolchain.clone()).ok()
136159
};
137160
}
138161

@@ -169,6 +192,38 @@ version = "0.1.0""#,
169192
)
170193
);
171194

195+
assert_eq!(
196+
split_input(
197+
&f(r#"fn main() {}"#),
198+
&[],
199+
&[],
200+
&bin_name,
201+
Some("stable".to_string())
202+
)
203+
.ok(),
204+
r!(
205+
format!(
206+
"{}{}",
207+
r#"[[bin]]
208+
name = "binary-name"
209+
path = "main.rs"
210+
211+
[dependencies]
212+
213+
[package]
214+
authors = ["Anonymous"]
215+
edition = "2021"
216+
name = "n"
217+
version = "0.1.0"
218+
219+
[package.metadata.rustscript]
220+
toolchain = "stable""#,
221+
STRIP_SECTION
222+
),
223+
r#"fn main() {}"#
224+
)
225+
);
226+
172227
// Ensure removed prefix manifests don't work.
173228
assert_eq!(
174229
si!(f(r#"

0 commit comments

Comments
 (0)