Skip to content

Commit

Permalink
Add the new "add" command
Browse files Browse the repository at this point in the history
Closes ponylang#34.
  • Loading branch information
ordepdev committed Nov 9, 2019
1 parent 8fa360e commit 4a67e45
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 16 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ changelog-tool get 0.2.2
changelog-tool unreleased -e
```

## Add an entry to an unreleased section
```bash
changelog-tool add fixed "We fixed some bad issues" -e
changelog-tool add added "We just added some new cool stuff" -e
changelog-tool add changed "And changed things a bit" -e
```

## Prepare a Changelog for a Release
```bash
changelog-tool release 0.13.1
Expand Down
Binary file added bin/changelog-tool
Binary file not shown.
37 changes: 21 additions & 16 deletions changelog-tool/changelog.pony
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class Changelog
unreleased = Release._unreleased()
end

fun ref add_entry(section_name: String, entry: String) ? =>
match unreleased
| let r: Release => r.add_entry(section_name, entry)?
end

fun string(): String iso^ =>
let str = (recover String end)
.> append("# Change Log\n\n")
Expand Down Expand Up @@ -79,25 +84,25 @@ class Release
added = Section._empty(Added)
changed = Section._empty(Changed)

fun ref add_entry(section_name: String, entry: String) ? =>
let section =
match section_name
| "fixed" => try fixed as Section else fixed = Section._empty(Fixed); fixed as Section end
| "added" => try added as Section else added = Section._empty(Added); added as Section end
| "changed" => try changed as Section else changed = Section._empty(Changed); changed as Section end
else error
end
section.entries.push("- " + entry)

fun string(): String iso^ =>
if heading == _unreleased_heading then
"\n\n".join(
[ heading
"### Fixed\n"
"### Added\n"
"### Changed\n"
""
].values())
else
let str = recover String .> append(heading) .> append("\n\n") end
for section in [fixed; added; changed].values() do
match section
| let s: Section box =>
str .> append(s.string()) .> append("\n")
end
let str = recover String .> append(heading) .> append("\n\n") end
for section in [fixed; added; changed].values() do
match section
| let s: Section box =>
str .> append(s.string()) .> append("\n")
end
str
end
str

class Section
let label: TSection
Expand Down
27 changes: 27 additions & 0 deletions changelog-tool/main.pony
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ actor Main
].values()),
[edit],
[ArgSpec.string("version")])?
CommandSpec.leaf(
"add",
"Add a new entry at the end of the section",
[edit],
[ ArgSpec.string("section")
ArgSpec.string("entry")
])?
])?
.> add_help("help", "Print this message and exit")?

Expand All @@ -95,6 +102,8 @@ actor Main
| "changelog-tool/release" =>
cmd_release(
path, filename, cmd.arg("version").string(), cmd.option("edit").bool())
| "changelog-tool/add" =>
cmd_add(path, filename, cmd.arg("section").string(), cmd.arg("entry").string(), cmd.option("edit").bool())
else
err("unknown command: " + cmd.fullname())
please_report()
Expand Down Expand Up @@ -171,6 +180,24 @@ actor Main
err("unable to perform release preparation")
end

fun cmd_add(
filepath: FilePath,
filename: String,
section: String,
entry: String,
edit: Bool)
=>
try
edit_or_print(
filepath,
edit,
Changelog(parse(filepath, filename)?)?
.> add_entry(section, entry)?
.string())
else
err("unable add a new changelog entry")
end

fun parse(filepath: FilePath, filename: String): peg.AST ? =>
let source = peg.Source(filepath)?
match recover val ChangelogParser().parse(source) end
Expand Down
67 changes: 67 additions & 0 deletions tests/main.pony
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,39 @@ class iso _TestRelease is UnitTest
""")?

_ReleaseTestAfterAddingSomeEntries(h, ChangelogParser()).run(
"""
# Change Log
## [unreleased] - unreleased
### Fixed
### Added
### Changed
""",
"""
# Change Log
## [0.0.0] - 0000-00-00
### Fixed
- We made some fixes...
- Oh, and we made a final one.
### Added
- We added some stuff as well.
### Changed
- And we changed a few things also.
""")?

class ParseTest
let _h: TestHelper
let _parser: Parser
Expand Down Expand Up @@ -286,6 +319,40 @@ class _ReleaseTest
_h.fail()
end

class _ReleaseTestAfterAddingSomeEntries
let _h: TestHelper
let _parser: Parser

new create(h: TestHelper, parser: Parser) =>
(_h, _parser) = (h, parser)

fun run(input: String, expected: String) ? =>
let source = Source.from_string(input)
match recover val _parser.parse(source) end
| (let n: USize, let r: (AST | Token | NotPresent)) =>
match r
| let ast: AST =>
_h.log(recover val _Printer(ast) end)
// _h.log(Changelog(ast)?.string())
let changelog = Changelog(ast)?
.> add_entry("fixed", "We made some fixes...\n")?
.> add_entry("fixed", "Oh, and we made a final one.\n")?
.> add_entry("added", "We added some stuff as well.\n")?
.> add_entry("changed", "And we changed a few things also.\n")?
.> create_release("0.0.0", "0000-00-00")
let output: String = changelog.string()
_h.log(output)
_h.assert_eq[String](expected, output)
else
_h.log(recover val _Printer(r) end)
_h.fail()
end
| (let offset: USize, let r: Parser val) =>
let e = recover val SyntaxError(source, offset, r) end
_Logv(_h, PegFormatError.console(e))
_h.fail()
end

primitive _Logv
fun apply(h: TestHelper, bsi: ByteSeqIter) =>
let str = recover String end
Expand Down

0 comments on commit 4a67e45

Please sign in to comment.