Skip to content

Commit 64fe2da

Browse files
authored
Annotations (#372)
* update wasm-tools to 0.225.0 * add unit test * generate annotations on build * fix empty authors case * disable author generation * add support for 'authors' in metadata
1 parent 1fc82ee commit 64fe2da

File tree

2 files changed

+171
-42
lines changed

2 files changed

+171
-42
lines changed

src/lib.rs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -970,14 +970,8 @@ fn componentize(
970970
})?
971971
.validate(true);
972972

973-
let mut producers = wasm_metadata::Producers::empty();
974-
producers.add(
975-
"processed-by",
976-
env!("CARGO_PKG_NAME"),
977-
option_env!("CARGO_VERSION_INFO").unwrap_or(env!("CARGO_PKG_VERSION")),
978-
);
979-
980-
let component = producers.add_to_wasm(&encoder.encode()?).with_context(|| {
973+
let package = &cargo_metadata[&artifact.package_id];
974+
let component = add_component_metadata(&package, &encoder.encode()?).with_context(|| {
981975
format!(
982976
"failed to add metadata to output component `{path}`",
983977
path = path.display()
@@ -1026,8 +1020,46 @@ pub struct PublishOptions<'a> {
10261020
pub dry_run: bool,
10271021
}
10281022

1029-
fn add_registry_metadata(_package: &Package, bytes: &[u8], _path: &Path) -> Result<Vec<u8>> {
1030-
Ok(bytes.to_owned())
1023+
/// Read metadata from `Cargo.toml` and add it to the component
1024+
fn add_component_metadata(package: &Package, wasm: &[u8]) -> Result<Vec<u8>> {
1025+
let metadata = wasm_metadata::AddMetadata {
1026+
name: Some(package.name.clone()),
1027+
language: vec![("Rust".to_string(), "".to_string())],
1028+
processed_by: vec![(
1029+
env!("CARGO_PKG_NAME").to_string(),
1030+
option_env!("CARGO_VERSION_INFO")
1031+
.unwrap_or(env!("CARGO_PKG_VERSION"))
1032+
.to_string(),
1033+
)],
1034+
sdk: vec![],
1035+
authors: match package.authors.len() {
1036+
0 => None,
1037+
_ => Some(wasm_metadata::Authors::new(package.authors.join(","))),
1038+
},
1039+
description: package
1040+
.description
1041+
.as_ref()
1042+
.map(|d| wasm_metadata::Description::new(d.clone())),
1043+
licenses: package
1044+
.license
1045+
.as_ref()
1046+
.map(|s| wasm_metadata::Licenses::new(&s))
1047+
.transpose()?,
1048+
source: package
1049+
.repository
1050+
.as_ref()
1051+
.map(|s| wasm_metadata::Source::new(s.to_string().as_str()))
1052+
.transpose()?,
1053+
homepage: package
1054+
.homepage
1055+
.as_ref()
1056+
.map(|s| wasm_metadata::Homepage::new(s.to_string().as_str()))
1057+
.transpose()?,
1058+
// TODO: get the git commit hash
1059+
revision: None,
1060+
version: Some(wasm_metadata::Version::new(package.version.to_string())),
1061+
};
1062+
metadata.to_wasm(wasm)
10311063
}
10321064

10331065
/// Publish a component for the given workspace and publish options.
@@ -1050,8 +1082,6 @@ pub async fn publish(
10501082
)
10511083
})?;
10521084

1053-
let bytes = add_registry_metadata(options.package, &bytes, options.path)?;
1054-
10551085
config.terminal().status(
10561086
"Publishing",
10571087
format!("component {path}", path = options.path.display()),

tests/build.rs

Lines changed: 129 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -128,36 +128,6 @@ fn it_supports_wit_keywords() -> Result<()> {
128128
Ok(())
129129
}
130130

131-
#[test]
132-
fn it_adds_a_producers_field() -> Result<()> {
133-
let project = Project::new("foo", true)?;
134-
135-
project
136-
.cargo_component(["build", "--release"])
137-
.assert()
138-
.stderr(contains("Finished `release` profile [optimized] target(s)"))
139-
.success();
140-
141-
let path = project.release_wasm("foo");
142-
143-
validate_component(&path)?;
144-
145-
let wasm = fs::read(&path)
146-
.with_context(|| format!("failed to read wasm file `{path}`", path = path.display()))?;
147-
let section = wasm_metadata::Producers::from_wasm(&wasm)?.expect("missing producers section");
148-
149-
assert_eq!(
150-
section
151-
.get("processed-by")
152-
.expect("missing processed-by field")
153-
.get(env!("CARGO_PKG_NAME"))
154-
.expect("missing cargo-component field"),
155-
option_env!("CARGO_VERSION_INFO").unwrap_or(env!("CARGO_PKG_VERSION"))
156-
);
157-
158-
Ok(())
159-
}
160-
161131
#[test]
162132
fn it_builds_wasm32_unknown_unknown_from_cli() -> Result<()> {
163133
let project = Project::new("foo", true)?;
@@ -1008,3 +978,132 @@ edition = "2021"
1008978

1009979
Ok(())
1010980
}
981+
982+
#[test]
983+
fn it_adds_a_producers_field() -> Result<()> {
984+
let project = Project::new("foo", true)?;
985+
986+
project
987+
.cargo_component(["build", "--release"])
988+
.assert()
989+
.stderr(contains("Finished `release` profile [optimized] target(s)"))
990+
.success();
991+
992+
let path = project.release_wasm("foo");
993+
994+
validate_component(&path)?;
995+
996+
let wasm = fs::read(&path)
997+
.with_context(|| format!("failed to read wasm file `{path}`", path = path.display()))?;
998+
let section = wasm_metadata::Producers::from_wasm(&wasm)?.expect("missing producers section");
999+
1000+
assert_eq!(
1001+
section
1002+
.get("processed-by")
1003+
.expect("missing processed-by field")
1004+
.get(env!("CARGO_PKG_NAME"))
1005+
.expect("missing cargo-component field"),
1006+
option_env!("CARGO_VERSION_INFO").unwrap_or(env!("CARGO_PKG_VERSION"))
1007+
);
1008+
1009+
Ok(())
1010+
}
1011+
1012+
#[test]
1013+
fn it_adds_metadata_from_cargo_toml() -> Result<()> {
1014+
let name = "foo";
1015+
let authors = "Jane Doe <jane@example.com>";
1016+
let description = "A test package";
1017+
let license = "Apache-2.0";
1018+
let version = "1.0.0";
1019+
let documentation = "https://example.com/docs";
1020+
let homepage = "https://example.com/home";
1021+
let repository = "https://example.com/repo";
1022+
1023+
let project = Project::new(name, true)?;
1024+
project.update_manifest(|mut doc| {
1025+
let package = &mut doc["package"];
1026+
package["name"] = value(name);
1027+
package["version"] = value(version);
1028+
package["authors"] = value(Array::from_iter([authors]));
1029+
package["description"] = value(description);
1030+
package["license"] = value(license);
1031+
package["documentation"] = value(documentation);
1032+
package["homepage"] = value(homepage);
1033+
package["repository"] = value(repository);
1034+
Ok(doc)
1035+
})?;
1036+
1037+
project
1038+
.cargo_component(["build", "--release"])
1039+
.assert()
1040+
.stderr(contains("Finished `release` profile [optimized] target(s)"))
1041+
.success();
1042+
1043+
let path = project.release_wasm("foo");
1044+
1045+
validate_component(&path)?;
1046+
1047+
let wasm = fs::read(&path)
1048+
.with_context(|| format!("failed to read wasm file `{path}`", path = path.display()))?;
1049+
1050+
let metadata = match wasm_metadata::Payload::from_binary(&wasm)? {
1051+
wasm_metadata::Payload::Component { metadata, .. } => metadata,
1052+
wasm_metadata::Payload::Module(_) => unreachable!("found a wasm module"),
1053+
};
1054+
1055+
assert_eq!(
1056+
&metadata.name.as_ref().expect("missing name").to_string(),
1057+
name
1058+
);
1059+
assert_eq!(
1060+
&metadata
1061+
.authors
1062+
.as_ref()
1063+
.expect("missing authors")
1064+
.to_string(),
1065+
authors
1066+
);
1067+
assert_eq!(
1068+
&metadata
1069+
.description
1070+
.as_ref()
1071+
.expect("missing description")
1072+
.to_string(),
1073+
description
1074+
);
1075+
assert_eq!(
1076+
&metadata
1077+
.licenses
1078+
.as_ref()
1079+
.expect("missing licenses")
1080+
.to_string(),
1081+
license
1082+
);
1083+
assert_eq!(
1084+
&metadata
1085+
.source
1086+
.as_ref()
1087+
.expect("missing source")
1088+
.to_string(),
1089+
repository
1090+
);
1091+
assert_eq!(
1092+
&metadata
1093+
.homepage
1094+
.as_ref()
1095+
.expect("missing homepage")
1096+
.to_string(),
1097+
homepage
1098+
);
1099+
assert_eq!(
1100+
&metadata
1101+
.version
1102+
.as_ref()
1103+
.expect("missing version")
1104+
.to_string(),
1105+
version
1106+
);
1107+
1108+
Ok(())
1109+
}

0 commit comments

Comments
 (0)