-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBuild.cs
More file actions
136 lines (115 loc) · 4.9 KB
/
Build.cs
File metadata and controls
136 lines (115 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// ReSharper disable RedundantUsingDirective - prevent PrettyBot from getting confused about unused code.
using System;
using System.IO;
using JetBrains.Annotations;
using Nuke.Common;
using Nuke.Common.CI;
using Nuke.Common.Execution;
using Nuke.Common.IO;
using Nuke.Common.ProjectModel;
using Nuke.Common.Tools.DotNet;
using Nuke.Common.Tools.OctoVersion;
using Nuke.Common.Utilities.Collections;
using static Nuke.Common.IO.PathConstruction;
using static Nuke.Common.Tools.DotNet.DotNetTasks;
// The nuke convention is to use underscores for target lambda's
// ReSharper disable AllUnderscoreLocalParameterName
[ShutdownDotNetAfterServerBuild]
class Build : NukeBuild
{
const string CiBranchNameEnvVariable = "OCTOVERSION_CurrentBranch";
[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")] readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;
[Parameter("Test filter expression", Name = "where")] readonly string TestFilter = string.Empty;
[Solution] readonly Solution Solution;
[Parameter("Whether to auto-detect the branch name - this is okay for a local build, but should not be used under CI.")] readonly bool AutoDetectBranch = IsLocalBuild;
[OctoVersion(BranchMember = nameof(BranchName), AutoDetectBranchMember = nameof(AutoDetectBranch), Framework = "net8.0")]
public OctoVersionInfo OctoVersionInfo;
[Parameter("Branch name for OctoVersion to use to calculate the version number. Can be set via the environment variable " + CiBranchNameEnvVariable + ".", Name = CiBranchNameEnvVariable)]
string BranchName { get; set; }
AbsolutePath SourceDirectory => RootDirectory / "source";
AbsolutePath ArtifactsDirectory => RootDirectory / "artifacts";
AbsolutePath LocalPackagesDirectory => RootDirectory / ".." / "LocalPackages";
string FullSemVer => OctoVersionInfo.FullSemVer.Length <= 35
? OctoVersionInfo.FullSemVer
: OctoVersionInfo.FullSemVer[..35].TrimEnd('.');
Target Clean => _ => _
.Before(Restore)
.Executes(() =>
{
OverrideGitHubSemVer();
SourceDirectory.GlobDirectories("**/bin", "**/obj", "**/TestResults").ForEach(x => x.DeleteDirectory());
ArtifactsDirectory.CreateOrCleanDirectory();
LocalPackagesDirectory.CreateOrCleanDirectory();
});
Target Restore => _ => _
.DependsOn(Clean)
.Executes(() =>
{
DotNetRestore(s => s
.SetProjectFile(Solution));
});
Target Compile => _ => _
.DependsOn(Restore)
.Executes(() =>
{
DotNetBuild(s => s
.SetProjectFile(Solution)
.SetConfiguration(Configuration)
.EnableNoRestore()
.SetVersion(FullSemVer)
.SetInformationalVersion(OctoVersionInfo.InformationalVersion));
});
Target Test => _ => _
.DependsOn(Compile)
.Executes(() =>
{
DotNetTest(_ => _
.SetProjectFile(Solution)
.SetConfiguration(Configuration)
.SetLoggers("trx")
.SetVerbosity(DotNetVerbosity.normal)
.SetFilter(TestFilter)
.EnableNoBuild()
.EnableNoRestore());
foreach (var file in SourceDirectory.GlobFiles("**/*.trx"))
file.CopyToDirectory(ArtifactsDirectory);
});
Target Pack => _ => _
.DependsOn(Compile)
.DependsOn(Test)
.Executes(() =>
{
DotNetPack(_ => _
.SetProject(Solution)
.SetConfiguration(Configuration)
.SetOutputDirectory(ArtifactsDirectory)
.EnableNoBuild()
.AddProperty("Version", FullSemVer)
);
});
[PublicAPI]
Target CopyToLocalPackages => _ => _
.OnlyWhenStatic(() => IsLocalBuild)
.TriggeredBy(Pack)
.Executes(() =>
{
LocalPackagesDirectory.CreateOrCleanDirectory();
foreach (var file in ArtifactsDirectory.GlobFiles($"*.{FullSemVer}.nupkg"))
file.CopyToDirectory(LocalPackagesDirectory);
});
void OverrideGitHubSemVer()
{
var gitHubSetEnvFilePath = Environment.GetEnvironmentVariable("GITHUB_ENV");
if (gitHubSetEnvFilePath != null)
{
using var streamWriter = File.AppendText(gitHubSetEnvFilePath);
streamWriter.WriteLine($"OCTOVERSION_FullSemVer={FullSemVer}");
}
}
/// Support plugins are available for:
/// - JetBrains ReSharper https://nuke.build/resharper
/// - JetBrains Rider https://nuke.build/rider
/// - Microsoft VisualStudio https://nuke.build/visualstudio
/// - Microsoft VSCode https://nuke.build/vscode
public static int Main() => Execute<Build>(x => x.Pack);
}