-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuild.cs
More file actions
182 lines (160 loc) · 6.88 KB
/
Build.cs
File metadata and controls
182 lines (160 loc) · 6.88 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// ReSharper disable RedundantUsingDirective
using System;
using System.IO;
using System.Linq;
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.Tooling;
using Nuke.Common.Tools.DotNet;
using Nuke.Common.Tools.ILRepack;
using Nuke.Common.Tools.OctoVersion;
using Nuke.Common.Utilities.Collections;
using Serilog;
using static Nuke.Common.IO.FileSystemTasks;
using static Nuke.Common.Tools.DotNet.DotNetTasks;
using static Nuke.Common.Tools.SignTool.SignToolTasks;
[UnsetVisualStudioEnvironmentVariables]
[VerbosityMapping(typeof(DotNetVerbosity), Verbose = nameof(DotNetVerbosity.diagnostic))]
class Build : NukeBuild
{
/// 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
[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;
[Solution(GenerateProjects = true)] 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;
[Parameter(
"Branch name for OctoVersion to use to calculate the version number. Can be set via the environment variable OCTOVERSION_CurrentBranch.",
Name = "OCTOVERSION_CurrentBranch")]
readonly string BranchName;
[OctoVersion(BranchMember = nameof(BranchName),
AutoDetectBranchMember = nameof(AutoDetectBranch), Framework = "net8.0")]
public OctoVersionInfo OctoVersionInfo;
AbsolutePath SourceDirectory => RootDirectory / "source";
AbsolutePath ArtifactsDirectory => RootDirectory / "artifacts";
AbsolutePath LocalPackagesDirectory => RootDirectory / ".." / "LocalPackages";
AbsolutePath OctopusCommandLineFolder => SourceDirectory / "CommandLine";
Target Clean => _ => _
.Executes(() =>
{
SourceDirectory.GlobDirectories("**/bin", "**/obj").ForEach(DeleteDirectory);
EnsureCleanDirectory(ArtifactsDirectory);
});
Target Restore => _ => _
.DependsOn(Clean)
.Executes(() =>
{
DotNetRestore(_ => _
.SetProjectFile(Solution));
});
Target Compile => _ => _
.DependsOn(Restore)
.Executes(() =>
{
DotNetBuild(_ => _
.SetProjectFile(Solution)
.SetConfiguration(Configuration)
.SetVersion(OctoVersionInfo.FullSemVer)
.SetInformationalVersion(OctoVersionInfo.InformationalVersion)
.EnableNoRestore());
});
[PublicAPI]
Target Test => _ => _
.DependsOn(Compile)
.Executes(() =>
{
DotNetTest(_ => _
.SetProjectFile(Solution)
.SetConfiguration(Configuration)
.EnableNoBuild()
.EnableNoRestore());
});
Target Merge => _ => _
.DependsOn(Compile)
.Executes(() =>
{
var targets = Solution.CommandLine.GetTargetFrameworks();
foreach (var target in targets)
{
var inputFolder = OctopusCommandLineFolder / "bin" / Configuration / target;
var outputFolder = OctopusCommandLineFolder / "bin" / Configuration / $"{target}-Merged";
EnsureExistingDirectory(outputFolder);
// The call to ILRepack with .EnableInternalize() requires the Octopus.CommandLine.dll assembly to be first in the list.
var inputAssemblies = inputFolder.GlobFiles("NewtonSoft.Json.dll", "Octopus.*.dll")
.Select(x => x.ToString())
.OrderByDescending(x => x.Contains("Octopus.CommandLine.dll"))
.ThenBy(x => x)
.ToArray();
//note: ilmerge requires CopyLocalLockFileAssemblies set to true in the csproj
ILRepackTasks.ILRepack(_ => _
.SetAssemblies(inputAssemblies)
.SetOutput(outputFolder / "Octopus.CommandLine.dll")
.EnableInternalize()
.DisableParallel()
.EnableXmldocs()
.SetLib(inputFolder)
);
DeleteDirectory(inputFolder);
MoveDirectory(outputFolder, inputFolder);
}
});
Target Pack => _ => _
.DependsOn(Merge)
.Executes(() =>
{
var nuspec = "Octopus.CommandLine.nuspec";
var nuspecPath = OctopusCommandLineFolder / nuspec;
try
{
ReplaceTextInFiles(nuspecPath, "<version>$version$</version>", $"<version>{OctoVersionInfo.FullSemVer}</version>");
ReplaceTextInFiles(nuspecPath, "\\$configuration$\\", $"\\{Configuration.ToString()}\\");
DotNetPack(_ => _
.SetProject(Solution)
.SetProcessArgumentConfigurator(args =>
{
args.Add("/p:NuspecFile=" + nuspec);
return args;
})
.SetVersion(OctoVersionInfo.FullSemVer)
.SetConfiguration(Configuration)
.SetOutputDirectory(ArtifactsDirectory)
.EnableNoBuild()
.DisableIncludeSymbols()
.SetVerbosity(DotNetVerbosity.normal)
);
}
finally
{
ReplaceTextInFiles(nuspecPath, $"<version>{OctoVersionInfo.FullSemVer}</version>", "<version>$version$</version>");
ReplaceTextInFiles(nuspecPath, $"\\{Configuration.ToString()}\\", "\\$configuration$\\");
}
static void ReplaceTextInFiles(AbsolutePath path, string oldValue, string newValue)
{
var fileText = File.ReadAllText(path);
fileText = fileText.Replace(oldValue, newValue);
File.WriteAllText(path, fileText);
}
});
Target CopyToLocalPackages => _ => _
.OnlyWhenStatic(() => IsLocalBuild)
.TriggeredBy(Pack)
.Executes(() =>
{
EnsureExistingDirectory(LocalPackagesDirectory);
ArtifactsDirectory.GlobFiles("*.nupkg")
.ForEach(package => CopyFileToDirectory(package, LocalPackagesDirectory, FileExistsPolicy.Overwrite));
});
Target Default => _ => _
.DependsOn(Pack)
.DependsOn(CopyToLocalPackages);
public static int Main() => Execute<Build>(x => x.Default);
}