Skip to content

Commit ee3cae2

Browse files
committed
Add compile result
1 parent 9b52132 commit ee3cae2

File tree

8 files changed

+172
-45
lines changed

8 files changed

+172
-45
lines changed

core/IncrementalCompiler/CompilerOptions.cs renamed to core/IncrementalCompiler/CompileOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace IncrementalCompiler
77
{
88
[DataContract]
9-
public class CompilerOptions
9+
public class CompileOptions
1010
{
1111
[DataMember] public string AssemblyName;
1212
[DataMember] public string Output;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.Serialization;
4+
5+
namespace IncrementalCompiler
6+
{
7+
[DataContract]
8+
public class CompileResult
9+
{
10+
[DataMember] public bool Succeeded;
11+
[DataMember] public List<string> Warnings = new List<string>();
12+
[DataMember] public List<string> Errors = new List<string>();
13+
}
14+
}

core/IncrementalCompiler/Compiler.cs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,39 @@
55
using Microsoft.CodeAnalysis.CSharp;
66
using System.Linq;
77
using System.Text;
8+
using NLog;
89

910
namespace IncrementalCompiler
1011
{
1112
public class Compiler
1213
{
14+
private Logger _logger = LogManager.GetLogger("Compiler");
1315
private CSharpCompilation _compilation;
14-
private CompilerOptions _options;
16+
private CompileOptions _options;
1517
private FileTimeList _referenceFileList;
1618
private FileTimeList _sourceFileList;
1719
private Dictionary<string, MetadataReference> _referenceMap;
1820
private Dictionary<string, SyntaxTree> _sourceMap;
1921

20-
public void Build(CompilerOptions options)
22+
public CompileResult Build(CompileOptions options)
2123
{
2224
if (_compilation == null ||
2325
_options.AssemblyName != options.AssemblyName ||
2426
_options.Output != options.Output ||
2527
Enumerable.SequenceEqual(_options.Defines, options.Defines) == false)
2628
{
27-
BuildFull(options);
29+
return BuildFull(options);
2830
}
2931
else
3032
{
31-
BuildIncremental(options);
33+
return BuildIncremental(options);
3234
}
3335
}
3436

35-
private void BuildFull(CompilerOptions options)
37+
private CompileResult BuildFull(CompileOptions options)
3638
{
39+
var result = new CompileResult();
40+
3741
_options = options;
3842

3943
_referenceFileList = new FileTimeList();
@@ -57,11 +61,15 @@ private void BuildFull(CompilerOptions options)
5761
references: _referenceMap.Values,
5862
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
5963

60-
Emit();
64+
Emit(result);
65+
66+
return result;
6167
}
6268

63-
private void BuildIncremental(CompilerOptions options)
69+
private CompileResult BuildIncremental(CompileOptions options)
6470
{
71+
var result = new CompileResult();
72+
6573
_options = options;
6674

6775
// TODO: guard failure of compilation, ...
@@ -111,7 +119,9 @@ private void BuildIncremental(CompilerOptions options)
111119
_sourceMap.Remove(file);
112120
}
113121

114-
Emit();
122+
Emit(result);
123+
124+
return result;
115125
}
116126

117127
private MetadataReference CreateReference(string file)
@@ -127,30 +137,29 @@ private SyntaxTree ParseSource(string file, CSharpParseOptions parseOption)
127137
Encoding.UTF8);
128138
}
129139

130-
private void Emit()
140+
private void Emit(CompileResult result)
131141
{
132142
using (var peStream = new FileStream(_options.Output, FileMode.Create))
133143
using (var pdbStream = new FileStream(Path.ChangeExtension(_options.Output, ".pdb"), FileMode.Create))
134144
{
135-
var result = _compilation.Emit(peStream, pdbStream);
145+
var r = _compilation.Emit(peStream, pdbStream);
136146

137-
if (!result.Success)
147+
foreach (var d in r.Diagnostics)
138148
{
139-
var failures = result.Diagnostics.Where(diagnostic =>
140-
diagnostic.IsWarningAsError ||
141-
diagnostic.Severity == DiagnosticSeverity.Error);
142-
143-
foreach (var diagnostic in failures)
144-
{
145-
var line = diagnostic.Location.GetLineSpan();
146-
Console.Error.WriteLine("{0}({1}): {2} {3}",
147-
line.Path,
148-
line.StartLinePosition.Line + 1,
149-
diagnostic.Id,
150-
diagnostic.GetMessage());
151-
}
149+
if (d.Severity == DiagnosticSeverity.Warning && d.IsWarningAsError == false)
150+
result.Warnings.Add(GetDiagnosticString(d));
151+
else if (d.Severity == DiagnosticSeverity.Error || d.IsWarningAsError)
152+
result.Errors.Add(GetDiagnosticString(d));
152153
}
154+
155+
result.Succeeded = r.Success;
153156
}
154157
}
158+
159+
private static string GetDiagnosticString(Diagnostic diagnostic)
160+
{
161+
var line = diagnostic.Location.GetLineSpan();
162+
return $"{line.Path}({line.StartLinePosition.Line + 1}): {diagnostic.Id} {diagnostic.GetMessage()}";
163+
}
155164
}
156165
}

