Skip to content

Commit 8f3a32d

Browse files
committed
Show error and warning message in Unity Editor
1 parent 5f2377b commit 8f3a32d

File tree

4 files changed

+76
-38
lines changed

4 files changed

+76
-38
lines changed

core/IncrementalCompiler/CompileOptions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ namespace IncrementalCompiler
88
[DataContract]
99
public class CompileOptions
1010
{
11+
[DataMember] public string WorkDirectory;
1112
[DataMember] public string AssemblyName;
1213
[DataMember] public string Output;
1314
[DataMember] public List<string> Defines = new List<string>();
1415
[DataMember] public List<string> References = new List<string>();
1516
[DataMember] public List<string> Files = new List<string>();
1617

17-
public void ParseArgument(string[] args, string currentPath)
18+
public void ParseArgument(string[] args)
1819
{
1920
foreach (var arg in args)
2021
{
@@ -39,29 +40,28 @@ public void ParseArgument(string[] args, string currentPath)
3940
{
4041
case "r":
4142
case "reference":
42-
References.Add(Path.Combine(currentPath, value.Trim('"')));
43+
References.Add(value.Trim('"'));
4344
break;
4445

4546
case "define":
4647
Defines.Add(value);
4748
break;
4849

4950
case "out":
50-
Output = Path.Combine(currentPath, value.Trim('"'));
51+
Output = value.Trim('"');
5152
AssemblyName = Path.GetFileNameWithoutExtension(value);
5253
break;
5354
}
5455
}
5556
else if (arg.StartsWith("@"))
5657
{
5758
// more options in specified file
58-
var argPath = Path.Combine(currentPath, arg.Substring(1));
59-
var lines = File.ReadAllLines(argPath);
60-
ParseArgument(lines, currentPath);
59+
var lines = File.ReadAllLines(arg.Substring(1));
60+
ParseArgument(lines);
6161
}
6262
else
6363
{
64-
var path = Path.Combine(currentPath, arg.Trim('"'));
64+
var path = arg.Trim('"');
6565
Files.Add(path);
6666
}
6767
}

core/IncrementalCompiler/Compiler.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ public class Compiler
2222
public CompileResult Build(CompileOptions options)
2323
{
2424
if (_compilation == null ||
25+
_options.WorkDirectory != options.WorkDirectory ||
2526
_options.AssemblyName != options.AssemblyName ||
2627
_options.Output != options.Output ||
27-
Enumerable.SequenceEqual(_options.Defines, options.Defines) == false)
28+
_options.Defines.SequenceEqual(options.Defines) == false)
2829
{
2930
return BuildFull(options);
3031
}
@@ -57,9 +58,9 @@ private CompileResult BuildFull(CompileOptions options)
5758

5859
_compilation = CSharpCompilation.Create(
5960
options.AssemblyName,
60-
syntaxTrees: _sourceMap.Values,
61-
references: _referenceMap.Values,
62-
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
61+
_sourceMap.Values,
62+
_referenceMap.Values,
63+
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
6364

6465
Emit(result);
6566

@@ -126,40 +127,41 @@ private CompileResult BuildIncremental(CompileOptions options)
126127

127128
private MetadataReference CreateReference(string file)
128129
{
129-
return MetadataReference.CreateFromFile(file);
130+
return MetadataReference.CreateFromFile(Path.Combine(_options.WorkDirectory, file));
130131
}
131132

132133
private SyntaxTree ParseSource(string file, CSharpParseOptions parseOption)
133134
{
134-
return CSharpSyntaxTree.ParseText(File.ReadAllText(file),
135-
parseOption,
136-
file,
137-
Encoding.UTF8);
135+
var text = File.ReadAllText(Path.Combine(_options.WorkDirectory, file));
136+
return CSharpSyntaxTree.ParseText(text, parseOption, file, Encoding.UTF8);
138137
}
139138

140139
private void Emit(CompileResult result)
141140
{
142-
using (var peStream = new FileStream(_options.Output, FileMode.Create))
143-
using (var pdbStream = new FileStream(Path.ChangeExtension(_options.Output, ".pdb"), FileMode.Create))
141+
var outputFile = Path.Combine(_options.WorkDirectory, _options.Output);
142+
var debugFile = Path.Combine(_options.WorkDirectory, Path.ChangeExtension(_options.Output, ".pdb"));
143+
using (var peStream = new FileStream(outputFile, FileMode.Create))
144+
using (var pdbStream = new FileStream(debugFile, FileMode.Create))
144145
{
145146
var r = _compilation.Emit(peStream, pdbStream);
146147

147148
foreach (var d in r.Diagnostics)
148149
{
149150
if (d.Severity == DiagnosticSeverity.Warning && d.IsWarningAsError == false)
150-
result.Warnings.Add(GetDiagnosticString(d));
151+
result.Warnings.Add(GetDiagnosticString(d, "warning"));
151152
else if (d.Severity == DiagnosticSeverity.Error || d.IsWarningAsError)
152-
result.Errors.Add(GetDiagnosticString(d));
153+
result.Errors.Add(GetDiagnosticString(d, "error"));
153154
}
154155

155156
result.Succeeded = r.Success;
156157
}
157158
}
158159

159-
private static string GetDiagnosticString(Diagnostic diagnostic)
160+
private static string GetDiagnosticString(Diagnostic diagnostic, string type)
160161
{
161162
var line = diagnostic.Location.GetLineSpan();
162-
return $"{line.Path}({line.StartLinePosition.Line + 1}): {diagnostic.Id} {diagnostic.GetMessage()}";
163+
return $"{line.Path}({line.StartLinePosition.Line + 1},{line.StartLinePosition.Character + 1}): " +
164+
$"{type} {diagnostic.Id}: {diagnostic.GetMessage()}";
163165
}
164166
}
165167
}

