Skip to content

Commit

Permalink
feat(cli): add push, major, minor and patch flags
Browse files Browse the repository at this point in the history
Refs: #2, #3
  • Loading branch information
pedraal committed Aug 25, 2024
1 parent 6e496e2 commit a03d541
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 7 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ gleam add --dev releam
## Usage
Once you are ready to create a new release from your main git branch, run the following :
```sh
gleam run -m releam
gleam run -m releam [-- FLAGS]
```
It will :
- parse the new commits since the last git tag
Expand All @@ -26,6 +26,9 @@ It will :
- create a release commit and a new tag
- if your repository host is supported, it will print a link to your terminal to create a new release (currently only github is supported)

To force a bump type, you can use `--major`, `--minor`, and `--patch` flags.
You can also push git release commit and new tag automatically with the `--push` flag.

If you have specific requirements, this package exposes all its internal function for you to build your custom release script.

## Configuration
Expand Down
3 changes: 3 additions & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ shellout = ">= 1.6.0 and < 2.0.0"
simplifile = ">= 2.0.1 and < 3.0.0"
tom = ">= 1.0.1 and < 2.0.0"
gleamsver = ">= 1.0.1 and < 2.0.0"
glint = ">= 1.0.1 and < 2.0.0"
argv = ">= 1.0.2 and < 2.0.0"
snag = ">= 0.3.0 and < 1.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
Expand Down
10 changes: 10 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@
# You typically do not need to edit this file

