forked from game-ci/unity3d-gitlab-ci-example-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildCommand.cs
124 lines (110 loc) · 3.74 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
using UnityEditor;
using System.Linq;
using System;
static class BuildCommand
{
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;
}
static string[] GetEnabledScenes ()
{
return (
from scene in EditorBuildSettings.scenes
where scene.enabled
where !string.IsNullOrEmpty(scene.path)
select scene.path
).ToArray ();
}
static BuildTarget GetBuildTarget ()
{
string buildTargetName = GetArgument ("customBuildTarget");
Console.WriteLine (":: Received customBuildTarget " + buildTargetName);
if (buildTargetName.ToLower () == "android") {
#if !UNITY_5_6_OR_NEWER
// https://issuetracker.unity3d.com/issues/buildoptions-dot-acceptexternalmodificationstoplayer-causes-unityexception-unknown-project-type-0
// Fixed in Unity 5.6.0
// side effect to fix android build system:
EditorUserBuildSettings.androidBuildSystem = AndroidBuildSystem.Internal;
#endif
}
return ToEnum<BuildTarget> (buildTargetName, BuildTarget.NoTarget);
}
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 = buildName + ".exe";
} else if (buildTarget.ToString().ToLower().Contains("webgl")) {
// webgl produces a folder with index.html inside, there is no executable name for this buildTarget
buildName = "";
}
return buildPath + buildName;
}
static BuildOptions GetBuildOptions ()
{
string buildOptions = GetArgument ("customBuildOptions");
return buildOptions == "AcceptExternalModificationsToPlayer" ? BuildOptions.AcceptExternalModificationsToPlayer : BuildOptions.None;
}
// https://stackoverflow.com/questions/1082532/how-to-tryparse-for-enum-value
static TEnum ToEnum<TEnum> (this string strEnumValue, TEnum defaultValue)
{
if (!Enum.IsDefined (typeof(TEnum), strEnumValue)) {
return defaultValue;
}
return (TEnum)Enum.Parse (typeof(TEnum), strEnumValue);
}
static string getEnv (string key, bool secret = false, bool verbose = true)
{
var env_var = Environment.GetEnvironmentVariable (key);
if (verbose) {
if (env_var != null) {
if (secret) {
Console.WriteLine (":: env['" + key + "'] set");
} else {
Console.WriteLine (":: env['" + key + "'] set to '" + env_var + "'");
}
} else {
Console.WriteLine (":: env['" + key + "'] is null");
}
}
return env_var;
}
static void PerformBuild ()
{
Console.WriteLine (":: Performing build");
//PlayerSettings.keystorePass = getEnv ("KEYSTORE_PASS", true);
//PlayerSettings.keyaliasPass = getEnv ("KEY_ALIAS_PASS", true);
//EditorSetup.AndroidSdkRoot = getEnv ("ANDROID_SDK_HOME");
//EditorSetup.JdkRoot = getEnv ("JAVA_HOME");
//EditorSetup.AndroidNdkRoot = getEnv ("ANDROID_NDK_HOME");
var buildTarget = GetBuildTarget ();
var buildPath = GetBuildPath ();
var buildName = GetBuildName ();
var fixedBuildPath = GetFixedBuildPath (buildTarget, buildPath, buildName);
BuildPipeline.BuildPlayer (GetEnabledScenes (), fixedBuildPath, buildTarget, GetBuildOptions ());
Console.WriteLine (":: Done with build");
}
}