core/IncrementalCompiler/Program.Dev.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ partial class Program
1515
{
1616
static int RunAsDev(string[] args)
1717
{
18-
SetupLogger("IncrementalCompiler.log");
18+
SetupLogger("IncrementalCompiler.log", true);
19+
20+
var workDirectory = args[1];
21+
var reponseFile = args[2];
1922

2023
var logger = LogManager.GetLogger("Dev");
2124
logger.Info("Started");
2225

23-
Directory.SetCurrentDirectory(@"..\..\..\..\samples\Basic");
26+
Directory.SetCurrentDirectory(workDirectory);
2427
var curPath = Directory.GetCurrentDirectory();
2528

2629
var options = new CompileOptions();
@@ -32,9 +35,9 @@ static int RunAsDev(string[] args)
3235
"-r:" + @"C:/Program Files/Unity/Editor/Data\Mono/lib/mono/2.0/System.dll",
3336
"-r:" + @"C:/Program Files/Unity/Editor/Data\Mono/lib/mono/2.0/System.Core.dll",
3437
"-r:" + @"C:/Program Files/Unity/Editor/Data\Mono/lib/mono/2.0/System.Xml.dll",
35-
@"@Temp/UnityTempFile-abde314464ae1bc4cae691cbf128cb26",
36-
},
37-
curPath);
38+
"@Temp/" + reponseFile,
39+
});
40+
options.WorkDirectory = curPath;
3841
options.References = options.References.Distinct().ToList();
3942
options.Files = options.Files.Distinct().ToList();
4043

@@ -94,7 +97,6 @@ static int RunAsDev(string[] args)
9497
return 1;
9598
}
9699
}
97-
return 0;
98100
}
99101
}
100102
}

