Skip to content

Commit

Permalink
refactor: Move separator into configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Crown0815 committed Oct 29, 2023
1 parent a7efe1f commit 5230b9d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/ConventionalChangelog/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace ConventionalChangelog;

public class Configuration : IConfiguration, IComparer<string>
{
private const string ConventionalCommitSeparator = ": "; // see https://www.conventionalcommits.org/en/v1.0.0/#specification

// language=regex
private const string BreakingChangeIndicator = "(?<inner>[a-z]+)!";
// language=regex
Expand Down Expand Up @@ -54,7 +56,9 @@ private Configuration(IEnumerable<CommitType> commitTypes, string versionTagPref
_order = order;
}

public string TypeFor(string typeIndicator, IEnumerable<CommitMessage.Footer> footers)
public string Separator => ConventionalCommitSeparator;

public string Sanitize(string typeIndicator, IEnumerable<CommitMessage.Footer> footers)
{
if (footers.Any(x => x is IPrintable))
return typeIndicator.ReplaceWith(BreakingChangeIndicator, "inner");
Expand All @@ -74,7 +78,7 @@ private CommitType InnerTypeFor(string typeIndicator)
public bool IsVersionTag(string tagName) =>
tagName.Matches(_versionTagPrefix + SemanticVersionPattern);

public IEnumerable<T> Ordered<T>(IEnumerable<T> logEntries) where T: IHasCommitType
public IEnumerable<T> Ordered<T>(IEnumerable<T> logEntries) where T : IHasCommitType
{
if (_order == ChangelogOrder.OldestToNewest)
logEntries = logEntries.Reverse();
Expand Down
9 changes: 4 additions & 5 deletions src/ConventionalChangelog/Conventional/MessageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace ConventionalChangelog.Conventional;

public class MessageParser
{
private const string Separator = ": "; // see https://www.conventionalcommits.org/en/v1.0.0/#specification
private readonly IConfiguration _configuration;

public MessageParser(IConfiguration configuration)
Expand All @@ -22,21 +21,21 @@ private CommitMessage Read(TextReader lines)
{
var (typeIndicator, description) = HeaderFrom(lines.ReadLine());
var (body, footers) = BodyFrom(lines);
typeIndicator = _configuration.TypeFor(typeIndicator, footers);
typeIndicator = _configuration.Sanitize(typeIndicator, footers);

return new CommitMessage(typeIndicator, description, body, footers);
}

#if NET6_0
private static (string, string) HeaderFrom(string? header)
private (string, string) HeaderFrom(string? header)
{
var twoParts = header?.Split(Separator);
var twoParts = header?.Split(_configuration.Separator);
return twoParts?.Length == 2
? (twoParts.First(), twoParts.Last().Trim())
: ("", "");
}
#elif NET7_0_OR_GREATER
private static (string, string) HeaderFrom(string? header) => header?.Split(Separator) is [var first, var second]
private (string, string) HeaderFrom(string? header) => header?.Split(_configuration.Separator) is [var first, var second]
? (first,second.Trim())
: ("", "");
#endif
Expand Down
4 changes: 3 additions & 1 deletion src/ConventionalChangelog/IConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ConventionalChangelog;

public interface IConfiguration
{
string TypeFor(string typeIndicator, IEnumerable<CommitMessage.Footer> footers);
string Sanitize(string typeIndicator, IEnumerable<CommitMessage.Footer> footers);
ChangelogType TypeFor(string typeIndicator);

bool IsFooter(string line);
Expand All @@ -13,4 +13,6 @@ public interface IConfiguration
IEnumerable<T> Ordered<T>(IEnumerable<T> logEntries) where T: IHasCommitType;

bool IsVersionTag(string tagName);

string Separator { get; }
}

0 comments on commit 5230b9d

Please sign in to comment.