From f15b456df1a0d484cb22901f72610d00a3e81158 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Sun, 21 Jan 2024 12:31:28 -0500 Subject: [PATCH 1/2] github: Change release job to use auto-release https://crates.io/crates/auto-release --- .github/workflows/release.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 26785cd2c..a4858c5d8 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,7 +14,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: Swatinem/rust-cache@v2 - - run: cargo xtask auto-release + - run: | + cargo install auto-release + auto-release --condition body -p uefi-raw -p uefi-macros -p uefi -p uefi-services env: CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} From 9e097c1c74c250a118caa823ad5d91f4f2b84b47 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Sun, 21 Jan 2024 12:39:18 -0500 Subject: [PATCH 2/2] xtask: Remove auto-release action The Github Actions release workflow now uses https://crates.io/crates/auto-release instead, so we don't need the xtask implementation anymore. This reverts (with some minor edits) these commits: * d8bb47b79540c25a97e9d48b9021688509266298 "xtask: Add auto-release action" * ad75b2f5733b7b00f918b3623c1560020d907a89 "xtask: Add util::run_cmd_capture_stdout" * 910bfc3318d1e7a66ab47c388e333747b701fc53 "xtask: rename Package::as_str to Package::name and make pub" --- Cargo.lock | 194 ------------------------------------------ xtask/Cargo.toml | 4 +- xtask/src/cargo.rs | 2 +- xtask/src/main.rs | 2 - xtask/src/opt.rs | 8 -- xtask/src/release.rs | 196 ------------------------------------------- xtask/src/util.rs | 15 +--- 7 files changed, 3 insertions(+), 418 deletions(-) delete mode 100644 xtask/src/release.rs diff --git a/Cargo.lock b/Cargo.lock index e03ba1844..17a54f710 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -104,44 +104,6 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" -[[package]] -name = "bytes" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" - -[[package]] -name = "camino" -version = "1.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c59e92b5a388f549b863a7bea62612c09f24c8393560709a54558a9abdfb3b9c" -dependencies = [ - "serde", -] - -[[package]] -name = "cargo-platform" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ceed8ef69d8518a5dda55c07425450b58a4e1946f4951eab6d7191ee86c2443d" -dependencies = [ - "serde", -] - -[[package]] -name = "cargo_metadata" -version = "0.18.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d886547e41f740c616ae73108f6eb70afe6d940c7bc697cb30f13daec073037" -dependencies = [ - "camino", - "cargo-platform", - "semver", - "serde", - "serde_json", - "thiserror", -] - [[package]] name = "cc" version = "1.0.83" @@ -204,26 +166,6 @@ dependencies = [ "libc", ] -[[package]] -name = "crates-index" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fd4fd24d7d0ac47a323fb609dfe0d80fa711b3cfe9a595eaaabbd19597c4c55" -dependencies = [ - "hex", - "home", - "http", - "memchr", - "rustc-hash", - "semver", - "serde", - "serde_derive", - "serde_json", - "smol_str", - "thiserror", - "toml", -] - [[package]] name = "crc" version = "3.0.1" @@ -274,12 +216,6 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" -[[package]] -name = "equivalent" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" - [[package]] name = "errno" version = "0.3.8" @@ -329,12 +265,6 @@ dependencies = [ "miniz_oxide", ] -[[package]] -name = "fnv" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - [[package]] name = "form_urlencoded" version = "1.2.1" @@ -386,47 +316,12 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" -[[package]] -name = "hashbrown" -version = "0.14.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" - [[package]] name = "heck" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" -[[package]] -name = "hex" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" -dependencies = [ - "serde", -] - -[[package]] -name = "home" -version = "0.5.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" -dependencies = [ - "windows-sys 0.52.0", -] - -[[package]] -name = "http" -version = "0.2.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8947b1a6fad4393052c7ba1f4cd97bed3e953a95c79c92ad9b051a04611d9fbb" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - [[package]] name = "idna" version = "0.3.0" @@ -437,16 +332,6 @@ dependencies = [ "unicode-normalization", ] -[[package]] -name = "indexmap" -version = "2.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "433de089bd45971eecf4668ee0ee8f4cec17db4f8bd8f7bc3197a6ce37aa7d9b" -dependencies = [ - "equivalent", - "hashbrown", -] - [[package]] name = "itertools" version = "0.12.1" @@ -653,12 +538,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "rustc-hash" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" - [[package]] name = "rustix" version = "0.38.31" @@ -718,15 +597,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "semver" -version = "1.0.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b97ed7a9823b74f99c7742f5336af7be5ecd3eeafcb1507d1fa93347b1d589b0" -dependencies = [ - "serde", -] - [[package]] name = "serde" version = "1.0.196" @@ -767,15 +637,6 @@ dependencies = [ "serde", ] -[[package]] -name = "serde_spanned" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" -dependencies = [ - "serde", -] - [[package]] name = "sha2" version = "0.10.8" @@ -787,15 +648,6 @@ dependencies = [ "digest", ] -[[package]] -name = "smol_str" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6845563ada680337a52d43bb0b29f396f2d911616f6573012645b9e3d048a49" -dependencies = [ - "serde", -] - [[package]] name = "spin" version = "0.9.8" @@ -903,40 +755,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" -[[package]] -name = "toml" -version = "0.8.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1a195ec8c9da26928f773888e0742ca3ca1040c6cd859c919c9f59c1954ab35" -dependencies = [ - "serde", - "serde_spanned", - "toml_datetime", - "toml_edit", -] - -[[package]] -name = "toml_datetime" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" -dependencies = [ - "serde", -] - -[[package]] -name = "toml_edit" -version = "0.21.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d34d383cd00a163b4a5b85053df514d45bc330f6de7737edfe0a93311d1eaa03" -dependencies = [ - "indexmap", - "serde", - "serde_spanned", - "toml_datetime", - "winnow", -] - [[package]] name = "trybuild" version = "1.0.89" @@ -1069,7 +887,6 @@ checksum = "11f214ce18d8b2cbe84ed3aa6486ed3f5b285cf8d8fbdbce9f3f767a724adc35" dependencies = [ "base64", "flate2", - "http", "log", "once_cell", "rustls", @@ -1284,15 +1101,6 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" -[[package]] -name = "winnow" -version = "0.5.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1931d78a9c73861da0134f453bb1f790ce49b2e30eba8410b4b79bac72b46a2d" -dependencies = [ - "memchr", -] - [[package]] name = "wyz" version = "0.5.1" @@ -1318,9 +1126,7 @@ name = "xtask" version = "0.0.0" dependencies = [ "anyhow", - "cargo_metadata", "clap", - "crates-index", "fatfs", "fs-err", "heck", diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 3637a208e..bd16cd8b1 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -7,9 +7,7 @@ edition.workspace = true [dependencies] anyhow = "1.0.51" -cargo_metadata = "0.18.1" clap = { version = "4.4.0", default-features = false, features = ["derive", "help", "usage", "std"] } -crates-index = "2.3.0" fatfs = { version = "0.3.6", default-features = false, features = ["alloc", "std"] } fs-err = "2.6.0" heck = "0.4.0" @@ -26,5 +24,5 @@ sha2 = "0.10.6" syn = { version = "2.0.0", features = ["full"] } tar = "0.4.38" tempfile = "3.6.0" -ureq = { version = "2.8.0", features = ["http-interop"] } walkdir = "2.4.0" +ureq = "2.8.0" diff --git a/xtask/src/cargo.rs b/xtask/src/cargo.rs index 2fc782995..0ac87cd41 100644 --- a/xtask/src/cargo.rs +++ b/xtask/src/cargo.rs @@ -17,7 +17,7 @@ pub enum Package { impl Package { /// Get package name. - pub fn name(self) -> &'static str { + fn name(self) -> &'static str { match self { Self::Uefi => "uefi", Self::UefiApp => "uefi_app", diff --git a/xtask/src/main.rs b/xtask/src/main.rs index f3804e7ea..8cba0feeb 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -8,7 +8,6 @@ mod opt; mod pipe; mod platform; mod qemu; -mod release; mod tpm; mod util; @@ -315,6 +314,5 @@ fn main() -> Result<()> { Action::Run(qemu_opt) => run_vm_tests(qemu_opt), Action::Test(test_opt) => run_host_tests(test_opt), Action::Fmt(fmt_opt) => run_fmt_project(fmt_opt), - Action::AutoRelease(_) => release::auto_release(), } } diff --git a/xtask/src/opt.rs b/xtask/src/opt.rs index 67b1b538e..af3f06250 100644 --- a/xtask/src/opt.rs +++ b/xtask/src/opt.rs @@ -74,7 +74,6 @@ pub enum Action { Run(QemuOpt), Test(TestOpt), Fmt(FmtOpt), - AutoRelease(AutoReleaseOpt), } /// Build all the uefi packages. @@ -203,10 +202,3 @@ pub struct FmtOpt { #[clap(long, action)] pub check: bool, } - -/// Run the auto-release process (should only be run by Github Action). -/// -/// This is run by the `release.yml` workflow. It is not intended to be run -/// locally, and will exit immediately if `GITHUB_SHA` is not set. -#[derive(Debug, Parser)] -pub struct AutoReleaseOpt {} diff --git a/xtask/src/release.rs b/xtask/src/release.rs deleted file mode 100644 index b514fa28c..000000000 --- a/xtask/src/release.rs +++ /dev/null @@ -1,196 +0,0 @@ -use crate::cargo::Package; -use crate::util; -use anyhow::{Context, Result}; -use cargo_metadata::{Metadata, MetadataCommand}; -use crates_index::SparseIndex; -use std::env; -use std::process::Command; - -/// Entry point for the auto-release process. This is intended to be run from a -/// Github Actions workflow, see `.github/workflows/release.yml`. -pub fn auto_release() -> Result<()> { - let commit_sha = get_commit_sha()?; - let commit_message_body = get_commit_message_body(&commit_sha)?; - - if !commit_message_body.starts_with("release:") { - println!("{commit_sha} does not contain the release trigger"); - return Ok(()); - } - - fetch_git_tags()?; - - let local_metadata = get_local_package_metadata()?; - let mut index = SparseIndex::new_cargo_default()?; - - for package in Package::published() { - auto_release_package(package, &local_metadata, &mut index, &commit_sha)?; - } - - Ok(()) -} - -/// Release a single package, if needed. -/// -/// This publishes to crates.io if the corresponding version does not already -/// exist there, and also pushes a new git tag if one doesn't exist yet. -fn auto_release_package( - package: Package, - local_metadata: &Metadata, - index: &mut SparseIndex, - commit_sha: &str, -) -> Result<()> { - let local_version = get_local_package_version(package, local_metadata)?; - println!("local version of {} is {local_version}", package.name()); - - // Create the remote git tag if it doesn't exist. - let tag = get_git_tag_name(package, &local_version); - if does_git_tag_exist(&tag)? { - println!("git tag {tag} already exists"); - } else { - make_and_push_git_tag(&tag, commit_sha)?; - } - - // Create the crates.io release if it doesn't exist. - if does_crates_io_release_exist(package, &local_version, index)? { - println!( - "{}-{local_version} has already been published", - package.name() - ); - } else { - publish_package(package)?; - } - - Ok(()) -} - -/// Get the commit to operate on from the `GITHUB_SHA` env var. When running in -/// Github Actions, this will be set to the SHA of the merge commit that was -/// pushed to the branch. -fn get_commit_sha() -> Result { - let commit_var_name = "GITHUB_SHA"; - env::var(commit_var_name).context(format!("failed to get env var {commit_var_name}")) -} - -/// Create a git command with the given args. -fn get_git_command(args: [&str; N]) -> Command { - let mut cmd = Command::new("git"); - cmd.args(args); - cmd -} - -/// Get the body of the commit message for the given commit. -fn get_commit_message_body(commit_sha: &str) -> Result { - let cmd = get_git_command([ - "log", - "-1", - // Only get the body of the commit message. - "--format=format:%b", - commit_sha, - ]); - util::run_cmd_get_stdout(cmd) -} - -/// Use the `cargo_metadata` crate to get local info about packages in the -/// workspace. -fn get_local_package_metadata() -> Result { - let mut cmd = MetadataCommand::new(); - // Ignore deps, we only need local packages. - cmd.no_deps(); - Ok(cmd.exec()?) -} - -/// Fetch git tags from the remote. -fn fetch_git_tags() -> Result<()> { - let cmd = get_git_command(["fetch", "--tags"]); - util::run_cmd(cmd) -} - -/// Format a package version as a git tag. -fn get_git_tag_name(package: Package, local_version: &str) -> String { - format!("{}-v{}", package.name(), local_version) -} - -/// Check if a git tag exists locally. -/// -/// All git tags were fetched at the start of auto-release, so checking locally -/// is sufficient. -fn does_git_tag_exist(tag: &str) -> Result { - let cmd = get_git_command(["tag", "--list", tag]); - let output = util::run_cmd_get_stdout(cmd)?; - - Ok(output.lines().any(|line| line == tag)) -} - -/// Create a git tag locally and push it. -fn make_and_push_git_tag(tag: &str, commit_sha: &str) -> Result<()> { - // Create the tag. - let cmd = get_git_command(["tag", tag, commit_sha]); - util::run_cmd(cmd)?; - - // Push it. - let cmd = get_git_command(["push", "--tags"]); - util::run_cmd(cmd) -} - -/// Update the local crates.io cache. -/// -/// Based on https://github.com/frewsxcv/rust-crates-index/blob/HEAD/examples/sparse_http_ureq.rs -fn update_index(index: &mut SparseIndex, package: Package) -> Result<()> { - let crate_name = package.name(); - - println!("fetching updates for {}", package.name()); - let request: ureq::Request = index.make_cache_request(crate_name).unwrap().into(); - let response = request.call()?; - - index.parse_cache_response(crate_name, response.into(), true)?; - - Ok(()) -} - -/// Check if a new release of `package` should be published. -fn does_crates_io_release_exist( - package: Package, - local_version: &str, - index: &mut SparseIndex, -) -> Result { - let remote_versions = get_remote_package_versions(package, index)?; - if remote_versions.contains(&local_version.to_string()) { - return Ok(true); - } - - Ok(false) -} - -/// Get the local version of `package`. -fn get_local_package_version(package: Package, local_metadata: &Metadata) -> Result { - let metadata = local_metadata - .packages - .iter() - .find(|pm| pm.name == package.name()) - .context(format!( - "failed to find {} in local metadata", - package.name() - ))?; - Ok(metadata.version.to_string()) -} - -/// Get all remote versions of `package`. -fn get_remote_package_versions(package: Package, index: &mut SparseIndex) -> Result> { - // The local cache may be out of date, fetch updates from the remote. - update_index(index, package)?; - - let cr = index.crate_from_cache(package.name())?; - - Ok(cr - .versions() - .iter() - .map(|v| v.version().to_string()) - .collect()) -} - -/// Publish `package` to crates.io. -fn publish_package(package: Package) -> Result<()> { - let mut cmd = Command::new("cargo"); - cmd.args(["publish", "--package", package.name()]); - util::run_cmd(cmd) -} diff --git a/xtask/src/util.rs b/xtask/src/util.rs index 9d46ee4c2..86a2da63b 100644 --- a/xtask/src/util.rs +++ b/xtask/src/util.rs @@ -1,4 +1,4 @@ -use anyhow::{bail, Context, Result}; +use anyhow::{bail, Result}; use std::process::Command; /// Format a `Command` as a `String. @@ -44,19 +44,6 @@ pub fn run_cmd(mut cmd: Command) -> Result<()> { } } -/// Print a `Command` and run it, then check that it completes -/// successfully. Return the command's stdout as a `String`. -pub fn run_cmd_get_stdout(mut cmd: Command) -> Result { - println!("run_cmd: '{}'", command_to_string(&cmd)); - - let output = cmd.output()?; - if output.status.success() { - String::from_utf8(output.stdout).context("command output is not utf-8") - } else { - bail!("command failed: {}", output.status); - } -} - #[cfg(test)] mod tests { use super::*;