-
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 change files to control generated changelog file
- Loading branch information
Showing
19 changed files
with
309 additions
and
26 deletions.
There are no files selected for viewing
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
10 changes: 10 additions & 0 deletions
10
.autover/changes/1641dced-a01e-4511-a4e2-d8b0d60df721.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,10 @@ | ||
{ | ||
"Projects": [ | ||
{ | ||
"Name": "AutoVer", | ||
"ChangelogMessages": [ | ||
"Add change files to control generated changelog file" | ||
] | ||
} | ||
] | ||
} |
10 changes: 10 additions & 0 deletions
10
.autover/changes/e7ce1bf3-66dc-4266-a4a1-7c1b75da56e6.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,10 @@ | ||
{ | ||
"Projects": [ | ||
{ | ||
"Name": "AutoVer", | ||
"ChangelogMessages": [ | ||
"Changelog command now uses the contents of last git tag instead of HEAD" | ||
] | ||
} | ||
] | ||
} |
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,40 @@ | ||
using AutoVer.Models; | ||
using AutoVer.Services; | ||
|
||
namespace AutoVer.Commands; | ||
|
||
public class ChangeCommand( | ||
IConfigurationManager configurationManager, | ||
IToolInteractiveService toolInteractiveService, | ||
IChangeFileHandler changeFileHandler) | ||
{ | ||
public async Task ExecuteAsync( | ||
string? optionProjectPath, | ||
string? optionIncrementType, | ||
string? optionMessage) | ||
{ | ||
if (!Enum.TryParse(optionIncrementType, out IncrementType incrementType)) | ||
{ | ||
incrementType = IncrementType.Patch; | ||
} | ||
|
||
if (string.IsNullOrEmpty(optionProjectPath)) | ||
optionProjectPath = Directory.GetCurrentDirectory(); | ||
|
||
var userConfiguration = await configurationManager.RetrieveUserConfiguration(optionProjectPath, incrementType); | ||
if (userConfiguration.UseCommitsForChangelog) | ||
{ | ||
toolInteractiveService.WriteErrorLine($"This repository is not configured to use change files. Change '{nameof(userConfiguration.UseCommitsForChangelog)}' to 'false' in the repo's '.autover/autover.json' file."); | ||
return; | ||
} | ||
if (userConfiguration.Projects.Count > 1 && !string.IsNullOrEmpty(optionMessage)) | ||
{ | ||
toolInteractiveService.WriteErrorLine("You need to specify a project name with the change message. Use the '--project-name' argument to specify the project name."); | ||
return; | ||
} | ||
|
||
var changeFile = changeFileHandler.GenerateChangeFile(userConfiguration, optionMessage); | ||
|
||
await changeFileHandler.PersistChangeFile(userConfiguration, changeFile); | ||
} | ||
} |
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
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,13 @@ | ||
namespace AutoVer.Models; | ||
|
||
public class ChangeFile | ||
{ | ||
public List<ProjectChange> Projects { get; set; } = []; | ||
} | ||
|
||
public class ProjectChange | ||
{ | ||
public required string Name { get; set; } | ||
|
||
public List<string> ChangelogMessages { get; set; } = []; | ||
} |
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,99 @@ | ||
using System.Text.Json; | ||
using AutoVer.Constants; | ||
using AutoVer.Exceptions; | ||
using AutoVer.Models; | ||
using AutoVer.Services.IO; | ||
|
||
namespace AutoVer.Services; | ||
|
||
public class ChangeFileHandler( | ||
IPathManager pathManager, | ||
IDirectoryManager directoryManager, | ||
IFileManager fileManager, | ||
IToolInteractiveService toolInteractiveService) : IChangeFileHandler | ||
{ | ||
public ChangeFile GenerateChangeFile(UserConfiguration configuration, string? changeMessage) | ||
{ | ||
var changeFile = new ChangeFile(); | ||
foreach (var project in configuration.Projects) | ||
{ | ||
changeFile.Projects.Add(new ProjectChange | ||
{ | ||
Name = project.Name, | ||
ChangelogMessages = !string.IsNullOrEmpty(changeMessage) ? [changeMessage] : [] | ||
}); | ||
} | ||
|
||
return changeFile; | ||
} | ||
|
||
public async Task PersistChangeFile(UserConfiguration configuration, ChangeFile changeFile) | ||
{ | ||
if (string.IsNullOrEmpty(configuration.GitRoot)) | ||
throw new InvalidProjectException("The project path you have specified is not a valid git repository."); | ||
|
||
var changeFolder = pathManager.Combine(configuration.GitRoot, ConfigurationConstants.ConfigFolderName, | ||
ConfigurationConstants.ChangesFolderName); | ||
|
||
if (!directoryManager.Exists(changeFolder)) | ||
directoryManager.CreateDirectory(changeFolder); | ||
|
||
var changeFilePath = pathManager.Combine(changeFolder, $"{Guid.NewGuid().ToString().ToLower()}.json"); | ||
|
||
await fileManager.WriteAllTextAsync(changeFilePath, | ||
JsonSerializer.Serialize(changeFile, new JsonSerializerOptions | ||
{ | ||
WriteIndented = true | ||
})); | ||
} | ||
|
||
public async Task<IList<ChangeFile>> LoadChangeFilesFromRepository(string repositoryRoot) | ||
{ | ||
var changeFilesPath = pathManager.Combine(repositoryRoot, ConfigurationConstants.ConfigFolderName, ConfigurationConstants.ChangesFolderName); | ||
|
||
var changeFilePaths = directoryManager.GetFiles(changeFilesPath, "*.json").ToList(); | ||
|
||
var changeFiles = new List<ChangeFile>(); | ||
|
||
foreach (var changeFilePath in changeFilePaths) | ||
{ | ||
try | ||
{ | ||
var content = await fileManager.ReadAllTextAsync(changeFilePath); | ||
var changeFile = JsonSerializer.Deserialize<ChangeFile>(content); | ||
if (changeFile != null) | ||
changeFiles.Add(changeFile); | ||
} | ||
catch (Exception) | ||
{ | ||
toolInteractiveService.WriteErrorLine($"Unable to deserialize the change file '{changeFilePath}'."); | ||
} | ||
} | ||
|
||
return changeFiles; | ||
} | ||
|
||
public void ResetChangeFiles(UserConfiguration userConfiguration) | ||
{ | ||
if (string.IsNullOrEmpty(userConfiguration.GitRoot)) | ||
throw new InvalidProjectException("The project path you have specified is not a valid git repository."); | ||
|
||
var changeFolderPath = pathManager.Combine(userConfiguration.GitRoot, ConfigurationConstants.ConfigFolderName, ConfigurationConstants.ChangesFolderName); | ||
if (!directoryManager.Exists(changeFolderPath)) | ||
return; | ||
|
||
var changeFilePaths = directoryManager.GetFiles(changeFolderPath, "*", SearchOption.AllDirectories).ToList(); | ||
|
||
foreach (var changeFilePath in changeFilePaths) | ||
{ | ||
try | ||
{ | ||
fileManager.Delete(changeFilePath); | ||
} | ||
catch (Exception) | ||
{ | ||
toolInteractiveService.WriteErrorLine($"Unable to delete the change file '{changeFilePath}'."); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.