Skip to content

Commit cad9673

Browse files
committed
refactor(package): Move preamble to Manifest
1 parent 97ed4ff commit cad9673

File tree

10 files changed

+42
-40
lines changed

10 files changed

+42
-40
lines changed

src/cargo/core/manifest.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ use crate::util::errors::*;
2222
use crate::util::interning::InternedString;
2323
use crate::util::{short_hash, Filesystem, GlobalContext};
2424

25+
pub const MANIFEST_PREAMBLE: &str = "\
26+
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
27+
#
28+
# When uploading crates to the registry Cargo will automatically
29+
# \"normalize\" Cargo.toml files for maximal compatibility
30+
# with all versions of Cargo and also rewrite `path` dependencies
31+
# to registry (e.g., crates.io) dependencies.
32+
#
33+
# If you are reading this file be aware that the original Cargo.toml
34+
# will likely look very different (and much more reasonable).
35+
# See Cargo.toml.orig for the original contents.
36+
";
37+
2538
pub enum EitherManifest {
2639
Real(Manifest),
2740
Virtual(VirtualManifest),
@@ -470,6 +483,10 @@ impl Manifest {
470483
pub fn contents(&self) -> &str {
471484
self.contents.as_str()
472485
}
486+
pub fn to_resolved_contents(&self) -> CargoResult<String> {
487+
let toml = toml::to_string_pretty(self.resolved_toml())?;
488+
Ok(format!("{}\n{}", MANIFEST_PREAMBLE, toml))
489+
}
473490
/// Collection of spans for the original TOML
474491
pub fn document(&self) -> &toml_edit::ImDocument<String> {
475492
&self.document

src/cargo/core/package.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::core::compiler::{CompileKind, RustcTargetData};
2222
use crate::core::dependency::DepKind;
2323
use crate::core::resolver::features::ForceAllTargets;
2424
use crate::core::resolver::{HasDevUnits, Resolve};
25-
use crate::core::{Dependency, Manifest, PackageId, PackageIdSpec, SourceId, Target};
25+
use crate::core::{manifest, Dependency, Manifest, PackageId, PackageIdSpec, SourceId, Target};
2626
use crate::core::{Summary, Workspace};
2727
use crate::sources::source::{MaybePackage, SourceMap};
2828
use crate::util::cache_lock::{CacheLock, CacheLockMode};
@@ -35,19 +35,6 @@ use crate::util::network::sleep::SleepTracker;
3535
use crate::util::toml::prepare_for_publish;
3636
use crate::util::{self, internal, GlobalContext, Progress, ProgressStyle};
3737

38-
pub const MANIFEST_PREAMBLE: &str = "\
39-
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
40-
#
41-
# When uploading crates to the registry Cargo will automatically
42-
# \"normalize\" Cargo.toml files for maximal compatibility
43-
# with all versions of Cargo and also rewrite `path` dependencies
44-
# to registry (e.g., crates.io) dependencies.
45-
#
46-
# If you are reading this file be aware that the original Cargo.toml
47-
# will likely look very different (and much more reasonable).
48-
# See Cargo.toml.orig for the original contents.
49-
";
50-
5138
/// Information about a package that is available somewhere in the file system.
5239
///
5340
/// A package is a `Cargo.toml` file plus all the files that are part of it.
@@ -200,7 +187,7 @@ impl Package {
200187
pub fn to_registry_toml(&self, ws: &Workspace<'_>) -> CargoResult<String> {
201188
let manifest = prepare_for_publish(self.manifest().resolved_toml(), ws, self.root())?;
202189
let toml = toml::to_string_pretty(&manifest)?;
203-
Ok(format!("{}\n{}", MANIFEST_PREAMBLE, toml))
190+
Ok(format!("{}\n{}", manifest::MANIFEST_PREAMBLE, toml))
204191
}
205192

206193
/// Returns if package should include `Cargo.lock`.

src/cargo/ops/vendor.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::core::package::MANIFEST_PREAMBLE;
21
use crate::core::shell::Verbosity;
32
use crate::core::{GitReference, Package, Workspace};
43
use crate::ops;
@@ -360,8 +359,7 @@ fn cp_sources(
360359
let cksum = if dst.file_name() == Some(OsStr::new("Cargo.toml"))
361360
&& pkg.package_id().source_id().is_git()
362361
{
363-
let original_toml = toml::to_string_pretty(pkg.manifest().resolved_toml())?;
364-
let contents = format!("{}\n{}", MANIFEST_PREAMBLE, original_toml);
362+
let contents = pkg.manifest().to_resolved_contents()?;
365363
copy_and_checksum(
366364
&dst,
367365
&mut dst_opts,

tests/testsuite/artifact_dep.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2184,7 +2184,7 @@ artifact = [
21842184
"staticlib",
21852185
]
21862186
target = "target""#,
2187-
cargo::core::package::MANIFEST_PREAMBLE
2187+
cargo::core::manifest::MANIFEST_PREAMBLE
21882188
),
21892189
)],
21902190
);

tests/testsuite/features2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ homepage = "https://example.com/"
17081708
license = "MIT"
17091709
resolver = "2"
17101710
"#,
1711-
cargo::core::package::MANIFEST_PREAMBLE
1711+
cargo::core::manifest::MANIFEST_PREAMBLE
17121712
);
17131713

17141714
let f = File::open(&p.root().join("target/package/a-0.1.0.crate")).unwrap();

tests/testsuite/features_namespaced.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ optional = true
10001000
[features]
10011001
feat = ["opt-dep1"]
10021002
"#,
1003-
cargo::core::package::MANIFEST_PREAMBLE
1003+
cargo::core::manifest::MANIFEST_PREAMBLE
10041004
),
10051005
)],
10061006
);
@@ -1116,7 +1116,7 @@ feat1 = []
11161116
feat2 = ["dep:bar"]
11171117
feat3 = ["feat2"]
11181118
"#,
1119-
cargo::core::package::MANIFEST_PREAMBLE
1119+
cargo::core::manifest::MANIFEST_PREAMBLE
11201120
),
11211121
)],
11221122
);

