client-dotnet/build/Build.cs

130 lines
5.1 KiB
C#
Raw Normal View History

using System;
2021-02-22 12:54:22 +00:00
using JetBrains.Annotations;
2022-01-27 10:03:21 +00:00
using NuGet.Versioning;
using Nuke.Common;
using Nuke.Common.CI;
2021-02-22 12:54:22 +00:00
using Nuke.Common.CI.GitLab;
using Nuke.Common.Execution;
using Nuke.Common.Git;
using Nuke.Common.IO;
using Nuke.Common.ProjectModel;
using Nuke.Common.Tooling;
using Nuke.Common.Tools.DotNet;
using Nuke.Common.Tools.GitVersion;
using Nuke.Common.Utilities.Collections;
using static Nuke.Common.IO.FileSystemTasks;
using static Nuke.Common.Tools.DotNet.DotNetTasks;
[CheckBuildProjectConfigurations]
[ShutdownDotNetAfterServerBuild]
class Build : NukeBuild
{
2021-02-22 14:10:34 +00:00
private const string NuGetSourceName = "GitLab";
public static int Main() => Execute<Build>(x => x.Test);
[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;
[Parameter("Username to use for publishing NuGet packages", Name = "nuget-username")]
2022-01-27 10:03:21 +00:00
string NuGetUsername { get; set; } = Environment.GetEnvironmentVariable("NUGET_USERNAME") ?? string.Empty;
[Parameter("Password to use for publishing NuGet packages", Name = "nuget-password")]
2022-01-27 10:03:21 +00:00
string NuGetPassword { get; set; } = Environment.GetEnvironmentVariable("NUGET_PASSWORD") ?? string.Empty;
2022-01-27 10:03:21 +00:00
[Solution] readonly Solution? Solution;
[GitRepository] readonly GitRepository? GitRepository;
[GitVersion(NoFetch = true, Framework = "net6.0")] readonly GitVersion? GitVersion;
2021-02-22 14:10:34 +00:00
2022-01-27 10:03:21 +00:00
GitLab? CI => GitLab.Instance;
2021-02-22 12:54:22 +00:00
AbsolutePath SourceDirectory => RootDirectory / "src";
AbsolutePath TestsDirectory => RootDirectory / "tests";
AbsolutePath ArtifactsDirectory => RootDirectory / "artifacts";
2021-02-22 14:10:34 +00:00
Target Clean => _ => _
.Before(Restore)
.Executes(() =>
{
SourceDirectory.GlobDirectories("**/bin", "**/obj").ForEach(DeleteDirectory);
TestsDirectory.GlobDirectories("**/bin", "**/obj").ForEach(DeleteDirectory);
EnsureCleanDirectory(ArtifactsDirectory);
});
Target Restore => _ => _
.Executes(() =>
{
DotNetRestore(s => s
.SetProjectFile(Solution));
});
Target Compile => _ => _
.DependsOn(Restore)
.Executes(() =>
{
2022-01-27 10:03:21 +00:00
var assemblyVersion = GitVersion?.AssemblySemVer ??
SemanticVersion.Parse(CI?.CommitTag?.TrimStart('v') ?? "0.0.1").ToNormalizedString();
var informationalVersion = GitVersion?.InformationalVersion ??
SemanticVersion.Parse(CI?.CommitTag?.TrimStart('v') ?? "0.0.1").ToFullString();
DotNetBuild(s => s
.SetProjectFile(Solution)
.SetConfiguration(Configuration)
2022-01-27 10:03:21 +00:00
.SetAssemblyVersion(assemblyVersion)
.SetFileVersion(assemblyVersion)
.SetInformationalVersion(informationalVersion)
.EnableNoRestore());
});
Target Format => _ => _
.DependsOn(Restore)
.Executes(() => DotNet(CI == null ? "format --no-restore" : "format --no-restore --verify-no-changes"));
Target Test => _ => _
.DependsOn(Compile, Format)
.Executes(() => DotNetTest(s => s
.SetProjectFile(Solution)
.SetConfiguration(Configuration)
.EnableNoRestore()
.EnableNoBuild()
.EnableProcessLogOutput()));
Target Pack => _ => _
.DependsOn(Compile)
2022-01-27 10:03:21 +00:00
.OnlyWhenStatic(() => CI != null && !string.IsNullOrEmpty(CI.CommitTag))
.Executes(() => SourceDirectory
.GlobFiles("**/*.csproj")
.ForEach(csproj => DotNetPack(s => s
.SetProject(csproj)
.SetConfiguration(Configuration)
.SetOutputDirectory(ArtifactsDirectory)
2022-01-27 10:03:21 +00:00
.SetVersion(GitVersion?.FullSemVer ?? SemanticVersion.Parse(CI?.CommitTag?.TrimStart('v') ?? "0.0.1").ToNormalizedString())
.EnableIncludeSource()
.EnableIncludeSymbols()
.EnableNoRestore()
.EnableNoBuild()
.EnableProcessLogOutput()
)));
2021-02-22 14:10:34 +00:00
2022-01-27 10:03:21 +00:00
Target AddNugetSource => _ => _
.OnlyWhenStatic(() => CI != null && !string.IsNullOrEmpty(CI.CommitTag))
.OnlyWhenStatic(() => !string.IsNullOrEmpty(NuGetUsername) && !string.IsNullOrEmpty(NuGetPassword))
.ProceedAfterFailure()
.Executes(() => DotNetNuGetAddSource(s => s
.SetName(NuGetSourceName)
.SetSource($"https://gitlab.com/api/v4/projects/{CI?.ProjectId}/packages/nuget/index.json")
.SetUsername(NuGetUsername)
.SetPassword(NuGetPassword)
.EnableStorePasswordInClearText()));
2021-02-22 15:26:04 +00:00
Target NuGetPush => _ => _
2021-02-22 14:10:34 +00:00
.DependsOn(Pack, AddNugetSource)
2022-01-27 10:03:21 +00:00
.OnlyWhenStatic(() => CI != null && !string.IsNullOrEmpty(CI.CommitTag))
2021-02-22 15:26:04 +00:00
.Executes(() => ArtifactsDirectory
.GlobFiles("**/*.nupkg")
.ForEach(nupkg => DotNetNuGetPush(s => s
.SetSource(NuGetSourceName)
.SetTargetPath(nupkg)
.EnableProcessLogOutput())));
}