core/IncrementalCompiler/Program.cs

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,44 @@ static int Main(string[] args)
2323
{
2424
return RunAsServer(args);
2525
}
26-
else
26+
else if (args.Length > 0 && args[0] != "/?")
2727
{
2828
return RunAsClient(args);
2929
}
30+
else
31+
{
32+
ShowUsage();
33+
return 1;
34+
}
35+
}
36+
37+
static void ShowUsage()
38+
{
39+
Console.WriteLine("Unity3D Incremental C# Compiler using Roslyn");
40+
Console.WriteLine("https://github.com/SaladbowlCreative/Unity3D.IncrementalCompiler/");
41+
Console.WriteLine("");
42+
Console.WriteLine("* Client");
43+
Console.WriteLine(" -out:<file> Specifies the output file name.");
44+
Console.WriteLine(" -r:<file> References metadata from the specified assembly files.");
45+
Console.WriteLine(" -define:<file> Defines conditional compilation symbols.");
46+
Console.WriteLine(" @<file> Reads a response file for more options.");
47+
Console.WriteLine("");
48+
Console.WriteLine("* Server");
49+
Console.WriteLine(" -server processid Spawn server for specified process.");
50+
Console.WriteLine("");
3051
}
3152

3253
static int RunAsClient(string[] args)
3354
{
34-
SetupLogger("IncrementalCompiler.log");
55+
SetupLogger("IncrementalCompiler.log", false);
3556

3657
var logger = LogManager.GetLogger("Client");
3758
logger.Info("Started");
3859

3960
var currentPath = Directory.GetCurrentDirectory();
4061
var options = new CompileOptions();
41-
options.ParseArgument(args, currentPath);
62+
options.ParseArgument(args);
63+
options.WorkDirectory = currentPath;
4264
options.References = options.References.Distinct().ToList();
4365
options.Files = options.Files.Distinct().ToList();
4466

@@ -87,10 +109,18 @@ static int RunAsClient(string[] args)
87109
var result = CompilerServiceClient.Request(parentProcessId, currentPath, options);
88110
w.Stop();
89111
logger.Info("Done: Succeeded={0}. Duration={1}sec.", result.Succeeded, w.Elapsed.TotalSeconds);
112+
Console.WriteLine("Compile {0}. (Duration={1}sec)", result.Succeeded ? "succeeded" : "failed",
113+
w.Elapsed.TotalSeconds);
90114
foreach (var warning in result.Warnings)
115+
{
91116
logger.Info(warning);
117+
Console.Error.WriteLine(warning);
118+
}
92119
foreach (var error in result.Errors)
120+
{
93121
logger.Info(error);
122+
Console.Error.WriteLine(error);
123+
}
94124
return result.Succeeded ? 0 : 1;
95125
}
96126
catch (EndpointNotFoundException)
@@ -118,14 +148,15 @@ static int RunAsClient(string[] args)
118148
catch (Exception e)
119149
{
120150
logger.Error(e, "Error in request");
151+
Console.Error.WriteLine("Internal error: " + e);
121152
return 1;
122153
}
123154
}
124155
}
125156

126157
static int RunAsServer(string[] args)
127158
{
128-
SetupLogger("IncrementalCompiler-Server.log");
159+
SetupLogger("IncrementalCompiler-Server.log", false);
129160

130161
var logger = LogManager.GetLogger("Server");
131162
logger.Info("Started");
@@ -140,16 +171,19 @@ static int RunAsServer(string[] args)
140171
return CompilerServiceServer.Run(logger, parentProcessId);
141172
}
142173

143-
static void SetupLogger(string fileName)
174+
static void SetupLogger(string fileName, bool useConsole)
144175
{
145176
var config = new LoggingConfiguration();
146177

147-
var consoleTarget = new ColoredConsoleTarget
178+
if (useConsole)
148179
{
149-
Layout = @"${date}|${logger}|${message}|${exception:format=tostring}"
150-
};
151-
config.AddTarget("console", consoleTarget);
152-
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, consoleTarget));
180+
var consoleTarget = new ColoredConsoleTarget
181+
{
182+
Layout = @"${date}|${logger}|${message}|${exception:format=tostring}"
183+
};
184+
config.AddTarget("console", consoleTarget);
185+
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, consoleTarget));
186+
}
153187

154188
var logDirectory = Directory.Exists(".\\Temp") ? ".\\Temp\\" : ".\\";
155189
var fileTarget = new FileTarget

0 commit comments

Comments
 (0)