From f442931b76e386b564ef8fed966b7f29be620b6a Mon Sep 17 00:00:00 2001 From: Jake Park Date: Sat, 15 Jan 2022 00:11:42 +0900 Subject: [PATCH 1/2] enable multiple binaries --- cli/src/cargo.rs | 20 +++++++++++++------- cli/src/main.rs | 26 +++++++++++++++----------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/cli/src/cargo.rs b/cli/src/cargo.rs index e83661a..d10331e 100644 --- a/cli/src/cargo.rs +++ b/cli/src/cargo.rs @@ -73,17 +73,16 @@ pub fn build(mode: Mode) -> Result<()> { } /// Get the release binary path. -pub fn binary_name() -> Result { +pub fn binary_names() -> Result> { let metadata = metadata::MetadataCommand::new().exec()?; let package = metadata.root_package().context("no root package")?; - let binary = package + let binaries = package .targets .iter() - .find(|target| target.kind.iter().any(|kind| kind == "bin")) - .context("no binary")? - .name - .clone(); - Ok(binary) + .filter(|target| target.kind.iter().any(|kind| kind == "bin")) + .map(|target| target.name.clone()) + .collect(); + Ok(binaries) } /// Get the workspace directory. @@ -112,3 +111,10 @@ pub fn write_manifest(dir: &Path, doc: &toml::Document) -> Result<()> { fs::write(&path, &doc.to_string_in_original_order())?; Ok(()) } + +/// Get the package name +pub fn package_name() -> Result { + let metadata = metadata::MetadataCommand::new().exec()?; + let package = metadata.root_package().context("no root package")?; + Ok(package.name.clone()) +} diff --git a/cli/src/main.rs b/cli/src/main.rs index ce950fe..931fbe8 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -83,18 +83,22 @@ fn build(release: bool) -> Result<()> { let workspace_dir = cargo::workspace_directory()?; let target_dir = cargo::target_directory()?; - let binary_name = cargo::binary_name()?; - fs::create_dir_all(workspace_dir.join("workflow"))?; + let binary_names = cargo::binary_names()?; + let workflow_dir = workspace_dir.join("workflow"); + fs::create_dir_all(&workflow_dir)?; - let src = target_dir.join(mode.dir()).join(&binary_name); - let dst = workspace_dir.join("workflow").join(&binary_name); - fs::copy(&src, &dst)?; + binary_names.iter().for_each(|binary_name| { + let src = target_dir.join(mode.dir()).join(binary_name); + let dst = workflow_dir.join(binary_name); + fs::copy(&src, &dst).unwrap(); + }); print( "Copied", format!( - "binary to `{}`", - dst.strip_prefix(env::current_dir()?)?.display() + "binary to {}/{{{}}}", + workflow_dir.strip_prefix(env::current_dir()?)?.display(), + binary_names.join(", ") ), ); @@ -144,14 +148,14 @@ fn link() -> Result<()> { fn package() -> Result<()> { let workflow_dir = cargo::workspace_directory()?.join("workflow"); let dist_dir = cargo::target_directory()?.join("workflow"); - let mut binary_name = cargo::binary_name()?; + let mut package_name = cargo::package_name()?; // Just a hack because I tend to suffix my workflows with this. - if let Some(new) = binary_name.strip_suffix("-alfred-workflow") { - binary_name = new.to_owned(); + if let Some(new) = package_name.strip_suffix("-alfred-workflow") { + package_name = new.to_owned(); } - let dst = &dist_dir.join(binary_name).with_extension("alfredworkflow"); + let dst = &dist_dir.join(package_name).with_extension("alfredworkflow"); fs::create_dir_all(&dist_dir)?; alfred::package(&workflow_dir, dst)?; From 7dce1f7a697f4dc84e29299be7e7a6efd2071af9 Mon Sep 17 00:00:00 2001 From: Jake Park Date: Tue, 18 Jan 2022 00:36:50 +0900 Subject: [PATCH 2/2] apply changes --- cli/src/main.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 931fbe8..3448038 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -87,20 +87,19 @@ fn build(release: bool) -> Result<()> { let workflow_dir = workspace_dir.join("workflow"); fs::create_dir_all(&workflow_dir)?; - binary_names.iter().for_each(|binary_name| { + for binary_name in &binary_names { let src = target_dir.join(mode.dir()).join(binary_name); let dst = workflow_dir.join(binary_name); - fs::copy(&src, &dst).unwrap(); - }); - - print( - "Copied", - format!( - "binary to {}/{{{}}}", - workflow_dir.strip_prefix(env::current_dir()?)?.display(), - binary_names.join(", ") - ), - ); + fs::copy(&src, &dst)?; + + print( + "Copied", + format!( + "binary to `{}`", + dst.strip_prefix(env::current_dir()?)?.display() + ), + ); + } Ok(()) }