packages = [
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
{ name = "gleam_community_ansi", version = "1.4.1", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "4CD513FC62523053E62ED7BAC2F36136EC17D6A8942728250A9A00A15E340E4B" },
{ name = "gleam_community_colour", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "795964217EBEDB3DA656F5EB8F67D7AD22872EB95182042D3E7AFEF32D3FD2FE" },
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" },
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
{ name = "gleamsver", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleamsver", source = "hex", outer_checksum = "EA74FDC66BF15CB2CF4F8FF9B6FA01D511712EE2B1F4BE0371076ED3F685EEAE" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "glint", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "369C8A289017F73581D6B9FE2F5748169EB6FA021FFAA34FA7A49EE2094C73B3" },
{ name = "shellout", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "shellout", source = "hex", outer_checksum = "E2FCD18957F0E9F67E1F497FC9FF57393392F8A9BAEAEA4779541DE7A68DD7E0" },
{ name = "simplifile", version = "2.0.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "5FFEBD0CAB39BDD343C3E1CCA6438B2848847DC170BA2386DF9D7064F34DF000" },
{ name = "snag", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "54D32E16E33655346AA3E66CBA7E191DE0A8793D2C05284E3EFB90AD2CE92BCC" },
{ name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" },
{ name = "tom", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "9EECB60150E834A07238BD5C7DF1FF07F7D4C5862BB8A773923D1981C7875FB0" },
]

[requirements]
argv = { version = ">= 1.0.2 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleamsver = { version = ">= 1.0.1 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
glint = { version = ">= 1.0.1 and < 2.0.0" }
shellout = { version = ">= 1.6.0 and < 2.0.0" }
simplifile = { version = ">= 2.0.1 and < 3.0.0" }
snag = { version = ">= 0.3.0 and < 1.0.0" }
tom = { version = ">= 1.0.1 and < 2.0.0" }
53 changes: 51 additions & 2 deletions src/releam.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import argv
import gleam/io
import gleam/option.{None, Some}
import gleamsver as gs
import glint
import releam/changelog
import releam/commit
import releam/git
Expand All @@ -10,6 +12,44 @@ import releam/semver
import simplifile

pub fn main() {
glint.new()
|> glint.with_name("releam")
|> glint.pretty_help(glint.default_pretty_help())
|> glint.add(at: [], do: release())
|> glint.run(argv.load().arguments)
}

fn push_flag() -> glint.Flag(Bool) {
glint.bool_flag("push")
|> glint.flag_help("Automatically push git commit and tag")
}

fn major_flag() -> glint.Flag(Bool) {
glint.bool_flag("major")
|> glint.flag_help("Force a major release")
}

fn minor_flag() -> glint.Flag(Bool) {
glint.bool_flag("minor")
|> glint.flag_help("Force a minor release")
}

fn patch_flag() -> glint.Flag(Bool) {
glint.bool_flag("patch")
|> glint.flag_help("Force a patch release")
}

fn release() {
use <- glint.command_help("Generate a new release")

use push <- glint.flag(push_flag())

use major <- glint.flag(major_flag())
use minor <- glint.flag(minor_flag())
use patch <- glint.flag(patch_flag())

use _, _, flags <- glint.command()

let current_tag = git.get_last_tag()

let commits =
Expand All @@ -18,9 +58,18 @@ pub fn main() {
|> commit.parse_list

let assert Ok(raw_config) = simplifile.read("gleam.toml")
let config = package_config.parse(raw_config)
let config =
package_config.parse(
raw_config,
package_config.Overrides(auto_push: push(flags)),
)

let bump_type = semver.define_bump_type(commits)
let bump_type = case major(flags), minor(flags), patch(flags) {
Ok(True), _, _ -> Some(semver.Major)
_, Ok(True), _ -> Some(semver.Minor)
_, _, Ok(True) -> Some(semver.Patch)
_, _, _ -> semver.define_bump_type(commits)
}

let new_version = case bump_type {
Some(semver.Major) ->
Expand Down
12 changes: 11 additions & 1 deletion src/releam/package_config.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import gleam/dict
import gleam/regex
import gleam/result
import gleamsver.{type SemVer}
import snag
import tom

pub type RepositoryHost {
Expand All @@ -21,8 +22,12 @@ pub type PackageConfig {
)
}

pub type Overrides {
Overrides(auto_push: Result(Bool, snag.Snag))
}

/// Parses the content of a gleam.toml to return a PackageConfig
pub fn parse(raw_config: String) {
pub fn parse(raw_config: String, overrides: Overrides) {
let assert Ok(config) = tom.parse(raw_config)

let raw_version =
Expand Down Expand Up @@ -59,6 +64,11 @@ pub fn parse(raw_config: String) {
_ -> False
}

let auto_push = case overrides.auto_push {
Ok(v) -> v
_ -> auto_push
}

PackageConfig(version, repository, auto_push)
}

Expand Down
27 changes: 24 additions & 3 deletions test/releam/package_config_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import gleamsver as gs
import gleeunit
import gleeunit/should
import releam/package_config as pc
import snag

pub fn main() {
gleeunit.main()
Expand All @@ -14,7 +15,7 @@ repository = { type = \"github\", user = \"johndoe\", repo = \"leftpad\" }
[releam]
auto_push = true
"
|> pc.parse
|> pc.parse(pc.Overrides(snag.error("")))
|> should.equal(pc.PackageConfig(
gs.SemVer(1, 2, 3, "", ""),
Ok(pc.Repository(pc.Github, "johndoe", "leftpad")),
Expand All @@ -29,7 +30,7 @@ repository = { type = \"gitlab\", user = \"johndoe\", repo = \"leftpad\" }
[releam]
auto_push = false
"
|> pc.parse
|> pc.parse(pc.Overrides(snag.error("")))
|> should.equal(pc.PackageConfig(
gs.SemVer(1, 2, 3, "", ""),
Ok(pc.Repository(pc.NotImplemented("gitlab"), "johndoe", "leftpad")),
Expand All @@ -39,14 +40,34 @@ auto_push = false

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

pub fn parse_with_overrides_test() {
"
version = \"1.0.0\"
[releam]
auto_push = false"
|> pc.parse(pc.Overrides(Ok(True)))
|> should.equal(pc.PackageConfig(gs.SemVer(1, 0, 0, "", ""), Error(Nil), True))

"
version = \"1.0.0\"
[releam]
auto_push = true"
|> pc.parse(pc.Overrides(Ok(False)))
|> should.equal(pc.PackageConfig(
gs.SemVer(1, 0, 0, "", ""),
Error(Nil),
False,
))
}

pub fn replace_version_test() {
"
version = \"0.0.0\"
Expand Down

0 comments on commit a03d541

Please sign in to comment.