-
Notifications
You must be signed in to change notification settings - Fork 40
/
utility.cake
180 lines (162 loc) · 5.37 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// 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";
}
}
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;
}
// appType usually "Puppet" or "Demo"
static string GetBuildFolder(string appType, string projectPath)
{
return projectPath + "/" + Statics.TemporaryPrefix + appType + "Builds";
}
static void ExecuteUnityMethod(string buildMethodName, string buildTarget = null, string projectPath = ".")
{
Statics.Context.Information("Executing method " + buildMethodName + ", this could take a while...");
var command = "-executeMethod " + buildMethodName;
if (buildTarget != null)
{
command += " -buildTarget " + buildTarget;
}
var result = ExecuteUnityCommand(command, projectPath);
if (result != 0)
{
throw new Exception("Failed to execute method " + buildMethodName + ".");
}
}
// Copy files to a clean directory using string names instead of FilePath[] and DirectoryPath
static void CopyFiles(IEnumerable<string> files, string targetDirectory, bool clean = true)
{
if (clean)
{
CleanDirectory(targetDirectory);
}
foreach (var file in files)
{
Statics.Context.CopyFile(file, targetDirectory + "/" + System.IO.Path.GetFileName(file));
}
}
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);
}
static IEnumerable<string> GetBuiltPackages()
{
var files = Statics.Context.GetFiles("output/*.unitypackage");
foreach (var file in files)
{
if (!file.FullPath.Contains("AppCenter-v"))
{
yield return file.FullPath;
}
}
}
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 });
}
});