-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildCommand.cs
218 lines (171 loc) · 8.1 KB
/
BuildCommand.cs
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
using UnityEditor;
using System.Linq;
using System;
#if UNITY_EDITOR
static class BuildCommand {
private const string IS_DEVELOPMENT_BUILD = "IS_DEVELOPMENT_BUILD";
private const string BUILD_OPTIONS_ENV_VAR = "BUILD_OPTIONS_ENV_VAR";
//////////////////// Main Method ///////////////////////
public static void PerformBuild() {
Console.WriteLine(":: Performing build");
// Common for all Platforms
var buildTarget = GetBuildTarget();
var targetGroup = GetBuildTargetGroup(buildTarget);
string xrPluginCommand = GetArgument("xrPlugin");
if (xrPluginCommand != null) {
var xrPlugin = GetXrPlugin(xrPluginCommand);
foreach (XRPluginManagementSettings.Plugin plugin in Enum.GetValues(typeof(XRPluginManagementSettings.Plugin))) {
XRPluginManagementSettings.DisablePlugin(targetGroup, plugin);
}
XRPluginManagementSettings.EnablePlugin(targetGroup, xrPlugin);
}
bool isDevelopmentBuild = IsDevelopmentType();
HandleDevelopmentType(isDevelopmentBuild);
var buildPath = GetBuildPath();
var buildName = GetBuildName();
PlayerSettings.productName = buildName;
var buildOptions = GetBuildOptions();
var scriptingBackend = GetScriptingBackend();
PlayerSettings.SetScriptingBackend(targetGroup, scriptingBackend);
Console.WriteLine($":: Ready to start build on {buildTarget}.");
var fixedBuildPath = GetFixedBuildPath(buildTarget, buildPath, buildName);
var buildReport = BuildPipeline.BuildPlayer(GetEnabledScenes(), fixedBuildPath, buildTarget, buildOptions);
if (buildReport.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
throw new Exception($"Build ended with {buildReport.summary.result} status");
Console.WriteLine(":: Done with build");
}
/////////////////////////////////////////////////////////
///////////////////// Utility Methods /////////////////////
static bool IsDevelopmentType() {
if (TryGetEnv(IS_DEVELOPMENT_BUILD, out string value)) {
return bool.Parse(value);
}
Console.WriteLine($":: {IS_DEVELOPMENT_BUILD} env var not detected");
throw new Exception($"{IS_DEVELOPMENT_BUILD} env var not detected");
}
private static void HandleDevelopmentType(bool isDevelopmentBuild) {
EditorUserBuildSettings.development = isDevelopmentBuild;
PlayerSettings.SplashScreen.show = isDevelopmentBuild;
UnityEngine.Debug.unityLogger.logEnabled = isDevelopmentBuild;
Console.WriteLine(
$":: {IS_DEVELOPMENT_BUILD} env var detected, setting \"Development Build\" to {isDevelopmentBuild}.");
}
static ScriptingImplementation GetScriptingBackend() {
string scriptingBackendName = GetArgument("scriptingBackend");
Console.WriteLine(":: Received scriptingBackend: " + scriptingBackendName);
if (scriptingBackendName.TryConvertToEnum(out ScriptingImplementation scriptingBackend))
return scriptingBackend;
Console.WriteLine($":: {nameof(scriptingBackendName)} \"{scriptingBackendName}\" not defined on enum {nameof(ScriptingImplementation)}, using {nameof(ScriptingImplementation.Mono2x)} enum to build");
return ScriptingImplementation.Mono2x;
}
static BuildTarget GetBuildTarget() {
string buildTargetName = GetArgument("buildTarget");
Console.WriteLine(":: Received buildTarget: " + buildTargetName);
if (buildTargetName.TryConvertToEnum(out BuildTarget target))
return target;
Console.WriteLine($":: {nameof(buildTargetName)} \"{buildTargetName}\" not defined on enum {nameof(BuildTarget)}, using {nameof(BuildTarget.NoTarget)} enum to build");
return BuildTarget.NoTarget;
}
static XRPluginManagementSettings.Plugin GetXrPlugin(string xrPlugin) {
Console.WriteLine(":: Received xrPlugin: " + xrPlugin);
if (xrPlugin.TryConvertToEnum(out XRPluginManagementSettings.Plugin plugin))
return plugin;
Console.WriteLine($":: {nameof(xrPlugin)} \"{xrPlugin}\" not defined on enum {nameof(XRPluginManagementSettings.Plugin)}, using {nameof(XRPluginManagementSettings.Plugin.Oculus)} enum to build");
return XRPluginManagementSettings.Plugin.Oculus;
}
static BuildTargetGroup GetBuildTargetGroup(BuildTarget buildTarget) {
string targetGroup;
string platform = buildTarget.ToString();
if (buildTarget.ToString().ToLower().Contains("standalone")) {
targetGroup = "Standalone";
}
else if (buildTarget.ToString().ToLower().Contains("wsa")) {
targetGroup = "WSA";
}
else {
targetGroup = platform;
}
Console.WriteLine(":: Received targetGroup: " + targetGroup);
if (targetGroup.TryConvertToEnum(out BuildTargetGroup target))
return target;
Console.WriteLine($":: {nameof(targetGroup)} \"{targetGroup}\" not defined on enum {nameof(BuildTargetGroup)}, using {nameof(BuildTargetGroup.Unknown)} enum to build");
return BuildTargetGroup.Unknown;
}
static bool TryGetEnv(string key, out string value) {
value = Environment.GetEnvironmentVariable(key);
return !string.IsNullOrEmpty(value);
}
static string GetArgument(string name) {
string[] args = Environment.GetCommandLineArgs();
for (int i = 0; i < args.Length; i++) {
if (args[i].Contains(name)) {
return args[i + 1];
}
}
return null;
}
public static bool TryConvertToEnum<TEnum>(this string strEnumValue, out TEnum value) {
if (!Enum.IsDefined(typeof(TEnum), strEnumValue)) {
value = default;
return false;
}
value = (TEnum)Enum.Parse(typeof(TEnum), strEnumValue);
return true;
}
static string GetBuildPath() {
string buildPath = GetArgument("customBuildPath");
Console.WriteLine(":: Received customBuildPath " + buildPath);
if (buildPath == "") {
throw new Exception("customBuildPath argument is missing");
}
return buildPath;
}
static string GetBuildName() {
string buildName = GetArgument("customBuildName");
Console.WriteLine(":: Received customBuildName " + buildName);
if (buildName == "") {
throw new Exception("customBuildName argument is missing");
}
return buildName;
}
static string GetFixedBuildPath(BuildTarget buildTarget, string buildPath, string buildName) {
if (buildTarget.ToString().ToLower().Contains("windows")) {
buildName += ".exe";
}
else if (buildTarget == BuildTarget.Android) {
buildName += EditorUserBuildSettings.buildAppBundle ? ".aab" : ".apk";
}
return buildPath + buildName;
}
static BuildOptions GetBuildOptions() {
if (TryGetEnv(BUILD_OPTIONS_ENV_VAR, out string envVar)) {
string[] allOptionVars = envVar.Split(',');
BuildOptions allOptions = BuildOptions.None;
BuildOptions option;
string optionVar;
int length = allOptionVars.Length;
Console.WriteLine($":: Detecting {BUILD_OPTIONS_ENV_VAR} env var with {length} elements ({envVar})");
for (int i = 0; i < length; i++) {
optionVar = allOptionVars[i];
if (optionVar.TryConvertToEnum(out option)) {
allOptions |= option;
}
else {
Console.WriteLine($":: Cannot convert {optionVar} to {nameof(BuildOptions)} enum, skipping it.");
}
}
return allOptions;
}
return BuildOptions.None;
}
static string[] GetEnabledScenes() {
return (
from scene in EditorBuildSettings.scenes
where scene.enabled
where !string.IsNullOrEmpty(scene.path)
select scene.path
).ToArray();
}
/////////////////////////////////////////////////////////////
}
#endif