core/IncrementalCompiler/CompilerService.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
using NLog;
22
using System;
33
using System.Collections.Generic;
4-
using System.Diagnostics;
5-
using System.Linq;
64
using System.ServiceModel;
75

86
namespace IncrementalCompiler
97
{
10-
[ServiceContract(Namespace = "http://github.com/Unity3D.RoslynCompiler")]
8+
[ServiceContract(Namespace = "https://github.com/SaladbowlCreative/Unity3D.IncrementalCompiler")]
119
public interface ICompilerService
1210
{
1311
[OperationContract]
14-
bool Build(string projectPath, CompilerOptions options);
12+
CompileResult Build(string projectPath, CompileOptions options);
1513
}
1614

1715
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true)]
@@ -21,7 +19,7 @@ public class CompilerService : ICompilerService
2119
private string _projectPath;
2220
private Dictionary<string, Compiler> _compilerMap;
2321

24-
public bool Build(string projectPath, CompilerOptions options)
22+
public CompileResult Build(string projectPath, CompileOptions options)
2523
{
2624
_logger.Info("Build(projectPath={0}, output={1})", projectPath, options.Output);
2725

@@ -45,15 +43,13 @@ public bool Build(string projectPath, CompilerOptions options)
4543

4644
try
4745
{
48-
compiler.Build(options);
46+
return compiler.Build(options);
4947
}
5048
catch (Exception e)
5149
{
5250
_logger.Error(e, "Error in build.");
5351
throw;
5452
}
55-
56-
return true;
5753
}
5854
}
5955
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
using NLog;
2-
using System;
3-
using System.ServiceModel;
1+
using System.ServiceModel;
42

53
namespace IncrementalCompiler
64
{
75
public class CompilerServiceClient
86
{
9-
public static void Request(int parentProcessId, string currentPath, CompilerOptions options)
7+
public static CompileResult Request(int parentProcessId, string currentPath, CompileOptions options)
108
{
119
var address = "net.pipe://localhost/Unity3D.IncrementalCompiler/" + parentProcessId;
1210

@@ -17,7 +15,7 @@ public static void Request(int parentProcessId, string currentPath, CompilerOpti
1715
};
1816
var ep = new EndpointAddress(address);
1917
var channel = ChannelFactory<ICompilerService>.CreateChannel(binding, ep);
20-
channel.Build(currentPath, options);
18+
return channel.Build(currentPath, options);
2119
}
2220
}
2321
}

