codegen: Add rust_crate field to cargo-rdme

This commit is contained in:
Taiki Endo
2024-03-02 17:49:10 +09:00
parent e77cabb736
commit 7c2d35d7f6
7 changed files with 68 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
{ {
"rust_crate": null, "rust_crate": "cargo-rdme",
"template": { "template": {
"x86_64_linux_musl": { "x86_64_linux_musl": {
"url": "https://github.com/orium/cargo-rdme/releases/download/v${version}/cargo-rdme_v${version}_x86_64-unknown-linux-musl.tar.bz2" "url": "https://github.com/orium/cargo-rdme/releases/download/v${version}/cargo-rdme_v${version}_x86_64-unknown-linux-musl.tar.bz2"

View File

@@ -14,6 +14,7 @@ serde_json = "1"
sha2 = "0.10" sha2 = "0.10"
tar = "0.4" tar = "0.4"
toml_edit = { version = "0.22", default-features = false, features = ["parse", "serde"] } toml_edit = { version = "0.22", default-features = false, features = ["parse", "serde"] }
# TODO: call curl command instead of using ureq?
ureq = { version = "2", features = ["json"] } ureq = { version = "2", features = ["json"] }
[lints] [lints]

View File

@@ -2,7 +2,7 @@
"repository": "https://github.com/sonos/dinghy", "repository": "https://github.com/sonos/dinghy",
"tag_prefix": "", "tag_prefix": "",
"rust_crate": "${package}", "rust_crate": "${package}",
"asset_name": "${package}-${os_name}-${version}.tgz", "asset_name": "${package}-${rust_target_os}-${version}.tgz",
"bin": "${package}-${version}/${package}${exe}", "bin": "${package}-${version}/${package}${exe}",
"platform": { "platform": {
"x86_64_linux_musl": {}, "x86_64_linux_musl": {},

View File

@@ -1,6 +1,7 @@
{ {
"repository": "https://github.com/orium/cargo-rdme", "repository": "https://github.com/orium/cargo-rdme",
"tag_prefix": "v", "tag_prefix": "v",
"rust_crate": "${package}",
"broken": ["1.4.3"], "broken": ["1.4.3"],
"platform": { "platform": {
"x86_64_linux_musl": { "x86_64_linux_musl": {

View File

@@ -2,26 +2,15 @@
"repository": "https://github.com/bytecodealliance/wasmtime", "repository": "https://github.com/bytecodealliance/wasmtime",
"tag_prefix": "v", "tag_prefix": "v",
"rust_crate": "wasmtime-cli", "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": { "platform": {
"x86_64_linux_gnu": { "x86_64_linux_gnu": {},
"asset_name": "${package}-v${version}-x86_64-linux.tar.xz", "x86_64_macos": {},
"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_windows": { "x86_64_windows": {
"asset_name": "${package}-v${version}-x86_64-windows.zip", "asset_name": "${package}-v${version}-${rust_target_arch}-${rust_target_os}.zip"
"bin": "${package}-v${version}-x86_64-windows/${package}${exe}"
}, },
"aarch64_linux_gnu": { "aarch64_linux_gnu": {},
"asset_name": "${package}-v${version}-aarch64-linux.tar.xz", "aarch64_macos": {}
"bin": "${package}-v${version}-aarch64-linux/${package}${exe}"
},
"aarch64_macos": {
"asset_name": "${package}-v${version}-aarch64-macos.tar.xz",
"bin": "${package}-v${version}-aarch64-macos/${package}${exe}"
}
} }
} }

View File

@@ -2,8 +2,8 @@
"repository": "https://github.com/rust-mobile/xbuild", "repository": "https://github.com/rust-mobile/xbuild",
"tag_prefix": "v", "tag_prefix": "v",
"rust_crate": "${package}", "rust_crate": "${package}",
"asset_name": "${package}-${os_name}-x64${exe}", "asset_name": "${package}-${rust_target_os}-x64${exe}",
"bin": "${package}-${os_name}-x64${exe}", "bin": "${package}-${rust_target_os}-x64${exe}",
"version_range": ">= 0.2.0", "version_range": ">= 0.2.0",
"platform": { "platform": {
"x86_64_linux_gnu": {}, "x86_64_linux_gnu": {},

View File

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