Skip to content

Commit

Permalink
Simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
credfeto committed Dec 8, 2023
1 parent 5fa67d2 commit e5138a7
Showing 1 changed file with 49 additions and 25 deletions.
74 changes: 49 additions & 25 deletions src/Credfeto.ChangeLog/ChangeLogUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public static string AddEntry(string changeLog, string type, string message)

private static string AddEntryCommon(string changeLog, string type, string message)
{
List<string> text = EnsureChangelog(changeLog)
.SplitToLines()
.ToList();
List<string> text = ChangeLogAsLines(changeLog);

string entryText = CreateEntryText(message);
int index = FindInsertPosition(changeLog: text, type: type, entryText: entryText);
Expand All @@ -68,16 +66,21 @@ private static string AddEntryCommon(string changeLog, string type, string messa
.Trim();
}

private static List<string> ChangeLogAsLines(string changeLog)
{
return EnsureChangelog(changeLog)
.SplitToLines()
.ToList();
}

public static string RemoveEntry(string changeLog, string type, string message)
{
return RemoveEntryCommon(changeLog: changeLog, type: type, message: message);
}

private static string RemoveEntryCommon(string changeLog, string type, string message)
{
List<string> text = EnsureChangelog(changeLog)
.SplitToLines()
.ToList();
List<string> text = ChangeLogAsLines(changeLog);

string entryText = CreateEntryText(message);
int index = FindRemovePosition(changeLog: text, type: type, entryText: entryText);
Expand Down Expand Up @@ -120,12 +123,7 @@ private static int FindRemovePosition(List<string> changeLog, string type, strin
findSection: false);
}

private static int FindMatchPosition(List<string> changeLog,
string type,
Func<string, bool> isMatch,
Func<int, int> exactMatchAction,
Func<int, int> emptySectionAction,
bool findSection)
private static int FindMatchPosition(List<string> changeLog, string type, Func<string, bool> isMatch, Func<int, int> exactMatchAction, Func<int, int> emptySectionAction, bool findSection)
{
bool foundUnreleased = false;

Expand Down Expand Up @@ -205,9 +203,7 @@ public static string CreateRelease(string changeLog, string version, bool pendin

private static string CreateReleaseCommon(string changeLog, string version, bool pending)
{
List<string> text = EnsureChangelog(changeLog)
.SplitToLines()
.ToList();
List<string> text = ChangeLogAsLines(changeLog);

Dictionary<string, int> releases = FindReleasePositions(text);

Expand Down Expand Up @@ -283,7 +279,7 @@ private static void RemoveItems(List<string> text, List<int> removeIndexes)
private static string CreateReleaseVersionHeader(string version, bool pending)
{
string releaseDate = CreateReleaseDate(pending);
string releaseVersionHeader = "## [" + version + "] - " + releaseDate;
string releaseVersionHeader = string.Concat("## [", version, "] - ", releaseDate);

return releaseVersionHeader;
}
Expand Down Expand Up @@ -396,9 +392,7 @@ private static string CurrentDate()

private static int FindInsertPosition(string releaseVersionToFind, IReadOnlyDictionary<string, int> releases, int endOfFilePosition)
{
string? latestRelease = releases.Keys.Where(x => x != Constants.Unreleased)
.OrderByDescending(x => new Version(x))
.FirstOrDefault();
string? latestRelease = GetLatestRelease(releases);

int releaseInsertPos;

Expand All @@ -411,12 +405,12 @@ private static int FindInsertPosition(string releaseVersionToFind, IReadOnlyDict

if (latestNumeric == numericalVersion)
{
throw new ReleaseAlreadyExistsException($"Release {releaseVersionToFind} already exists");
return ReleaseAlreadyExists(releaseVersionToFind);
}

if (latestNumeric > numericalVersion)
{
throw new ReleaseTooOldException($"Release {latestRelease} already exists and is newer than {releaseVersionToFind}");
return ReleaseTooOld(releaseVersionToFind: releaseVersionToFind, latestRelease: latestRelease);
}

releaseInsertPos = releases[latestRelease];
Expand All @@ -429,20 +423,50 @@ private static int FindInsertPosition(string releaseVersionToFind, IReadOnlyDict
return releaseInsertPos;
}

[DoesNotReturn]
private static int ReleaseTooOld(string releaseVersionToFind, string latestRelease)
{
throw new ReleaseTooOldException($"Release {latestRelease} already exists and is newer than {releaseVersionToFind}");
}

[DoesNotReturn]
private static int ReleaseAlreadyExists(string releaseVersionToFind)
{
throw new ReleaseAlreadyExistsException($"Release {releaseVersionToFind} already exists");
}

private static string? GetLatestRelease(IReadOnlyDictionary<string, int> releases)
{
return releases.Keys.Where(x => x != Constants.Unreleased)
.OrderByDescending(x => new Version(x))
.FirstOrDefault();
}

private static Dictionary<string, int> FindReleasePositions(IReadOnlyList<string> text)
{
Dictionary<string, int> releases = text.Select((line, index) => new { line, index })
.Where(i => IsRelease(i.line))
.ToDictionary(keySelector: i => ExtractRelease(i.line), elementSelector: i => i.index, comparer: StringComparer.Ordinal);
Dictionary<string, int> releases = GetReleasePositions(text);

if (releases.Count == 0)
{
throw new EmptyChangeLogException("Could not find unreleased section");
return CouldNotFindUnreleasedSection();
}

return releases;
}

[DoesNotReturn]
private static Dictionary<string, int> CouldNotFindUnreleasedSection()
{
throw new EmptyChangeLogException("Could not find unreleased section");
}

private static Dictionary<string, int> GetReleasePositions(IReadOnlyList<string> text)
{
return text.Select((line, index) => new { line, index })
.Where(i => IsRelease(i.line))
.ToDictionary(keySelector: i => ExtractRelease(i.line), elementSelector: i => i.index, comparer: StringComparer.Ordinal);
}

private static string ExtractRelease(string line)
{
if (line == Constants.UnreleasedHeader)
Expand Down

0 comments on commit e5138a7

Please sign in to comment.