Skip to content

Commit

Permalink
feat: initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
MangelMaxime committed Nov 13, 2024
1 parent 801deaa commit e797f3c
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 106 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Changelog

All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

This changelog is generated using [EasyBuild.ChangelogGen](https://github.com/easybuild-org/EasyBuild.ChangelogGen). Do not edit this file manually.

<!-- EasyBuild: START -->
<!-- last_commit_released: 41a35e6c856a6b6cd6aa9369f37ab1018cfc661e -->
<!-- EasyBuild: END -->

## 0.0.0

<!--
This version is here for programs like [EasyBuild.PackageReleaseNotes.Tasks](https://github.com/easybuild-org/EasyBuild.PackageReleaseNotes.Tasks)
to be able to build your project when working on the first version.
-->
19 changes: 14 additions & 5 deletions src/Commands/Generate.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open FsToolkit.ErrorHandling
open EasyBuild.ChangelogGen
open EasyBuild.ChangelogGen.Generate
open EasyBuild.ChangelogGen.Generate.Types
open System.IO

type GenerateCommand() =
inherit Command<GenerateSettings>()
Expand All @@ -13,6 +14,8 @@ type GenerateCommand() =

override __.Execute(context, settings) =

printfn "Setting cwd: %s" settings.Cwd

let res =
result {
let! config = ConfigLoader.tryLoadConfig settings.Cwd settings.Config
Expand All @@ -22,11 +25,17 @@ type GenerateCommand() =
let commits = ReleaseContext.getCommits settings changelogInfo
let! releaseContext = ReleaseContext.compute settings changelogInfo commits config.CommitParserConfig

// match releaseContext with
// | NoVersionBumpRequired -> Log.success "No version bump required."
// | BumpRequired bumpInfo ->
// Log.info $"Bumping version to {bumpInfo.NewVersion}"
()
match releaseContext with
| NoVersionBumpRequired -> Log.success "No version bump required."
| BumpRequired bumpInfo ->
let newChangelogContent = Changelog.updateWithNewVersion config.ChangelogGenConfig bumpInfo changelogInfo

if settings.DryRun then
Log.info "Dry run enabled, not writing to file."
printfn "%s" newChangelogContent
else
File.WriteAllText(changelogInfo.File.FullName, newChangelogContent)
Log.success ($"Changelog updated with new version {bumpInfo.NewVersion}.")
}

match res with
Expand Down
154 changes: 54 additions & 100 deletions src/Generate/Changelog.fs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ let generateNewVersionSection (config: ChangelogGenConfig) (releaseContext: Bump

for blockLines in additionalChangelogContent do
appendLine ""

for line in blockLines do
$" %s{line}" |> _.TrimEnd() |> appendLine

Expand All @@ -170,103 +171,56 @@ let generateNewVersionSection (config: ChangelogGenConfig) (releaseContext: Bump

newVersionLines |> String.concat "\n"

// let private updateChangelog
// (config: ChangelogGenConfig)
// (releaseContext: BumpInfo)
// (changelogInfo: ChangelogInfo)
// =
// let newVersionLines = ResizeArray<string>()

// let appendLine (line: string) = newVersionLines.Add(line)

// let newLine () = newVersionLines.Add("")

// appendLine ($"## %s{releaseContext.NewVersion.ToString()}")
// newLine ()

// releaseContext.CommitsForRelease
// |> Seq.groupBy (fun commit -> commit.SemanticCommit.Type)
// |> Seq.iter (fun (commitType, commits) ->
// match commitType with
// | "feat" -> appendLine "### 🚀 Features"
// | "fix" -> appendLine "### 🐞 Bug Fixes"
// | _ -> ()

// newLine ()

// for commit in commits do
// let githubCommitUrl sha =
// $"https://github.com/glutinum-org/cli/commit/%s{sha}"

// let commitUrl = githubCommitUrl commit.OriginalCommit.Hash

// let description = capitalizeFirstLetter commit.SemanticCommit.Description

// $"* %s{description} ([%s{commit.OriginalCommit.AbbrevHash}](%s{commitUrl}))"
// |> appendLine

// let additionalChangelogContent =
// tryFindAdditionalChangelogContent commit.SemanticCommit.Body
// // Indent the additional lines to be under item bullet point
// |> List.map (fun line -> $" %s{line}")
// // Trim empty lines
// |> List.map (fun line ->
// if String.IsNullOrWhiteSpace line then
// ""
// else
// line
// )

// if not additionalChangelogContent.IsEmpty then
// appendLine ""
// additionalChangelogContent |> List.iter appendLine
// appendLine ""

// newLine ()
// )

// newLine ()

// // TODO: Add contributors list
// // TODO: Add breaking changes list 🏗️ Breaking changes

// let rec removeConsecutiveEmptyLines
// (previousLineWasBlank: bool)
// (result: string list)
// (lines: string list)
// =
// match lines with
// | [] -> result
// | line :: rest ->
// // printfn $"%A{String.IsNullOrWhiteSpace(line)}"
// if previousLineWasBlank && String.IsNullOrWhiteSpace(line) then
// removeConsecutiveEmptyLines true result rest
// else
// removeConsecutiveEmptyLines
// (String.IsNullOrWhiteSpace(line))
// (result @ [ line ])
// rest

// let newChangelogContent =
// [
// // Add title and description of the original changelog
// yield!
// changelogInfo.Lines
// |> Seq.takeWhile (fun line -> "<!-- EasyBuild: START -->" <> line)

// // Ad EasyBuild metadata
// "<!-- EasyBuild: START -->"
// $"<!-- last_commit_released: {releaseContext.LastCommitSha} -->"
// "<!-- EasyBuild: END -->"
// ""

// // New version
// yield! newVersionLines

// // Add the rest of the changelog
// yield! changelogInfo.Lines |> Seq.skipWhile (fun line -> not (line.StartsWith("##")))
// ]
// |> removeConsecutiveEmptyLines false []
// |> String.concat "\n"

// File.WriteAllText(changelogInfo.File.FullName, newChangelogContent)
let updateWithNewVersion
(config: ChangelogGenConfig)
(releaseContext: BumpInfo)
(changelogInfo: ChangelogInfo)
=
let newVersionLines = generateNewVersionSection config releaseContext

let rec removeConsecutiveEmptyLines
(previousLineWasBlank: bool)
(result: string list)
(lines: string list)
=
match lines with
| [] -> result
| line :: rest ->
if previousLineWasBlank && String.IsNullOrWhiteSpace(line) then
removeConsecutiveEmptyLines true result rest
else
removeConsecutiveEmptyLines
(String.IsNullOrWhiteSpace(line))
(result @ [ line ])
rest

let hasEasyBuildMetadata =
changelogInfo.Lines |> Seq.contains "<!-- EasyBuild: START -->"

let newChangelogContent =
[
// Add title and description of the original changelog
if hasEasyBuildMetadata then
yield!
changelogInfo.Lines
|> Seq.takeWhile (fun line -> "<!-- EasyBuild: START -->" <> line)
else
yield!
changelogInfo.Lines |> Seq.takeWhile (fun line -> not (line.StartsWith("##")))

// Ad EasyBuild metadata
"<!-- EasyBuild: START -->"
$"<!-- last_commit_released: {releaseContext.LastCommitSha} -->"
"<!-- EasyBuild: END -->"
""

// New version
newVersionLines

// Add the rest of the changelog
yield! changelogInfo.Lines |> Seq.skipWhile (fun line -> not (line.StartsWith("##")))
]
|> removeConsecutiveEmptyLines false []
|> String.concat "\n"

newChangelogContent
9 changes: 9 additions & 0 deletions src/Generate/ReleaseContext.fs
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,29 @@ let compute
LastCommitSha = commitsCandidates[0].Hash
}

let applyPreRelease (newVersion : SemVersion) =
if settings.PreRelease.IsSet then
newVersion.WithPrerelease(settings.PreRelease.Value)
else
newVersion.WithPrerelease("")

let bumpMajor () =
refVersion.WithMajor(refVersion.Major + 1).WithMinor(0).WithPatch(0)
|> applyPreRelease
|> makeVersionBump
|> BumpRequired
|> Ok

let bumpMinor () =
refVersion.WithMinor(refVersion.Minor + 1).WithPatch(0)
|> applyPreRelease
|> makeVersionBump
|> BumpRequired
|> Ok

let bumpPatch () =
refVersion.WithPatch(refVersion.Patch + 1)

|> makeVersionBump
|> BumpRequired
|> Ok
Expand Down
6 changes: 5 additions & 1 deletion src/Generate/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ open System.Text.RegularExpressions
type GenerateSettings() =
inherit CommandSettings()

[<CommandOption("[changelog]")>]
[<CommandArgument(0, "[changelog]")>]
[<Description("Path to the changelog file. Default is CHANGELOG.md")>]
member val Changelog: string = "CHANGELOG.md" with get, set

Expand Down Expand Up @@ -74,6 +74,10 @@ type GenerateSettings() =
"fix"
|] with get, set

[<CommandOption("--dry-run")>]
[<Description("Run the command without writing to the changelog file")>]
member val DryRun: bool = false with get, set

type CommitForRelease =
{
OriginalCommit: Git.Commit
Expand Down
94 changes: 94 additions & 0 deletions tests/Changelog.fs
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,105 @@ This is a list of changes:
)
]

let private updateChangelogWithNewVersionTests =
testList
"Changelog.updateChangelogWithNewVersion"
[
testMarkdown (
"works if metadata was existing",
(fun _ ->
let changelogInfo =
let settings = GenerateSettings(Changelog = Workspace.``valid_changelog.md``)

match Changelog.load settings with
| Ok changelogInfo -> changelogInfo
| Error _ -> failwith "Expected Ok"

Changelog.updateWithNewVersion
ChangelogGenConfig.Default
{
NewVersion = Semver.SemVersion(1, 1, 0)
CommitsForRelease =
[
Git.Commit.Create(
"0b1899bb03d3eb86a30c84aa4c66c037527fbd14",
"fix: Fix bug"
)
|> gitCommitToCommitForRelease
Git.Commit.Create(
"2a6f3b3403aaa629de6e65558448b37f126f8e86",
"fix: Fix another bug"
)
|> gitCommitToCommitForRelease
Git.Commit.Create(
"fef5f479d65172bd385b781bbed83f6eee2a32c6",
"feat: Add feature"
)
|> gitCommitToCommitForRelease
Git.Commit.Create(
"46d380257c08fe1f74e4596b8720d71a39f6e629",
"feat: Add another feature"
)
|> gitCommitToCommitForRelease
]
LastCommitSha = "0b1899bb03d3eb86a30c84aa4c66c037527fbd14"
}
changelogInfo

)
)

testMarkdown (
"works if metadata was not existing",
(fun _ ->
let changelogInfo =
let settings = GenerateSettings(Changelog = Workspace.``valid_changelog_no_metadata.md``)

match Changelog.load settings with
| Ok changelogInfo -> changelogInfo
| Error _ -> failwith "Expected Ok"

Changelog.updateWithNewVersion
ChangelogGenConfig.Default
{
NewVersion = Semver.SemVersion(1, 1, 0)
CommitsForRelease =
[
Git.Commit.Create(
"0b1899bb03d3eb86a30c84aa4c66c037527fbd14",
"fix: Fix bug"
)
|> gitCommitToCommitForRelease
Git.Commit.Create(
"2a6f3b3403aaa629de6e65558448b37f126f8e86",
"fix: Fix another bug"
)
|> gitCommitToCommitForRelease
Git.Commit.Create(
"fef5f479d65172bd385b781bbed83f6eee2a32c6",
"feat: Add feature"
)
|> gitCommitToCommitForRelease
Git.Commit.Create(
"46d380257c08fe1f74e4596b8720d71a39f6e629",
"feat: Add another feature"
)
|> gitCommitToCommitForRelease
]
LastCommitSha = "0b1899bb03d3eb86a30c84aa4c66c037527fbd14"
}
changelogInfo

)
)
]

let tests =
testList
"Changelog"
[
loadTests
tryFindAdditionalChangelogContentTests
generateNewVersionSectionTests
updateChangelogWithNewVersionTests
]
Loading

0 comments on commit e797f3c

Please sign in to comment.