Skip to content

Commit

Permalink
Improved detection for mostly larger version tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed Oct 27, 2023
1 parent 339f22b commit 63c7979
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
2 changes: 2 additions & 0 deletions README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ nuspecファイルを使ってパッケージを生成する場合は、デフ

## 履歴

* 3.2.50:
* 同じコミットに複数のバージョンタグが含まれる場合に、最も大きいバージョン番号を使用するように改良。
* 3.2.40:
* .NET 8.0 RC2に対応しました。恐らくそのまま正式な.NET 8.0バージョンにも使用できますが、
.NET 8.0がリリースされたのちに再ビルドしたバージョンをリリースします。
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ When you are using a nuspec file to generate a NuGet package, there are addition

## History

* 3.2.50:
* Improved to use the highest version number when multiple version tags are included in the same commit.
* 3.2.40:
* .NET 8.0 RC2 is now supported. Although it can probably be used for the .NET 8.0 release without any modification, but will release a rebuilt version after .NET 8.0 is released.
* 3.2.20:
Expand Down
22 changes: 10 additions & 12 deletions RelaxVersioner.Core/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#nullable enable

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -89,22 +90,19 @@ internal static class Analyzer
break;
}

var found = false;
foreach (var tag in commit.Tags)
{
if (Version.TryParse(tag.Name, out var v2))
{
version = v2;
reached.Add(commit, version);
found = true;
break;
}
}
if (found)
// Detected mostly larger version tag.
var candidates = commit.Tags.
Collect(tag => Version.TryParse(tag.Name, out var v) ? v : null!).
OrderByDescending(v => v).
ToArray();
if (candidates.Length >= 1)
{
version = candidates[0];
reached.Add(commit, version);
break;
}

// Traverse next commits.
var parents = await commit.GetParentCommitsAsync(ct);
if (parents.Length == 0)
{
Expand Down
35 changes: 26 additions & 9 deletions RelaxVersioner.Core/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//
////////////////////////////////////////////////////////////////////////////////////////

#nullable enable

using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -41,12 +43,12 @@ internal static class Utilities
return typeof(Utilities).Assembly.
GetTypes().
Where(type => type.IsSealed && type.IsClass && typeof(WriterBase).IsAssignableFrom(type)).
Select(type => (WriterBase)Activator.CreateInstance(type)).
Select(type => (WriterBase)Activator.CreateInstance(type)!).
ToDictionary(writer => writer.Language, StringComparer.InvariantCultureIgnoreCase);
}

private static async Task<T> TraversePathToRootAsync<T>(
string candidatePath, Func<string, Task<T>> action)
private static async Task<T?> TraversePathToRootAsync<T>(
string candidatePath, Func<string, Task<T?>> action)
where T : class
{
var path = Path.GetFullPath(candidatePath).
Expand Down Expand Up @@ -76,7 +78,7 @@ internal static class Utilities
public static string GetDirectoryNameWithTrailingSeparator(string path) =>
GetDirectoryNameWithoutTrailingSeparator(path) + Path.DirectorySeparatorChar;

public static async Task<StructuredRepository> OpenRepositoryAsync(
public static async Task<StructuredRepository?> OpenRepositoryAsync(
Logger logger, string candidatePath)
{
var repository = await TraversePathToRootAsync(candidatePath, async path =>
Expand All @@ -103,15 +105,30 @@ internal static class Utilities
return repository;
}

public static IEnumerable<U> Collect<T, U>(
this IEnumerable<T> enumerable,
Func<T, U?> selector)
where U : class
{
foreach (var item in enumerable)
{
if (selector(item) is { } value)
{
yield return value;
}
}
}

public static TValue GetValue<TKey, TValue>(
this Dictionary<TKey, TValue> dictionary,
TKey key,
TValue defaultValue)
where TKey : notnull
{
Debug.Assert(dictionary != null);
Debug.Assert(key != null);

if (dictionary.TryGetValue(key, out TValue value) == false)
if (dictionary!.TryGetValue(key!, out var value) == false)
{
value = defaultValue;
}
Expand Down Expand Up @@ -153,7 +170,7 @@ public static IEnumerable<XElement> LoadRuleSets(string candidatePath)
var rulePath = Path.Combine(path, "RelaxVersioner.rules");
if (File.Exists(rulePath))
{
XElement element = null;
XElement? element = null;
try
{
element = XElement.Load(rulePath);
Expand Down Expand Up @@ -210,15 +227,15 @@ public static IEnumerable<Rule> AggregateRules(XElement wrules)
return (from rule in wrules.Elements("Rule")
let name = rule.Attribute("name")
let key = rule.Attribute("key")
where !string.IsNullOrWhiteSpace(name?.Value)
select new Rule(name.Value.Trim(), key?.Value.Trim(), rule.Value.Trim()));
where !string.IsNullOrWhiteSpace(name?.Value) && !string.IsNullOrWhiteSpace(key?.Value)
select new Rule(name.Value.Trim(), key.Value.Trim(), rule.Value.Trim()));
}

public static XElement GetDefaultRuleSet()
{
var type = typeof(Utilities);
using (var stream = type.Assembly.GetManifestResourceStream(
type, "DefaultRuleSet.rules"))
type, "DefaultRuleSet.rules")!)
{
return XElement.Load(stream);
}
Expand Down

0 comments on commit 63c7979

Please sign in to comment.