Skip to content

Commit

Permalink
refactor(package): Consolidate creation of published package
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Mar 28, 2024
1 parent 866d51d commit 8a74899
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
16 changes: 5 additions & 11 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,15 +448,11 @@ fn error_custom_build_file_not_in_package(
}

/// Construct `Cargo.lock` for the package to be published.
fn build_lock(ws: &Workspace<'_>, orig_pkg: &Package) -> CargoResult<String> {
fn build_lock(ws: &Workspace<'_>, publish_pkg: &Package) -> CargoResult<String> {
let gctx = ws.gctx();
let orig_resolve = ops::load_pkg_lockfile(ws)?;

let manifest = prepare_for_publish(orig_pkg, ws)?;
let new_pkg = Package::new(manifest, orig_pkg.manifest_path());

// Regenerate Cargo.lock using the old one as a guide.
let tmp_ws = Workspace::ephemeral(new_pkg, ws.gctx(), None, true)?;
let tmp_ws = Workspace::ephemeral(publish_pkg.clone(), ws.gctx(), None, true)?;
let mut tmp_reg = PackageRegistry::new(ws.gctx())?;
let mut new_resolve = ops::resolve_with_previous(
&mut tmp_reg,
Expand Down Expand Up @@ -687,6 +683,7 @@ fn tar(

let base_name = format!("{}-{}", pkg.name(), pkg.version());
let base_path = Path::new(&base_name);
let publish_pkg = prepare_for_publish(pkg, ws)?;

let mut uncompressed_size = 0;
for ar_file in ar_files {
Expand Down Expand Up @@ -717,11 +714,8 @@ fn tar(
}
FileContents::Generated(generated_kind) => {
let contents = match generated_kind {
GeneratedFile::Manifest => {
let manifest = prepare_for_publish(pkg, ws)?;
manifest.to_resolved_contents()?
}
GeneratedFile::Lockfile => build_lock(ws, pkg)?,
GeneratedFile::Manifest => publish_pkg.manifest().to_resolved_contents()?,
GeneratedFile::Lockfile => build_lock(ws, &publish_pkg)?,
GeneratedFile::VcsInfo(ref s) => serde_json::to_string_pretty(s)?,
};
header.set_entry_type(EntryType::file());
Expand Down
5 changes: 3 additions & 2 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ fn warn_on_unused(unused: &BTreeSet<String>, warnings: &mut Vec<String>) {
}
}

pub fn prepare_for_publish(me: &Package, ws: &Workspace<'_>) -> CargoResult<Manifest> {
pub fn prepare_for_publish(me: &Package, ws: &Workspace<'_>) -> CargoResult<Package> {
let contents = me.manifest().contents();
let document = me.manifest().document();
let toml_manifest = prepare_toml_for_publish(me.manifest().resolved_toml(), ws, me.root())?;
Expand All @@ -236,7 +236,8 @@ pub fn prepare_for_publish(me: &Package, ws: &Workspace<'_>) -> CargoResult<Mani
&mut warnings,
&mut errors,
)?;
Ok(manifest)
let new_pkg = Package::new(manifest, me.manifest_path());
Ok(new_pkg)
}

/// Prepares the manifest for publishing.
Expand Down

0 comments on commit 8a74899

Please sign in to comment.