Skip to content

Commit

Permalink
add ability to version all projects as one
Browse files Browse the repository at this point in the history
  • Loading branch information
philasmar committed Oct 4, 2024
1 parent 9f824a1 commit 797e3e6
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/AutoVer/AutoVer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<PackageReference Include="LibGit2Sharp" Version="0.30.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 9 additions & 2 deletions src/AutoVer/Commands/CommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class CommandFactory(
IConfigurationManager configurationManager,
IChangelogHandler changelogHandler,
IChangeFileHandler changeFileHandler,
IVersionHandler versionHandler
IVersionHandler versionHandler,
IVersionIncrementer versionIncrementer
) : ICommandFactory
{
private static readonly Option<string> OptionProjectPath = new("--project-path", Directory.GetCurrentDirectory, "Path to the project");
Expand Down Expand Up @@ -77,7 +78,13 @@ private Command BuildVersionCommand()
var optionNoTag = context.ParseResult.GetValueForOption(noTagOption);
var optionUseVersion = context.ParseResult.GetValueForOption(useVersionOption);

var command = new VersionCommand(projectHandler, gitHandler, configurationManager, changeFileHandler, versionHandler);
var command = new VersionCommand(
projectHandler,
gitHandler,
configurationManager,
changeFileHandler,
versionHandler,
versionIncrementer);
await command.ExecuteAsync(optionProjectPath, optionIncrementType, optionSkipVersionTagCheck, optionNoCommit, optionNoTag, optionUseVersion);

context.ExitCode = CommandReturnCodes.Success;
Expand Down
44 changes: 37 additions & 7 deletions src/AutoVer/Commands/VersionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class VersionCommand(
IGitHandler gitHandler,
IConfigurationManager configurationManager,
IChangeFileHandler changeFileHandler,
IVersionHandler versionHandler)
IVersionHandler versionHandler,
IVersionIncrementer versionIncrementer)
{
public async Task ExecuteAsync(
string? optionProjectPath,
Expand Down Expand Up @@ -46,25 +47,54 @@ public async Task ExecuteAsync(
projectIncrements = changeFileHandler.GetProjectIncrementTypesFromChangeFiles(changeFiles);
}

ThreePartVersion? maxNextVersion = null;
if (userConfiguration.UseSameVersionForAllProjects)
{
maxNextVersion = versionIncrementer.GetNextMaxVersion(
userConfiguration.Projects,
userConfiguration.ChangeFilesDetermineIncrementType ? projectIncrements : null,
incrementType);
}

var projectsIncremented = false;
foreach (var availableProject in userConfiguration.Projects)
{
if (availableProject.ProjectDefinition is null)
throw new InvalidUserConfigurationException($"The configured project '{availableProject.Path}' is invalid.");
if (!availableProject.IncrementType.Equals(IncrementType.None))
projectsIncremented = true;
if (userConfiguration.ChangeFilesDetermineIncrementType)
if (userConfiguration.UseSameVersionForAllProjects)
{
var projectIncrementType = IncrementType.None;
if (projectIncrements.ContainsKey(availableProject.Name))
var projectIncrementType = availableProject.IncrementType ?? IncrementType.Patch;
if (userConfiguration.ChangeFilesDetermineIncrementType &&
projectIncrements.ContainsKey(availableProject.Name))
projectIncrementType = projectIncrements[availableProject.Name];
projectHandler.UpdateVersion(availableProject.ProjectDefinition, projectIncrementType, availableProject.PrereleaseLabel, optionUseVersion);
projectHandler.UpdateVersion(
availableProject.ProjectDefinition,
projectIncrementType,
availableProject.PrereleaseLabel,
optionUseVersion ?? maxNextVersion?.ToString());
}
else
{
var projectIncrementType = availableProject.IncrementType ?? IncrementType.Patch;
projectHandler.UpdateVersion(availableProject.ProjectDefinition, projectIncrementType, availableProject.PrereleaseLabel, optionUseVersion);
if (userConfiguration.ChangeFilesDetermineIncrementType)
{
var projectIncrementType = IncrementType.None;
if (projectIncrements.ContainsKey(availableProject.Name))
projectIncrementType = projectIncrements[availableProject.Name];
projectHandler.UpdateVersion(
availableProject.ProjectDefinition,
projectIncrementType,
availableProject.PrereleaseLabel,
optionUseVersion);
}
else
{
var projectIncrementType = availableProject.IncrementType ?? IncrementType.Patch;
projectHandler.UpdateVersion(availableProject.ProjectDefinition, projectIncrementType, availableProject.PrereleaseLabel, optionUseVersion);
}
}

gitHandler.StageChanges(userConfiguration, availableProject.Path);
}

Expand Down
42 changes: 41 additions & 1 deletion src/AutoVer/Models/ThreePartVersion.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace AutoVer.Models;

public class ThreePartVersion
public class ThreePartVersion : IComparable<ThreePartVersion>
{
public required int Major { get; set; }
public required int Minor { get; set; }
Expand Down Expand Up @@ -59,4 +59,44 @@ public static bool TryParse(string? versionString, out ThreePartVersion version)
return false;
}
}

public int CompareTo(ThreePartVersion? other)
{
if (other == null) return 1;

int result = Major.CompareTo(other.Major);
if (result != 0) return result;

result = Minor.CompareTo(other.Minor);
if (result != 0) return result;

result = Patch.CompareTo(other.Patch);
if (result != 0) return result;

return string.Compare(PrereleaseLabel, other.PrereleaseLabel, StringComparison.Ordinal);
}

public static bool operator >(ThreePartVersion? left, ThreePartVersion? right)
{
if (left is null) return false;
return left.CompareTo(right) > 0;
}

public static bool operator <(ThreePartVersion? left, ThreePartVersion? right)
{
if (left is null) return right is not null;
return left.CompareTo(right) < 0;
}

public static bool operator >=(ThreePartVersion? left, ThreePartVersion? right)
{
if (left is null) return right is null;
return left.CompareTo(right) >= 0;
}

public static bool operator <=(ThreePartVersion? left, ThreePartVersion? right)
{
if (left is null) return true;
return left.CompareTo(right) <= 0;
}
}
1 change: 1 addition & 0 deletions src/AutoVer/Models/UserConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class UserConfiguration
internal bool PersistConfiguration { get; set; }
public List<Project> Projects { get; set; } = [];
public bool UseCommitsForChangelog { get; set; } = true;
public bool UseSameVersionForAllProjects { get; set; } = false;