tests/testsuite/inheritable_workspace_fields.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ repository = "https://github.com/example/example"
245245
branch = "master"
246246
repository = "https://gitlab.com/rust-lang/rust"
247247
"#,
248-
cargo::core::package::MANIFEST_PREAMBLE
248+
cargo::core::manifest::MANIFEST_PREAMBLE
249249
),
250250
)],
251251
);
@@ -406,7 +406,7 @@ version = "0.5.2"
406406
[build-dependencies.dep-build]
407407
version = "0.8"
408408
"#,
409-
cargo::core::package::MANIFEST_PREAMBLE
409+
cargo::core::manifest::MANIFEST_PREAMBLE
410410
),
411411
)],
412412
);
@@ -532,7 +532,7 @@ authors = []
532532
version = "0.1.2"
533533
features = ["testing"]
534534
"#,
535-
cargo::core::package::MANIFEST_PREAMBLE
535+
cargo::core::manifest::MANIFEST_PREAMBLE
536536
),
537537
)],
538538
);
@@ -796,7 +796,7 @@ repository = "https://github.com/example/example"
796796
branch = "master"
797797
repository = "https://gitlab.com/rust-lang/rust"
798798
"#,
799-
cargo::core::package::MANIFEST_PREAMBLE
799+
cargo::core::manifest::MANIFEST_PREAMBLE
800800
),
801801
)],
802802
);
@@ -959,7 +959,7 @@ version = "0.5.2"
959959
[build-dependencies.dep-build]
960960
version = "0.8"
961961
"#,
962-
cargo::core::package::MANIFEST_PREAMBLE
962+
cargo::core::manifest::MANIFEST_PREAMBLE
963963
),
964964
)],
965965
);

tests/testsuite/package.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ registry-index = "{}"
12371237
[dependencies.ghi]
12381238
version = "1.0"
12391239
"#,
1240-
cargo::core::package::MANIFEST_PREAMBLE,
1240+
cargo::core::manifest::MANIFEST_PREAMBLE,
12411241
registry.index_url()
12421242
);
12431243

