Skip to content

Commit

Permalink
codegen: Add rust_crate field to cargo-rdme
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Mar 2, 2024
1 parent e77cabb commit 7c2d35d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 31 deletions.
2 changes: 1 addition & 1 deletion manifests/cargo-rdme.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tools/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ serde_json = "1"
sha2 = "0.10"
tar = "0.4"
toml_edit = { version = "0.22", default-features = false, features = ["parse", "serde"] }
# TODO: call curl command instead of using ureq?
ureq = { version = "2", features = ["json"] }

[lints]
Expand Down
2 changes: 1 addition & 1 deletion tools/codegen/base/cargo-dinghy.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"repository": "https://github.com/sonos/dinghy",
"tag_prefix": "",
"rust_crate": "${package}",
"asset_name": "${package}-${os_name}-${version}.tgz",
"asset_name": "${package}-${rust_target_os}-${version}.tgz",
"bin": "${package}-${version}/${package}${exe}",
"platform": {
"x86_64_linux_musl": {},
Expand Down
1 change: 1 addition & 0 deletions tools/codegen/base/cargo-rdme.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"repository": "https://github.com/orium/cargo-rdme",
"tag_prefix": "v",
"rust_crate": "${package}",
"broken": ["1.4.3"],
"platform": {
"x86_64_linux_musl": {
Expand Down
25 changes: 7 additions & 18 deletions tools/codegen/base/wasmtime.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,15 @@
"repository": "https://github.com/bytecodealliance/wasmtime",
"tag_prefix": "v",
"rust_crate": "wasmtime-cli",
"asset_name": "${package}-v${version}-${rust_target_arch}-${rust_target_os}.tar.xz",
"bin": "${package}-v${version}-${rust_target_arch}-${rust_target_os}/${package}${exe}",
"platform": {
"x86_64_linux_gnu": {
"asset_name": "${package}-v${version}-x86_64-linux.tar.xz",
"bin": "${package}-v${version}-x86_64-linux/${package}${exe}"
},
"x86_64_macos": {
"asset_name": "${package}-v${version}-x86_64-macos.tar.xz",
"bin": "${package}-v${version}-x86_64-macos/${package}${exe}"
},
"x86_64_linux_gnu": {},
"x86_64_macos": {},
"x86_64_windows": {
"asset_name": "${package}-v${version}-x86_64-windows.zip",
"bin": "${package}-v${version}-x86_64-windows/${package}${exe}"
},
"aarch64_linux_gnu": {
"asset_name": "${package}-v${version}-aarch64-linux.tar.xz",
"bin": "${package}-v${version}-aarch64-linux/${package}${exe}"
"asset_name": "${package}-v${version}-${rust_target_arch}-${rust_target_os}.zip"
},
"aarch64_macos": {
"asset_name": "${package}-v${version}-aarch64-macos.tar.xz",
"bin": "${package}-v${version}-aarch64-macos/${package}${exe}"
}
"aarch64_linux_gnu": {},
"aarch64_macos": {}
}
}
4 changes: 2 additions & 2 deletions tools/codegen/base/xbuild.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"repository": "https://github.com/rust-mobile/xbuild",
"tag_prefix": "v",
"rust_crate": "${package}",
"asset_name": "${package}-${os_name}-x64${exe}",
"bin": "${package}-${os_name}-x64${exe}",
"asset_name": "${package}-${rust_target_os}-x64${exe}",
"bin": "${package}-${rust_target_os}-x64${exe}",
"version_range": ">= 0.2.0",
"platform": {
"x86_64_linux_gnu": {},
Expand Down
64 changes: 55 additions & 9 deletions tools/codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ fn main() -> Result<()> {
}

let mut crates_io_info = None;
base_info.rust_crate =
base_info.rust_crate.as_ref().map(|s| replace_vars(s, package, None, None)).transpose()?;
base_info.rust_crate = base_info
.rust_crate
.as_ref()
.map(|s| replace_vars(s, package, None, None, base_info.rust_crate.as_deref()))
.transpose()?;
if let Some(crate_name) = &base_info.rust_crate {
eprintln!("downloading crate info from https://crates.io/api/v1/crates/{crate_name}");
crates_io_info = Some(
Expand Down Expand Up @@ -185,7 +188,15 @@ fn main() -> Result<()> {
.with_context(|| format!("asset_name is needed for {package} on {platform:?}"))?
.as_slice()
.iter()
.map(|asset_name| replace_vars(asset_name, package, Some(version), Some(platform)))
.map(|asset_name| {
replace_vars(
asset_name,
package,
Some(version),
Some(platform),
base_info.rust_crate.as_deref(),
)
})
.collect::<Result<Vec<_>>>()?;
let (url, asset_name) = match asset_names.iter().find_map(|asset_name| {
release
Expand Down Expand Up @@ -306,7 +317,15 @@ fn main() -> Result<()> {
.bin
.as_ref()
.or(base_info.bin.as_ref())
.map(|s| replace_vars(s, package, Some(version), Some(platform)))
.map(|s| {
replace_vars(
s,
package,
Some(version),
Some(platform),
base_info.rust_crate.as_deref(),
)
})
.transpose()?,
});
buf.clear();
Expand Down Expand Up @@ -471,18 +490,33 @@ fn replace_vars(
package: &str,
version: Option<&str>,
platform: Option<HostPlatform>,
rust_crate: Option<&str>,
) -> Result<String> {
const RUST_SPECIFIC: &[(&str, fn(HostPlatform) -> &'static str)] = &[
("${rust_target}", HostPlatform::rust_target),
("${rust_target_arch}", HostPlatform::rust_target_arch),
("${rust_target_os}", HostPlatform::rust_target_os),
];
let mut s = s.replace("${package}", package).replace("${tool}", package);
if let Some(platform) = platform {
s = s
.replace("${rust_target}", platform.rust_target())
.replace("${os_name}", platform.os_name())
.replace("${exe}", platform.exe_suffix());
s = s.replace("${exe}", platform.exe_suffix());
if rust_crate.is_some() {
for &(var, f) in RUST_SPECIFIC {
s = s.replace(var, f(platform));
}
}
}
if let Some(version) = version {
s = s.replace("${version}", version);
}
if s.contains('$') {
for &(var, _) in RUST_SPECIFIC {
if s.contains(var) {
bail!(
"base manifest for {package} refers {var}, but 'rust_crate' field is not set"
);
}
}
bail!("variable not fully replaced: '{s}'");
}
Ok(s)
Expand Down Expand Up @@ -799,7 +833,19 @@ impl HostPlatform {
Self::aarch64_windows => "aarch64-pc-windows-msvc",
}
}
fn os_name(self) -> &'static str {
fn rust_target_arch(self) -> &'static str {
match self {
Self::aarch64_linux_gnu
| Self::aarch64_linux_musl
| Self::aarch64_macos
| Self::aarch64_windows => "aarch64",
Self::x86_64_linux_gnu
| Self::x86_64_linux_musl
| Self::x86_64_macos
| Self::x86_64_windows => "x86_64",
}
}
fn rust_target_os(self) -> &'static str {
match self {
Self::aarch64_linux_gnu
| Self::aarch64_linux_musl
Expand Down

0 comments on commit 7c2d35d

Please sign in to comment.