-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathutility.cake
131 lines (116 loc) · 3.85 KB
/
utility.cake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// This file contains various utilities that are or can be used by multiple cake scripts.
// Static variables defined outside of a class can cause issues.
public class Statics
{
// Cake context.
public static ICakeContext Context { get; set; }
// Prefix for temporary intermediates that are created by this script.
public const string TemporaryPrefix = "CAKE_SCRIPT_TEMP";
}
// Can't reference Context within the class, so set value outside
Statics.Context = Context;
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";
}
}
// Unity log file
var unityLogFile = "CAKE_SCRIPT_TEMPunity_build_log.log";
if (System.IO.File.Exists(unityLogFile))
{
unityLogFile += "1";
}
var unityArgs = "-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(0) && (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;
}
static void DeleteDirectoryIfExists(string directoryName)
{
if (Statics.Context.DirectoryExists(directoryName))
{
Statics.Context.DeleteDirectory(directoryName, new DeleteDirectorySettings() { Recursive = true });
}
}
static void DeleteFileIfExists(string fileName)
{
try
{
if (Statics.Context.FileExists(fileName))
{
Statics.Context.DeleteFile(fileName);
}
}
catch
{
Statics.Context.Information("Unable to delete file '" + fileName + "'.");
}
}
static void CleanDirectory(string directoryName)
{
DeleteDirectoryIfExists(directoryName);
Statics.Context.CreateDirectory(directoryName);
}
void HandleError(Exception exception)
{
RunTarget("clean");
throw new Exception("Error occurred, see inner exception for details", exception);
}
// Remove all temporary files and folders
Task("RemoveTemporaries").Does(()=>
{
DeleteFiles(Statics.TemporaryPrefix + "*");
var dirs = GetDirectories(Statics.TemporaryPrefix + "*");
foreach (var directory in dirs)
{
DeleteDirectory(directory, new DeleteDirectorySettings() { Recursive = true });
}
});