Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to override the version of all projects #5

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .autover/changes/2f83d9bb-5c10-40f7-80a7-8e2457359f3f.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Projects": [
{
"Name": "AutoVer",
"Type": "Patch",
"ChangelogMessages": [
"Add ability to override the version of all projects"
]
}
]
}
5 changes: 4 additions & 1 deletion src/AutoVer/Commands/CommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private Command BuildVersionCommand()
Option<bool> skipVersionTagCheckOption = new(new[] { "--skip-version-tag-check" }, $"Skip version tag check and increment projects even if some don't have a {ProjectConstants.VersionTag} tag.");
Option<bool> noCommitOption = new(new[] { "--no-commit" }, $"Do not commit changes after versioning.");
Option<bool> noTagOption = new(new[] { "--no-tag" }, $"Do not add a Git Tag after versioning.");
Option<string> useVersionOption = new("--use-version", "Use a specific version for all projects.");

lock (ChildCommandLock)
{
Expand All @@ -62,6 +63,7 @@ private Command BuildVersionCommand()
versionCommand.Add(skipVersionTagCheckOption);
versionCommand.Add(noCommitOption);
versionCommand.Add(noTagOption);
versionCommand.Add(useVersionOption);
}

versionCommand.SetHandler(async (context) =>
Expand All @@ -73,9 +75,10 @@ private Command BuildVersionCommand()
var optionSkipVersionTagCheck = context.ParseResult.GetValueForOption(skipVersionTagCheckOption);
var optionNoCommit = context.ParseResult.GetValueForOption(noCommitOption);
var optionNoTag = context.ParseResult.GetValueForOption(noTagOption);
var optionUseVersion = context.ParseResult.GetValueForOption(useVersionOption);

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

context.ExitCode = CommandReturnCodes.Success;
}
Expand Down
9 changes: 5 additions & 4 deletions src/AutoVer/Commands/VersionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public async Task ExecuteAsync(
string? optionIncrementType,
bool optionSkipVersionTagCheck,
bool optionNoCommit,
bool optionNoTag)
bool optionNoTag,
string? optionUseVersion)
{
if (!Enum.TryParse(optionIncrementType, out IncrementType incrementType))
{
Expand Down Expand Up @@ -57,12 +58,12 @@ public async Task ExecuteAsync(
var projectIncrementType = IncrementType.None;
if (projectIncrements.ContainsKey(availableProject.Name))
projectIncrementType = projectIncrements[availableProject.Name];
projectHandler.UpdateVersion(availableProject.ProjectDefinition, projectIncrementType, availableProject.PrereleaseLabel);
projectHandler.UpdateVersion(availableProject.ProjectDefinition, projectIncrementType, availableProject.PrereleaseLabel, optionUseVersion);
}
else
{
var projectIncrementType = availableProject.IncrementType ?? IncrementType.Patch;
projectHandler.UpdateVersion(availableProject.ProjectDefinition, projectIncrementType, availableProject.PrereleaseLabel);
projectHandler.UpdateVersion(availableProject.ProjectDefinition, projectIncrementType, availableProject.PrereleaseLabel, optionUseVersion);
}
gitHandler.StageChanges(userConfiguration, availableProject.Path);
}
Expand All @@ -79,7 +80,7 @@ public async Task ExecuteAsync(
}
}

if (!projectsIncremented)
if (!projectsIncremented && string.IsNullOrEmpty(optionUseVersion))
return;

if (!optionNoCommit)
Expand Down
4 changes: 4 additions & 0 deletions src/AutoVer/Exceptions/InvalidArgumentException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace AutoVer.Exceptions;

public class InvalidArgumentException(string message, Exception? innerException = null)
: AutoVerException(message, innerException);
19 changes: 19 additions & 0 deletions src/AutoVer/Models/ThreePartVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,23 @@ public static ThreePartVersion Parse(string? version)
PrereleaseLabel = prereleaseLabel
};
}

public static bool TryParse(string? versionString, out ThreePartVersion version)
{
try
{
version = Parse(versionString);
return true;
}
catch (Exception)
{
version = new ThreePartVersion
{
Major = 0,
Minor = 0,
Patch = 1
};
return false;
}
}
}
2 changes: 1 addition & 1 deletion src/AutoVer/Services/IProjectHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace AutoVer.Services;
public interface IProjectHandler
{
Task<List<ProjectDefinition>> GetAvailableProjects(string? projectPath);
void UpdateVersion(ProjectDefinition projectDefinition, IncrementType incrementType, string? prereleaseLabel = null);
void UpdateVersion(ProjectDefinition projectDefinition, IncrementType incrementType, string? prereleaseLabel = null, string? overrideVersion = null);
bool ProjectHasVersionTag(ProjectDefinition projectDefinition);
}
22 changes: 18 additions & 4 deletions src/AutoVer/Services/ProjectHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,29 @@ public async Task<List<ProjectDefinition>> GetAvailableProjects(string? projectP
return projectDefinitions;
}

public void UpdateVersion(ProjectDefinition projectDefinition, IncrementType incrementType, string? prereleaseLabel = null)
public void UpdateVersion(ProjectDefinition projectDefinition, IncrementType incrementType, string? prereleaseLabel = null, string? overrideVersion = null)
{
var versionTagList = projectDefinition.Contents.GetElementsByTagName(ProjectConstants.VersionTag).Cast<XmlNode>().ToList();
if (!versionTagList.Any())
throw new NoVersionTagException($"The project '{projectDefinition.ProjectPath}' does not have a {ProjectConstants.VersionTag} tag. Add a {ProjectConstants.VersionTag} tag and run the tool again.");

var versionTag = versionTagList.First();
var nextVersion = versionIncrementer.GetNextVersion(versionTag.InnerText, incrementType, prereleaseLabel);
versionTag.InnerText = nextVersion.ToString();
if (string.IsNullOrEmpty(overrideVersion))
{
var nextVersion = versionIncrementer.GetNextVersion(versionTag.InnerText, incrementType, prereleaseLabel);
versionTag.InnerText = nextVersion.ToString();
}
else
{
if (ThreePartVersion.TryParse(overrideVersion, out var version))
{
versionTag.InnerText = version.ToString();
}
else
{
throw new InvalidArgumentException($"The version '{overrideVersion}' you are trying to update to is invalid.");
}
}

projectDefinition.Contents.Save(projectDefinition.ProjectPath);
}
Expand Down