From 9bf07b02fd90dfeadd93c45ec4ba81b19a086244 Mon Sep 17 00:00:00 2001 From: ordepdev Date: Fri, 8 Nov 2019 22:37:11 +0000 Subject: [PATCH 1/4] 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 From 632f9af690cf0a36c9cc7356bb1bb09375d70404 Mon Sep 17 00:00:00 2001 From: ordepdev Date: Sat, 9 Nov 2019 18:10:39 +0000 Subject: [PATCH 2/4] Make things shine --- changelog-tool/changelog.pony | 12 +++++++++--- changelog-tool/main.pony | 12 +++++++----- tests/main.pony | 1 - 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/changelog-tool/changelog.pony b/changelog-tool/changelog.pony index 8519f7a..3143d37 100644 --- a/changelog-tool/changelog.pony +++ b/changelog-tool/changelog.pony @@ -87,9 +87,15 @@ class Release 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 + | "fixed" => + if fixed is None then fixed = Section._empty(Fixed) end + fixed as Section + | "added" => + if added is None then added = Section._empty(Added) end + added as Section + | "changed" => + if changed is None then changed = Section._empty(Changed) end + changed as Section else error end section.entries.push("- " + entry) diff --git a/changelog-tool/main.pony b/changelog-tool/main.pony index e502b6c..fd93b8d 100644 --- a/changelog-tool/main.pony +++ b/changelog-tool/main.pony @@ -75,7 +75,7 @@ actor Main "Add a new entry at the end of the section", [edit], [ ArgSpec.string("section") - ArgSpec.string("entry") + ArgSpec.string("entry") ])? ])? .> add_help("help", "Print this message and exit")? @@ -103,7 +103,9 @@ actor Main 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()) + cmd_add( + path, filename, cmd.arg("section").string(), + cmd.arg("entry").string(),cmd.option("edit").bool()) else err("unknown command: " + cmd.fullname()) please_report() @@ -191,9 +193,9 @@ actor Main edit_or_print( filepath, edit, - Changelog(parse(filepath, filename)?)? - .> add_entry(section, entry)? - .string()) + Changelog(parse(filepath, filename)?)? + .> add_entry(section, entry)? + .string()) else err("unable add a new changelog entry") end diff --git a/tests/main.pony b/tests/main.pony index 7bb7243..ec4c64f 100644 --- a/tests/main.pony +++ b/tests/main.pony @@ -333,7 +333,6 @@ class _ReleaseTestAfterAddingSomeEntries 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")? From 2f34f3a1f9fe31b1ba95072b3f5bf5741461717a Mon Sep 17 00:00:00 2001 From: ordepdev Date: Sat, 9 Nov 2019 19:28:03 +0000 Subject: [PATCH 3/4] Don't ignore empty unreleased sections, make the build pass. --- changelog-tool/changelog.pony | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/changelog-tool/changelog.pony b/changelog-tool/changelog.pony index 3143d37..ae9baa4 100644 --- a/changelog-tool/changelog.pony +++ b/changelog-tool/changelog.pony @@ -1,4 +1,5 @@ use "peg" +use "debug" class Changelog let heading: String @@ -101,8 +102,26 @@ class Release section.entries.push("- " + entry) fun string(): String iso^ => + // In order to represent the empty sections of unreleased releases, + // we must use the empty correspoding section when printing instead + // of None, otherwise it will be ignored. + let fixed' = match fixed + | None if heading == _unreleased_heading => Section._empty(Fixed) + else fixed + end + + let added' = match added + | None if heading == _unreleased_heading => Section._empty(Added) + else added + end + + let changed' = match changed + | None if heading == _unreleased_heading => Section._empty(Changed) + else changed + end + let str = recover String .> append(heading) .> append("\n\n") end - for section in [fixed; added; changed].values() do + for section in [fixed'; added'; changed'].values() do match section | let s: Section box => str .> append(s.string()) .> append("\n") From 443bb211e5292639c731a555660816163b369427 Mon Sep 17 00:00:00 2001 From: ordepdev Date: Sat, 9 Nov 2019 19:41:14 +0000 Subject: [PATCH 4/4] Just. Read. The. Style. Guide. --- changelog-tool/changelog.pony | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/changelog-tool/changelog.pony b/changelog-tool/changelog.pony index ae9baa4..e935de1 100644 --- a/changelog-tool/changelog.pony +++ b/changelog-tool/changelog.pony @@ -1,5 +1,4 @@ use "peg" -use "debug" class Changelog let heading: String @@ -105,20 +104,23 @@ class Release // In order to represent the empty sections of unreleased releases, // we must use the empty correspoding section when printing instead // of None, otherwise it will be ignored. - let fixed' = match fixed - | None if heading == _unreleased_heading => Section._empty(Fixed) - else fixed - end + let fixed' = + match fixed + | None if heading == _unreleased_heading => Section._empty(Fixed) + else fixed + end - let added' = match added - | None if heading == _unreleased_heading => Section._empty(Added) - else added - end + let added' = + match added + | None if heading == _unreleased_heading => Section._empty(Added) + else added + end - let changed' = match changed - | None if heading == _unreleased_heading => Section._empty(Changed) - else changed - end + let changed' = + match changed + | None if heading == _unreleased_heading => Section._empty(Changed) + else changed + end let str = recover String .> append(heading) .> append("\n\n") end for section in [fixed'; added'; changed'].values() do