-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.cake
353 lines (296 loc) · 11.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#addin "nuget:?package=Cake.FileHelpers&version=1.0.4"
#l "cake/utilities.cake"
using System.Text.RegularExpressions;
using YamlDotNet.Serialization;
public sealed class BuildConfiguration
{
[YamlMember(Alias = "ksp_dir")]
public string KspDir { get; set; }
[YamlMember(Alias = "ksp_bin")]
public string KspBin { get; set; }
public string KspPath(params string[] paths)
{
return KspDir == null ? null : System.IO.Path.Combine(KspDir, System.IO.Path.Combine(paths));
}
}
public sealed class Globals
{
public string ModIdentifier { get; set; }
public string ModName { get; set; }
public string ModCopyrightDates { get; set; }
public string[] KspLibs { get; set; }
public string Target { get; set; }
public string Configuration { get; set; }
public BuildConfiguration BuildConfiguration { get; set; }
public SemVer BuildVersion { get; set; }
public FilePath Solution { get; set; }
public DirectoryPath RootDirectory { get; set; }
public DirectoryPath BuildDirectory { get; set; }
public DirectoryPath BuildLibDirectory { get; set; }
public DirectoryPath BuildLibKspDirectory { get; set; }
public DirectoryPath BuildLibNugetDirectory { get; set; }
public DirectoryPath BuildMetaDirectory { get; set; }
public DirectoryPath BuildPkgDirectory { get; set; }
public DirectoryPath BuildStageDirectory { get; set; }
public DirectoryPath BuildStageGameDataDirectory { get; set; }
public DirectoryPath BuildStageGameDataModDirectory { get; set; }
public DirectoryPath BuildOutDirectory { get; set; }
public DirectoryPath SrcDirectory { get; set; }
public DirectoryPath DeployDirectory { get; set; }
}
// HACK: Terrible workaround for Mono's script compiler not supporting globals like Roslyn
private Globals GetGlobals()
{
var globals = new Globals();
globals.ModIdentifier = "PlaneMode";
globals.ModName = "Plane Mode";
globals.ModCopyrightDates = "2015-2016";
globals.KspLibs = new []
{
"Assembly-CSharp.dll",
"Assembly-CSharp-firstpass.dll",
"UnityEngine.dll",
"UnityEngine.UI.dll"
};
globals.Target = Argument<string>("target", "Package");
globals.Configuration = Argument<string>("configuration", "Debug");
globals.BuildConfiguration = GetBuildConfiguration<BuildConfiguration>();
globals.BuildVersion = GetBuildVersion();
globals.Solution = GetSolution();
globals.RootDirectory = Context.Environment.WorkingDirectory;
globals.BuildDirectory = globals.RootDirectory.Combine(".build");
globals.BuildLibDirectory = globals.BuildDirectory.Combine("lib");
globals.BuildLibKspDirectory = globals.BuildLibDirectory.Combine("ksp");
globals.BuildLibNugetDirectory = globals.BuildLibDirectory.Combine("nuget");
globals.BuildMetaDirectory = globals.BuildDirectory.Combine("meta");
globals.BuildPkgDirectory = globals.BuildDirectory.Combine("pkg").Combine(globals.Configuration);
globals.BuildStageDirectory = globals.BuildDirectory.Combine("stage");
globals.BuildStageGameDataDirectory = globals.BuildStageDirectory.Combine("GameData");
globals.BuildStageGameDataModDirectory = globals.BuildStageGameDataDirectory.Combine(globals.ModIdentifier);
globals.BuildOutDirectory = globals.BuildDirectory.Combine("out");
globals.SrcDirectory = globals.RootDirectory.Combine("src");
var deployDirectory = globals.BuildConfiguration.KspPath("GameData", globals.ModIdentifier);
if (deployDirectory != null) { globals.DeployDirectory = deployDirectory; }
return globals;
}
Task("Init")
.IsDependentOn("InitLibKsp");
Task("InitLibKsp")
.Does(() =>
{
const string kspLibsUrlBase = "https://build.apokee.com/dependencies/ksp/1.3.0.1804";
var globals = GetGlobals();
CreateDirectory(globals.BuildLibKspDirectory);
var missingKspLibs = globals.KspLibs.Where(i => !FileExists(globals.BuildLibKspDirectory.CombineWithFilePath(i)));
foreach (var kspLib in missingKspLibs)
{
var destinationKspLibFilePath = globals.BuildLibKspDirectory.CombineWithFilePath(kspLib);
var localKspLib = globals.BuildConfiguration.KspPath("KSP_x64_Data", "Managed", kspLib);
if (localKspLib != null && FileExists(localKspLib))
CopyFile(localKspLib, destinationKspLibFilePath);
else
DownloadFile(string.Format("{0}/{1}", kspLibsUrlBase, kspLib), destinationKspLibFilePath);
}
});
Task("CleanStage")
.Does(() =>
{
var globals = GetGlobals();
CleanDirectories(new [] { globals.BuildStageDirectory });
});
Task("CleanPackage")
.Does(() =>
{
var globals = GetGlobals();
CleanDirectories(new [] { globals.BuildPkgDirectory });
});
Task("CleanDeploy")
.Does(() =>
{
var globals = GetGlobals();
CleanDirectories(new [] {globals.DeployDirectory });
});
Task("Restore")
.Does(() =>
{
var globals = GetGlobals();
NuGetRestore(globals.Solution);
});
Task("BuildMeta")
.IsDependentOn("BuildAssemblyInfo")
.Does(() =>
{
var globals = GetGlobals();
CreateDirectory(globals.BuildMetaDirectory);
FileWriteText(
globals.BuildMetaDirectory.CombineWithFilePath("VERSION"),
globals.BuildVersion
);
FileWriteText(
globals.BuildMetaDirectory.CombineWithFilePath("PRERELEASE"),
(globals.BuildVersion.Pre != null).ToString().ToLowerInvariant()
);
FileWriteText(
globals.BuildMetaDirectory.CombineWithFilePath("CHANGELOG"),
GetChangeLog().LatestChanges
);
});
Task("BuildAssemblyInfo")
.IsDependentOn("BuildGlobalAssemblyVersionInfo")
.IsDependentOn("BuildGlobalKspAssemblyVersionInfo");
Task("BuildGlobalAssemblyVersionInfo")
.Does(() =>
{
var globals = GetGlobals();
CreateDirectory(globals.BuildMetaDirectory);
CreateAssemblyInfo(globals.BuildMetaDirectory.CombineWithFilePath("GlobalAssemblyVersionInfo.cs"), new AssemblyInfoSettings
{
Version = string.Format("{0}.{1}", globals.BuildVersion.Major, globals.BuildVersion.Minor),
FileVersion = string.Format("{0}.{1}.{2}", globals.BuildVersion.Major, globals.BuildVersion.Minor, globals.BuildVersion.Patch),
InformationalVersion = globals.BuildVersion.ToString()
});
});
Task("BuildGlobalKspAssemblyVersionInfo")
.Does(() =>
{
var globals = GetGlobals();
CreateDirectory(globals.BuildMetaDirectory);
var sb = new StringBuilder();
sb.AppendLine("//------------------------------------------------------------------------------");
sb.AppendLine("// <auto-generated>");
sb.AppendLine("// This code was generated by Cake.");
sb.AppendLine("// </auto-generated>");
sb.AppendLine("//------------------------------------------------------------------------------");
sb.AppendLine();
sb.AppendLine(
string.Format(@"[assembly: KSPAssembly(""{0}"", {1}, {2})]",
globals.ModIdentifier,
globals.BuildVersion.Major,
globals.BuildVersion.Minor
)
);
FileWriteText(globals.BuildMetaDirectory.CombineWithFilePath("GlobalAssemblyKspVersionInfo.cs"), sb.ToString());
});
Task("Build")
.IsDependentOn("Init")
.IsDependentOn("Restore")
.IsDependentOn("BuildMeta")
.Does(() =>
{
var globals = GetGlobals();
DotNetBuild(globals.Solution, s => { s.Configuration = globals.Configuration; });
});
Task("Stage")
.IsDependentOn("CleanStage")
.IsDependentOn("Restore")
.IsDependentOn("Build")
.Does(() =>
{
var globals = GetGlobals();
var artworkDirectory = GetNuGetPackageDirectory("Apokee.Artwork").Combine("Content");
var binDirectory = globals
.BuildOutDirectory
.Combine(globals.ModIdentifier)
.Combine(globals.Configuration)
.Combine("bin");
var srcGameDataDirectory = globals.SrcDirectory.Combine("GameData");
var buildStageGameDataModPluginsDirectory = globals.BuildStageGameDataModDirectory.Combine("Plugins");
var buildStageGameDataModTexturesDirectory = globals.BuildStageGameDataModDirectory.Combine("Textures");
CreateDirectory(globals.BuildStageGameDataDirectory);
CreateDirectory(globals.BuildStageGameDataModDirectory);
CreateDirectory(buildStageGameDataModPluginsDirectory);
CreateDirectory(buildStageGameDataModTexturesDirectory);
CopyFiles(
GetFiles(string.Format("{0}/*", binDirectory)),
buildStageGameDataModPluginsDirectory
);
CopyDirectory(
srcGameDataDirectory,
globals.BuildStageGameDataModDirectory
);
CopyFile(
artworkDirectory.CombineWithFilePath("airplane-white-38x38.png"),
buildStageGameDataModTexturesDirectory.CombineWithFilePath("AppLauncherPlane.png")
);
CopyFile(
artworkDirectory.CombineWithFilePath("rocket-white-38x38.png"),
buildStageGameDataModTexturesDirectory.CombineWithFilePath("AppLauncherRocket.png")
);
CopyFileToDirectory("CHANGELOG.md", globals.BuildStageGameDataModDirectory);
CopyFileToDirectory("LICENSE.md", globals.BuildStageGameDataModDirectory);
CopyFileToDirectory("README.md", globals.BuildStageGameDataModDirectory);
});
Task("Deploy")
.IsDependentOn("Stage")
.IsDependentOn("CleanDeploy")
.Does(() =>
{
var globals = GetGlobals();
CopyDirectory(globals.BuildStageGameDataModDirectory, globals.DeployDirectory);
});
Task("Run")
.IsDependentOn("Deploy")
.Does(() =>
{
var globals = GetGlobals();
StartAndReturnProcess(globals.BuildConfiguration.KspPath(globals.BuildConfiguration.KspBin), new ProcessSettings
{
WorkingDirectory = globals.BuildConfiguration.KspDir
});
});
Task("Package")
.IsDependentOn("CleanPackage")
.IsDependentOn("Stage")
.Does(() =>
{
var globals = GetGlobals();
CreateDirectory(globals.BuildPkgDirectory);
Zip(
globals.BuildStageDirectory,
globals.BuildPkgDirectory.CombineWithFilePath(
string.Format("{0}-{1}.zip", globals.ModIdentifier, globals.BuildVersion)
)
);
});
Task("Version")
.Does(() =>
{
Information(GetVersion());
});
Task("ChangeLog")
.Does(() =>
{
Information(GetChangeLog().LatestChanges);
});
RunTarget(GetGlobals().Target);
public SemVer GetBuildVersion()
{
SemVer buildVersion;
var release = Argument<bool>("release", false);;
var changeLog = GetChangeLog();
var version = changeLog.LatestVersion;
var rev = GetGitRevision(useShort: true);
if (rev != null && !release)
{
if (version.Build == null)
buildVersion = new SemVer(version.Major, version.Minor, version.Patch, version.Pre, rev);
else
throw new Exception("VERSION already contains build metadata");
}
else
{
buildVersion = version;
}
return buildVersion;
}
private DirectoryPath GetNuGetPackageDirectory(string package)
{
var globals = GetGlobals();
return GetDirectories(string.Format("{0}/*", globals.BuildLibNugetDirectory))
.Where(i => i.GetDirectoryName().StartsWith(package))
.OrderByDescending(i => {
var directoryName = i.GetDirectoryName();
return new Version(directoryName.Substring(package.Length + 1, directoryName.Length - package.Length - 1));
})
.FirstOrDefault();
}