[JsonConverter(typeof(JsonStringEnumConverter))]
public IncrementType DefaultIncrementType { get; set; } = IncrementType.Patch;
Expand Down
23 changes: 20 additions & 3 deletions src/AutoVer/Services/ChangelogHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ public async Task<ChangelogEntry> GenerateChangelog(UserConfiguration configurat
changeFile.Projects.RemoveAll(x => !configuredProjects.Contains(x.Name));
}

if (configuration.UseSameVersionForAllProjects)
{
changeFiles.Add(new ChangeFile
{
Projects = configuration.Projects.Select(x => new ProjectChange
{
Name = x.Name,
Type = x.IncrementType,
ChangelogMessages = new List<string>()
}).ToList()
});
}

var changelogCategories = new Dictionary<string, ChangelogCategory>();
foreach (var changeFile in changeFiles)
{
Expand All @@ -98,9 +111,6 @@ public async Task<ChangelogEntry> GenerateChangelog(UserConfiguration configurat
}
else
{
if (project.ChangelogMessages.Count == 0)
continue;

var configuredProject = configuration.Projects.First(x => x.Name.Equals(project.Name));
if (configuredProject.ProjectDefinition is null)
throw new InvalidProjectException($"The project '{configuredProject.Path}' is invalid.");
Expand All @@ -110,6 +120,13 @@ public async Task<ChangelogEntry> GenerateChangelog(UserConfiguration configurat
Name = configuredProject.Name,
Version = configuredProject.ProjectDefinition?.Version
};

if (!configuration.UseSameVersionForAllProjects)
{
if (project.ChangelogMessages.Count == 0)
continue;
}

foreach (var changelogMessage in project.ChangelogMessages)
{
changelogCategory.Changes.Add(new ChangelogChange { Description = changelogMessage });
Expand Down
1 change: 1 addition & 0 deletions src/AutoVer/Services/IVersionIncrementer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public interface IVersionIncrementer
{
ThreePartVersion GetCurrentVersion(string? versionText);
ThreePartVersion GetNextVersion(string? versionText, IncrementType incrementType, string? prereleaseLabel = null);
ThreePartVersion GetNextMaxVersion(List<UserConfiguration.Project> projects, IDictionary<string, IncrementType>? projectIncrements, IncrementType globalIncrementType);
}
27 changes: 27 additions & 0 deletions src/AutoVer/Services/ThreePartVersionIncrementer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using AutoVer.Exceptions;
using AutoVer.Models;

namespace AutoVer.Services;
Expand Down Expand Up @@ -43,4 +44,30 @@ public ThreePartVersion GetNextVersion(string? versionText, IncrementType increm

return nextVersion;
}

public ThreePartVersion GetNextMaxVersion(List<UserConfiguration.Project> projects, IDictionary<string, IncrementType>? projectIncrements, IncrementType globalIncrementType)
{
ThreePartVersion? maxNextVersion = null;
foreach (var availableProject in projects)
{
if (availableProject.ProjectDefinition is null)
throw new InvalidUserConfigurationException($"The configured project '{availableProject.Path}' is invalid.");

var projectIncrementType = availableProject.IncrementType ?? globalIncrementType;
if (projectIncrements is not null &&
projectIncrements.ContainsKey(availableProject.Name))
projectIncrementType = projectIncrements[availableProject.Name];
var version = GetNextVersion(availableProject.ProjectDefinition.Version, projectIncrementType, availableProject.PrereleaseLabel);
maxNextVersion ??= version;
if (version > maxNextVersion)
maxNextVersion = version;
}

return maxNextVersion ?? new ThreePartVersion
{
Major = 0,
Minor = 0,
Patch = 1
};
}
}

0 comments on commit 797e3e6

Please sign in to comment.