1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . IO ;
3
4
using System . Linq ;
4
5
using JetBrains . Annotations ;
5
6
using UnityEditor ;
@@ -16,6 +17,8 @@ namespace UnityBuilderAction
16
17
public static class BuildScript
17
18
{
18
19
private static readonly string Eol = Environment . NewLine ;
20
+ private const bool LogVerboseBatchMode = true ;
21
+ private const bool LogVerboseNonBatchMode = false ;
19
22
20
23
private static readonly string [ ] Secrets =
21
24
{ "androidKeystorePass" , "androidKeyaliasName" , "androidKeyaliasPass" } ;
@@ -49,16 +52,16 @@ public static void Build(string[] args)
49
52
{
50
53
EditorUserBuildSettings . buildAppBundle = options [ "customBuildPath" ] . EndsWith ( ".aab" ) ;
51
54
if ( options . TryGetValue ( "androidKeystoreName" , out string keystoreName ) &&
52
- ! string . IsNullOrEmpty ( keystoreName ) )
55
+ ! string . IsNullOrEmpty ( keystoreName ) )
53
56
PlayerSettings . Android . keystoreName = keystoreName ;
54
57
if ( options . TryGetValue ( "androidKeystorePass" , out string keystorePass ) &&
55
- ! string . IsNullOrEmpty ( keystorePass ) )
58
+ ! string . IsNullOrEmpty ( keystorePass ) )
56
59
PlayerSettings . Android . keystorePass = keystorePass ;
57
60
if ( options . TryGetValue ( "androidKeyaliasName" , out string keyaliasName ) &&
58
- ! string . IsNullOrEmpty ( keyaliasName ) )
61
+ ! string . IsNullOrEmpty ( keyaliasName ) )
59
62
PlayerSettings . Android . keyaliasName = keyaliasName ;
60
63
if ( options . TryGetValue ( "androidKeyaliasPass" , out string keyaliasPass ) &&
61
- ! string . IsNullOrEmpty ( keyaliasPass ) )
64
+ ! string . IsNullOrEmpty ( keyaliasPass ) )
62
65
PlayerSettings . Android . keyaliasPass = keyaliasPass ;
63
66
break ;
64
67
}
@@ -72,7 +75,7 @@ public static void Build(string[] args)
72
75
#endif
73
76
74
77
if ( options . TryGetValue ( "tag" , out string tagVersion ) &&
75
- ! string . IsNullOrEmpty ( tagVersion ) )
78
+ ! string . IsNullOrEmpty ( tagVersion ) )
76
79
{
77
80
string [ ] tagParameters = tagVersion . Split ( '-' ) ;
78
81
if ( tagParameters . Contains ( "minsize" ) )
@@ -128,19 +131,48 @@ public static void Build(string[] args)
128
131
{
129
132
buildPlayerOptions . options |= BuildOptions . AutoRunPlayer ;
130
133
}
134
+
135
+ var projectPath = Application . dataPath . Substring ( 0 , Application . dataPath . Length - "/Assets" . Length ) ;
136
+ BackupLastBuild ( $ "{ projectPath } /{ options [ "customBuildPath" ] } ") ;
131
137
}
132
138
133
139
// Custom build
134
140
Build ( buildTarget , options [ "customBuildPath" ] ) ;
135
141
}
136
142
143
+ private static void BackupLastBuild ( string buildPath )
144
+ {
145
+ if ( Directory . Exists ( buildPath ) )
146
+ {
147
+ string backupFolderPath = $ "{ buildPath } -Previous";
148
+ if ( Directory . Exists ( backupFolderPath ) )
149
+ {
150
+ Directory . Delete ( backupFolderPath , true ) ;
151
+ }
152
+ Log ( $ "Moving current build folder to backup location: { backupFolderPath } ") ;
153
+ Directory . Move ( buildPath , backupFolderPath ) ;
154
+ }
155
+ else if ( File . Exists ( buildPath ) )
156
+ {
157
+ string extension = Path . GetExtension ( buildPath ) ;
158
+ string pathWithoutExtension = buildPath . Substring ( 0 , buildPath . Length - extension . Length ) ;
159
+ string backupFilePath = $ "{ pathWithoutExtension } -Previous{ extension } ";
160
+ if ( File . Exists ( backupFilePath ) )
161
+ {
162
+ File . Delete ( backupFilePath ) ;
163
+ }
164
+ Log ( $ "Moving current build file to backup location: { backupFilePath } ") ;
165
+ File . Move ( buildPath , backupFilePath ) ;
166
+ }
167
+ }
168
+
137
169
private static void SetWebGlOptimization ( string value )
138
170
{
139
171
#if UNITY_2019_4_OR_NEWER
140
- EditorUserBuildSettings . SetPlatformSettings ( BuildPipeline . GetBuildTargetName ( BuildTarget . WebGL ) ,
172
+ EditorUserBuildSettings . SetPlatformSettings ( BuildPipeline . GetBuildTargetName ( BuildTarget . WebGL ) ,
141
173
"CodeOptimization" , value ) ;
142
174
#else
143
- Console . WriteLine ( $ "Setting { nameof ( SetWebGlOptimization ) } not supported by this unity version") ;
175
+ Log ( $ "Setting { nameof ( SetWebGlOptimization ) } not supported by this unity version") ;
144
176
#endif
145
177
}
146
178
@@ -150,13 +182,13 @@ private static Dictionary<string, string> GetValidatedOptions(string[] args)
150
182
151
183
if ( ! validatedOptions . TryGetValue ( "projectPath" , out string _ ) )
152
184
{
153
- Console . WriteLine ( "Missing argument -projectPath" ) ;
185
+ LogError ( "Missing argument -projectPath" ) ;
154
186
EndBuild ( 110 ) ;
155
187
}
156
188
157
189
if ( ! validatedOptions . TryGetValue ( "buildTarget" , out string buildTarget ) )
158
190
{
159
- Console . WriteLine ( "Missing argument -buildTarget" ) ;
191
+ LogError ( "Missing argument -buildTarget" ) ;
160
192
EndBuild ( 120 ) ;
161
193
}
162
194
@@ -167,19 +199,19 @@ private static Dictionary<string, string> GetValidatedOptions(string[] args)
167
199
168
200
if ( ! validatedOptions . TryGetValue ( "customBuildPath" , out string _ ) )
169
201
{
170
- Console . WriteLine ( "Missing argument -customBuildPath" ) ;
202
+ LogError ( "Missing argument -customBuildPath" ) ;
171
203
EndBuild ( 130 ) ;
172
204
}
173
205
174
206
const string defaultCustomBuildName = "TestBuild" ;
175
207
if ( ! validatedOptions . TryGetValue ( "customBuildName" , out string customBuildName ) )
176
208
{
177
- Console . WriteLine ( $ "Missing argument -customBuildName, defaulting to { defaultCustomBuildName } .") ;
209
+ LogError ( $ "Missing argument -customBuildName, defaulting to { defaultCustomBuildName } .") ;
178
210
validatedOptions . Add ( "customBuildName" , defaultCustomBuildName ) ;
179
211
}
180
212
else if ( customBuildName == "" )
181
213
{
182
- Console . WriteLine ( $ "Invalid argument -customBuildName, defaulting to { defaultCustomBuildName } .") ;
214
+ LogError ( $ "Invalid argument -customBuildName, defaulting to { defaultCustomBuildName } .") ;
183
215
validatedOptions . Add ( "customBuildName" , defaultCustomBuildName ) ;
184
216
}
185
217
@@ -190,10 +222,10 @@ private static void ParseCommandLineArguments(string[] args, out Dictionary<stri
190
222
{
191
223
providedArguments = new Dictionary < string , string > ( ) ;
192
224
193
- Console . WriteLine (
225
+ LogVerbose (
194
226
$ "{ Eol } " +
195
227
$ "###########################{ Eol } " +
196
- $ "# Parsing settings #{ Eol } " +
228
+ $ "# Parsing settings #{ Eol } " +
197
229
$ "###########################{ Eol } " +
198
230
$ "{ Eol } "
199
231
) ;
@@ -213,7 +245,7 @@ private static void ParseCommandLineArguments(string[] args, out Dictionary<stri
213
245
string displayValue = secret ? "*HIDDEN*" : "\" " + value + "\" " ;
214
246
215
247
// Assign
216
- Console . WriteLine ( $ "Found flag \" { flag } \" with value { displayValue } .") ;
248
+ LogVerbose ( $ "Found flag \" { flag } \" with value { displayValue } .") ;
217
249
providedArguments . Add ( flag , value ) ;
218
250
}
219
251
}
@@ -232,39 +264,46 @@ private static void Build(BuildTarget buildTarget, string filePath)
232
264
233
265
private static void ReportSummary ( BuildSummary summary )
234
266
{
235
- Console . WriteLine (
236
- $ "{ Eol } " +
267
+ string summaryText = $ "{ Eol } " +
237
268
$ "###########################{ Eol } " +
238
- $ "# Build results #{ Eol } " +
269
+ $ "# Build results #{ Eol } " +
239
270
$ "###########################{ Eol } " +
240
271
$ "{ Eol } " +
241
272
$ "Duration: { summary . totalTime . ToString ( ) } { Eol } " +
242
273
$ "Warnings: { summary . totalWarnings . ToString ( ) } { Eol } " +
243
274
$ "Errors: { summary . totalErrors . ToString ( ) } { Eol } " +
244
275
$ "Size: { summary . totalSize . ToString ( ) } bytes{ Eol } " +
245
- $ "{ Eol } "
246
- ) ;
247
- }
276
+ $ "{ Eol } ";
277
+
278
+ if ( summary . totalErrors == 0 )
279
+ {
280
+ Log ( summaryText ) ;
281
+ }
282
+ else
283
+ {
284
+ LogError ( summaryText ) ;
285
+ }
286
+ }
248
287
249
288
private static void ExitWithResult ( BuildResult result )
250
289
{
251
290
switch ( result )
252
291
{
253
292
case BuildResult . Succeeded :
254
- Console . WriteLine ( "Build succeeded!" ) ;
293
+ Log ( "Build succeeded!" ) ;
255
294
EndBuild ( 0 ) ;
256
295
break ;
257
296
case BuildResult . Failed :
258
- Console . WriteLine ( "Build failed!" ) ;
297
+ LogError ( "Build failed!" ) ;
259
298
EndBuild ( 101 ) ;
260
299
break ;
261
300
case BuildResult . Cancelled :
262
- Console . WriteLine ( "Build cancelled!" ) ;
301
+ LogError ( "Build cancelled!" ) ;
263
302
EndBuild ( 102 ) ;
264
303
break ;
265
304
case BuildResult . Unknown :
266
305
default :
267
- Console . WriteLine ( "Build result is unknown!" ) ;
306
+ LogError ( "Build result is unknown!" ) ;
268
307
EndBuild ( 103 ) ;
269
308
break ;
270
309
}
@@ -284,5 +323,47 @@ private static void EndBuild(int returnValue)
284
323
}
285
324
}
286
325
}
326
+
327
+ private static void LogVerbose ( string message )
328
+ {
329
+ if ( Application . isBatchMode )
330
+ {
331
+ if ( LogVerboseBatchMode )
332
+ {
333
+ Console . WriteLine ( message ) ;
334
+ }
335
+ }
336
+ else
337
+ {
338
+ if ( LogVerboseNonBatchMode )
339
+ {
340
+ Debug . Log ( message ) ;
341
+ }
342
+ }
343
+ }
344
+
345
+ private static void Log ( string message )
346
+ {
347
+ if ( Application . isBatchMode )
348
+ {
349
+ Console . WriteLine ( message ) ;
350
+ }
351
+ else
352
+ {
353
+ Debug . Log ( message ) ;
354
+ }
355
+ }
356
+
357
+ private static void LogError ( string message )
358
+ {
359
+ if ( Application . isBatchMode )
360
+ {
361
+ Console . WriteLine ( message ) ;
362
+ }
363
+ else
364
+ {
365
+ Debug . LogError ( message ) ;
366
+ }
367
+ }
287
368
}
288
369
}
0 commit comments