Skip to content

Commit

Permalink
Enable multiple binaries (#5)
Browse files Browse the repository at this point in the history
Co-authored-by: Jake Park <ja-vely@velymonkeys.com>
  • Loading branch information
danbi2990 and Jake Park authored Jan 18, 2022
1 parent 35f2a7c commit 34d19f7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
20 changes: 13 additions & 7 deletions cli/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,16 @@ pub fn build(mode: Mode) -> Result<()> {
}

/// Get the release binary path.
pub fn binary_name() -> Result<String> {
pub fn binary_names() -> Result<Vec<String>> {
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.
Expand Down Expand Up @@ -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<String> {
let metadata = metadata::MetadataCommand::new().exec()?;
let package = metadata.root_package().context("no root package")?;
Ok(package.name.clone())
}
37 changes: 20 additions & 17 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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)?;
Expand Down

0 comments on commit 34d19f7

Please sign in to comment.