Skip to content

Commit

Permalink
Add assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nice3point committed Dec 27, 2023
1 parent 90028a1 commit fbc9ba1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 deletions.
55 changes: 30 additions & 25 deletions build/Build.CI.GitHub.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text;
using System.Text.RegularExpressions;
using Nuke.Common;
using Nuke.Common.Git;
using Nuke.Common.Tools.Git;
Expand All @@ -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)
{
Expand All @@ -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);
}

Expand All @@ -64,39 +65,31 @@ static async Task UploadArtifactsAsync(Release createdRelease, IEnumerable<strin
}
}

string CreateChangelog(string version)
string CreateGithubChangelog(string version)
{
if (!File.Exists(ChangeLogPath))
{
Log.Warning("Unable to locate the changelog file: {Log}", ChangeLogPath);
return string.Empty;
}

Assert.True(File.Exists(ChangeLogPath), $"Unable to locate the changelog file: {ChangeLogPath}");
Log.Information("Changelog: {Path}", ChangeLogPath);

var changelog = ReadChangelog(version);
if (changelog.Length == 0)
{
Log.Warning("No version entry exists in the changelog: {Version}", version);
return string.Empty;
}
var changelog = BuildChangelog(version);
Assert.True(changelog.Length > 0, $"No version entry exists in the changelog: {version}");

WriteCompareUrl(version, changelog);
return changelog.ToString();
}

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();
Expand All @@ -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;
}

Expand All @@ -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);
}
}
}
4 changes: 1 addition & 3 deletions build/Build.Compile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ IEnumerable<string> 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;
}
}
8 changes: 5 additions & 3 deletions build/Build.Installer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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}");
}
}
});
Expand Down
12 changes: 6 additions & 6 deletions install/Installer.Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ namespace Installer;

public static class Generator
{
public static WixEntity[] GenerateWixEntities(string[] args, Version version)
public static WixEntity[] GenerateWixEntities(IEnumerable<string> args, Version version)
{
var entities = new List<WixEntity>();
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<WixEntity> entities)
private static void GenerateRootEntities(string directory, ICollection<WixEntity> entities)
{
foreach (var file in Directory.GetFiles(directory))
{
Expand All @@ -55,11 +55,11 @@ private static void ProcessDirectory(string directory, List<WixEntity> 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))
{
Expand All @@ -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);
}
}
}
2 changes: 2 additions & 0 deletions install/Installer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit fbc9ba1

Please sign in to comment.