-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild.cake
237 lines (197 loc) · 6.87 KB
/
build.cake
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#tool dotnet:?package=GitVersion.Tool&version=5.12.0
#addin nuget:?package=Cake.Incubator&version=8.0.0
#r Newtonsoft.Json
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var assemblyVersion = "1.0.0";
var packageVersion = "1.0.0";
var artifactsDir = MakeAbsolute(Directory("artifacts"));
var packagesDir = artifactsDir.Combine(Directory("packages"));
var testResultsDir = artifactsDir.Combine(Directory("test-results"));
var solutionPath = "./build.sln";
var testProjects = new List<TestProject>();
Task("Clean")
.Does(() =>
{
CleanDirectory(artifactsDir);
var settings = new DotNetCleanSettings
{
Configuration = configuration
};
DotNetClean(solutionPath, settings);
DeleteFiles("./**/*.trx");
});
Task("Restore")
.IsDependentOn("Clean")
.Does(() =>
{
DotNetRestore();
});
Task("Format")
.IsDependentOn("Restore")
.Does(() =>
{
var settings = new DotNetFormatSettings
{
NoRestore = true,
VerifyNoChanges = true
};
DotNetFormat(solutionPath, settings);
});
Task("SemVer")
.IsDependentOn("Format")
.Does(() =>
{
var gitVersionSettings = new GitVersionSettings
{
NoFetch = true,
};
var gitVersion = GitVersion(gitVersionSettings);
assemblyVersion = gitVersion.AssemblySemVer;
packageVersion = gitVersion.NuGetVersion;
Information($"AssemblySemVer: {assemblyVersion}");
Information($"NuGetVersion: {packageVersion}");
});
Task("SetGitHubVersion")
.IsDependentOn("Semver")
.WithCriteria(() => GitHubActions.IsRunningOnGitHubActions)
.Does(() =>
{
var gitHubEnvironmentFile = Environment.GetEnvironmentVariable("GITHUB_ENV");
var packageVersionEnvironmentVariable = $"PACKAGE_VERSION={packageVersion}";
System.IO.File.WriteAllText(gitHubEnvironmentFile, packageVersionEnvironmentVariable);
});
Task("Build")
.IsDependentOn("SetGitHubVersion")
.Does(() =>
{
var settings = new DotNetBuildSettings
{
Configuration = configuration,
NoIncremental = true,
NoRestore = true,
MSBuildSettings = new DotNetMSBuildSettings()
.SetVersion(assemblyVersion)
.WithProperty("FileVersion", packageVersion)
.WithProperty("InformationalVersion", packageVersion)
.WithProperty("nowarn", "7035")
};
if (IsRunningOnLinuxOrDarwin())
{
settings.Framework = "netstandard2.0";
GetFiles("./src/*/*.csproj")
.ToList()
.ForEach(f => DotNetBuild(f.FullPath, settings));
settings.Framework = "net8.0";
GetFiles("./tests/*/*Tests.csproj")
.ToList()
.ForEach(f => DotNetBuild(f.FullPath, settings));
}
else
{
DotNetBuild(solutionPath, settings);
}
});
Task("ListTestProjectsAndFrameworkVersions")
.IsDependentOn("Build")
.DoesForEach(GetFiles("./tests/*/*Tests.csproj"), (testProject) =>
{
var parsedProject = ParseProject(testProject.FullPath, configuration: configuration);
parsedProject.TargetFrameworkVersions.ToList().ForEach(frameworkVersion =>
{
if (IsRunningOnLinuxOrDarwin() && frameworkVersion != "net8.0")
{
Information($"Skipping test project '{parsedProject.AssemblyName}' framework '{frameworkVersion}' as we're not running on Windows");
return;
}
var projectToTest = new TestProject
{
FullPath = testProject.FullPath,
AssemblyName = parsedProject.AssemblyName,
FrameworkVersion = frameworkVersion
};
testProjects.Add(projectToTest);
});
});
Task("Test")
.IsDependentOn("ListTestProjectsAndFrameworkVersions")
.DoesForEach(() => testProjects, (testProject) =>
{
var settings = new DotNetTestSettings
{
Configuration = configuration,
NoBuild = true,
Framework = testProject.FrameworkVersion
};
if (IsRunningOnCircleCI() || AzurePipelines.IsRunningOnAzurePipelines)
{
var jUnitTestResultsFile = testResultsDir
.Combine("junit")
.Combine("{assembly}.{framework}.xml");
settings.Loggers.Add($"\"junit;LogFilePath={jUnitTestResultsFile}\"");
}
if (GitHubActions.IsRunningOnGitHubActions)
{
var trxTestResultsFile = testResultsDir
.Combine("trx")
.Combine($"{testProject.AssemblyName}.{testProject.FrameworkVersion}.trx");
settings.Loggers.Add($"\"trx;LogFileName={trxTestResultsFile}\"");
settings.Loggers.Add($"\"GitHubActions;annotations.titleFormat=$test@{testProject.FrameworkVersion}\"");
}
DotNetTest(testProject.FullPath, settings);
})
.DeferOnError();
Task("Pack")
.IsDependentOn("Test")
.WithCriteria(() => HasArgument("pack"))
.Does(() =>
{
var settings = new DotNetPackSettings
{
Configuration = configuration,
NoBuild = true,
NoRestore = true,
IncludeSymbols = true,
OutputDirectory = packagesDir,
MSBuildSettings = new DotNetMSBuildSettings()
.WithProperty("PackageVersion", packageVersion)
.WithProperty("Copyright", $"Copyright Contoso {DateTime.Now.Year}")
};
if (IsRunningOnLinuxOrDarwin())
{
settings.MSBuildSettings.WithProperty("TargetFrameworks", "netstandard2.0");
}
GetFiles("./src/*/*.csproj")
.ToList()
.ForEach(f => DotNetPack(f.FullPath, settings));
});
Task("PublishAppVeyorArtifacts")
.IsDependentOn("Pack")
.WithCriteria(() => HasArgument("pack") && AppVeyor.IsRunningOnAppVeyor)
.Does(() =>
{
CopyFiles($"{packagesDir}/*nupkg", MakeAbsolute(Directory("./")), false);
GetFiles($"./*nupkg")
.ToList()
.ForEach(f => AppVeyor.UploadArtifact(f, new AppVeyorUploadArtifactsSettings { DeploymentName = "packages" }));
});
Task("Default")
.IsDependentOn("PublishAppVeyorArtifacts");
RunTarget(target);
/// <summary>
/// - No .NET 4.6.x Framework installed, only .NET 6.x
/// </summary>
private bool IsRunningOnLinuxOrDarwin()
{
return Context.Environment.Platform.IsUnix();
}
private bool IsRunningOnCircleCI()
{
return !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("CIRCLECI"));
}
class TestProject
{
public string FullPath { get; set; }
public string AssemblyName { get; set; }
public string FrameworkVersion { get; set; }
}