diff --git a/src/Cake.Unity/UnityAliases.cs b/src/Cake.Unity/UnityAliases.cs index d20e078..9e00338 100644 --- a/src/Cake.Unity/UnityAliases.cs +++ b/src/Cake.Unity/UnityAliases.cs @@ -48,7 +48,7 @@ public static void UnityBuild(this ICakeContext context, DirectoryPath projectPa [CakeMethodAlias] [CakeAliasCategory("Build")] public static void UnityEditor(this ICakeContext context, FilePath unityEditorPath, UnityEditorArguments arguments) => - new UnityEditor(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools) + new UnityEditor(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log) .Run(unityEditorPath, arguments); /// diff --git a/src/Cake.Unity/UnityEditor.cs b/src/Cake.Unity/UnityEditor.cs index 71aed88..a8b3541 100644 --- a/src/Cake.Unity/UnityEditor.cs +++ b/src/Cake.Unity/UnityEditor.cs @@ -1,5 +1,7 @@ using System.Collections.Generic; +using System.IO; using Cake.Core; +using Cake.Core.Diagnostics; using Cake.Core.IO; using Cake.Core.Tooling; @@ -7,22 +9,81 @@ namespace Cake.Unity { internal class UnityEditor : Tool { + private readonly IFileSystem fileSystem; private readonly ICakeEnvironment environment; + private readonly ICakeLog log; - public UnityEditor(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools) - : base(fileSystem, environment, processRunner, tools) => + public UnityEditor(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator tools, ICakeLog log) + : base(fileSystem, environment, processRunner, tools) + { + this.fileSystem = fileSystem; this.environment = environment; + this.log = log; + } protected override string GetToolName() => "Unity Editor"; protected override IEnumerable GetToolExecutableNames() => new[] { "Unity.exe" }; - public void Run(FilePath unityEditorPath, UnityEditorArguments arguments) => - Run( - new UnityEditorSettings(arguments, environment) + public void Run(FilePath unityEditorPath, UnityEditorArguments arguments) + { + WarnIfLogLocationNotSet(arguments); + + try + { + Run(new UnityEditorSettings(arguments, environment) { ToolPath = unityEditorPath }); + } + catch + { + if (arguments.LogFile == null) + { + log.Error("Execution of Unity Editor failed."); + log.Warning("Cannot forward log file to output because LogFile argument is missing."); + } + else { - ToolPath = unityEditorPath, - }, - new ProcessArgumentBuilder()); + log.Error("Execution of Unity Editor failed."); + log.Error("Please analyze log below for the reasons of failure."); + ForwardLogFileToOutput(arguments.LogFile); + } + + throw; + } + } + + private void Run(UnityEditorSettings settings) => Run(settings, new ProcessArgumentBuilder()); + + private void ForwardLogFileToOutput(FilePath logPath) + { + var logFile = fileSystem.GetFile(logPath); + + if (!logFile.Exists) + { + log.Warning("Unity Editor log file not found: {0}", logPath); + return; + } + + foreach (var line in ReadLogSafe(logFile)) + { + log.Information(line); + } + } + + private static IEnumerable ReadLogSafe(IFile file) + { + using (var stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + using (var reader = new StreamReader(stream)) + while (!reader.EndOfStream) + yield return reader.ReadLine(); + } + + private void WarnIfLogLocationNotSet(UnityEditorArguments arguments) + { + if (arguments.LogFile == null) + { + log.Warning("LogFile is not specified by Unity Editor arguments."); + log.Warning("Please specify it for ability to forward Unity log to console."); + } + } } }