Skip to content

Commit

Permalink
cli: Only fetch Cargo metadata once
Browse files Browse the repository at this point in the history
  • Loading branch information
rossmacarthur committed Jan 18, 2022
1 parent 34d19f7 commit 4992c94
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
58 changes: 32 additions & 26 deletions cli/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ pub enum Mode {
Release,
}

#[derive(Debug)]
pub struct Metadata {
pub workspace_dir: PathBuf,
pub target_dir: PathBuf,
pub package_name: String,
pub binary_names: Vec<String>,
}

impl Cargo {
fn new<S: AsRef<OsStr>>(subcmd: S) -> Self {
let mut cmd = process::Command::new("cargo");
Expand Down Expand Up @@ -72,29 +80,34 @@ pub fn build(mode: Mode) -> Result<()> {
cmd.run()
}

/// Get the release binary path.
pub fn binary_names() -> Result<Vec<String>> {
let metadata = metadata::MetadataCommand::new().exec()?;
let package = metadata.root_package().context("no root package")?;
let binaries = package
/// Run a `cargo metadata` command.
pub fn metadata() -> Result<Metadata> {
let metadata::Metadata {
packages,
workspace_root,
target_directory,
resolve,
..
} = metadata::MetadataCommand::new().exec()?;

let root = move || {
let root = resolve.as_ref()?.root.as_ref()?;
packages.into_iter().find(|pkg| &pkg.id == root)
};
let package = root().context("no root package")?;
let binary_names = package
.targets
.iter()
.into_iter()
.filter(|target| target.kind.iter().any(|kind| kind == "bin"))
.map(|target| target.name.clone())
.map(|target| target.name)
.collect();
Ok(binaries)
}

/// Get the workspace directory.
pub fn workspace_directory() -> Result<PathBuf> {
let metadata = metadata::MetadataCommand::new().exec()?;
Ok(metadata.workspace_root.into())
}

/// Get the target directory.
pub fn target_directory() -> Result<PathBuf> {
let metadata = metadata::MetadataCommand::new().exec()?;
Ok(metadata.target_directory.into())
Ok(Metadata {
workspace_dir: workspace_root.into(),
target_dir: target_directory.into(),
package_name: package.name,
binary_names,
})
}

/// Read the Cargo manifest.
Expand All @@ -111,10 +124,3 @@ 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())
}
21 changes: 10 additions & 11 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,14 @@ fn build(release: bool) -> Result<()> {
};
cargo::build(mode)?;

let workspace_dir = cargo::workspace_directory()?;
let target_dir = cargo::target_directory()?;
let binary_names = cargo::binary_names()?;
let workflow_dir = workspace_dir.join("workflow");
let metadata = cargo::metadata()?;
let workflow_dir = metadata.workspace_dir.join("workflow");
fs::create_dir_all(&workflow_dir)?;

for binary_name in &binary_names {
let src = target_dir.join(mode.dir()).join(binary_name);
for binary_name in &metadata.binary_names {
let src = metadata.target_dir.join(mode.dir()).join(binary_name);
let dst = workflow_dir.join(binary_name);
fs::copy(&src, &dst)?;

print(
"Copied",
format!(
Expand Down Expand Up @@ -120,7 +117,8 @@ fn find_link(workflow_dir: &Path, workflows_dir: &Path) -> Result<Option<PathBuf

/// Link the workflow.
fn link() -> Result<()> {
let workflow_dir = cargo::workspace_directory()?.join("workflow");
let metadata = cargo::metadata()?;
let workflow_dir = metadata.workspace_dir.join("workflow");
let workflows_dir = alfred::workflows_directory()?;

match find_link(&workflow_dir, &workflows_dir)? {
Expand All @@ -145,9 +143,10 @@ fn link() -> Result<()> {

/// Package the workflow into a `.alfredworkflow` file.
fn package() -> Result<()> {
let workflow_dir = cargo::workspace_directory()?.join("workflow");
let dist_dir = cargo::target_directory()?.join("workflow");
let mut package_name = cargo::package_name()?;
let metadata = cargo::metadata()?;
let workflow_dir = metadata.workspace_dir.join("workflow");
let dist_dir = metadata.target_dir.join("workflow");
let mut package_name = metadata.package_name;

// Just a hack because I tend to suffix my workflows with this.
if let Some(new) = package_name.strip_suffix("-alfred-workflow") {
Expand Down

0 comments on commit 4992c94

Please sign in to comment.