Skip to content

Use the source input file as bin path if possible #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ pub const SCRIPT_BODY_SUB: &str = "script";
/// Substitution for the script prelude.
pub const SCRIPT_PRELUDE_SUB: &str = "prelude";

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

/// The template used for `--expr` input.
pub const EXPR_TEMPLATE: &str = r#"
Expand Down
28 changes: 14 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,11 @@ fn generate_package(action: &InputAction) -> MainResult<()> {

info!("generating Cargo package...");
let mani_path = action.manifest_path();
let script_path = action.script_path();

overwrite_file(&mani_path, &action.manifest)?;
overwrite_file(&script_path, &action.script)?;
if let Some(script) = &action.script {
overwrite_file(&action.script_path, script)?;
}

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

script_name: String,
/// Path of the source code that Cargo.toml refers.
script_path: PathBuf,

/**
Is the package directory in the cache?
Expand All @@ -315,8 +317,8 @@ struct InputAction {
/// The package manifest contents.
manifest: String,

/// The script source.
script: String,
/// The script source in case it has to be written.
script: Option<String>,

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

fn script_path(&self) -> PathBuf {
self.pkg_path.join(&self.script_name)
}

fn cargo(&self, script_args: &[String]) -> MainResult<Command> {
let release_mode = !self.debug && !matches!(self.build_kind, BuildKind::Bench);

Expand All @@ -359,14 +357,15 @@ impl InputAction {
};

if matches!(self.build_kind, BuildKind::Normal) && !self.force_compile {
let script_path = self.script_path();

match fs::File::open(&built_binary_path) {
Ok(built_binary_file) => {
// Use ctime instead of mtime as cargo may copy an already
// built binary (with old mtime) here:
let built_binary_ctime = built_binary_file.metadata()?.created()?;
match (fs::File::open(script_path), fs::File::open(manifest_path)) {
match (
fs::File::open(&self.script_path),
fs::File::open(manifest_path),
) {
(Ok(script_file), Ok(manifest_file)) => {
let script_mtime = script_file.metadata()?.modified()?;
let manifest_mtime = manifest_file.metadata()?.modified()?;
Expand Down Expand Up @@ -470,10 +469,11 @@ fn decide_action_for(

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

let (mani_str, script_str) = manifest::split_input(
let (mani_str, script_path, script_str) = manifest::split_input(
input,
&deps,
&prelude,
&pkg_path,
&bin_name,
&script_name,
toolchain_version.clone(),
Expand All @@ -491,7 +491,7 @@ fn decide_action_for(
force_compile: args.force,
execute: !args.gen_pkg_only,
pkg_path,
script_name,
script_path,
using_cache,
toolchain_version,
debug,
Expand Down
Loading