-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for project containers (#14)
- Loading branch information
Showing
26 changed files
with
1,503 additions
and
84 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
.autover/changes/107cbaff-fc23-40ee-a9cb-24fc12d7d744.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"Projects": [ | ||
{ | ||
"Name": "AutoVer", | ||
"Type": "Patch", | ||
"ChangelogMessages": [ | ||
"Add support for project containers, which allow multiple projects to be versioned as one", | ||
"Add a unit test project that tests a combination of project configurations" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,10 @@ jobs: | |
uses: actions/setup-dotnet@v4 | ||
with: | ||
dotnet-version: 8.0.x | ||
- name: Setup Git User | ||
run: | | ||
git config --global user.email "[email protected]" | ||
git config --global user.name "GitHub User" | ||
- name: Restore dependencies | ||
run: dotnet restore | ||
- name: Build | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Xml; | ||
using AutoVer.Constants; | ||
using AutoVer.Exceptions; | ||
using AutoVer.Models; | ||
|
||
namespace AutoVer.Extensions; | ||
|
||
public static class ProjectExtensions | ||
{ | ||
public static void EnsureProjectHasVersionTag(this ProjectContainer projectContainer) | ||
{ | ||
foreach (var project in projectContainer.Projects) | ||
{ | ||
var extension = Path.GetExtension(project.ProjectDefinition.ProjectPath); | ||
var versionTag = ProjectConstants.VersionTag; | ||
if (string.Equals(extension, ".nuspec")) | ||
versionTag = ProjectConstants.NuspecVersionTag; | ||
var versionTagList = project.ProjectDefinition.Contents.GetElementsByTagName(versionTag).Cast<XmlNode>().ToList(); | ||
if (!versionTagList.Any()) | ||
throw new NoVersionTagException($"The project '{projectContainer.Name}' does not have a '{versionTag}' tag. Add a '{versionTag}' tag and run the tool again."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,164 @@ | ||
using System.Text.Json.Serialization; | ||
using System.Xml; | ||
using AutoVer.Constants; | ||
using AutoVer.Services.IO; | ||
|
||
namespace AutoVer.Models; | ||
|
||
public class UserConfiguration | ||
{ | ||
[JsonIgnore] public string GitRoot { get; set; } = string.Empty; | ||
internal bool PersistConfiguration { get; set; } | ||
public List<Project> Projects { get; set; } = []; | ||
public List<ProjectContainer> 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; | ||
public Dictionary<string, string>? ChangelogCategories { get; set; } | ||
public bool ChangeFilesDetermineIncrementType { get; set; } = false; | ||
} | ||
|
||
public class ProjectContainer : IJsonOnDeserialized | ||
{ | ||
private IFileManager? _fileManager; | ||
private IPathManager? _pathManager; | ||
|
||
public required string Name { get; set; } | ||
public string? Path { get; set; } | ||
|
||
public List<string>? Paths { get; set; } | ||
|
||
public class Project | ||
{ | ||
public required string Name { get; set; } | ||
public required string Path { get; set; } | ||
internal List<Project> Projects { get; set; } = []; | ||
|
||
[JsonConverter(typeof(JsonStringEnumConverter))] | ||
public IncrementType? IncrementType { get; set; } | ||
[JsonConverter(typeof(JsonStringEnumConverter))] | ||
public IncrementType? IncrementType { get; set; } | ||
|
||
public string? PrereleaseLabel { get; set; } | ||
public string? PrereleaseLabel { get; set; } | ||
|
||
public List<string> GetPaths() | ||
{ | ||
if (!string.IsNullOrEmpty(Path)) | ||
{ | ||
return new List<string> { Path }; | ||
} | ||
else | ||
{ | ||
return Paths ?? []; | ||
} | ||
} | ||
|
||
public void OnDeserialized() | ||
{ | ||
if (_fileManager == null || _pathManager == null) | ||
return; | ||
|
||
bool isPathProvided = !string.IsNullOrEmpty(Path); | ||
bool isPathsProvided = Paths is { Count: > 0 }; | ||
|
||
if (!isPathProvided && !isPathsProvided) | ||
{ | ||
var errorMessage = $"{Name} - Either 'Path' or 'Paths' must be provided."; | ||
Console.WriteLine(errorMessage); | ||
throw new Exception(errorMessage); | ||
} | ||
|
||
if (isPathProvided && isPathsProvided) | ||
{ | ||
var errorMessage = $"{Name} - 'Path' and 'Paths' cannot both be provided. Please provide only one."; | ||
Console.WriteLine(errorMessage); | ||
throw new Exception(errorMessage); | ||
} | ||
|
||
foreach (var path in GetPaths()) | ||
{ | ||
var normalizedPath = path.Replace('\\', _pathManager.DirectorySeparatorChar).Replace('/', _pathManager.DirectorySeparatorChar); | ||
if (!_fileManager.Exists(normalizedPath)) | ||
throw new Exception($"Failed to find a valid .csproj or .nuspec file at path {normalizedPath}"); | ||
|
||
var extension = _pathManager.GetExtension(normalizedPath); | ||
if (!string.Equals(extension, ".csproj") && !string.Equals(extension, ".nuspec")) | ||
{ | ||
var errorMessage = $"Invalid project path {normalizedPath}. The project path must point to a .csproj or .nuspec file"; | ||
throw new Exception(errorMessage); | ||
} | ||
|
||
var xmlProjectFile = new XmlDocument{ PreserveWhitespace = true }; | ||
xmlProjectFile.LoadXml(_fileManager.ReadAllText(normalizedPath)); | ||
|
||
var projectDefinition = new ProjectDefinition( | ||
xmlProjectFile, | ||
_pathManager.GetFullPath(normalizedPath) | ||
); | ||
|
||
var versionTag = ProjectConstants.VersionTag; | ||
if (string.Equals(extension, ".nuspec")) | ||
versionTag = ProjectConstants.NuspecVersionTag; | ||
var version = xmlProjectFile.GetElementsByTagName(versionTag); | ||
if (version.Count > 0) | ||
{ | ||
projectDefinition.Version = version[0]?.InnerText; | ||
} | ||
|
||
Projects.Add(new Project(normalizedPath, projectDefinition)); | ||
} | ||
} | ||
|
||
public void InjectDependency(IFileManager fileManager, IPathManager pathManager) | ||
{ | ||
_fileManager = fileManager; | ||
_pathManager = pathManager; | ||
} | ||
} | ||
|
||
public class Project(string path, ProjectDefinition definition) | ||
{ | ||
private IFileManager? _fileManager; | ||
private IPathManager? _pathManager; | ||
|
||
public string Path { get; set; } = path; | ||
|
||
internal ProjectDefinition? ProjectDefinition { get; set; } | ||
internal ProjectDefinition ProjectDefinition { get; set; } = definition; | ||
|
||
public void OnDeserialized() | ||
{ | ||
if (_fileManager == null || _pathManager == null) | ||
return; | ||
|
||
var normalizedPath = Path.Replace('\\', _pathManager.DirectorySeparatorChar).Replace('/', _pathManager.DirectorySeparatorChar); | ||
if (!_fileManager.Exists(normalizedPath)) | ||
throw new Exception($"Failed to find a valid .csproj or .nuspec file at path {normalizedPath}"); | ||
|
||
var extension = _pathManager.GetExtension(normalizedPath); | ||
if (!string.Equals(extension, ".csproj") && !string.Equals(extension, ".nuspec")) | ||
{ | ||
var errorMessage = $"Invalid project path {normalizedPath}. The project path must point to a .csproj or .nuspec file"; | ||
throw new Exception(errorMessage); | ||
} | ||
|
||
var xmlProjectFile = new XmlDocument{ PreserveWhitespace = true }; | ||
xmlProjectFile.LoadXml(_fileManager.ReadAllText(normalizedPath)); | ||
|
||
var projectDefinition = new ProjectDefinition( | ||
xmlProjectFile, | ||
_pathManager.GetFullPath(normalizedPath) | ||
); | ||
|
||
var versionTag = ProjectConstants.VersionTag; | ||
if (string.Equals(extension, ".nuspec")) | ||
versionTag = ProjectConstants.NuspecVersionTag; | ||
var version = xmlProjectFile.GetElementsByTagName(versionTag); | ||
if (version.Count > 0) | ||
{ | ||
projectDefinition.Version = version[0]?.InnerText; | ||
} | ||
|
||
ProjectDefinition = projectDefinition; | ||
} | ||
|
||
public void InjectDependency(IFileManager fileManager, IPathManager pathManager) | ||
{ | ||
_fileManager = fileManager; | ||
_pathManager = pathManager; | ||
} | ||
} |
Oops, something went wrong.