From 34d19f7fec009f7595d57de426ac85b7e6c4f581 Mon Sep 17 00:00:00 2001 From: Jake Park Date: Tue, 18 Jan 2022 18:11:57 +0900 Subject: [PATCH] Enable multiple binaries (#5) Co-authored-by: Jake Park --- cli/src/cargo.rs | 20 +++++++++++++------- cli/src/main.rs | 37 ++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 24 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 a80ef68..516eb6d 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -83,20 +83,23 @@ 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 src = target_dir.join(mode.dir()).join(&binary_name); - let dst = workspace_dir.join("workflow").join(&binary_name); - fs::copy(&src, &dst)?; + let binary_names = cargo::binary_names()?; + let workflow_dir = workspace_dir.join("workflow"); + fs::create_dir_all(&workflow_dir)?; - print( - "Copied", - format!( - "binary to `{}`", - dst.strip_prefix(env::current_dir()?)?.display() - ), - ); + 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)?; + + print( + "Copied", + format!( + "binary to `{}`", + dst.strip_prefix(env::current_dir()?)?.display() + ), + ); + } Ok(()) } @@ -144,14 +147,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)?;