From 26fe17aa204e30552175f4ba72e763399891722d Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Thu, 3 Aug 2023 21:55:49 +0900 Subject: [PATCH] codegen: Prevent yanked version from being selected as candidate for latest or omitted version --- tools/codegen/Cargo.toml | 2 +- tools/codegen/src/main.rs | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/tools/codegen/Cargo.toml b/tools/codegen/Cargo.toml index 06aab29c..a3d4e2a8 100644 --- a/tools/codegen/Cargo.toml +++ b/tools/codegen/Cargo.toml @@ -7,7 +7,7 @@ publish = false [dependencies] anyhow = "1" fs-err = "2" -semver = "1" +semver = { version = "1", features = ["serde"] } serde = { version = "1", features = ["derive"] } serde_json = "1" sha2 = "0.10" diff --git a/tools/codegen/src/main.rs b/tools/codegen/src/main.rs index f57deb3c..4bb1ee22 100644 --- a/tools/codegen/src/main.rs +++ b/tools/codegen/src/main.rs @@ -74,6 +74,15 @@ fn main() -> Result<()> { }) .collect(); + let mut crates_io_info = None; + if base_info.bin_dir.is_none() { + eprintln!("downloading crate info from https://crates.io/api/v1/crates/{package}"); + crates_io_info = Some( + download(&format!("https://crates.io/api/v1/crates/{package}"))? + .into_json::()?, + ); + } + let mut manifests: Manifests = Manifests::default(); let mut semver_versions = BTreeSet::new(); let mut has_build_metadata = false; @@ -250,6 +259,14 @@ fn main() -> Result<()> { download_info.remove(&HostPlatform::aarch64_linux_gnu); } } + if download_info.contains_key(&HostPlatform::x86_64_macos) + && download_info.contains_key(&HostPlatform::aarch64_macos) + && download_info[&HostPlatform::x86_64_macos].url + == download_info[&HostPlatform::aarch64_macos].url + { + // macOS universal binary or x86_64 binary that works on both x86_64 and aarch64 (rosetta). + download_info.remove(&HostPlatform::aarch64_macos); + } has_build_metadata |= !semver_version.build.is_empty(); if semver_version.pre.is_empty() { semver_versions.insert(semver_version.clone()); @@ -266,6 +283,13 @@ fn main() -> Result<()> { } else if !semver_versions.is_empty() { let mut prev_version = semver_versions.iter().next().unwrap(); for version in &semver_versions { + if let Some(crates_io_info) = &crates_io_info { + if let Some(v) = crates_io_info.versions.iter().find(|v| v.num == *version) { + if v.yanked { + continue; + } + } + } if !(version.major == 0 && version.minor == 0) { manifests.map.insert( Reverse(Version::omitted(version.major, Some(version.minor))), @@ -741,3 +765,19 @@ mod github { pub browser_download_url: String, } } + +mod crates_io { + use serde::Deserialize; + + // https://crates.io/api/v1/crates/ + #[derive(Debug, Deserialize)] + pub struct Crate { + pub versions: Vec, + } + + #[derive(Debug, Deserialize)] + pub struct Version { + pub num: semver::Version, + pub yanked: bool, + } +}