Skip to content

Commit c98f7fe

Browse files
authored
Merge pull request #103 from MiSawa/use-direct-source
Use the source input file as bin path if possible
2 parents 847f679 + d0a2e7e commit c98f7fe

File tree

3 files changed

+134
-86
lines changed

3 files changed

+134
-86
lines changed

src/consts.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ pub const SCRIPT_BODY_SUB: &str = "script";
1414
/// Substitution for the script prelude.
1515
pub const SCRIPT_PRELUDE_SUB: &str = "prelude";
1616

17-
/// The template used for script file inputs.
18-
pub const FILE_TEMPLATE: &str = r#"#{script}"#;
17+
/// The template used for script file inputs that doesn't have main function.
18+
pub const FILE_NO_MAIN_TEMPLATE: &str = r#"
19+
fn main() -> Result<(), Box<dyn std::error::Error+Sync+Send>> {
20+
{#{script}}
21+
Ok(())
22+
}
23+
"#;
1924

2025
/// The template used for `--expr` input.
2126
pub const EXPR_TEMPLATE: &str = r#"

src/main.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,11 @@ fn generate_package(action: &InputAction) -> MainResult<()> {
261261

262262
info!("generating Cargo package...");
263263
let mani_path = action.manifest_path();
264-
let script_path = action.script_path();
265264

266265
overwrite_file(&mani_path, &action.manifest)?;
267-
overwrite_file(&script_path, &action.script)?;
266+
if let Some(script) = &action.script {
267+
overwrite_file(&action.script_path, script)?;
268+
}
268269

269270
info!("disarming pkg dir cleanup...");
270271
cleanup_dir.disarm();
@@ -293,7 +294,8 @@ struct InputAction {
293294
/// Directory where the package should live.
294295
pkg_path: PathBuf,
295296

296-
script_name: String,
297+
/// Path of the source code that Cargo.toml refers.
298+
script_path: PathBuf,
297299

298300
/**
299301
Is the package directory in the cache?
@@ -315,8 +317,8 @@ struct InputAction {
315317
/// The package manifest contents.
316318
manifest: String,
317319

318-
/// The script source.
319-
script: String,
320+
/// The script source in case it has to be written.
321+
script: Option<String>,
320322

321323
/// Did the user ask to run tests or benchmarks?
322324
build_kind: BuildKind,
@@ -330,10 +332,6 @@ impl InputAction {
330332
self.pkg_path.join("Cargo.toml")
331333
}
332334

333-
fn script_path(&self) -> PathBuf {
334-
self.pkg_path.join(&self.script_name)
335-
}
336-
337335
fn cargo(&self, script_args: &[String]) -> MainResult<Command> {
338336
let release_mode = !self.debug && !matches!(self.build_kind, BuildKind::Bench);
339337

@@ -359,14 +357,15 @@ impl InputAction {
359357
};
360358

361359
if matches!(self.build_kind, BuildKind::Normal) && !self.force_compile {
362-
let script_path = self.script_path();
363-
364360
match fs::File::open(&built_binary_path) {
365361
Ok(built_binary_file) => {
366362
// Use ctime instead of mtime as cargo may copy an already
367363
// built binary (with old mtime) here:
368364
let built_binary_ctime = built_binary_file.metadata()?.created()?;
369-
match (fs::File::open(script_path), fs::File::open(manifest_path)) {
365+
match (
366+
fs::File::open(&self.script_path),
367+
fs::File::open(manifest_path),
368+
) {
370369
(Ok(script_file), Ok(manifest_file)) => {
371370
let script_mtime = script_file.metadata()?.modified()?;
372371
let manifest_mtime = manifest_file.metadata()?.modified()?;
@@ -470,10 +469,11 @@ fn decide_action_for(
470469

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

473-
let (mani_str, script_str) = manifest::split_input(
472+
let (mani_str, script_path, script_str) = manifest::split_input(
474473
input,
475474
&deps,
476475
&prelude,
476+
&pkg_path,
477477
&bin_name,
478478
&script_name,
479479
toolchain_version.clone(),
@@ -491,7 +491,7 @@ fn decide_action_for(
491491
force_compile: args.force,
492492
execute: !args.gen_pkg_only,
493493
pkg_path,
494-
script_name,
494+
script_path,
495495
using_cache,
496496
toolchain_version,
497497
debug,

0 commit comments

Comments
 (0)