Skip to content

Commit

Permalink
Unity Editor log is now forwarded to output if build error happens.
Browse files Browse the repository at this point in the history
  • Loading branch information
kolesnick committed Jan 21, 2019
1 parent b5fb9b0 commit 557499e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Cake.Unity/UnityAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// <summary>
Expand Down
77 changes: 69 additions & 8 deletions src/Cake.Unity/UnityEditor.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,89 @@
using System.Collections.Generic;
using System.IO;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Core.Tooling;

namespace Cake.Unity
{
internal class UnityEditor : Tool<UnityEditorSettings>
{
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<string> 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<string> 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.");
}
}
}
}

0 comments on commit 557499e

Please sign in to comment.