Skip to content

Commit

Permalink
update build tools
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles committed Jul 13, 2023
1 parent 52feacb commit 3b20300
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .nuke/build.schema.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Build Schema",
"$ref": "#/definitions/build",
"title": "Build Schema",
"definitions": {
"build": {
"type": "object",
Expand Down Expand Up @@ -140,4 +140,4 @@
}
}
}
}
}
32 changes: 17 additions & 15 deletions build/Build.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class BuildProject : NukeBuild
{
[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;
readonly Configuration Configuration =
IsLocalBuild ? Configuration.Debug : Configuration.Release;

[Parameter(List = false)] readonly bool DotnetRunningInContainer;
[GlobalJson] readonly GlobalJson GlobalJson;
Expand All @@ -17,10 +18,11 @@ class BuildProject : NukeBuild

Target Clean => _ => _
.Description("Clean project directories")
.OnlyWhenStatic(() => BuildProjectDirectory is not null)
.Executes(() => RootDirectory
.GlobDirectories("**/bin", "**/obj", "**/TestResults")
.Where(x => !x.ToString().StartsWith(BuildProjectDirectory))
.ForEach(EnsureCleanDirectory));
.Where(x => !x.ToString().StartsWith(BuildProjectDirectory ?? string.Empty))
.ForEach(d => d.CreateOrCleanDirectory()));

Target Restore => _ => _
.Description("Run dotnet restore in every project")
Expand All @@ -41,14 +43,12 @@ class BuildProject : NukeBuild
Target Test => _ => _
.Description("Run all tests")
.DependsOn(Build)
.Executes(() => Solution
.GetProjects("*.Tests")
.ForEach(project =>
DotNetTest(s => s
.EnableNoBuild()
.EnableNoRestore()
.SetConfiguration(Configuration)
.SetProjectFile(project))));
.Executes(() =>
DotNetTest(s => s
.EnableNoBuild()
.EnableNoRestore()
.SetConfiguration(Configuration)
.SetProjectFile(Solution)));

Target TestCoverage => _ => _
.Description("Run tests with coverage")
Expand All @@ -68,10 +68,11 @@ class BuildProject : NukeBuild
.SetReports(CoverageFiles)
.SetTargetDirectory(TestReportDirectory)
.SetReportTypes(ReportTypes.TextSummary));
ReadAllLines(TestReportDirectory / "Summary.txt").ForEach(l => Console.WriteLine(l));
(TestReportDirectory / "Summary.txt").ReadAllLines().ForEach(l => Console.WriteLine(l));
});

const string localstackContainerName = "sub-localstack";

Target Localstack => _ => _
.Description("Starts the localstack container in docker")
.OnlyWhenStatic(() => !DotnetRunningInContainer)
Expand All @@ -91,7 +92,8 @@ class BuildProject : NukeBuild
Target Lint => _ => _
.Description("Check for codebase formatting and analysers")
.DependsOn(Build)
.Executes(() => DotNet($"format -v normal --no-restore --verify-no-changes \"{Solution.Path}\""));
.Executes(() =>
DotNet($"format -v normal --no-restore --verify-no-changes \"{Solution.Path}\""));

Target Format => _ => _
.Description("Try fix codebase formatting and analysers")
Expand Down Expand Up @@ -146,7 +148,7 @@ class BuildProject : NukeBuild
.Executes(() =>
{
var output = RootDirectory / "Badges";
EnsureCleanDirectory(output);
output.CreateOrCleanDirectory();
Badges.ForCoverage(output, CoverageFiles);
Badges.ForDotNetVersion(output, GlobalJson);
Badges.ForTests(output, TestResultFile);
Expand All @@ -159,4 +161,4 @@ protected override void OnBuildInitialized()
DockerLogger = (_, msg) => Log.Information(msg);
DotNetToolRestore(c => c.DisableProcessLogOutput());
}
}
}
12 changes: 8 additions & 4 deletions build/Helpers/Badges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public static void ForCoverage(AbsolutePath output, AbsolutePath files) =>
.SetReportTypes(ReportTypes.Badges));

public static void ForDotNetVersion(AbsolutePath output, GlobalJson globalJson) =>
DownloadShieldsIo(output / "dotnet_version_badge.svg", ".NET", globalJson.Sdk.Version.ToString(), "blue");
DownloadShieldsIo(output / "dotnet_version_badge.svg", ".NET",
globalJson.Sdk.Version.ToString(), "blue");

public static void ForTests(AbsolutePath output, string resultName)
{
Expand Down Expand Up @@ -44,8 +45,9 @@ public static void ForTests(AbsolutePath output, string resultName)

static void DownloadShieldsIo(AbsolutePath fileName, string label, string message, string color)
{
EnsureExistingParentDirectory(fileName.Parent);
var url = "https://img.shields.io/badge/" + Uri.EscapeDataString($"{label}-{message}-{color}");
fileName.Parent.CreateOrCleanDirectory();
var url = "https://img.shields.io/badge/" +
Uri.EscapeDataString($"{label}-{message}-{color}");
HttpTasks.HttpDownloadFile(url, fileName);
}

Expand All @@ -57,7 +59,9 @@ static TestSummary ExtractResults(AbsolutePath testResult)
?.XPathSelectElement("//*[local-name() = 'Counters']");

var value = (string name) =>
counters is not null && int.TryParse(counters.Attribute(name)?.Value, out var n) ? n : default;
counters is not null && int.TryParse(counters.Attribute(name)?.Value, out var n)
? n
: default;

return new(value("passed"), value("failed"), value("total") - value("executed"));
}
Expand Down
9 changes: 4 additions & 5 deletions build/Helpers/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Reflection;
using JetBrains.Annotations;
using Nuke.Common.Utilities;

namespace Helpers;

[TypeConverter(typeof(TypeConverter<Configuration>))]
public class Configuration : Enumeration
{
Expand All @@ -16,8 +16,7 @@ public record Sdk(Version Version, string RollForward);

public record GlobalJson(Sdk Sdk);

[PublicAPI]
[UsedImplicitly(ImplicitUseKindFlags.Assign)]
[PublicAPI, UsedImplicitly(ImplicitUseKindFlags.Assign)]
public class GlobalJsonAttribute : ParameterAttribute
{
readonly AbsolutePath filePath;
Expand All @@ -31,5 +30,5 @@ public GlobalJsonAttribute()
public override bool List { get; set; }

public override object GetValue(MemberInfo member, object instance)
=> SerializationTasks.JsonDeserializeFromFile<GlobalJson>(filePath);
}
=> filePath.ReadJson<GlobalJson>();
}
6 changes: 6 additions & 0 deletions build/Helpers/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using JetBrains.Annotations;

namespace Helpers;

public static class Extensions
Expand All @@ -8,6 +10,10 @@ public static T LocalTool<T>(this T tool, string localtool = "")
.SetProcessToolPath(DotNetPath)
.SetProcessArgumentConfigurator(args => new Arguments().Add(localtool).Concatenate(args));

[CanBeNull]
public static Project FindProjects(this Solution sln, string name) =>
sln.AllProjects.SingleOrDefault(x => name.Equals(x.Name, StringComparison.Ordinal));

public static ITargetDefinition TryExecutes(
this ITargetDefinition @this,
Action @try,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Amazon.KeyManagementService;
using Amazon.KeyManagementService.Model;
using DotNet.Testcontainers.Builders;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Subdivisions.Clients;
Expand Down

0 comments on commit 3b20300

Please sign in to comment.