Skip to content

Commit

Permalink
build: add bump_version.er
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Mar 23, 2024
1 parent 242171b commit 54bd111
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
75 changes: 75 additions & 0 deletions bump_version.er
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# usage: erg bump_version.er -- <major|minor|patch|pre|nightly>
# 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
9 changes: 7 additions & 2 deletions crates/erg_compiler/lib/std/semver.er
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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__()})"
Expand All @@ -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 { "~", "==", "<", ">", "<=", "<=", }
Expand Down
10 changes: 9 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 54bd111

Please sign in to comment.