Skip to content

Commit a5a88be

Browse files
committed
[cargo-bazel] Cleanup MetadataGenerator usage
1 parent 3585384 commit a5a88be

File tree

3 files changed

+20
-91
lines changed

3 files changed

+20
-91
lines changed

crate_universe/src/cli/splice.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! The cli entrypoint for the `splice` subcommand
22
33
use std::path::PathBuf;
4+
use std::fs::File;
5+
use std::process::Stdio;
46

57
use anyhow::Context;
68
use camino::Utf8PathBuf;
@@ -10,7 +12,7 @@ use itertools::Itertools;
1012
use crate::cli::Result;
1113
use crate::config::Config;
1214
use crate::metadata::{
13-
write_metadata, Cargo, CargoUpdateRequest, Generator, MetadataGenerator, TreeResolver,
15+
Cargo, CargoUpdateRequest, TreeResolver,
1416
};
1517
use crate::splicing::{
1618
generate_lockfile, Splicer, SplicerKind, SplicingManifest, WorkspaceMetadata,
@@ -111,6 +113,7 @@ pub fn splice(opt: SpliceOptions) -> Result<()> {
111113
&config.supported_platform_triples,
112114
)
113115
.context("Failed to generate features")?;
116+
114117
// Write the registry url info to the manifest now that a lockfile has been generated
115118
WorkspaceMetadata::write_registry_urls_and_feature_map(
116119
&cargo,
@@ -121,13 +124,19 @@ pub fn splice(opt: SpliceOptions) -> Result<()> {
121124
)
122125
.context("Failed to write registry URLs and feature map")?;
123126

124-
let output_dir = opt.output_dir.clone();
127+
// Generate the consumable outputs of the splicing process
128+
std::fs::create_dir_all(&opt.output_dir)
129+
.with_context(|| format!("Failed to create directories for {}", opt.output_dir.display()))?;
130+
131+
let metadata_json = File::create(opt.output_dir.join("metadata.json"))?;
125132

126133
// Write metadata to the workspace for future reuse
127-
let (cargo_metadata, _) = Generator::new()
128-
.with_cargo(cargo.clone())
129-
.with_rustc(opt.rustc.clone())
130-
.generate(manifest_path.as_path_buf())
134+
cargo
135+
.metadata_command_with_options(manifest_path.as_path_buf().as_ref(), vec!["--locked".to_owned()])?
136+
.cargo_command()
137+
.stdout(Stdio::from(metadata_json))
138+
.stderr(Stdio::null())
139+
.status()
131140
.context("Failed to generate cargo metadata")?;
132141

133142
let cargo_lockfile_path = manifest_path
@@ -141,14 +150,7 @@ pub fn splice(opt: SpliceOptions) -> Result<()> {
141150
})?
142151
.join("Cargo.lock");
143152

144-
// Generate the consumable outputs of the splicing process
145-
std::fs::create_dir_all(&output_dir)
146-
.with_context(|| format!("Failed to create directories for {}", &output_dir.display()))?;
147-
148-
write_metadata(&opt.output_dir.join("metadata.json"), &cargo_metadata)
149-
.context("Failed to write metadata")?;
150-
151-
std::fs::copy(cargo_lockfile_path, output_dir.join("Cargo.lock"))
153+
std::fs::copy(cargo_lockfile_path, opt.output_dir.join("Cargo.lock"))
152154
.context("Failed to copy lockfile")?;
153155

154156
if let SplicerKind::Workspace { path, .. } = prepared_splicer {

crate_universe/src/cli/vendor.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::context::Context;
1616
use crate::lockfile::{lock_context, write_lockfile};
1717
use crate::metadata::CargoUpdateRequest;
1818
use crate::metadata::TreeResolver;
19-
use crate::metadata::{Annotations, Cargo, Generator, MetadataGenerator, VendorGenerator};
19+
use crate::metadata::{Annotations, Cargo, VendorGenerator};
2020
use crate::rendering::{render_module_label, write_outputs, Renderer};
2121
use crate::splicing::{generate_lockfile, Splicer, SplicingManifest, WorkspaceMetadata};
2222
use crate::utils::normalize_cargo_file_paths;
@@ -246,10 +246,9 @@ pub fn vendor(opt: VendorOptions) -> anyhow::Result<()> {
246246
)?;
247247

248248
// Write metadata to the workspace for future reuse
249-
let (cargo_metadata, cargo_lockfile) = Generator::new()
250-
.with_cargo(cargo.clone())
251-
.with_rustc(opt.rustc.clone())
252-
.generate(manifest_path.as_path_buf())?;
249+
let cargo_metadata = cargo
250+
.metadata_command_with_options(manifest_path.as_path_buf().as_ref(), vec!["--locked".to_owned()])?
251+
.exec()?;
253252

254253
// Annotate metadata
255254
let annotations = Annotations::new(

crate_universe/src/metadata.rs

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,83 +5,19 @@ mod cargo_tree_resolver;
55
mod dependency;
66
mod metadata_annotation;
77

8-
use std::env;
98
use std::fs;
109
use std::path::{Path, PathBuf};
1110
use std::str::FromStr;
1211

1312
use anyhow::{bail, Context, Result};
1413
use camino::Utf8Path;
15-
use cargo_lock::Lockfile as CargoLockfile;
16-
use cargo_metadata::Metadata as CargoMetadata;
1714
use tracing::debug;
1815

1916
pub(crate) use self::cargo_bin::*;
2017
pub(crate) use self::cargo_tree_resolver::*;
2118
pub(crate) use self::dependency::*;
2219
pub(crate) use self::metadata_annotation::*;
2320

24-
// TODO: This should also return a set of [crate-index::IndexConfig]s for packages in metadata.packages
25-
/// A Trait for generating metadata (`cargo metadata` output and a lock file) from a Cargo manifest.
26-
pub(crate) trait MetadataGenerator {
27-
fn generate<T: AsRef<Path>>(&self, manifest_path: T) -> Result<(CargoMetadata, CargoLockfile)>;
28-
}
29-
30-
/// Generates Cargo metadata and a lockfile from a provided manifest.
31-
pub(crate) struct Generator {
32-
/// The path to a `cargo` binary
33-
cargo_bin: Cargo,
34-
35-
/// The path to a `rustc` binary
36-
rustc_bin: PathBuf,
37-
}
38-
39-
impl Generator {
40-
pub(crate) fn new() -> Self {
41-
let rustc_bin = PathBuf::from(env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string()));
42-
Generator {
43-
cargo_bin: Cargo::new(
44-
PathBuf::from(env::var("CARGO").unwrap_or_else(|_| "cargo".to_string())),
45-
rustc_bin.clone(),
46-
),
47-
rustc_bin,
48-
}
49-
}
50-
51-
pub(crate) fn with_cargo(mut self, cargo_bin: Cargo) -> Self {
52-
self.cargo_bin = cargo_bin;
53-
self
54-
}
55-
56-
pub(crate) fn with_rustc(mut self, rustc_bin: PathBuf) -> Self {
57-
self.rustc_bin = rustc_bin;
58-
self
59-
}
60-
}
61-
62-
impl MetadataGenerator for Generator {
63-
fn generate<T: AsRef<Path>>(&self, manifest_path: T) -> Result<(CargoMetadata, CargoLockfile)> {
64-
let manifest_dir = manifest_path
65-
.as_ref()
66-
.parent()
67-
.expect("The manifest should have a parent directory");
68-
let lockfile = {
69-
let lock_path = manifest_dir.join("Cargo.lock");
70-
if !lock_path.exists() {
71-
bail!("No `Cargo.lock` file was found with the given manifest")
72-
}
73-
cargo_lock::Lockfile::load(lock_path)?
74-
};
75-
76-
let metadata = self
77-
.cargo_bin
78-
.metadata_command_with_options(manifest_path.as_ref(), vec!["--locked".to_owned()])?
79-
.exec()?;
80-
81-
Ok((metadata, lockfile))
82-
}
83-
}
84-
8521
/// A configuration describing how to invoke [cargo update](https://doc.rust-lang.org/cargo/commands/cargo-update.html).
8622
#[derive(Debug, Clone, PartialEq, Eq)]
8723
pub enum CargoUpdateRequest {
@@ -340,14 +276,6 @@ impl VendorGenerator {
340276
}
341277
}
342278

343-
/// A helper function for writing Cargo metadata to a file.
344-
pub(crate) fn write_metadata(path: &Path, metadata: &cargo_metadata::Metadata) -> Result<()> {
345-
let content =
346-
serde_json::to_string_pretty(metadata).context("Failed to serialize Cargo Metadata")?;
347-
348-
fs::write(path, content).context("Failed to write metadata to disk")
349-
}
350-
351279
/// A helper function for deserializing Cargo metadata and lockfiles
352280
pub(crate) fn load_metadata(
353281
metadata_path: &Path,

0 commit comments

Comments
 (0)