forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildDeployTools.cs
More file actions
410 lines (349 loc) · 17.1 KB
/
BuildDeployTools.cs
File metadata and controls
410 lines (349 loc) · 17.1 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Microsoft.Win32;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;
namespace HoloToolkit.Unity
{
/// <summary>
/// Contains utility functions for building for the device
/// </summary>
public class BuildDeployTools
{
public const string DefaultMSBuildVersion = "15.0";
public static bool CanBuild()
{
if (PlayerSettings.GetScriptingBackend(BuildTargetGroup.WSA) == ScriptingImplementation.IL2CPP && IsIl2CppAvailable())
{
return true;
}
return PlayerSettings.GetScriptingBackend(BuildTargetGroup.WSA) == ScriptingImplementation.WinRTDotNET && IsDotNetAvailable();
}
public static bool IsDotNetAvailable()
{
return Directory.Exists(EditorApplication.applicationContentsPath + "\\PlaybackEngines\\MetroSupport\\Managed\\UAP");
}
public static bool IsIl2CppAvailable()
{
return Directory.Exists(EditorApplication.applicationContentsPath + "\\PlaybackEngines\\MetroSupport\\Managed\\il2cpp");
}
/// <summary>
/// Displays a dialog if no scenes are present in the build and returns true if build can proceed.
/// </summary>
/// <returns></returns>
public static bool CheckBuildScenes()
{
if (EditorBuildSettings.scenes.Length == 0)
{
return EditorUtility.DisplayDialog("Attention!",
"No scenes are present in the build settings!\n\n Do you want to cancel and add one?",
"Continue Anyway", "Cancel Build");
}
return true;
}
/// <summary>
/// Do a build configured for Mixed Reality Applications, returns the error from BuildPipeline.BuildPlayer
/// </summary>
public static bool BuildSLN()
{
return BuildSLN(BuildDeployPrefs.BuildDirectory, false);
}
public static bool BuildSLN(string buildDirectory, bool showDialog = true)
{
// Use BuildSLNUtilities to create the SLN
bool buildSuccess = false;
if (CheckBuildScenes() == false)
{
return false;
}
var buildInfo = new BuildInfo
{
// These properties should all match what the Standalone.proj file specifies
OutputDirectory = buildDirectory,
Scenes = EditorBuildSettings.scenes.Where(scene => scene.enabled).Select(scene => scene.path),
BuildTarget = BuildTarget.WSAPlayer,
WSASdk = WSASDK.UWP,
WSAUWPBuildType = EditorUserBuildSettings.wsaUWPBuildType,
WSAUwpSdk = EditorUserBuildSettings.wsaUWPSDK,
// Configure a post build action that will compile the generated solution
#if UNITY_2018_1_OR_NEWER
PostBuildAction = (innerBuildInfo, buildReport) =>
{
if (buildReport.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
{
EditorUtility.DisplayDialog(string.Format("{0} WindowsStoreApp Build {1}!", PlayerSettings.productName, buildReport.summary.result), "See console for details", "OK");
}
#else
PostBuildAction = (innerBuildInfo, buildError) =>
{
if (!string.IsNullOrEmpty(buildError))
{
EditorUtility.DisplayDialog(string.Format("{0} WindowsStoreApp Build Failed!", PlayerSettings.productName), buildError, "OK");
}
#endif
else
{
if (showDialog)
{
if (!EditorUtility.DisplayDialog(PlayerSettings.productName, "Build Complete", "OK", "Build AppX"))
{
BuildAppxFromSLN(
PlayerSettings.productName,
BuildDeployPrefs.MsBuildVersion,
BuildDeployPrefs.ForceRebuild,
BuildDeployPrefs.BuildConfig,
BuildDeployPrefs.BuildPlatform,
BuildDeployPrefs.BuildDirectory,
BuildDeployPrefs.IncrementBuildVersion);
}
}
buildSuccess = true;
}
}
};
BuildSLNUtilities.RaiseOverrideBuildDefaults(ref buildInfo);
BuildSLNUtilities.PerformBuild(buildInfo);
return buildSuccess;
}
public static string CalcMSBuildPath(string msBuildVersion)
{
if (msBuildVersion.Equals("14.0"))
{
using (RegistryKey key =
Registry.LocalMachine.OpenSubKey(
string.Format(@"Software\Microsoft\MSBuild\ToolsVersions\{0}", msBuildVersion)))
{
if (key != null)
{
var msBuildBinFolder = (string)key.GetValue("MSBuildToolsPath");
return Path.Combine(msBuildBinFolder, "msbuild.exe");
}
}
}
// If we got this far then we don't have VS 2015 installed and need to use msBuild 15
msBuildVersion = "15.0";
// For MSBuild 15+ we should to use vswhere to give us the correct instance
string output = @"/C vswhere -version " + msBuildVersion + " -products * -requires Microsoft.Component.MSBuild -property installationPath";
// get the right program files path based on whether the PC is x86 or x64
string programFiles = @"C:\Program Files (x86)\Microsoft Visual Studio\Installer";
var vswherePInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = false,
Arguments = output,
WorkingDirectory = programFiles
};
using (var vswhereP = new Process())
{
vswhereP.StartInfo = vswherePInfo;
vswhereP.Start();
output = vswhereP.StandardOutput.ReadToEnd();
vswhereP.WaitForExit();
}
string[] paths = output.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
if (paths.Length > 0)
{
// if there are multiple 2017 installs,
// prefer enterprise, then pro, then community
string bestPath = paths.OrderBy(p => p.ToLower().Contains("enterprise"))
.ThenBy(p => p.ToLower().Contains("professional"))
.ThenBy(p => p.ToLower().Contains("community")).First();
return bestPath + @"\MSBuild\" + msBuildVersion + @"\Bin\MSBuild.exe";
}
Debug.LogError("Unable to find a valid path to Visual Studio Instance!");
return string.Empty;
}
public static bool RestoreNugetPackages(string nugetPath, string storePath)
{
Debug.Assert(File.Exists(nugetPath));
Debug.Assert(Directory.Exists(storePath));
var nugetPInfo = new ProcessStartInfo
{
FileName = nugetPath,
CreateNoWindow = true,
UseShellExecute = false,
Arguments = "restore \"" + storePath + "/project.json\""
};
using (var nugetP = new Process())
{
nugetP.StartInfo = nugetPInfo;
nugetP.Start();
nugetP.WaitForExit();
nugetP.Close();
nugetP.Dispose();
}
return File.Exists(storePath + "\\project.lock.json");
}
public static bool BuildAppxFromSLN(string productName, string msBuildVersion, bool forceRebuildAppx, string buildConfig, string buildPlatform, string buildDirectory, bool incrementVersion, bool showDialog = true)
{
EditorUtility.DisplayProgressBar("Build AppX", "Building AppX Package...", 0);
string slnFilename = Path.Combine(buildDirectory, PlayerSettings.productName + ".sln");
if (!File.Exists(slnFilename))
{
Debug.LogError("Unable to find Solution to build from!");
EditorUtility.ClearProgressBar();
return false;
}
// Get and validate the msBuild path...
var msBuildPath = CalcMSBuildPath(msBuildVersion);
if (!File.Exists(msBuildPath))
{
Debug.LogErrorFormat("MSBuild.exe is missing or invalid:\n{0}.", msBuildPath);
EditorUtility.ClearProgressBar();
return false;
}
// Get the path to the NuGet tool
string unity = Path.GetDirectoryName(EditorApplication.applicationPath);
System.Diagnostics.Debug.Assert(unity != null, "unity != null");
string storePath = Path.GetFullPath(Path.Combine(Path.Combine(Application.dataPath, ".."), buildDirectory));
string solutionProjectPath = Path.GetFullPath(Path.Combine(storePath, productName + @".sln"));
// Bug in Unity editor that doesn't copy project.json and project.lock.json files correctly if solutionProjectPath is not in a folder named UWP.
if (!File.Exists(storePath + "\\project.json"))
{
File.Copy(unity + @"\Data\PlaybackEngines\MetroSupport\Tools\project.json", storePath + "\\project.json");
}
string assemblyCSharp = string.Format("{0}/GeneratedProjects/UWP/Assembly-CSharp", storePath);
string assemblyCSharpFirstPass = string.Format("{0}/GeneratedProjects/UWP/Assembly-CSharp-firstpass", storePath);
bool restoreFirstPass = Directory.Exists(assemblyCSharpFirstPass);
string nugetPath = Path.Combine(unity, @"Data\PlaybackEngines\MetroSupport\Tools\NuGet.exe");
// Before building, need to run a nuget restore to generate a json.lock file. Failing to do this breaks the build in VS RTM
if (PlayerSettings.GetScriptingBackend(BuildTargetGroup.WSA) == ScriptingImplementation.WinRTDotNET &&
(!RestoreNugetPackages(nugetPath, storePath) ||
!RestoreNugetPackages(nugetPath, storePath + "\\" + productName) ||
EditorUserBuildSettings.wsaGenerateReferenceProjects && !RestoreNugetPackages(nugetPath, assemblyCSharp) ||
EditorUserBuildSettings.wsaGenerateReferenceProjects && restoreFirstPass && !RestoreNugetPackages(nugetPath, assemblyCSharpFirstPass)))
{
Debug.LogError("Failed to restore nuget packages");
EditorUtility.ClearProgressBar();
return false;
}
EditorUtility.DisplayProgressBar("Build AppX", "Building AppX Package...", 25);
// Ensure that the generated .appx version increments by modifying Package.appxmanifest
if (!SetPackageVersion(incrementVersion))
{
Debug.LogError("Failed to increment package version!");
EditorUtility.ClearProgressBar();
return false;
}
// Now do the actual build
var pInfo = new ProcessStartInfo
{
FileName = msBuildPath,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
Arguments = string.Format("\"{0}\" /t:{1} /p:Configuration={2} /p:Platform={3} /verbosity:m",
solutionProjectPath,
forceRebuildAppx ? "Rebuild" : "Build",
buildConfig,
buildPlatform)
};
var process = new Process
{
StartInfo = pInfo,
EnableRaisingEvents = true
};
try
{
process.ErrorDataReceived += (sender, args) =>
{
if (!string.IsNullOrEmpty(args.Data))
{
Debug.LogError(args.Data);
}
};
process.OutputDataReceived += (sender, args) =>
{
if (!string.IsNullOrEmpty(args.Data))
{
Debug.Log(args.Data);
}
};
if (!process.Start())
{
Debug.LogError("Failed to start process!");
EditorUtility.ClearProgressBar();
process.Close();
process.Dispose();
return false;
}
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
EditorUtility.ClearProgressBar();
if (process.ExitCode == 0 && showDialog &&
!EditorUtility.DisplayDialog("Build AppX", "AppX Build Successful!", "OK", "Open AppX Folder"))
{
Process.Start("explorer.exe", string.Format("/f /open,{0}/{1}/AppPackages", Path.GetFullPath(BuildDeployPrefs.BuildDirectory), PlayerSettings.productName));
}
if (process.ExitCode != 0)
{
Debug.LogError(string.Format("MSBuild error (code = {0})", process.ExitCode));
EditorUtility.ClearProgressBar();
EditorUtility.DisplayDialog(PlayerSettings.productName + " build Failed!", "Failed to build appx from solution. Error code: " + process.ExitCode, "OK");
process.Close();
process.Dispose();
return false;
}
process.Close();
process.Dispose();
}
catch (Exception e)
{
process.Close();
process.Dispose();
Debug.LogError("Cmd Process EXCEPTION: " + e);
EditorUtility.ClearProgressBar();
return false;
}
return true;
}
private static bool SetPackageVersion(bool increment)
{
// Find the manifest, assume the one we want is the first one
string[] manifests = Directory.GetFiles(BuildDeployPrefs.AbsoluteBuildDirectory, "Package.appxmanifest", SearchOption.AllDirectories);
if (manifests.Length == 0)
{
Debug.LogError(string.Format("Unable to find Package.appxmanifest file for build (in path - {0})", BuildDeployPrefs.AbsoluteBuildDirectory));
return false;
}
string manifest = manifests[0];
var rootNode = XElement.Load(manifest);
var identityNode = rootNode.Element(rootNode.GetDefaultNamespace() + "Identity");
if (identityNode == null)
{
Debug.LogError(string.Format("Package.appxmanifest for build (in path - {0}) is missing an <Identity /> node", BuildDeployPrefs.AbsoluteBuildDirectory));
return false;
}
// We use XName.Get instead of string -> XName implicit conversion because
// when we pass in the string "Version", the program doesn't find the attribute.
// Best guess as to why this happens is that implicit string conversion doesn't set the namespace to empty
var versionAttr = identityNode.Attribute(XName.Get("Version"));
if (versionAttr == null)
{
Debug.LogError(string.Format("Package.appxmanifest for build (in path - {0}) is missing a version attribute in the <Identity /> node.", BuildDeployPrefs.AbsoluteBuildDirectory));
return false;
}
// Assume package version always has a '.' between each number.
// According to https://msdn.microsoft.com/en-us/library/windows/apps/br211441.aspx
// Package versions are always of the form Major.Minor.Build.Revision.
// Note: Revision number reserved for Windows Store, and a value other than 0 will fail WACK.
var version = PlayerSettings.WSA.packageVersion;
var newVersion = new Version(version.Major, version.Minor, increment ? version.Build + 1 : version.Build, version.Revision);
PlayerSettings.WSA.packageVersion = newVersion;
versionAttr.Value = newVersion.ToString();
rootNode.Save(manifest);
return true;
}
}
}