diff --git a/Cargo.lock b/Cargo.lock index c87cc9595266..1dbb76e9ba0c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -510,7 +510,7 @@ dependencies = [ "bitflags 1.3.2", "clap_derive 3.2.25", "clap_lex 0.2.4", - "indexmap", + "indexmap 1.9.3", "once_cell", "strsim", "termcolor", @@ -767,7 +767,7 @@ dependencies = [ "cranelift-entity", "fxhash", "hashbrown 0.12.3", - "indexmap", + "indexmap 1.9.3", "log", "smallvec", ] @@ -1226,6 +1226,12 @@ dependencies = [ "syn 2.0.37", ] +[[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.1" @@ -1482,7 +1488,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4" dependencies = [ "fallible-iterator", - "indexmap", + "indexmap 1.9.3", "stable_deref_trait", ] @@ -1504,7 +1510,7 @@ dependencies = [ "futures-sink", "futures-util", "http", - "indexmap", + "indexmap 1.9.3", "slab", "tokio", "tokio-util", @@ -1544,6 +1550,12 @@ dependencies = [ "ahash 0.8.3", ] +[[package]] +name = "hashbrown" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dfda62a12f55daeae5015f81b0baea145391cb4520f86c248fc615d72640d12" + [[package]] name = "heapless" version = "0.7.16" @@ -1737,6 +1749,16 @@ dependencies = [ "serde", ] +[[package]] +name = "indexmap" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897" +dependencies = [ + "equivalent", + "hashbrown 0.14.1", +] + [[package]] name = "instant" version = "0.1.12" @@ -2547,7 +2569,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4dd7d28ee937e54fe3080c91faa1c3a46c06de6252988a7f4592ba2310ef22a4" dependencies = [ "fixedbitset", - "indexmap", + "indexmap 1.9.3", ] [[package]] @@ -2717,7 +2739,7 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" dependencies = [ - "toml", + "toml 0.5.9", ] [[package]] @@ -2727,7 +2749,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" dependencies = [ "once_cell", - "toml_edit", + "toml_edit 0.19.8", ] [[package]] @@ -3034,7 +3056,7 @@ dependencies = [ "bitvec", "bytecheck", "hashbrown 0.12.3", - "indexmap", + "indexmap 1.9.3", "ptr_meta", "rend", "rkyv_derive", @@ -3258,6 +3280,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96426c9936fd7a0124915f9185ea1d20aa9445cc9821142f0a73bc9207a2e186" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -3276,7 +3307,7 @@ version = "0.8.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "578a7433b776b56a35785ed5ce9a7e777ac0598aac5a6dd1b4b18a307c7fc71b" dependencies = [ - "indexmap", + "indexmap 1.9.3", "ryu", "serde", "yaml-rust", @@ -3626,7 +3657,7 @@ dependencies = [ "criterion", "dashmap", "either", - "indexmap", + "indexmap 1.9.3", "jsonc-parser", "lru", "napi", @@ -3695,7 +3726,7 @@ dependencies = [ "crc", "dashmap", "hex", - "indexmap", + "indexmap 1.9.3", "is-macro", "ntest", "once_cell", @@ -3826,7 +3857,7 @@ dependencies = [ name = "swc_config" version = "0.1.7" dependencies = [ - "indexmap", + "indexmap 1.9.3", "serde", "serde_json", "swc_config_macro", @@ -4209,7 +4240,7 @@ dependencies = [ "arrayvec", "backtrace", "criterion", - "indexmap", + "indexmap 1.9.3", "num-bigint", "num_cpus", "once_cell", @@ -4274,7 +4305,7 @@ dependencies = [ "anyhow", "criterion", "dashmap", - "indexmap", + "indexmap 1.9.3", "once_cell", "preset_env_base", "pretty_assertions", @@ -4365,7 +4396,7 @@ dependencies = [ "better_scoped_tls", "bitflags 2.3.2", "criterion", - "indexmap", + "indexmap 1.9.3", "once_cell", "phf", "rayon", @@ -4402,7 +4433,7 @@ name = "swc_ecma_transforms_compat" version = "0.160.1" dependencies = [ "arrayvec", - "indexmap", + "indexmap 1.9.3", "is-macro", "num-bigint", "rayon", @@ -4443,7 +4474,7 @@ dependencies = [ "Inflector", "anyhow", "bitflags 2.3.2", - "indexmap", + "indexmap 1.9.3", "is-macro", "path-clean", "pathdiff", @@ -4470,7 +4501,7 @@ name = "swc_ecma_transforms_optimization" version = "0.194.1" dependencies = [ "dashmap", - "indexmap", + "indexmap 1.9.3", "once_cell", "petgraph", "rayon", @@ -4525,7 +4556,7 @@ version = "0.180.1" dependencies = [ "base64 0.13.1", "dashmap", - "indexmap", + "indexmap 1.9.3", "once_cell", "rayon", "serde", @@ -4597,7 +4628,7 @@ dependencies = [ name = "swc_ecma_usage_analyzer" version = "0.20.0" dependencies = [ - "indexmap", + "indexmap 1.9.3", "rustc-hash", "swc_atoms", "swc_common", @@ -4612,7 +4643,7 @@ dependencies = [ name = "swc_ecma_utils" version = "0.124.0" dependencies = [ - "indexmap", + "indexmap 1.9.3", "num_cpus", "once_cell", "rayon", @@ -4718,7 +4749,7 @@ dependencies = [ name = "swc_fast_graph" version = "0.21.0" dependencies = [ - "indexmap", + "indexmap 1.9.3", "petgraph", "rustc-hash", "swc_common", @@ -5450,11 +5481,26 @@ dependencies = [ "serde", ] +[[package]] +name = "toml" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "185d8ab0dfbb35cf1399a6344d8484209c088f75f8f68230da55d48d95d43e3d" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit 0.20.2", +] + [[package]] name = "toml_datetime" -version = "0.6.1" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ab8ed2edee10b50132aed5f331333428b011c99402b5a534154ed15746f9622" +checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" +dependencies = [ + "serde", +] [[package]] name = "toml_edit" @@ -5462,9 +5508,22 @@ version = "0.19.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "239410c8609e8125456927e6707163a3b1fdb40561e4b803bc041f466ccfdc13" dependencies = [ - "indexmap", + "indexmap 1.9.3", + "toml_datetime", + "winnow 0.4.1", +] + +[[package]] +name = "toml_edit" +version = "0.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "396e4d48bbb2b7554c944bde63101b5ae446cff6ec4a24227428f15eb72ef338" +dependencies = [ + "indexmap 2.0.2", + "serde", + "serde_spanned", "toml_datetime", - "winnow", + "winnow 0.5.16", ] [[package]] @@ -5725,7 +5784,7 @@ dependencies = [ "fs_extra", "futures", "getrandom", - "indexmap", + "indexmap 1.9.3", "lazy_static", "libc", "pin-project-lite", @@ -5983,7 +6042,7 @@ dependencies = [ "bytes", "cfg-if", "derivative", - "indexmap", + "indexmap 1.9.3", "js-sys", "more-asserts", "rustc-demangle", @@ -6079,7 +6138,7 @@ dependencies = [ "bytecheck", "enum-iterator 0.7.0", "enumset", - "indexmap", + "indexmap 1.9.3", "more-asserts", "rkyv", "serde", @@ -6101,7 +6160,7 @@ dependencies = [ "derivative", "enum-iterator 0.7.0", "fnv", - "indexmap", + "indexmap 1.9.3", "lazy_static", "libc", "mach", @@ -6205,7 +6264,7 @@ version = "0.95.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2ea896273ea99b15132414be1da01ab0d8836415083298ecaffbe308eaac87a" dependencies = [ - "indexmap", + "indexmap 1.9.3", "url", ] @@ -6249,7 +6308,7 @@ dependencies = [ "base64 0.21.0", "byteorder", "bytes", - "indexmap", + "indexmap 1.9.3", "leb128", "lexical-sort", "once_cell", @@ -6520,6 +6579,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "winnow" +version = "0.5.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037711d82167854aff2018dfd193aa0fef5370f456732f0d5a0c59b0f1b4b907" +dependencies = [ + "memchr", +] + [[package]] name = "winreg" version = "0.10.1" @@ -6544,6 +6612,9 @@ version = "0.1.0" dependencies = [ "anyhow", "clap 4.4.6", + "serde", + "serde_derive", + "toml 0.8.2", "walkdir", ] diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 93b74dd5f84e..3669f26b7785 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -13,4 +13,7 @@ version = "0.1.0" [dependencies] anyhow = "1.0.75" clap = { version = "4.4.6", features = ["derive"] } +serde = "1.0.188" +serde_derive = "1.0.188" +toml = "0.8.2" walkdir = "2.4.0" diff --git a/xtask/src/git/mod.rs b/xtask/src/git/mod.rs new file mode 100644 index 000000000000..66e4e3d6a828 --- /dev/null +++ b/xtask/src/git/mod.rs @@ -0,0 +1,25 @@ +use anyhow::Result; +use clap::{Args, Subcommand}; + +use self::reduce::ReduceCmd; + +mod reduce; + +#[derive(Debug, Args)] +pub(super) struct GitCmd { + #[clap(subcommand)] + cmd: Inner, +} + +#[derive(Debug, Subcommand)] +enum Inner { + Reduce(ReduceCmd), +} + +impl GitCmd { + pub fn run(self) -> Result<()> { + match self.cmd { + Inner::Reduce(cmd) => cmd.run(), + } + } +} diff --git a/xtask/src/git/reduce/core_ver.rs b/xtask/src/git/reduce/core_ver.rs new file mode 100644 index 000000000000..33f0df86941f --- /dev/null +++ b/xtask/src/git/reduce/core_ver.rs @@ -0,0 +1,26 @@ +use anyhow::Result; +use clap::Args; + +use crate::util::get_commit_for_swc_core_version; + +/// Reduce the difference of the versions of `swc_core`s to the list of commits +/// and pull requests. +#[derive(Debug, Args)] +pub(super) struct CoreVerCmd { + from: String, + + to: String, +} + +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)?; + + eprintln!( + "GitHub diff: https://github.com/swc-project/swc/compare/{from_commit}...{to_commit}" + ); + + Ok(()) + } +} diff --git a/xtask/src/git/reduce/mod.rs b/xtask/src/git/reduce/mod.rs new file mode 100644 index 000000000000..586ed2ce68fa --- /dev/null +++ b/xtask/src/git/reduce/mod.rs @@ -0,0 +1,25 @@ +use anyhow::Result; +use clap::{Args, Subcommand}; + +use self::core_ver::CoreVerCmd; + +mod core_ver; + +#[derive(Debug, Args)] +pub(super) struct ReduceCmd { + #[clap(subcommand)] + cmd: Inner, +} + +#[derive(Debug, Subcommand)] +enum Inner { + CoreVer(CoreVerCmd), +} + +impl ReduceCmd { + pub fn run(self) -> Result<()> { + match self.cmd { + Inner::CoreVer(cmd) => cmd.run(), + } + } +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 4ef00afafe1d..ccbab9b945df 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,12 +1,12 @@ use anyhow::Result; -use bench::BenchCmd; use clap::{Parser, Subcommand}; -use crate::{clean::CleanCmd, es::EsCmd}; +use crate::{bench::BenchCmd, clean::CleanCmd, es::EsCmd, git::GitCmd}; mod bench; mod clean; mod es; +mod git; mod util; #[derive(Debug, Parser)] @@ -19,6 +19,7 @@ struct CliArgs { enum Cmd { Es(EsCmd), Bench(BenchCmd), + Git(GitCmd), Clean(CleanCmd), } @@ -28,6 +29,7 @@ fn main() -> Result<()> { match args.cmd { Cmd::Es(c) => c.run(), Cmd::Bench(c) => c.run(), + Cmd::Git(c) => c.run(), Cmd::Clean(c) => c.run(), } } diff --git a/xtask/src/util/mod.rs b/xtask/src/util/mod.rs index 1f3ff1b5789d..562edfa34140 100644 --- a/xtask/src/util/mod.rs +++ b/xtask/src/util/mod.rs @@ -4,7 +4,15 @@ use std::{ process::{Command, Stdio}, }; -use anyhow::{Context, Result}; +use anyhow::{bail, Context, Result}; +use serde_derive::Deserialize; + +pub fn wrap(op: F) -> Result +where + F: FnOnce() -> Result, +{ + op() +} pub fn repository_root() -> Result { let dir = env::var("CARGO_MANIFEST_DIR").context("failed to get manifest dir")?; @@ -23,3 +31,99 @@ pub fn run_cmd(cmd: &mut Command) -> Result<()> { Ok(()) } + +pub fn get_commit_for_swc_core_version(version: &str) -> Result { + wrap(|| { + eprintln!("Getting commit for swc_core@v{}", version); + + // We need to get the list of commits and pull requests which changed the + // version of swc_core. + let git_rev_list = Command::new("git") + .current_dir(repository_root()?) + .arg("rev-list") + .arg("--branches") + .arg("main") + .arg("--") + .arg("Cargo.lock") + .stderr(Stdio::inherit()) + .output() + .context("failed to spwan git rev-list")?; + + let git_rev_list = + String::from_utf8(git_rev_list.stdout).context("git rev-list output is not utf8")?; + + let git_grep_output = Command::new("git") + .current_dir(repository_root()?) + .arg("grep") + .arg(format!("version = \"{}\"", version)) + .args(git_rev_list.lines()) + .arg("--") + .arg("Cargo.lock") + .stderr(Stdio::piped()) + // .stdin(Stdio::from(git_rev_list.stdout.unwrap())) + .output() + .context("failed to execute git grep")?; + + // git grep returns all commits with the version string with the format, so we + // need to check if it's the version of swc_core + let output = + String::from_utf8(git_grep_output.stdout).context("git grep output is not utf8")?; + + let line_count = output.lines().count(); + + for line in output.lines() { + let commit = line.split(':').next().unwrap().to_string(); + + if line_count == 1 || get_version_of_swc_core_of_commit(&commit)? == version { + eprintln!("\tThe commit for swc_core@v{} is {}", 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 + ) + }) + .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 { + wrap(|| { + let output = Command::new("git") + .current_dir(repository_root()?) + .arg("show") + .arg(format!("{}:Cargo.lock", commit)) + .stderr(Stdio::inherit()) + .output() + .context("failed to spwan git show")?; + + let output_toml = + String::from_utf8(output.stdout).context("git show output is not utf8")?; + + let content = + toml::from_str::(&output_toml).context("failed to parse Cargo.lock")?; + + for pkg in content.package { + if pkg.name == "swc_core" { + return Ok(pkg.version); + } + } + + bail!("swc_core is not found in Cargo.lock") + }) + .with_context(|| format!("failed to get the version of swc_core of {}", commit)) +} + +#[derive(Debug, Deserialize)] +struct CargoLockfile { + package: Vec, +} + +#[derive(Debug, Deserialize)] +struct LockfilePkg { + name: String, + version: String, +}