core/IncrementalCompiler/IncrementalCompiler.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@
6464
<Reference Include="System.Xml" />
6565
</ItemGroup>
6666
<ItemGroup>
67+
<Compile Include="CompileOptions.cs" />
6768
<Compile Include="Compiler.cs" />
68-
<Compile Include="CompilerOptions.cs" />
69+
<Compile Include="CompileResult.cs" />
6970
<Compile Include="CompilerService.cs" />
7071
<Compile Include="CompilerServiceClient.cs" />
7172
<Compile Include="CompilerServiceServer.cs" />
7273
<Compile Include="FileTimeList.cs" />
7374
<Compile Include="Program.cs" />
75+
<Compile Include="Program.Dev.cs" />
7476
<Compile Include="Properties\AssemblyInfo.cs" />
7577
</ItemGroup>
7678
<ItemGroup>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Threading;
7+
using NLog;
8+
using NLog.Config;
9+
using NLog.Targets;
10+
using System.ServiceModel;
11+
12+
namespace IncrementalCompiler
13+
{
14+
partial class Program
15+
{
16+
static int RunAsDev(string[] args)
17+
{
18+
SetupLogger("IncrementalCompiler.log");
19+
20+
var logger = LogManager.GetLogger("Dev");
21+
logger.Info("Started");
22+
23+
Directory.SetCurrentDirectory(@"..\..\..\..\samples\Basic");
24+
var curPath = Directory.GetCurrentDirectory();
25+
26+
var options = new CompileOptions();
27+
options.ParseArgument(new string[]
28+
{
29+
"-nostdlib+",
30+
"-noconfig",
31+
"-r:" + @"C:/Program Files/Unity/Editor/Data\Mono/lib/mono/2.0/mscorlib.dll",
32+
"-r:" + @"C:/Program Files/Unity/Editor/Data\Mono/lib/mono/2.0/System.dll",
33+
"-r:" + @"C:/Program Files/Unity/Editor/Data\Mono/lib/mono/2.0/System.Core.dll",
34+
"-r:" + @"C:/Program Files/Unity/Editor/Data\Mono/lib/mono/2.0/System.Xml.dll",
35+
@"@Temp/UnityTempFile-abde314464ae1bc4cae691cbf128cb26",
36+
},
37+
curPath);
38+
options.References = options.References.Distinct().ToList();
39+
options.Files = options.Files.Distinct().ToList();
40+
41+
var parentProcessId = Process.GetCurrentProcess().Id;
42+
43+
Process serverProcess = null;
44+
45+
while (true)
46+
{
47+
try
48+
{
49+
var w = new Stopwatch();
50+
w.Start();
51+
Console.WriteLine("Run");
52+
53+
var result = CompilerServiceClient.Request(parentProcessId, curPath, options);
54+
55+
w.Stop();
56+
57+
Console.WriteLine("Done: Succeeded={0}. Duration={1}sec. ", result.Succeeded, w.Elapsed.TotalSeconds);
58+
foreach (var warning in result.Warnings)
59+
Console.WriteLine(warning);
60+
foreach (var error in result.Errors)
61+
Console.WriteLine(error);
62+
63+
Console.ReadLine();
64+
}
65+
catch (EndpointNotFoundException)
66+
{
67+
if (serverProcess == null)
68+
{
69+
var a = new Thread(() => CompilerServiceServer.Run(logger, parentProcessId));
70+
a.Start();
71+
serverProcess = Process.GetCurrentProcess();
72+
/*
73+
serverProcess = Process.Start(
74+
new ProcessStartInfo
75+
{
76+
FileName = Assembly.GetEntryAssembly().Location,
77+
Arguments = "-server " + parentProcessId,
78+
WindowStyle = ProcessWindowStyle.Hidden
79+
});
80+
*/
81+
Thread.Sleep(100);
82+
}
83+
else
84+
{
85+
if (serverProcess.HasExited == false)
86+
Thread.Sleep(100);
87+
else
88+
serverProcess = null;
89+
}
90+
}
91+
catch (Exception e)
92+
{
93+
Console.WriteLine(e);
94+
return 1;
95+
}
96+
}
97+
return 0;
98+
}
99+
}
100+
}

core/IncrementalCompiler/Program.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111

1212
namespace IncrementalCompiler
1313
{
14-
class Program
14+
partial class Program
1515
{
1616
static int Main(string[] args)
1717
{
18-
if (args.Length > 0 && args[0] == "-server")
18+
if (args.Length > 0 && args[0] == "-dev")
19+
{
20+
return RunAsDev(args);
21+
}
22+
else if (args.Length > 0 && args[0] == "-server")
1923
{
2024
return RunAsServer(args);
2125
}
@@ -33,7 +37,7 @@ static int RunAsClient(string[] args)
3337
logger.Info("Started");
3438

3539
var currentPath = Directory.GetCurrentDirectory();
36-
var options = new CompilerOptions();
40+
var options = new CompileOptions();
3741
options.ParseArgument(args, currentPath);
3842
options.References = options.References.Distinct().ToList();
3943
options.Files = options.Files.Distinct().ToList();
@@ -80,10 +84,14 @@ static int RunAsClient(string[] args)
8084
var w = new Stopwatch();
8185
w.Start();
8286
logger.Info("Request to server");
83-
CompilerServiceClient.Request(parentProcessId, currentPath, options);
87+
var result = CompilerServiceClient.Request(parentProcessId, currentPath, options);
8488
w.Stop();
85-
logger.Info("Done: " + w.Elapsed.TotalSeconds + "sec");
86-
return 0;
89+
logger.Info("Done: Succeeded={0}. Duration={1}sec.", result.Succeeded, w.Elapsed.TotalSeconds);
90+
foreach (var warning in result.Warnings)
91+
logger.Info(warning);
92+
foreach (var error in result.Errors)
93+
logger.Info(error);
94+
return result.Succeeded ? 0 : 1;
8795
}
8896
catch (EndpointNotFoundException)
8997
{

0 commit comments

Comments
 (0)