Skip to content

Commit

Permalink
refactor(package): Make prepare for publish a higher level operation
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Mar 28, 2024
1 parent 3c3a8cf commit 866d51d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
28 changes: 4 additions & 24 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::sync::Arc;
use std::task::Poll;

use crate::core::compiler::{BuildConfig, CompileMode, DefaultExecutor, Executor};
use crate::core::manifest;
use crate::core::manifest::Target;
use crate::core::resolver::CliFeatures;
use crate::core::{registry::PackageRegistry, resolver::HasDevUnits};
Expand All @@ -17,7 +16,7 @@ use crate::sources::PathSource;
use crate::util::cache_lock::CacheLockMode;
use crate::util::context::JobsConfig;
use crate::util::errors::CargoResult;
use crate::util::toml::{prepare_for_publish, to_real_manifest};
use crate::util::toml::prepare_for_publish;
use crate::util::{self, human_readable_bytes, restricted_names, FileLock, GlobalContext};
use crate::{drop_println, ops};
use anyhow::Context as _;
Expand Down Expand Up @@ -453,24 +452,7 @@ fn build_lock(ws: &Workspace<'_>, orig_pkg: &Package) -> CargoResult<String> {
let gctx = ws.gctx();
let orig_resolve = ops::load_pkg_lockfile(ws)?;

// Convert Package -> TomlManifest -> Manifest -> Package
let contents = orig_pkg.manifest().contents();
let document = orig_pkg.manifest().document();
let toml_manifest =
prepare_for_publish(orig_pkg.manifest().resolved_toml(), ws, orig_pkg.root())?;
let source_id = orig_pkg.package_id().source_id();
let mut warnings = Default::default();
let mut errors = Default::default();
let manifest = to_real_manifest(
contents.to_owned(),
document.clone(),
toml_manifest,
source_id,
orig_pkg.manifest_path(),
gctx,
&mut warnings,
&mut errors,
)?;
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.
Expand Down Expand Up @@ -736,10 +718,8 @@ fn tar(
FileContents::Generated(generated_kind) => {
let contents = match generated_kind {
GeneratedFile::Manifest => {
let manifest =
prepare_for_publish(pkg.manifest().resolved_toml(), ws, pkg.root())?;
let toml = toml::to_string_pretty(&manifest)?;
format!("{}\n{}", manifest::MANIFEST_PREAMBLE, toml)
let manifest = prepare_for_publish(pkg, ws)?;
manifest.to_resolved_contents()?
}
GeneratedFile::Lockfile => build_lock(ws, pkg)?,
GeneratedFile::VcsInfo(ref s) => serde_json::to_string_pretty(s)?,
Expand Down
25 changes: 23 additions & 2 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::core::dependency::{Artifact, ArtifactTarget, DepKind};
use crate::core::manifest::{ManifestMetadata, TargetSourcePath};
use crate::core::resolver::ResolveBehavior;
use crate::core::{find_workspace_root, resolve_relative_path, CliUnstable, FeatureValue};
use crate::core::{Dependency, Manifest, PackageId, Summary, Target};
use crate::core::{Dependency, Manifest, Package, PackageId, Summary, Target};
use crate::core::{Edition, EitherManifest, Feature, Features, VirtualManifest, Workspace};
use crate::core::{GitReference, PackageIdSpec, SourceId, WorkspaceConfig, WorkspaceRootConfig};
use crate::sources::{CRATES_IO_INDEX, CRATES_IO_REGISTRY};
Expand Down Expand Up @@ -218,10 +218,31 @@ fn warn_on_unused(unused: &BTreeSet<String>, warnings: &mut Vec<String>) {
}
}

pub fn prepare_for_publish(me: &Package, ws: &Workspace<'_>) -> CargoResult<Manifest> {
let contents = me.manifest().contents();
let document = me.manifest().document();
let toml_manifest = prepare_toml_for_publish(me.manifest().resolved_toml(), ws, me.root())?;
let source_id = me.package_id().source_id();
let mut warnings = Default::default();
let mut errors = Default::default();
let gctx = ws.gctx();
let manifest = to_real_manifest(
contents.to_owned(),
document.clone(),
toml_manifest,
source_id,
me.manifest_path(),
gctx,
&mut warnings,
&mut errors,
)?;
Ok(manifest)
}

/// Prepares the manifest for publishing.
// - Path and git components of dependency specifications are removed.
// - License path is updated to point within the package.
pub fn prepare_for_publish(
fn prepare_toml_for_publish(
me: &manifest::TomlManifest,
ws: &Workspace<'_>,
package_root: &Path,
Expand Down

0 comments on commit 866d51d

Please sign in to comment.