From 9bf07b02fd90dfeadd93c45ec4ba81b19a086244 Mon Sep 17 00:00:00 2001 From: ordepdev Date: Fri, 8 Nov 2019 22:37:11 +0000 Subject: [PATCH] Add the new "add" command Closes #34. --- README.md | 7 ++++ changelog-tool/changelog.pony | 37 ++++++++++--------- changelog-tool/main.pony | 27 ++++++++++++++ tests/main.pony | 67 +++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index f088906..41ec3ae 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,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 diff --git a/changelog-tool/changelog.pony b/changelog-tool/changelog.pony index 8f6d264..8519f7a 100644 --- a/changelog-tool/changelog.pony +++ b/changelog-tool/changelog.pony @@ -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") @@ -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 diff --git a/changelog-tool/main.pony b/changelog-tool/main.pony index 56e161b..e502b6c 100644 --- a/changelog-tool/main.pony +++ b/changelog-tool/main.pony @@ -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")? @@ -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() @@ -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 diff --git a/tests/main.pony b/tests/main.pony index caeedb4..7bb7243 100644 --- a/tests/main.pony +++ b/tests/main.pony @@ -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 @@ -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