chore(xtask): Make reducing version of swc_core easier (#9406)

**Description:**

This PR
 - makes auto-publish action store the version of `swc_core` in the commit message.
 - fixes `swc_core` version reducing logic.
This commit is contained in:
Donny/강동윤 2024-08-09 14:52:10 +09:00 committed by GitHub
parent 046da0cbaf
commit c977c2bec2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 51 additions and 36 deletions

View File

@ -0,0 +1,5 @@
---
swc_core: patch
---
chore(xtask): Make reducing version of `swc_core` easier

1
Cargo.lock generated
View File

@ -3811,6 +3811,7 @@ name = "swc-releaser"
version = "0.1.0"
dependencies = [
"anyhow",
"cargo_metadata",
"changesets",
"clap 4.5.9",
]

View File

@ -1,17 +0,0 @@
#!/usr/bin/env bash
set -eu
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
function toLine {
# >&2 echo "toLine: $@"
arr=(${1//,/ })
# >&2 echo "arr: ${arr[0]} ${arr[1]}"
dir="$(dirname ${arr[1]})"
echo "${arr[0]} = { path = '$dir' }"
}
export -f toLine
$SCRIPT_DIR/list-crates.sh | jq '[.name, .manifest_path] | @csv' -r | xargs -I {} bash -c 'toLine "$@"' _ {}

View File

@ -7,6 +7,7 @@ repository = { workspace = true }
version = "0.1.0"
[dependencies]
anyhow = { workspace = true }
changesets = { workspace = true }
clap = { version = "4.5.9", features = ["derive"] }
anyhow = { workspace = true }
cargo_metadata = { workspace = true }
changesets = { workspace = true }
clap = { version = "4.5.9", features = ["derive"] }

View File

@ -66,9 +66,27 @@ fn run_bump(workspace_dir: &Path, dry_run: bool) -> Result<()> {
Ok(())
}
fn get_swc_core_version() -> Result<String> {
let md = cargo_metadata::MetadataCommand::new()
.no_deps()
.exec()
.expect("failed to run cargo metadata");
md.packages
.iter()
.find(|p| p.name == "swc_core")
.map(|p| p.version.to_string())
.context("failed to find swc_core")
}
fn commit(dry_run: bool) -> Result<()> {
let core_ver = get_swc_core_version()?;
let mut cmd = Command::new("git");
cmd.arg("commit").arg("-am").arg("chore: Publish crates");
cmd.arg("commit").arg("-am").arg(format!(
"chore: Publish crates with `swc_core` `v{}`",
core_ver
));
eprintln!("Running {:?}", cmd);

View File

@ -1,7 +1,7 @@
use anyhow::Result;
use clap::Args;
use crate::util::get_commit_for_swc_core_version;
use crate::util::get_commit_for_core_version;
/// Reduce the difference of the versions of `swc_core`s to the list of commits
/// and pull requests.
@ -14,8 +14,8 @@ pub(super) struct CoreVerCmd {
impl CoreVerCmd {
pub fn run(self) -> Result<()> {
let from_commit = get_commit_for_swc_core_version(&self.from)?;
let to_commit = get_commit_for_swc_core_version(&self.to)?;
let from_commit = get_commit_for_core_version(&self.from, false)?;
let to_commit = get_commit_for_core_version(&self.to, true)?;
eprintln!(
"GitHub diff: https://github.com/swc-project/swc/compare/{from_commit}...{to_commit}"

View File

@ -32,7 +32,7 @@ pub fn run_cmd(cmd: &mut Command) -> Result<()> {
Ok(())
}
pub fn get_commit_for_swc_core_version(version: &str) -> Result<String> {
pub fn get_commit_for_core_version(version: &str, last: bool) -> Result<String> {
wrap(|| {
eprintln!("Getting commit for swc_core@v{}", version);
@ -71,26 +71,33 @@ pub fn get_commit_for_swc_core_version(version: &str) -> Result<String> {
let line_count = output.lines().count();
for line in output.lines() {
let commit = line.split(':').next().unwrap().to_string();
if line_count == 0 {
bail!("swc_core@v{} is not found in the repository", version);
}
if line_count == 1 || get_version_of_swc_core_of_commit(&commit)? == version {
eprintln!("\tThe commit for swc_core@v{} is {}", version, commit);
let iter: Box<dyn Iterator<Item = &str>> = if last {
Box::new(output.lines().rev())
} else {
Box::new(output.lines())
};
for line in iter {
let commit = line.split(':').next().unwrap().to_string();
let commit_version = get_version_of_swc_core_of_commit(&commit)?;
if Some(version) == commit_version.as_deref() {
eprintln!("\tFound commit for swc_core@v{}: https://github.com/swc-project/swc/commits/{}", version, commit);
return Ok(commit);
}
}
bail!(
"check if the version is the one of swc_core, where the output of git grep is\n{}",
output
)
bail!("swc_core@v{} is not found in the repository", version);
})
.with_context(|| format!("failed to get the commit for swc_core@v{}", version))
}
/// Read the version of swc_core from `Cargo.lock`
pub fn get_version_of_swc_core_of_commit(commit: &str) -> Result<String> {
pub fn get_version_of_swc_core_of_commit(commit: &str) -> Result<Option<String>> {
wrap(|| {
let output = Command::new("git")
.current_dir(repository_root()?)
@ -108,11 +115,11 @@ pub fn get_version_of_swc_core_of_commit(commit: &str) -> Result<String> {
for pkg in content.package {
if pkg.name == "swc_core" {
return Ok(pkg.version);
return Ok(Some(pkg.version));
}
}
bail!("swc_core is not found in Cargo.lock")
Ok(None)
})
.with_context(|| format!("failed to get the version of swc_core of {}", commit))
}