Fix claim crate names workflow (#3445)

This fixes the claim crate names GitHub Actions workflow that runs on
changes to main.

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
This commit is contained in:
John DiSanti 2024-02-28 09:21:24 -08:00 committed by GitHub
parent 094924cac5
commit e6791567b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 28 additions and 37 deletions

View File

@ -2,19 +2,21 @@
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
use crate::fs::Fs; use crate::package::PackageHandle;
use crate::package::{discover_packages, PackageHandle, Publish};
use crate::publish::{has_been_published_on_crates_io, publish}; use crate::publish::{has_been_published_on_crates_io, publish};
use crate::subcommand::publish::correct_owner; use crate::subcommand::publish::correct_owner;
use crate::{cargo, SDK_REPO_NAME}; use crate::{cargo, SDK_REPO_NAME};
use crate::{fs::Fs, package::discover_manifests};
use anyhow::{Context, Result};
use cargo_toml::Manifest;
use clap::Parser; use clap::Parser;
use dialoguer::Confirm; use dialoguer::Confirm;
use semver::Version; use semver::Version;
use smithy_rs_tool_common::git; use smithy_rs_tool_common::git;
use smithy_rs_tool_common::package::PackageCategory; use smithy_rs_tool_common::package::PackageCategory;
use std::collections::HashSet;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::time::Duration; use std::time::Duration;
use std::{collections::HashSet, fs};
use tracing::info; use tracing::info;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -24,7 +26,7 @@ pub struct ClaimCrateNamesArgs {
skip_confirmation: bool, skip_confirmation: bool,
} }
pub async fn subcommand_claim_crate_names(args: &ClaimCrateNamesArgs) -> anyhow::Result<()> { pub async fn subcommand_claim_crate_names(args: &ClaimCrateNamesArgs) -> Result<()> {
let ClaimCrateNamesArgs { skip_confirmation } = args; let ClaimCrateNamesArgs { skip_confirmation } = args;
// Make sure cargo exists // Make sure cargo exists
cargo::confirm_installed_on_path()?; cargo::confirm_installed_on_path()?;
@ -42,13 +44,12 @@ pub async fn subcommand_claim_crate_names(args: &ClaimCrateNamesArgs) -> anyhow:
s s
}; };
confirm_user_intent(&unpublished_package_names, *skip_confirmation)?;
if unpublished_package_names.is_empty() { if unpublished_package_names.is_empty() {
info!("All publishable packages already exist on crates.io - nothing to do."); info!("All publishable packages already exist on crates.io - nothing to do.");
return Ok(()); return Ok(());
} }
confirm_user_intent(&unpublished_package_names, *skip_confirmation)?;
for name in unpublished_package_names { for name in unpublished_package_names {
claim_crate_name(&name).await?; claim_crate_name(&name).await?;
} }
@ -56,7 +57,7 @@ pub async fn subcommand_claim_crate_names(args: &ClaimCrateNamesArgs) -> anyhow:
Ok(()) Ok(())
} }
async fn claim_crate_name(name: &str) -> anyhow::Result<()> { async fn claim_crate_name(name: &str) -> Result<()> {
let temporary_directory = tempfile::tempdir()?; let temporary_directory = tempfile::tempdir()?;
let crate_dir_path = temporary_directory.path(); let crate_dir_path = temporary_directory.path();
create_dummy_lib_crate(Fs::Real, name, crate_dir_path.to_path_buf()).await?; create_dummy_lib_crate(Fs::Real, name, crate_dir_path.to_path_buf()).await?;
@ -71,35 +72,32 @@ async fn claim_crate_name(name: &str) -> anyhow::Result<()> {
Ok(()) Ok(())
} }
/// Return the list of publishable crate names in the `smithy-rs` git repository. async fn load_publishable_crate_names(path: &Path) -> Result<HashSet<String>> {
async fn discover_publishable_crate_names(repository_root: &Path) -> anyhow::Result<Vec<String>> { let manifest_paths = discover_manifests(path.into()).await?;
async fn _discover_publishable_crate_names( let mut result = HashSet::new();
fs: Fs, for manifest_path in &manifest_paths {
path: PathBuf, let content =
) -> anyhow::Result<HashSet<String>> { fs::read(manifest_path).with_context(|| format!("failed to read {path:?}"))?;
let packages = discover_packages(fs, path).await?; let manifest = Manifest::from_slice(&content)
let mut publishable_package_names = HashSet::new(); .with_context(|| format!("failed to load crate manifest for {:?}", path))?;
for package in packages { if let Some(package) = manifest.package {
if let Publish::Allowed = package.publish { let crate_name = package.name();
publishable_package_names.insert(package.handle.name); if matches!(package.publish(), cargo_toml::Publish::Flag(true)) {
result.insert(crate_name.to_string());
} }
} }
Ok(publishable_package_names)
} }
Ok(result)
}
/// Return the list of publishable crate names in the `smithy-rs` git repository.
async fn discover_publishable_crate_names(repository_root: &Path) -> Result<Vec<String>> {
let packages = { let packages = {
let mut p = vec![]; let mut p = vec![];
info!("Discovering publishable crates..."); info!("Discovering publishable crates...");
p.extend(load_publishable_crate_names(&repository_root.join("rust-runtime")).await?);
p.extend( p.extend(
_discover_publishable_crate_names(Fs::Real, repository_root.join("rust-runtime")) load_publishable_crate_names(&repository_root.join("aws").join("rust-runtime")).await?,
.await?,
);
p.extend(
_discover_publishable_crate_names(
Fs::Real,
repository_root.join("aws").join("rust-runtime"),
)
.await?,
); );
info!("Finished crate discovery."); info!("Finished crate discovery.");
p p
@ -107,11 +105,7 @@ async fn discover_publishable_crate_names(repository_root: &Path) -> anyhow::Res
Ok(packages) Ok(packages)
} }
async fn create_dummy_lib_crate( async fn create_dummy_lib_crate(fs: Fs, package_name: &str, directory_path: PathBuf) -> Result<()> {
fs: Fs,
package_name: &str,
directory_path: PathBuf,
) -> anyhow::Result<()> {
let cargo_toml = format!( let cargo_toml = format!(
r#"[package] r#"[package]
name = "{}" name = "{}"
@ -130,10 +124,7 @@ repository = "https://github.com/smithy-lang/smithy-rs""#,
Ok(()) Ok(())
} }
fn confirm_user_intent( fn confirm_user_intent(crate_names: &HashSet<String>, skip_confirmation: bool) -> Result<()> {
crate_names: &HashSet<String>,
skip_confirmation: bool,
) -> anyhow::Result<()> {
use std::fmt::Write; use std::fmt::Write;
let prompt = { let prompt = {