forked from OctopusDeploy/OctopusClients
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
334 lines (289 loc) · 10.6 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
//////////////////////////////////////////////////////////////////////
// TOOLS
//////////////////////////////////////////////////////////////////////
#tool "nuget:?package=GitVersion.CommandLine&version=4.0.0-beta0007"
#tool "nuget:?package=ILRepack&version=2.0.11"
#addin "nuget:?package=Newtonsoft.Json&version=9.0.1"
#addin "nuget:?package=SharpCompress&version=0.12.4"
using Path = System.IO.Path;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using IO = System.IO;
using SharpCompress;
using SharpCompress.Common;
using SharpCompress.Writer;
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
///////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
///////////////////////////////////////////////////////////////////////////////
var publishDir = "./publish";
var artifactsDir = "./artifacts";
var assetDir = "./BuildAssets";
var localPackagesDir = "../LocalPackages";
var globalAssemblyFile = "./source/Octo/Properties/AssemblyInfo.cs";
var projectToPublish = "./source/Octo";
var projectToPublishProjectJson = Path.Combine(projectToPublish, "project.json");
var octopusClientFolder = "./source/Octopus.Client";
var isContinuousIntegrationBuild = !BuildSystem.IsLocalBuild;
var octoPublishFolder = Path.Combine(publishDir, "Octo");
var octoMergedFolder = Path.Combine(publishDir, "OctoMerged");
var cleanups = new List<Action>();
var gitVersionInfo = GitVersion(new GitVersionSettings {
OutputType = GitVersionOutput.Json
});
var nugetVersion = gitVersionInfo.NuGetVersion;
var runtimes = new[] {
"win7-x86",
"osx.10.10-x64",
"ubuntu.14.04-x64",
"ubuntu.16.04-x64",
"rhel.7-x64",
"debian.8-x64",
"fedora.23-x64",
"opensuse.13.2-x64",
"linuxmint.17-x64",
};
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(context =>
{
if(BuildSystem.IsRunningOnTeamCity)
BuildSystem.TeamCity.SetBuildNumber(gitVersionInfo.NuGetVersion);
if(BuildSystem.IsRunningOnAppVeyor)
AppVeyor.UpdateBuildVersion(gitVersionInfo.NuGetVersion);
Information("Building OctopusClients v{0}", nugetVersion);
});
Teardown(context =>
{
Information("Cleaning up");
foreach(var cleanup in cleanups)
cleanup();
Information("Finished running tasks for build v{0}", nugetVersion);
});
//////////////////////////////////////////////////////////////////////
// PRIVATE TASKS
//////////////////////////////////////////////////////////////////////
Task("__Default")
.IsDependentOn("__Clean")
.IsDependentOn("__Restore")
.IsDependentOn("__Build")
.IsDependentOn("__Test")
.IsDependentOn("__UpdateProjectJsonVersion")
.IsDependentOn("__Publish")
.IsDependentOn("__Zip")
.IsDependentOn("__PackNuget")
.IsDependentOn("__CopyToLocalPackages");
Task("__Clean")
.Does(() =>
{
CleanDirectory(artifactsDir);
CleanDirectory(publishDir);
CleanDirectories("./source/**/bin");
CleanDirectories("./source/**/obj");
});
Task("__Restore")
.Does(() => DotNetCoreRestore());
Task("__Build")
.IsDependentOn("__UpdateProjectJsonVersion")
.Does(() =>
{
DotNetCoreBuild("**/project.json", new DotNetCoreBuildSettings
{
Configuration = configuration
});
});
Task("__Test")
.Does(() =>
{
GetFiles("**/*Tests/project.json")
.ToList()
.ForEach(testProjectFile =>
{
DotNetCoreTest(testProjectFile.ToString(), new DotNetCoreTestSettings
{
Configuration = configuration,
WorkingDirectory = Path.GetDirectoryName(testProjectFile.ToString())
});
});
});
Task("__UpdateProjectJsonVersion")
.Does(() =>
{
foreach(var projectJson in GetFiles("**/project.json").Select(p => p.FullPath))
{
RestoreFileOnCleanup(projectJson);
Information("Updating {0} version -> {1}", projectJson, nugetVersion);
ModifyJson(projectJson, json => json["version"] = nugetVersion);
}
});
Task("__Publish")
.Does(() =>
{
DotNetCorePublish(projectToPublish, new DotNetCorePublishSettings
{
Framework = "net45",
Configuration = configuration,
OutputDirectory = $"{octoPublishFolder}/netfx"
});
var portablePublishDir = Path.Combine(octoPublishFolder, "portable");
DotNetCorePublish(projectToPublish, new DotNetCorePublishSettings
{
Framework = "netcoreapp1.0",
Configuration = configuration,
OutputDirectory = portablePublishDir
});
CopyFileToDirectory(Path.Combine(assetDir, "Octo"), portablePublishDir);
CopyFileToDirectory(Path.Combine(assetDir, "Octo.cmd"), portablePublishDir);
ConvertToJsonOutput(projectToPublishProjectJson);
DotNetCoreRestore();
foreach(var runtime in runtimes)
DotNetCorePublish(projectToPublish, new DotNetCorePublishSettings
{
Framework = "netcoreapp1.0",
Configuration = configuration,
Runtime = runtime,
OutputDirectory = Path.Combine(octoPublishFolder, runtime)
});
});
private void ConvertToJsonOutput(string projectJson)
{
ModifyJson(projectJson, json => {
var deps = (JObject)json["frameworks"]["netcoreapp1.0"]["dependencies"];
deps.Remove("Microsoft.NETCore.App");
deps["Microsoft.NETCore.Runtime.CoreCLR"] = new JValue("1.0.4");
deps["Microsoft.NETCore.DotNetHostPolicy"] = new JValue("1.0.1");
json["runtimes"] = new JObject();
foreach (var runtime in runtimes)
json["runtimes"][runtime] = new JObject();
});
}
private void ModifyJson(string jsonFile, Action<JObject> modify)
{
var json = JsonConvert.DeserializeObject<JObject>(IO.File.ReadAllText(jsonFile));
modify(json);
IO.File.WriteAllText(jsonFile, JsonConvert.SerializeObject(json, Formatting.Indented));
}
private void RestoreFileOnCleanup(string file)
{
var contents = System.IO.File.ReadAllBytes(file);
cleanups.Add(() => {
Information("Restoring {0}", file);
System.IO.File.WriteAllBytes(file, contents);
});
}
private void TarGzip(string path, string outputFile)
{
var outFile = $"{outputFile}.tar.gz";
Information("Creating TGZ file {0} from {1}", outFile, path);
using (var tarMemStream = new MemoryStream())
{
using (var tar = WriterFactory.Open(tarMemStream, ArchiveType.Tar, CompressionType.None, true))
{
tar.WriteAll(path, "*", SearchOption.AllDirectories);
}
tarMemStream.Seek(0, SeekOrigin.Begin);
using (Stream stream = IO.File.Open(outFile, FileMode.Create))
using (var zip = WriterFactory.Open(stream, ArchiveType.GZip, CompressionType.GZip))
zip.Write($"{outputFile}.tar", tarMemStream);
}
Information("Successfully created TGZ file: {0}", outFile);
}
Task("__MergeOctoExe")
.Does(() => {
var inputFolder = $"{octoPublishFolder}/netfx";
var outputFolder = $"{octoPublishFolder}/netfx-merged";
CreateDirectory(outputFolder);
ILRepack(
$"{outputFolder}/Octo.exe",
$"{inputFolder}/Octo.exe",
IO.Directory.EnumerateFiles(inputFolder, "*.dll").Select(f => (FilePath) f),
new ILRepackSettings {
Internalize = true,
Libs = new List<FilePath>() { inputFolder }
}
);
});
Task("__Zip")
.IsDependentOn("__MergeOctoExe")
.IsDependentOn("__Publish")
.Does(() => {
foreach(var dir in IO.Directory.EnumerateDirectories(octoPublishFolder))
{
var dirName = Path.GetFileName(dir);
if(dirName == "netfx")
continue;
if(dirName == "netfx-merged")
{
Zip(dir, $"{artifactsDir}/OctopusTools.{nugetVersion}.zip");
}
else
{
var outFile = $"{artifactsDir}/OctopusTools.{nugetVersion}.{dirName}";
if(dirName == "portable" || dirName.Contains("win"))
Zip(dir, outFile + ".zip");
if(!dirName.Contains("win"))
TarGzip(dir, outFile);
}
}
});
Task("__PackNuget")
.IsDependentOn("__PackClientNuget")
.IsDependentOn("__MergeOctoExe")
.IsDependentOn("__Publish")
.IsDependentOn("__PackOctopusToolsNuget");
Task("__PackClientNuget")
.Does(() => {
var inputFolder = $"{octopusClientFolder}/bin/{configuration}/net45";
var outputFolder = $"{octopusClientFolder}/bin/{configuration}/net45Merged";
CreateDirectory(outputFolder);
ILRepack(
$"{outputFolder}/Octopus.Client.dll",
$"{inputFolder}/Octopus.Client.dll",
IO.Directory.EnumerateFiles(inputFolder, "*.dll").Select(f => (FilePath) f),
new ILRepackSettings {
Internalize = true,
Libs = new List<FilePath>() { inputFolder }
}
);
DeleteDirectory(inputFolder, true);
MoveDirectory(outputFolder, inputFolder);
DotNetCorePack(octopusClientFolder, new DotNetCorePackSettings {
Configuration = configuration,
OutputDirectory = artifactsDir,
NoBuild = true
});
});
Task("__PackOctopusToolsNuget")
.Does(() => {
var nugetPackDir = Path.Combine(publishDir, "nuget");
var nuspecFile = "OctopusTools.nuspec";
CopyDirectory($"{octoPublishFolder}/netfx-merged", nugetPackDir);
CopyFileToDirectory(Path.Combine(assetDir, "init.ps1"), nugetPackDir);
CopyFileToDirectory(Path.Combine(assetDir, nuspecFile), nugetPackDir);
NuGetPack(Path.Combine(nugetPackDir, nuspecFile), new NuGetPackSettings {
Version = nugetVersion,
OutputDirectory = artifactsDir
});
});
Task("__CopyToLocalPackages")
.WithCriteria(BuildSystem.IsLocalBuild)
.IsDependentOn("__PackClientNuget")
.Does(() =>
{
CreateDirectory(localPackagesDir);
CopyFileToDirectory(Path.Combine(artifactsDir, $"Octopus.Client.{nugetVersion}.nupkg"), localPackagesDir);
});
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("__Default");
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);