-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathBuildPipeline.cs
222 lines (189 loc) · 6.64 KB
/
BuildPipeline.cs
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using UnityEngine;
using Ionic.Zip;
using UnityEditor;
using UnityEditor.Callbacks;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using clojure.lang;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
namespace Arcadia
{
public static class BuildPipeline
{
static int buildProgressCurrent = 1;
static float buildProgressTotal = 0;
public static string CompiledFolder = Path.Combine("Arcadia", "Compiled");
public static string ExportFolder = Path.Combine("Arcadia", "Export");
public static string ExportAssetsFolder = Path.Combine(BasicPaths.ArcadiaFolder, "Export");
static BuildPipeline ()
{
Util.require("arcadia.internal.editor-interop");
Util.require("arcadia.internal.config");
//EditorUtility.ClearProgressBar();
}
public static void EnsureCompiledFolders ()
{
UnityEngine.Debug.Log("Creating Compiled Folders");
if (!Directory.Exists(CompiledFolder))
Directory.CreateDirectory(CompiledFolder);
}
public static void EnsureEmptyFolder (string folder)
{
if (!Directory.Exists(folder)) {
Directory.CreateDirectory(folder);
} else {
Directory.Delete(folder, true);
Directory.CreateDirectory(folder);
}
}
public static void EnsureExportFolders ()
{
EnsureEmptyFolder(ExportFolder);
EnsureEmptyFolder(ExportAssetsFolder);
}
static void ResetProgress (float newTotal)
{
buildProgressCurrent = 1;
buildProgressTotal = newTotal;
EditorUtility.ClearProgressBar();
}
static bool Progress (string message)
{
return Progress("Arcadia", message);
}
static bool Progress (string title, string message)
{
return EditorUtility.DisplayCancelableProgressBar(title, message, buildProgressCurrent++ / buildProgressTotal);
}
public static bool IsAssemblyExportable (Assembly assembly)
{
return IsAssemblyExportable(assembly, PersistentHashSet.EMPTY);
}
public static bool IsAssemblyExportable (Assembly assembly, IPersistentSet checkedAssemblies)
{
if (checkedAssemblies.contains(assembly))
return true;
foreach (var referencedAssembly in assembly.GetReferencedAssemblies()) {
if (referencedAssembly.Name == "UnityEditor" || referencedAssembly.Name == "Assembly-CSharp-Editor") {
return false;
}
if (!IsAssemblyExportable(Assembly.Load(referencedAssembly), (IPersistentSet)checkedAssemblies.cons(assembly))) {
return false;
}
}
return true;
}
static string NamespaceFileName (string name)
{
return name.Replace("-", "_") + ".clj.dll";
}
static string NamespaceFileName (Symbol name)
{
return NamespaceFileName(name.ToString());
}
static string NamespaceFileName (Namespace ns)
{
return NamespaceFileName(ns.Name);
}
static Var aotNamespacesVar;
public class NsProgressReporter : clojure.lang.AFn
{
public override object invoke (object arg1)
{
Symbol nsSymbol = (Symbol)arg1;
Progress("Arcadia", "Compiling " + nsSymbol);
return null;
}
}
static void CompileNamespacesToFolder (IEnumerable userNameSpaces, string targetFolder)
{
// Initialization.SetBuildClojureLoadPath();
ResetProgress(userNameSpaces.Cast<object>().Count());
try {
string compilerNs = "arcadia.internal.compiler";
Arcadia.Util.require(compilerNs);
Arcadia.Util.getVar(ref aotNamespacesVar, compilerNs, "aot-namespaces");
Arcadia.Util.Invoke(
aotNamespacesVar,
targetFolder,
userNameSpaces,
RT.mapUniqueKeys(new object[] {
Keyword.intern(Symbol.intern("file-callback")),
new NsProgressReporter()
})
);
} finally {
EditorUtility.ClearProgressBar();
}
}
public static void BuildAll ()
{
EnsureCompiledFolders();
IList internalAndUserNameSpaces = (IList)RT.var("arcadia.internal.editor-interop", "internal-and-user-aot-root-namespaces").invoke();
CompileNamespacesToFolder(internalAndUserNameSpaces, CompiledFolder);
}
public static void PrepareExport ()
{
EnsureExportFolders();
var oldLoadPath = System.Environment.GetEnvironmentVariable("CLOJURE_LOAD_PATH");
try {
var newLoadPath = oldLoadPath.Replace(Path.GetFullPath(CompiledFolder) + Path.PathSeparator, "");
UnityEngine.Debug.Log("newLoadPath: " + newLoadPath);
System.Environment.SetEnvironmentVariable("CLOJURE_LOAD_PATH", newLoadPath);
var userNamespaces = ((IList)RT.var("arcadia.internal.editor-interop", "user-export-namespaces-symbols").invoke()).Cast<Symbol>();
CompileNamespacesToFolder(userNamespaces, ExportFolder);
var filesToExport = Directory.GetFiles(ExportFolder);
foreach (var src in filesToExport) {
var filename = Path.GetFileName(src);
var dst = Path.Combine(ExportAssetsFolder, filename);
File.Copy(src, dst);
}
UnityEngine.Debug.Log("Ready to build!");
} finally {
System.Environment.SetEnvironmentVariable("CLOJURE_LOAD_PATH", oldLoadPath);
}
AssetDatabase.Refresh(ImportAssetOptions.Default);
}
public static void CleanCompiled ()
{
if (Directory.Exists(CompiledFolder))
Directory.Delete(CompiledFolder, true);
}
// https://forum.unity.com/threads/postprocessbuild-preprocessbuild.293616/
static string GetDataManagedFolder(BuildTarget target, string pathToBuiltProject)
{
if (target.ToString ().Contains ("OSX"))
{
return pathToBuiltProject+"/Contents/Resources/Data/Managed/";
}
if (target.ToString ().Contains ("Windows"))
{
string name = Path.GetFileNameWithoutExtension(pathToBuiltProject);
string directory = Path.GetDirectoryName(pathToBuiltProject);
return BasicPaths.PathCombine(directory, name + "_Data", "Managed");
}
if (target.ToString ().Contains ("Linux"))
{
string name = Path.GetFileNameWithoutExtension(pathToBuiltProject);
string directory = Path.GetDirectoryName(pathToBuiltProject);
return BasicPaths.PathCombine(directory, name + "_Data", "Managed");
}
UnityEngine.Debug.Log(string.Format("Exported configuration for target {0} not supported. Configuration will not be usable in export.", target));
return Path.GetDirectoryName(pathToBuiltProject);
}
[PostProcessBuild(1)]
public static void OnPostprocessBuild (BuildTarget target, string pathToBuiltProject)
{
var configString = RT.var("arcadia.internal.config", "config").invoke().ToString();
var managedFolder = GetDataManagedFolder(target, pathToBuiltProject);
File.WriteAllText(Path.Combine(managedFolder, "exported-configuration.edn"), configString);
if (Directory.Exists(ExportFolder))
Directory.Delete(ExportFolder, true);
if (Directory.Exists(ExportAssetsFolder))
Directory.Delete(ExportAssetsFolder, true);
}
}
}