@@ -40,24 +40,14 @@ public DefaultBuilder(ILogger logger, IErrorFilter errorFilter)
4040
4141 public async Task < int > BuildAsync ( ISettings settings , TimeSpan timeout )
4242 {
43- logger . LogInformation ( $ "Preparing Unity Build.") ;
44-
45- // validate
46- if ( string . IsNullOrWhiteSpace ( settings . UnityPath ) )
47- throw new ArgumentException ( $ "Please pass Unity Executable path with argument `--unity-path` or environment variable `{ nameof ( settings . UnityPath ) } `.") ;
48- if ( ! File . Exists ( settings . UnityPath ) )
49- throw new FileNotFoundException ( $ "{ nameof ( settings . UnityPath ) } not found.{ settings . UnityPath } ") ;
50- if ( string . IsNullOrEmpty ( settings . LogFilePath ) )
51- throw new ArgumentException ( "Missing '-logFile filename' argument. Make sure you had targeted any log file path." ) ;
52-
5343 // Initialize
5444 logger . LogInformation ( $ "Initializing LogFilePath '{ settings . LogFilePath } '.") ;
5545 await InitializeAsync ( settings . LogFilePath ) ;
5646
5747 // Build
5848 logger . LogInformation ( "Starting Unity Build." ) ;
59- logger . LogInformation ( $ "Command: { settings . UnityPath } { settings . ArgumentString } ") ;
60- logger . LogInformation ( $ "WorkingDir: { settings . WorkingDirectory } ") ;
49+ logger . LogInformation ( $ " - Command: { settings . UnityPath } { settings . ArgumentString } ") ;
50+ logger . LogInformation ( $ " - WorkingDir: { settings . WorkingDirectory } ") ;
6151 var sw = Stopwatch . StartNew ( ) ;
6252 using var process = Process . Start ( new ProcessStartInfo ( )
6353 {
@@ -68,9 +58,12 @@ public async Task<int> BuildAsync(ISettings settings, TimeSpan timeout)
6858 CreateNoWindow = true ,
6959 } ) ;
7060
61+ var exitCode = BuildErrorCode . Success ;
62+
7163 if ( process is null )
7264 {
7365 sw . Stop ( ) ;
66+ exitCode = BuildErrorCode . ProcessNull ;
7467 throw new OperationCanceledException ( "Could not start Unity. Somthing blocked creating process." ) ;
7568 }
7669
@@ -86,13 +79,15 @@ public async Task<int> BuildAsync(ISettings settings, TimeSpan timeout)
8679 }
8780 else
8881 {
82+ exitCode = BuildErrorCode . ProcessTimeout ;
8983 throw new TimeoutException ( $ "Unity Process has been aborted. Waited 10 seconds but could't create logFilePath '{ settings . LogFilePath } '.") ;
9084 }
9185 }
9286
9387 // log file generated but process immediately exited.
9488 if ( process . HasExited )
9589 {
90+ exitCode = BuildErrorCode . ProcessImmediatelyExit ;
9691 throw new OperationCanceledException ( $ "Unity process started but build unexpectedly finished before began.") ;
9792 }
9893
@@ -104,6 +99,7 @@ public async Task<int> BuildAsync(ISettings settings, TimeSpan timeout)
10499 {
105100 if ( sw . Elapsed . TotalMilliseconds > timeout . TotalMilliseconds )
106101 {
102+ exitCode = BuildErrorCode . ProcessTimeout ;
107103 throw new TimeoutException ( $ "Timeout exceeded. { timeout . TotalMinutes } min has been passed, stopping build.") ;
108104 }
109105
@@ -124,46 +120,55 @@ public async Task<int> BuildAsync(ISettings settings, TimeSpan timeout)
124120 {
125121 sw . Stop ( ) ;
126122
127- if ( process . ExitCode == 0 )
123+ if ( exitCode is BuildErrorCode . Success )
128124 {
129- logger . LogInformation ( $ "Unity Build successfully complete. ( { process . ExitCode } ) ") ;
125+ logger . LogInformation ( $ "Unity Build successfully complete.") ;
130126 }
131127 else
132128 {
133- logger . LogInformation ( $ "Unity Build failed. ( { process . ExitCode } ) ") ;
129+ logger . LogInformation ( $ "Unity Build failed.") ;
134130 }
135131
136132 logger . LogInformation ( $ "Elapsed Time { sw . Elapsed } ") ;
137133
138134 // Assume exit Unity process
139- if ( ! process . HasExited )
135+ if ( process is not null && ! process . HasExited )
140136 {
141137 logger . LogInformation ( $ "Killing unterminated process. ({ process . Id } )") ;
142138 process . Kill ( true ) ;
143139 }
144140 }
145- return process . ExitCode ;
141+
142+ return exitCode . GetAttrubute < ErrorExitCodeAttribute > ( ) ? . ExitCode ?? 0 ;
146143 }
147144
148- public async Task InitializeAsync ( string path )
145+ public async Task InitializeAsync ( string logFilePath )
149146 {
150- if ( ! File . Exists ( path ) )
147+ await AssumeLogFileInitialized ( logFilePath ) . ConfigureAwait ( false ) ;
148+ }
149+
150+ /// <summary>
151+ /// Assume Logfile is not exists, or delete before run.
152+ /// </summary>
153+ /// <param name="logFilePath"></param>
154+ /// <returns></returns>
155+ public async Task AssumeLogFileInitialized ( string logFilePath )
156+ {
157+ if ( ! File . Exists ( logFilePath ) )
151158 {
152159 return ;
153160 }
154-
155- // retry 10 times
156- var retry = 10 ;
161+ var retry = 10 ; // retry 10 times
157162 for ( var i = 1 ; i <= retry ; i ++ )
158163 {
159164 try
160165 {
161- File . Delete ( path ) ;
166+ File . Delete ( logFilePath ) ;
162167 break ;
163168 }
164169 catch ( IOException ) when ( i < retry + 1 )
165170 {
166- logger . LogWarning ( $ "Couldn't delete file { path } , retrying... ({ i + 1 } /{ retry } )") ;
171+ logger . LogWarning ( $ "Couldn't delete file { logFilePath } , retrying... ({ i + 1 } /{ retry } )") ;
167172 await Task . Delay ( TimeSpan . FromSeconds ( 1 ) ) ;
168173 continue ;
169174 }
0 commit comments