forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssemblyBuilder.cs
More file actions
144 lines (115 loc) · 4.69 KB
/
Copy pathAssemblyBuilder.cs
File metadata and controls
144 lines (115 loc) · 4.69 KB
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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using UnityEditor.Scripting.ScriptCompilation;
namespace UnityEditor.Compilation
{
public enum AssemblyBuilderStatus
{
NotStarted = 0,
IsCompiling = 1,
Finished = 2,
}
[Flags]
public enum AssemblyBuilderFlags
{
None = 0,
EditorAssembly = 1,
DevelopmentBuild = 2,
};
public class AssemblyBuilder
{
public event Action<string> buildStarted;
public event Action<string, CompilerMessage[]> buildFinished;
public string[] scriptPaths { get; private set; }
public string assemblyPath { get; private set; }
public string[] additionalDefines { get; set; }
public string[] additionalReferences { get; set; }
public string[] excludeReferences { get; set; }
public ScriptCompilerOptions compilerOptions { get; set; }
public AssemblyBuilderFlags flags { get; set; }
public BuildTargetGroup buildTargetGroup { get; set; }
public BuildTarget buildTarget { get; set; }
CompilationTask compilationTask;
public AssemblyBuilder(string assemblyPath, params string[] scriptPaths)
{
if (string.IsNullOrEmpty(assemblyPath))
throw new ArgumentException("assemblyPath cannot be null or empty");
if (scriptPaths == null || scriptPaths.Length == 0)
throw new ArgumentException("scriptPaths cannot be null or empty");
this.scriptPaths = scriptPaths;
this.assemblyPath = assemblyPath;
compilerOptions = new ScriptCompilerOptions();
flags = AssemblyBuilderFlags.None;
buildTargetGroup = EditorUserBuildSettings.activeBuildTargetGroup;
buildTarget = EditorUserBuildSettings.activeBuildTarget;
}
public bool Build()
{
return Build(EditorCompilationInterface.Instance);
}
internal bool Build(EditorCompilation editorCompilation)
{
if (editorCompilation.IsCompilationTaskCompiling())
return false;
if (status != AssemblyBuilderStatus.NotStarted)
throw new Exception(string.Format("Cannot start AssemblyBuilder with status {0}. Expected {1}", status, AssemblyBuilderStatus.NotStarted));
var scriptAssembly = editorCompilation.CreateScriptAssembly(this);
compilationTask = new CompilationTask(new ScriptAssembly[] { scriptAssembly }, scriptAssembly.OutputDirectory,
EditorScriptCompilationOptions.BuildingEmpty, CompilationTaskOptions.StopOnFirstError, 1);
compilationTask.OnCompilationStarted += (assembly, phase) =>
{
editorCompilation.InvokeAssemblyCompilationStarted(assemblyPath);
OnCompilationStarted(assembly, phase);
};
compilationTask.OnCompilationFinished += (assembly, messages) =>
{
editorCompilation.InvokeAssemblyCompilationFinished(assemblyPath, messages);
OnCompilationFinished(assembly, messages);
};
compilationTask.Poll();
editorCompilation.AddAssemblyBuilder(this);
return true;
}
public AssemblyBuilderStatus status
{
get
{
if (compilationTask == null)
return AssemblyBuilderStatus.NotStarted;
if (compilationTask.IsCompiling)
return compilationTask.Poll() ? AssemblyBuilderStatus.Finished : AssemblyBuilderStatus.IsCompiling;
return AssemblyBuilderStatus.Finished;
}
}
void OnCompilationStarted(ScriptAssembly assembly, int phase)
{
if (buildStarted == null)
return;
try
{
buildStarted(assemblyPath);
}
catch (Exception e)
{
UnityEngine.Debug.LogException(e);
}
}
void OnCompilationFinished(ScriptAssembly assembly, List<UnityEditor.Scripting.Compilers.CompilerMessage> messages)
{
if (buildFinished == null)
return;
var convertedMessages = EditorCompilation.ConvertCompilerMessages(messages);
try
{
buildFinished(assemblyPath, convertedMessages);
}
catch (Exception e)
{
UnityEngine.Debug.LogException(e);
}
}
}
}