@@ -1294,7 +1294,7 @@ name = "bar"
12941294
version = "0.1.0"
12951295
authors = []
12961296
"#,
1297-
cargo::core::package::MANIFEST_PREAMBLE
1297+
cargo::core::manifest::MANIFEST_PREAMBLE
12981298
);
12991299
validate_crate_contents(
13001300
f,
@@ -1367,7 +1367,7 @@ version = "1.0.0"
13671367
[target.{host}.dependencies.baz]
13681368
version = "1.0.0"
13691369
"#,
1370-
cargo::core::package::MANIFEST_PREAMBLE,
1370+
cargo::core::manifest::MANIFEST_PREAMBLE,
13711371
host = rustc_host()
13721372
);
13731373
verify(&p, "package", rewritten_toml);
@@ -1387,7 +1387,7 @@ public = true
13871387
version = "1.0.0"
13881388
public = true
13891389
"#,
1390-
cargo::core::package::MANIFEST_PREAMBLE,
1390+
cargo::core::manifest::MANIFEST_PREAMBLE,
13911391
host = rustc_host()
13921392
);
13931393
verify(&p, "package -Zpublic-dependency", rewritten_toml);
@@ -2808,7 +2808,7 @@ name = "bar"
28082808
version = "0.1.0"
28092809
resolver = "1"
28102810
"#,
2811-
cargo::core::package::MANIFEST_PREAMBLE
2811+
cargo::core::manifest::MANIFEST_PREAMBLE
28122812
);
28132813
validate_crate_contents(
28142814
f,
@@ -2826,7 +2826,7 @@ edition = "2015"
28262826
name = "baz"
28272827
version = "0.1.0"
28282828
"#,
2829-
cargo::core::package::MANIFEST_PREAMBLE
2829+
cargo::core::manifest::MANIFEST_PREAMBLE
28302830
);
28312831
validate_crate_contents(
28322832
f,
@@ -2891,7 +2891,7 @@ description = "foo"
28912891
homepage = "https://example.com/"
28922892
license = "MIT"
28932893
"#,
2894-
cargo::core::package::MANIFEST_PREAMBLE
2894+
cargo::core::manifest::MANIFEST_PREAMBLE
28952895
);
28962896
let cargo_lock_contents = r#"# This file is automatically @generated by Cargo.
28972897
# It is not intended for manual editing.
@@ -2985,7 +2985,7 @@ description = "foo"
29852985
documentation = "https://example.com/"
29862986
license = "MIT"
29872987
"#,
2988-
cargo::core::package::MANIFEST_PREAMBLE
2988+
cargo::core::manifest::MANIFEST_PREAMBLE
29892989
);
29902990
let cargo_lock_contents = r#"# This file is automatically @generated by Cargo.
29912991
# It is not intended for manual editing.
@@ -3092,7 +3092,7 @@ description = "foo"
30923092
homepage = "https://example.com/"
30933093
license = "MIT"
30943094
"#,
3095-
cargo::core::package::MANIFEST_PREAMBLE
3095+
cargo::core::manifest::MANIFEST_PREAMBLE
30963096
);
30973097
let cargo_lock_contents = r#"# This file is automatically @generated by Cargo.
30983098
# It is not intended for manual editing.

tests/testsuite/publish.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ You may press ctrl-c [..]
15661566
[dependencies.dep1]\n\
15671567
version = \"1.0\"\n\
15681568
",
1569-
cargo::core::package::MANIFEST_PREAMBLE
1569+
cargo::core::manifest::MANIFEST_PREAMBLE
15701570
),
15711571
),
15721572
(
@@ -1680,7 +1680,7 @@ repository = "foo"
16801680
16811681
[dev-dependencies]
16821682
"#,
1683-
cargo::core::package::MANIFEST_PREAMBLE
1683+
cargo::core::manifest::MANIFEST_PREAMBLE
16841684
),
16851685
)],
16861686
);
@@ -1979,7 +1979,7 @@ features = ["cat"]
19791979
version = "1.0"
19801980
features = ["cat"]
19811981
"#,
1982-
cargo::core::package::MANIFEST_PREAMBLE
1982+
cargo::core::manifest::MANIFEST_PREAMBLE
19831983
),
19841984
)],
19851985
);

tests/testsuite/weak_dep_features.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ optional = true
642642
feat1 = []
643643
feat2 = ["bar?/feat"]
644644
"#,
645-
cargo::core::package::MANIFEST_PREAMBLE
645+
cargo::core::manifest::MANIFEST_PREAMBLE
646646
),
647647
)],
648648
);

0 commit comments

Comments
 (0)