From 54bd111a5f241620c6fec6dfcdfdbbecf0fa66c6 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Sun, 24 Mar 2024 00:55:01 +0900 Subject: [PATCH] build: add `bump_version.er` --- bump_version.er | 75 +++++++++++++++++++++++++++ crates/erg_compiler/lib/std/semver.er | 9 +++- tests/test.rs | 10 +++- 3 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 bump_version.er diff --git a/bump_version.er b/bump_version.er new file mode 100644 index 000000000..9d33c5d55 --- /dev/null +++ b/bump_version.er @@ -0,0 +1,75 @@ +# usage: erg bump_version.er -- +# e.g. +# erg bump_version.er -- major: 0.1.0 => 1.0.0 +# erg bump_version.er -- minor: 0.1.0 => 0.2.0 +# erg bump_version.er -- patch: 0.1.0 => 0.1.1 +# erg bump_version.er -- patch: 0.1.1-nightly.0 => 0.1.2 +# erg bump_version.er -- pre: 0.1.1-nightly.0 => 0.1.1-nightly.1 +# erg bump_version.er -- nightly: 0.1.1 => 0.1.2-nightly.0 + + +re = pyimport "re" +tomllib = pyimport "tomllib" +sys = pyimport "sys" +semver = import "semver" + +tomldata = with! open!("Cargo.toml"), f => f.read!() +els_tomldata = with! open!("crates/els/Cargo.toml"), f => f.read!() +data = tomllib.loads tomldata +els_data = tomllib.loads els_tomldata +ws = data["workspace"] +assert ws in {Str: Obj} +package = ws["package"] +els_package = els_data["package"] +assert package in {Str: Obj} +assert els_package in {Str: Obj} +version = package["version"] +els_version = els_package["version"] +assert version in Str +assert els_version in Str +ver = semver.SemVer.from_str version +els_ver = semver.SemVer.from_str els_version + +bumps = sys.argv[1] +new = match bumps: + "major" -> + semver.SemVer.new ver.major + 1, 0, 0, None + "minor" -> + semver.SemVer.new ver.major, ver.minor + 1, 0, None + "patch" -> + semver.SemVer.new ver.major, ver.minor, ver.patch + 1, None + "pre" -> + assert ver.pre != None, "not a pre-release version: \{ver}" + pre = semver.Identifier.new { .name = ver.pre.name; .num = ver.pre.num + 1; } + semver.SemVer.new ver.major, ver.minor, ver.patch, pre + "nightly" -> + assert ver.pre == None, "already a pre-release version: \{ver}" + pre = semver.Identifier.new { .name = "nightly"; .num = 0; } + semver.SemVer.new ver.major, ver.minor, ver.patch + 1, pre + _ -> todo "unknown bump: \{bumps}" +els_new = match bumps: + "major" -> + semver.SemVer.new els_ver.major + 1, 0, 0, None + "minor" -> + semver.SemVer.new els_ver.major, els_ver.minor + 1, 0, None + "patch" -> + semver.SemVer.new els_ver.major, els_ver.minor, els_ver.patch + 1, None + "pre" -> + assert els_ver.pre != None, "not a pre-release version: \{els_ver}" + pre = semver.Identifier.new { .name = els_ver.pre.name; .num = els_ver.pre.num + 1; } + semver.SemVer.new els_ver.major, els_ver.minor, els_ver.patch, pre + "nightly" -> + assert els_ver.pre == None, "already a pre-release version: \{els_ver}" + pre = semver.Identifier.new { .name = "nightly"; .num = 0; } + semver.SemVer.new els_ver.major, els_ver.minor, els_ver.patch + 1, pre + _ -> todo "unknown bump: \{bumps}" + +old_ver = re.compile "version = \"\{version}\"" +els_old_ver = re.compile "version = \"\{els_version}\"" +new_data_ = old_ver.sub "version = \"\{new}\"", tomldata +new_data = els_old_ver.sub "version = \"\{els_new}\"", new_data_ +els_new_data = els_old_ver.sub "version = \"\{els_new}\"", els_tomldata +with! open!("Cargo.toml", "w"), f => + discard f.write! new_data +with! open!("crates/els/Cargo.toml", "w"), f => + discard f.write! els_new_data diff --git a/crates/erg_compiler/lib/std/semver.er b/crates/erg_compiler/lib/std/semver.er index ed58506b3..2fea84b5d 100644 --- a/crates/erg_compiler/lib/std/semver.er +++ b/crates/erg_compiler/lib/std/semver.er @@ -4,6 +4,7 @@ __str__ ref self = "\{self.name}.\{self.num}" .Identifier|.Identifier <: Eq|. __eq__ self, other: .Identifier = + hasattr(other, "name") and hasattr(other, "num") and \ self.name == other.name and self.num == other.num .Identifier. from_str s: Str = @@ -29,8 +30,11 @@ match s.split("."): [major, minor, patch] -> .SemVer.new(nat(major), nat(minor), nat(patch)) - [major, minor, patch, pre] -> - .SemVer.new(nat(major), nat(minor), nat(patch), .Identifier.from_str(pre)) + [major, minor, patch_and_name, pre_num] -> + assert "-" in patch_and_name, "invalid semver string: \{s}" + [patch, name] = patch_and_name.split("-") + pre = .Identifier.new { .name; .num = nat pre_num } + .SemVer.new(nat(major), nat(minor), nat(patch), pre) _ -> panic "invalid semver string: \{s}" @Override __repr__ ref self = "SemVer(\{self.__str__()})" @@ -53,6 +57,7 @@ ]# .SemVer|<: Eq|. __eq__ self, other: .SemVer = + hasattr(other, "major") and hasattr(other, "minor") and hasattr(other, "patch") and hasattr(other, "pre") and \ self.major == other.major and self.minor == other.minor and self.patch == other.patch and self.pre == other.pre .SemVerPrefix = Class { "~", "==", "<", ">", "<=", "<=", } diff --git a/tests/test.rs b/tests/test.rs index 8b437afc5..6662fabc4 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,5 +1,8 @@ mod common; -use common::{expect_end_with, expect_error_location_and_msg, expect_failure, expect_success}; +use common::{ + expect_compile_success, expect_end_with, expect_error_location_and_msg, expect_failure, + expect_success, +}; use erg_common::error::Location; use erg_common::python_util::env_python_version; @@ -33,6 +36,11 @@ fn exec_associated_types() -> Result<(), ()> { expect_success("tests/should_ok/associated_types.er", 0) } +#[test] +fn exec_bump_version() -> Result<(), ()> { + expect_compile_success("bump_version.er", 0) +} + #[test] fn exec_class() -> Result<(), ()> { expect_success("examples/class.er", 0)