Nuke.Unreal
Build Unreal apps in Style.
Loading...
Searching...
No Matches
UnrealProcessException.cs
1using System;
2using System.IO;
3using System.Linq;
4using System.Runtime.Serialization;
5using System.Text;
6using JetBrains.Annotations;
7using Nuke.Common.Tooling;
8using Nuke.Common.Utilities;
9using Nuke.Common.Utilities.Collections;
10using Serilog;
11using Serilog.Events;
12
13namespace Nuke.Unreal.Tools;
14
15/// <summary>
16/// Version of Nuke's ProcessException which doesn't output the log by default
17/// </summary>
18public class UnrealProcessException(IProcess process, bool includeOutputInMessage = false)
19 : Exception(FormatMessage(process, includeOutputInMessage))
20{
21 private static string FormatMessage(IProcess process, bool includeOutputInMessage)
22 {
23 const string indentation = " ";
24
25 var messageBuilder = new StringBuilder()
26 .AppendLine($"Process '{Path.GetFileName(process.FileName)}' exited with code {process.ExitCode}.")
27 .AppendLine($"{indentation}> {process.FileName.DoubleQuoteIfNeeded()} {process.Arguments}")
28 .AppendLine($"{indentation}@ {process.WorkingDirectory}");
29
30 if (includeOutputInMessage)
31 {
32 var errorOutput = process.Output.Where(x => x.Type == OutputType.Err).ToList();
33 if (errorOutput.Count > 0)
34 {
35 messageBuilder.AppendLine("Error output:");
36 errorOutput.ForEach(x => messageBuilder.Append(indentation).AppendLine(x.Text));
37 }
38 else if (Log.IsEnabled(LogEventLevel.Verbose))
39 {
40 messageBuilder.AppendLine("Standard output:");
41 process.Output.ForEach(x => messageBuilder.Append(indentation).AppendLine(x.Text));
42 }
43 }
44
45 return messageBuilder.ToString();
46 }
47
48 internal IProcess Process { get; } = process;
49
50 public int ExitCode { get; } = process.ExitCode;
51}
52
53public static class UnrealProcessAssert
54{
55 [AssertionMethod]
56 public static IProcess AssertZeroExitCodeNoLog(this IProcess process, bool includeOutputInMessage = false)
57 {
58 process.AssertWaitForExit();
59
60 if (process.ExitCode != 0)
61 throw new UnrealProcessException(process, includeOutputInMessage);
62
63 return process;
64 }
65}
class UnrealProcessException(IProcess process, bool includeOutputInMessage=false)
Version of Nuke's ProcessException which doesn't output the log by default.