refactor(package): Move preamble to Manifest

This commit is contained in:
Ed Page 2024-03-27 19:39:34 -05:00
parent 97ed4ff1ad
commit cad9673785
10 changed files with 42 additions and 40 deletions

View File

@ -22,6 +22,19 @@ use crate::util::errors::*;
use crate::util::interning::InternedString;
use crate::util::{short_hash, Filesystem, GlobalContext};
pub const MANIFEST_PREAMBLE: &str = "\
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# \"normalize\" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
";
pub enum EitherManifest {
Real(Manifest),
Virtual(VirtualManifest),
@ -470,6 +483,10 @@ impl Manifest {
pub fn contents(&self) -> &str {
self.contents.as_str()
}
pub fn to_resolved_contents(&self) -> CargoResult<String> {
let toml = toml::to_string_pretty(self.resolved_toml())?;
Ok(format!("{}\n{}", MANIFEST_PREAMBLE, toml))
}
/// Collection of spans for the original TOML
pub fn document(&self) -> &toml_edit::ImDocument<String> {
&self.document

View File

@ -22,7 +22,7 @@ use crate::core::compiler::{CompileKind, RustcTargetData};
use crate::core::dependency::DepKind;
use crate::core::resolver::features::ForceAllTargets;
use crate::core::resolver::{HasDevUnits, Resolve};
use crate::core::{Dependency, Manifest, PackageId, PackageIdSpec, SourceId, Target};
use crate::core::{manifest, Dependency, Manifest, PackageId, PackageIdSpec, SourceId, Target};
use crate::core::{Summary, Workspace};
use crate::sources::source::{MaybePackage, SourceMap};
use crate::util::cache_lock::{CacheLock, CacheLockMode};
@ -35,19 +35,6 @@ use crate::util::network::sleep::SleepTracker;
use crate::util::toml::prepare_for_publish;
use crate::util::{self, internal, GlobalContext, Progress, ProgressStyle};
pub const MANIFEST_PREAMBLE: &str = "\
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# \"normalize\" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
";
/// Information about a package that is available somewhere in the file system.
///
/// A package is a `Cargo.toml` file plus all the files that are part of it.
@ -200,7 +187,7 @@ impl Package {
pub fn to_registry_toml(&self, ws: &Workspace<'_>) -> CargoResult<String> {
let manifest = prepare_for_publish(self.manifest().resolved_toml(), ws, self.root())?;
let toml = toml::to_string_pretty(&manifest)?;
Ok(format!("{}\n{}", MANIFEST_PREAMBLE, toml))
Ok(format!("{}\n{}", manifest::MANIFEST_PREAMBLE, toml))
}
/// Returns if package should include `Cargo.lock`.

View File

@ -1,4 +1,3 @@
use crate::core::package::MANIFEST_PREAMBLE;
use crate::core::shell::Verbosity;
use crate::core::{GitReference, Package, Workspace};
use crate::ops;
@ -360,8 +359,7 @@ fn cp_sources(
let cksum = if dst.file_name() == Some(OsStr::new("Cargo.toml"))
&& pkg.package_id().source_id().is_git()
{
let original_toml = toml::to_string_pretty(pkg.manifest().resolved_toml())?;
let contents = format!("{}\n{}", MANIFEST_PREAMBLE, original_toml);
let contents = pkg.manifest().to_resolved_contents()?;
copy_and_checksum(
&dst,
&mut dst_opts,

View File

@ -2184,7 +2184,7 @@ artifact = [
"staticlib",
]
target = "target""#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
),
)],
);

View File

@ -1708,7 +1708,7 @@ homepage = "https://example.com/"
license = "MIT"
resolver = "2"
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
);
let f = File::open(&p.root().join("target/package/a-0.1.0.crate")).unwrap();

View File

@ -1000,7 +1000,7 @@ optional = true
[features]
feat = ["opt-dep1"]
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
),
)],
);
@ -1116,7 +1116,7 @@ feat1 = []
feat2 = ["dep:bar"]
feat3 = ["feat2"]
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
),
)],
);

View File

@ -245,7 +245,7 @@ repository = "https://github.com/example/example"
branch = "master"
repository = "https://gitlab.com/rust-lang/rust"
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
),
)],
);
@ -406,7 +406,7 @@ version = "0.5.2"
[build-dependencies.dep-build]
version = "0.8"
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
),
)],
);
@ -532,7 +532,7 @@ authors = []
version = "0.1.2"
features = ["testing"]
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
),
)],
);
@ -796,7 +796,7 @@ repository = "https://github.com/example/example"
branch = "master"
repository = "https://gitlab.com/rust-lang/rust"
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
),
)],
);
@ -959,7 +959,7 @@ version = "0.5.2"
[build-dependencies.dep-build]
version = "0.8"
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
),
)],
);

View File

@ -1237,7 +1237,7 @@ registry-index = "{}"
[dependencies.ghi]
version = "1.0"
"#,
cargo::core::package::MANIFEST_PREAMBLE,
cargo::core::manifest::MANIFEST_PREAMBLE,
registry.index_url()
);
@ -1294,7 +1294,7 @@ name = "bar"
version = "0.1.0"
authors = []
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
);
validate_crate_contents(
f,
@ -1367,7 +1367,7 @@ version = "1.0.0"
[target.{host}.dependencies.baz]
version = "1.0.0"
"#,
cargo::core::package::MANIFEST_PREAMBLE,
cargo::core::manifest::MANIFEST_PREAMBLE,
host = rustc_host()
);
verify(&p, "package", rewritten_toml);
@ -1387,7 +1387,7 @@ public = true
version = "1.0.0"
public = true
"#,
cargo::core::package::MANIFEST_PREAMBLE,
cargo::core::manifest::MANIFEST_PREAMBLE,
host = rustc_host()
);
verify(&p, "package -Zpublic-dependency", rewritten_toml);
@ -2808,7 +2808,7 @@ name = "bar"
version = "0.1.0"
resolver = "1"
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
);
validate_crate_contents(
f,
@ -2826,7 +2826,7 @@ edition = "2015"
name = "baz"
version = "0.1.0"
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
);
validate_crate_contents(
f,
@ -2891,7 +2891,7 @@ description = "foo"
homepage = "https://example.com/"
license = "MIT"
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
);
let cargo_lock_contents = r#"# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
@ -2985,7 +2985,7 @@ description = "foo"
documentation = "https://example.com/"
license = "MIT"
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
);
let cargo_lock_contents = r#"# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
@ -3092,7 +3092,7 @@ description = "foo"
homepage = "https://example.com/"
license = "MIT"
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
);
let cargo_lock_contents = r#"# This file is automatically @generated by Cargo.
# It is not intended for manual editing.

View File

@ -1566,7 +1566,7 @@ You may press ctrl-c [..]
[dependencies.dep1]\n\
version = \"1.0\"\n\
",
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
),
),
(
@ -1680,7 +1680,7 @@ repository = "foo"
[dev-dependencies]
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
),
)],
);
@ -1979,7 +1979,7 @@ features = ["cat"]
version = "1.0"
features = ["cat"]
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
),
)],
);

View File

@ -642,7 +642,7 @@ optional = true
feat1 = []
feat2 = ["bar?/feat"]
"#,
cargo::core::package::MANIFEST_PREAMBLE
cargo::core::manifest::MANIFEST_PREAMBLE
),
)],
);