Skip to content
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

enable multiple binaries #5

Merged
merged 2 commits into from
Jan 18, 2022
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
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