forked from aaubry/YamlDotNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.cake
355 lines (296 loc) · 11 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#tool "nuget:?package=xunit.runner.console"
#tool "nuget:?package=Mono.TextTransform"
#tool "nuget:?package=GitVersion.CommandLine"
#tool "nuget:?package=Cake.Incubator"
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml.Linq;
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release-Unsigned");
var buildVerbosity = (Verbosity)Enum.Parse(typeof(Verbosity), Argument("buildVerbosity", "Minimal"), ignoreCase: true);
//////////////////////////////////////////////////////////////////////
// PREPARATION
//////////////////////////////////////////////////////////////////////
var solutionPath = "./YamlDotNet.sln";
var releaseConfigurations = new List<string>
{
"Release-Unsigned",
"Release-Signed"
};
if (!IsRunningOnWindows())
{
// AOT requires mono
releaseConfigurations.Add("Debug-AOT");
}
var packageTypes = new[] { "Unsigned", "Signed" };
var nugetVersion = "0.0.1";
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("Clean")
.Does(() =>
{
CleanDirectories(new[]
{
"./YamlDotNet/bin",
"./YamlDotNet.AotTest/bin",
"./YamlDotNet.Samples/bin",
"./YamlDotNet.Test/bin",
"./YamlDotNet/obj",
"./YamlDotNet.AotTest/obj",
"./YamlDotNet.Samples/obj",
"./YamlDotNet.Test/obj",
});
});
Task("Restore-NuGet-Packages")
.IsDependentOn("Clean")
.Does(() =>
{
NuGetRestore(solutionPath);
});
Task("Set-Build-Version")
.Does(() =>
{
var version = GitVersion(new GitVersionSettings
{
UpdateAssemblyInfo = false,
});
nugetVersion = version.NuGetVersion;
var assemblyInfo = TransformTextFile("YamlDotNet/Properties/AssemblyInfo.template")
.WithToken("assemblyVersion", $"{version.Major}.0.0.0")
.WithToken("assemblyFileVersion", $"{version.MajorMinorPatch}.0")
.WithToken("assemblyInformationalVersion", nugetVersion)
.ToString();
System.IO.File.WriteAllText("YamlDotNet/Properties/AssemblyInfo.cs", assemblyInfo);
if(AppVeyor.IsRunningOnAppVeyor)
{
if (!string.IsNullOrEmpty(version.PreReleaseTag))
{
nugetVersion = string.Format("{0}-{1}{2}", version.MajorMinorPatch, version.PreReleaseLabel, AppVeyor.Environment.Build.Version.Replace("0.0.", "").PadLeft(4, '0'));
}
AppVeyor.UpdateBuildVersion(nugetVersion);
}
});
Task("Build")
.IsDependentOn("Restore-NuGet-Packages")
.Does(() =>
{
BuildSolution(solutionPath, configuration, buildVerbosity);
});
Task("Quick-Build")
.Does(() =>
{
BuildSolution(solutionPath, configuration, buildVerbosity);
});
Task("Test")
.IsDependentOn("Build")
.Does(() =>
{
RunUnitTests(configuration);
});
Task("Build-Release-Configurations")
.IsDependentOn("Restore-NuGet-Packages")
.IsDependentOn("Set-Build-Version")
.Does(() =>
{
foreach(var releaseConfiguration in releaseConfigurations)
{
Information("");
Information("----------------------------------------");
Information("Building {0}", releaseConfiguration);
Information("----------------------------------------");
BuildSolution(solutionPath, releaseConfiguration, buildVerbosity);
}
});
Task("Test-Release-Configurations")
.IsDependentOn("Build-Release-Configurations")
.Does(() =>
{
foreach(var releaseConfiguration in releaseConfigurations)
{
if (releaseConfiguration.EndsWith("-Signed"))
{
Information("Skipping signed builds. Configuration: " + releaseConfiguration);
continue;
}
if (releaseConfiguration.Equals("Debug-AOT"))
{
RunProcess("mono", "--aot=full", "YamlDotNet.AotTest/bin/Debug/YamlDotNet.dll");
RunProcess("mono", "--aot=full", "YamlDotNet.AotTest/bin/Debug/YamlDotNet.AotTest.exe");
RunProcess("mono", "--full-aot", "YamlDotNet.AotTest/bin/Debug/YamlDotNet.AotTest.exe");
}
else
{
RunUnitTests(releaseConfiguration);
}
}
});
Task("Package")
.IsDependentOn("Test-Release-Configurations")
.Does(() =>
{
foreach(var packageType in packageTypes)
{
// Replace directory separator char
var baseNuspecFile = "YamlDotNet/YamlDotNet." + packageType + ".nuspec";
var nuspec = System.IO.File.ReadAllText(baseNuspecFile);
var finalNuspecFile = baseNuspecFile + ".tmp";
nuspec = nuspec.Replace('\\', System.IO.Path.DirectorySeparatorChar);
System.IO.File.WriteAllText(finalNuspecFile, nuspec);
NuGetPack(finalNuspecFile, new NuGetPackSettings
{
Version = nugetVersion,
OutputDirectory = Directory("YamlDotNet/bin"),
});
}
});
Task("Document")
.IsDependentOn("Build")
.Does(() =>
{
var samplesBinDir = "YamlDotNet.Samples/bin/" + configuration;
var testAssemblyFileName = samplesBinDir + "/YamlDotNet.Samples.dll";
var samplesAssembly = Assembly.LoadFrom(testAssemblyFileName);
XUnit2(testAssemblyFileName, new XUnit2Settings
{
OutputDirectory = Directory(samplesBinDir),
XmlReport = true
});
var samples = XDocument.Load(samplesBinDir + "/YamlDotNet.Samples.dll.xml")
.Descendants("test")
.Select(e => new
{
Title = e.Attribute("name").Value,
Type = samplesAssembly.GetType(e.Attribute("type").Value),
Method = e.Attribute("method").Value,
Output = e.Element("output") != null ? e.Element("output").Value : null,
});
var sampleList = new StringBuilder();
foreach (var sample in samples)
{
var fileName = sample.Type.Name;
Information("Generating sample documentation page for {0}", fileName);
var code = System.IO.File.ReadAllText("YamlDotNet.Samples/" + fileName + ".cs");
var sampleAttr = sample.Type
.GetMethod(sample.Method)
.GetCustomAttributes()
.Single(a => a.GetType().Name == "SampleAttribute");
var description = UnIndent((string)sampleAttr.GetType().GetProperty("Description").GetValue(sampleAttr, null));
var samplePage = TransformTextFile("YamlDotNet.Samples/build/SampleTransform.md")
.WithToken("title", sample.Title)
.WithToken("description", description)
.WithToken("code", code)
.WithToken("output", sample.Output)
.ToString();
System.IO.File.WriteAllText("../YamlDotNet.wiki/Samples." + fileName + ".md", samplePage);
sampleList
.AppendFormat("* *[{0}](Samples.{1})* \n", sample.Title, fileName)
.AppendFormat(" {0}\n", description.Replace("\n", "\n "));
}
var sampleIndexPage = TransformTextFile("YamlDotNet.Samples/build/SampleIndexTransform.md")
.WithToken("sampleList", sampleList.ToString())
.ToString();
System.IO.File.WriteAllText("../YamlDotNet.wiki/Samples.md", sampleIndexPage);
});
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("Default")
// .IsDependentOn("Document");
.IsDependentOn("Test");
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);
//////////////////////////////////////////////////////////////////////
// HELPERS
//////////////////////////////////////////////////////////////////////
string UnIndent(string text)
{
var lines = text
.Split('\n')
.Select(l => l.TrimEnd('\r', '\n'))
.SkipWhile(l => l.Trim(' ', '\t').Length == 0)
.ToList();
while (lines.Count > 0 && lines[lines.Count - 1].Trim(' ', '\t').Length == 0)
{
lines.RemoveAt(lines.Count - 1);
}
if (lines.Count > 0)
{
var indent = Regex.Match(lines[0], @"^(\s*)");
if (!indent.Success)
{
throw new ArgumentException("Invalid indentation");
}
lines = lines
.Select(l => l.Substring(indent.Groups[1].Length))
.ToList();
}
return string.Join("\n", lines.ToArray());
}
void BuildSolution(string solutionPath, string configuration, Verbosity verbosity)
{
const string appVeyorLogger = @"""C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll""";
MSBuild(solutionPath, settings =>
{
if (System.IO.File.Exists(appVeyorLogger)) settings.WithLogger(appVeyorLogger);
if(IsRunningOnUnix())
{
settings.ToolPath = "/usr/bin/msbuild";
}
settings
.SetVerbosity(verbosity)
.SetConfiguration(configuration)
.WithProperty("Version", nugetVersion);
});
}
void RunProcess(string processName, params string[] arguments)
{
var exitCode = StartProcess(processName, new ProcessSettings().WithArguments(a =>
{
foreach (var argument in arguments)
{
a.Append(argument);
}
}));
if (exitCode != 0)
{
throw new Exception(string.Format("{0} failed with exit code {1}", processName, exitCode));
}
}
void RunUnitTests(string configurationName)
{
if (configurationName.Contains("DotNetStandard"))
{
// Execute .NETCoreApp tests using `dotnet test`.
var settings = new DotNetCoreTestSettings
{
Framework = "netcoreapp1.0",
Configuration = configurationName,
NoBuild = true
};
// if (AppVeyor.IsRunningOnAppVeyor)
// {
// settings.ArgumentCustomization = args => args.Append("-appveyor");
// }
var path = MakeAbsolute(File("./YamlDotNet.Test/YamlDotNet.Test.csproj"));
DotNetCoreTest(path.FullPath, settings);
}
else
{
// Execute the full framework tests using xunit.console.runner.
// var settings = new XUnit2Settings
// {
// Parallelism = ParallelismOption.All
// };
// if (AppVeyor.IsRunningOnAppVeyor)
// {
// settings.ArgumentCustomization = args => args.Append("-appveyor");
// }
XUnit2("YamlDotNet.Test/bin/" + configurationName + "/net452/YamlDotNet.Test*.dll");
}
}