From fbc9ba1ecc6c3e7d0030fbcc414a3ad97feeccef Mon Sep 17 00:00:00 2001 From: Nice3point Date: Wed, 27 Dec 2023 22:36:22 +0300 Subject: [PATCH] Add assertions --- build/Build.CI.GitHub.cs | 55 ++++++++++++++++++---------------- build/Build.Compile.cs | 4 +-- build/Build.Installer.cs | 8 +++-- install/Installer.Generator.cs | 12 ++++---- install/Installer.cs | 2 ++ 5 files changed, 44 insertions(+), 37 deletions(-) diff --git a/build/Build.CI.GitHub.cs b/build/Build.CI.GitHub.cs index 58ac5470..53f3687b 100644 --- a/build/Build.CI.GitHub.cs +++ b/build/Build.CI.GitHub.cs @@ -1,5 +1,4 @@ using System.Text; -using System.Text.RegularExpressions; using Nuke.Common; using Nuke.Common.Git; using Nuke.Common.Tools.Git; @@ -24,10 +23,11 @@ sealed partial class Build var gitHubName = GitRepository.GetGitHubName(); var gitHubOwner = GitRepository.GetGitHubOwner(); - Validate(version); + ValidateRelease(version); var artifacts = Directory.GetFiles(ArtifactsDirectory, "*"); - var changelog = CreateChangelog(version); + var changelog = CreateGithubChangelog(version); + Assert.NotEmpty(artifacts, "No artifacts were found to create the Release"); var newRelease = new NewRelease(version) { @@ -40,11 +40,12 @@ sealed partial class Build await UploadArtifactsAsync(release, artifacts); }); - static void Validate(string version) + void ValidateRelease(string version) { - var tags = GitTasks.Git("tag --list"); - Assert.False(tags.Any(tag => tag.Text == version), $"A Release with the specified tag already exists in the repository: {version}"); + var tags = GitTasks.Git("describe --tags --abbrev=0", logInvocation: false, logOutput: false); + if (tags.Count == 0) return; + Assert.False(tags.Last().Text == version, $"A Release with the specified tag already exists in the repository: {version}"); Log.Information("Version: {Version}", version); } @@ -64,22 +65,13 @@ static async Task UploadArtifactsAsync(Release createdRelease, IEnumerable 0, $"No version entry exists in the changelog: {version}"); WriteCompareUrl(version, changelog); return changelog.ToString(); @@ -87,16 +79,17 @@ string CreateChangelog(string version) void WriteCompareUrl(string version, StringBuilder changelog) { - var tags = GitTasks.Git("tag --list"); + var tags = GitTasks.Git("describe --tags --abbrev=0", logInvocation: false, logOutput: false); if (tags.Count == 0) return; + if (changelog[^1] != '\r' || changelog[^1] != '\n') changelog.AppendLine(); changelog.Append("Full changelog: "); changelog.Append(GitRepository.GetGitHubCompareTagsUrl(version, tags.Last().Text)); } - StringBuilder ReadChangelog(string version) + StringBuilder BuildChangelog(string version) { - const char separator = '#'; + const string separator = "# "; var hasEntry = false; var changelog = new StringBuilder(); @@ -105,10 +98,8 @@ StringBuilder ReadChangelog(string version) if (hasEntry) { if (line.StartsWith(separator)) break; - if (line == string.Empty) continue; - if (changelog.Length > 0) changelog.AppendLine(); - changelog.Append(line); + changelog.AppendLine(line); continue; } @@ -118,6 +109,20 @@ StringBuilder ReadChangelog(string version) } } + TrimEmptyLines(changelog); return changelog; } + + static void TrimEmptyLines(StringBuilder builder) + { + while (builder[^1] == '\r' || builder[^1] == '\n') + { + builder.Remove(builder.Length - 1, 1); + } + + while (builder[0] == '\r' || builder[0] == '\n') + { + builder.Remove(0, 1); + } + } } \ No newline at end of file diff --git a/build/Build.Compile.cs b/build/Build.Compile.cs index 7d2a13f4..1631bb17 100644 --- a/build/Build.Compile.cs +++ b/build/Build.Compile.cs @@ -28,9 +28,7 @@ IEnumerable GlobBuildConfigurations() .Where(config => Configurations.Any(wildcard => FileSystemName.MatchesSimpleExpression(wildcard, config))) .ToList(); - if (configurations.Count == 0) - throw new Exception($"No solution configurations have been found. Pattern: {string.Join(" | ", Configurations)}"); - + Assert.NotEmpty(configurations, $"No solution configurations have been found. Pattern: {string.Join(" | ", Configurations)}"); return configurations; } } \ No newline at end of file diff --git a/build/Build.Installer.cs b/build/Build.Installer.cs index 6ce696cb..274789a7 100644 --- a/build/Build.Installer.cs +++ b/build/Build.Installer.cs @@ -18,10 +18,12 @@ sealed partial class Build Log.Information("Project: {Name}", project.Name); var exePattern = $"*{installer.Name}.exe"; - var exeFile = Directory.EnumerateFiles(installer.Directory, exePattern, SearchOption.AllDirectories).First(); + var exeFile = Directory.EnumerateFiles(installer.Directory, exePattern, SearchOption.AllDirectories) + .FirstOrDefault() + .NotNull($"No installer file was found for the project: {installer.Name}"); var directories = Directory.GetDirectories(project.Directory, "* Release *", SearchOption.AllDirectories); - if (directories.Length == 0) throw new Exception("No files were found to create an installer"); + Assert.NotEmpty(directories, "No files were found to create an installer"); foreach (var directory in directories) { @@ -36,7 +38,7 @@ sealed partial class Build RedirectStream(process.StandardError, LogEventLevel.Error); process.WaitForExit(); - if (process.ExitCode != 0) throw new Exception($"The installer creation failed with ExitCode {process.ExitCode}"); + if (process.ExitCode != 0) throw new InvalidOperationException($"The installer creation failed with ExitCode {process.ExitCode}"); } } }); diff --git a/install/Installer.Generator.cs b/install/Installer.Generator.cs index a6a3017f..dab21a3b 100644 --- a/install/Installer.Generator.cs +++ b/install/Installer.Generator.cs @@ -29,19 +29,19 @@ namespace Installer; public static class Generator { - public static WixEntity[] GenerateWixEntities(string[] args, Version version) + public static WixEntity[] GenerateWixEntities(IEnumerable args, Version version) { var entities = new List(); foreach (var directory in args) { Console.WriteLine($"Installer files for version '{version}':"); - ProcessDirectory(directory, entities); + GenerateRootEntities(directory, entities); } return entities.ToArray(); } - private static void ProcessDirectory(string directory, List entities) + private static void GenerateRootEntities(string directory, ICollection entities) { foreach (var file in Directory.GetFiles(directory)) { @@ -55,11 +55,11 @@ private static void ProcessDirectory(string directory, List entities) var entity = new Dir(folderName); entities.Add(entity); - ProcessDirectoryFiles(folder, entity); + GenerateSubEntities(folder, entity); } } - private static void ProcessDirectoryFiles(string directory, Dir parent) + private static void GenerateSubEntities(string directory, Dir parent) { foreach (var file in Directory.GetFiles(directory)) { @@ -73,7 +73,7 @@ private static void ProcessDirectoryFiles(string directory, Dir parent) var entity = new Dir(folderName); parent.AddDir(entity); - ProcessDirectoryFiles(subfolder, entity); + GenerateSubEntities(subfolder, entity); } } } \ No newline at end of file diff --git a/install/Installer.cs b/install/Installer.cs index 25f8d616..3d7fb708 100644 --- a/install/Installer.cs +++ b/install/Installer.cs @@ -24,7 +24,9 @@ var versions = Tools.ComputeVersions(args); if (!guidMap.TryGetValue(versions.RevitVersion, out var guid)) +{ throw new Exception($"Version GUID mapping missing for the specified version: '{versions.RevitVersion}'"); +} var project = new Project {