Skip to content

Commit

Permalink
feat(config): configurable git auto push behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
pedraal committed Aug 24, 2024
1 parent dce4002 commit eaa1b0f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 19 deletions.
3 changes: 3 additions & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ gleamsver = ">= 1.0.1 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"

[releam]
auto_push = false
27 changes: 15 additions & 12 deletions src/releam.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,42 @@ pub fn main() {
|> commit.parse_list

let assert Ok(raw_config) = simplifile.read("gleam.toml")
let package = package_config.parse(raw_config)
let config = package_config.parse(raw_config)

let bump_type = semver.define_bump_type(commits)

let new_version = case bump_type {
Some(semver.Major) ->
gs.SemVer(..package.version, major: package.version.major + 1)
gs.SemVer(..config.version, major: config.version.major + 1)
Some(semver.Minor) ->
gs.SemVer(..package.version, minor: package.version.minor + 1)
gs.SemVer(..config.version, minor: config.version.minor + 1)
Some(semver.Patch) ->
gs.SemVer(..package.version, patch: package.version.patch + 1)
None -> package.version
gs.SemVer(..config.version, patch: config.version.patch + 1)
None -> config.version
}

let new_tag = "v" <> gs.to_string(new_version)

let new_changelog =
changelog.new(
package,
config,
changelog.ChangelogConfig(current_tag, new_tag),
commits,
)

let assert Ok(_) = changelog.write_to_changelog_file(new_changelog)

let new_config = package_config.replace_version(raw_config, new_version)
let assert Ok(_) = simplifile.write("gleam.toml", new_config)
let assert Ok(_) =
simplifile.write(
"gleam.toml",
package_config.replace_version(raw_config, new_version),
)

let assert Ok(_) = git.commit_release(new_tag)
git.commit_release(new_tag, push: config.auto_push)

let release_link =
release.generate_repository_provider_release_link(package, new_changelog)
case release_link {
case
release.generate_repository_provider_release_link(config, new_changelog)
{
Ok(rl) -> {
io.println("Click on the following link to create a new release")
io.println(rl)
Expand Down
4 changes: 2 additions & 2 deletions src/releam/changelog.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn new(
)
}

pub fn render(changelog: Changelog, with_title: Bool) {
pub fn render(changelog: Changelog, with_title with_title: Bool) {
bool.guard(with_title, render_title(changelog.title), fn() { "" })
<> render_compare_link(changelog.compare_link)
<> {
Expand All @@ -71,7 +71,7 @@ pub fn write_to_changelog_file(changelog: Changelog) {
string.replace(
content,
insert_area,
insert_area <> render(changelog, True) <> "\n\n",
insert_area <> render(changelog, with_title: True) <> "\n\n",
)

case string.contains(content, changelog.title) {
Expand Down
11 changes: 9 additions & 2 deletions src/releam/git.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ pub fn get_commits_since_last_tag(tag: String) {
regex.scan(re, output) |> list.map(fn(m) { m.content })
}

pub fn commit_release(new_tag: String) {
pub fn commit_release(new_tag: String, push push: Bool) {
let assert Ok(_) =
exec_git(["add", changelog.changelog_file_path, "gleam.toml"])
let assert Ok(_) = exec_git(["commit", "-m", "chore(release): " <> new_tag])
let assert Ok(_) = exec_git(["tag", "-am", new_tag, new_tag])
let assert Ok(_) = exec_git(["push", "--follow-tags"])

case push {
True -> {
let assert Ok(_) = exec_git(["push", "--follow-tags"])
Nil
}
_ -> Nil
}
}

pub fn exec_git(args: List(String)) {
Expand Down
13 changes: 11 additions & 2 deletions src/releam/package_config.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ pub type Repository {
}

pub type PackageConfig {
PackageConfig(version: SemVer, repository: Result(Repository, Nil))
PackageConfig(
version: SemVer,
repository: Result(Repository, Nil),
auto_push: Bool,
)
}

pub fn parse(raw: String) {
Expand Down Expand Up @@ -49,7 +53,12 @@ pub fn parse(raw: String) {
_, _, _ -> Error(Nil)
}

PackageConfig(version, repository)
let auto_push = case config |> tom.get_bool(["releam", "auto_push"]) {
Ok(v) -> v
_ -> False
}

PackageConfig(version, repository, auto_push)
}

pub fn replace_version(raw_config: String, new_version: gleamsver.SemVer) {
Expand Down
2 changes: 2 additions & 0 deletions test/releam/changelog_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub fn new_with_repository_test() {
pc.PackageConfig(
gs.SemVer(1, 0, 0, "", ""),
Ok(pc.Repository(pc.Github, "johndoe", "blog")),
False,
)
let changelog_config = cl.ChangelogConfig("v1.0.0", "v1.1.0")

Expand Down Expand Up @@ -48,6 +49,7 @@ pub fn new_without_repository_test() {
pc.PackageConfig(
gs.SemVer(1, 0, 0, "", ""),
Ok(pc.Repository(pc.NotImplemented(""), "johndoe", "blog")),
False,
)
let changelog_config = cl.ChangelogConfig("v1.0.0", "v1.1.0")

Expand Down
12 changes: 11 additions & 1 deletion test/releam/package_config_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,40 @@ pub fn parse_valid_test() {
"
version = \"1.2.3\"
repository = { type = \"github\", user = \"johndoe\", repo = \"leftpad\" }
[releam]
auto_push = true
"
|> pc.parse
|> should.equal(pc.PackageConfig(
gs.SemVer(1, 2, 3, "", ""),
Ok(pc.Repository(pc.Github, "johndoe", "leftpad")),
True,
))
}

pub fn parse_unsupported_repository_provider_test() {
"
version = \"1.2.3\"
repository = { type = \"gitlab\", user = \"johndoe\", repo = \"leftpad\" }
[releam]
auto_push = false
"
|> pc.parse
|> should.equal(pc.PackageConfig(
gs.SemVer(1, 2, 3, "", ""),
Ok(pc.Repository(pc.NotImplemented("gitlab"), "johndoe", "leftpad")),
False,
))
}

pub fn parse_invalid_test() {
""
|> pc.parse
|> should.equal(pc.PackageConfig(gs.SemVer(0, 0, 0, "", ""), Error(Nil)))
|> should.equal(pc.PackageConfig(
gs.SemVer(0, 0, 0, "", ""),
Error(Nil),
False,
))
}

pub fn replace_version_test() {
Expand Down
1 change: 1 addition & 0 deletions test/releam/release_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub fn generate_repository_provider_release_link_test() {
pc.PackageConfig(
gs.SemVer(1, 0, 0, "", ""),
Ok(pc.Repository(pc.Github, "johndoe", "blog")),
False,
)

let compare_url = "https://github.com/johndoe/blog/compare/v1.0.0...v1.1.0"
Expand Down

0 comments on commit eaa1b0f

Please sign in to comment.