static int ExecuteUnityCommand()

in utility.cake [16:88]


static int ExecuteUnityCommand(string extraArgs, string projectPath = ".")
{
    var projectDir = projectPath == null ? null : Statics.Context.MakeAbsolute(Statics.Context.Directory(projectPath));
    var unityPath = Statics.Context.EnvironmentVariable("UNITY_PATH");

    // If environment variable is not set, use default locations
    if (unityPath == null)
    {
        if (Statics.Context.IsRunningOnUnix())
        {
            unityPath = "/Applications/Unity/Unity.app/Contents/MacOS/Unity";
        }
        else
        {
            unityPath = "C:\\Program Files\\Unity\\Editor\\Unity.exe";
        }
    }
    if (!System.IO.File.Exists(unityPath)) 
    {
        throw new Exception("Unity installation does not exist under " + unityPath);
    }

    // Unity log file
    var unityLogFile = "CAKE_SCRIPT_TEMPunity_build_log.log";
    if (System.IO.File.Exists(unityLogFile))
    {
        unityLogFile += "1";
    }
    var unityArgs = "-nographics -batchmode -quit -logFile " + unityLogFile;

    // If the command has an associated project, add it to the arguments
    if (projectDir != null)
    {
        unityArgs += " -projectPath " + projectDir;
    }

    unityArgs += " " + extraArgs;

    System.IO.File.Create(unityLogFile).Dispose();
    var logExec = "powershell.exe";
    var logArgs = "Get-Content -Path " + unityLogFile + " -Wait";
    if (Statics.Context.IsRunningOnUnix())
    {
        logExec = "tail";
        logArgs = "-f " + unityLogFile;
    }
    int result = 0;
    using (var unityProcess = Statics.Context.StartAndReturnProcess(unityPath, new ProcessSettings{ Arguments = unityArgs }))
    {
        using (var logProcess = Statics.Context.StartAndReturnProcess(logExec, new ProcessSettings{ Arguments = logArgs, RedirectStandardError = true}))
        {
            unityProcess.WaitForExit();
            result = unityProcess.GetExitCode();
            if (logProcess.WaitForExit(1000) && (logProcess.GetExitCode() != 0))
            {
                Statics.Context.Warning("There was an error logging, but command still executed.");
            }
            else
            {
                try
                {
                    logProcess.Kill();
                }
                catch
                {
                    // Log process was stopped right after checking
                }
            }
        }
    }
    DeleteFileIfExists(unityLogFile);
    return result;
}