forked from msgpack/msgpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeDomCodeGeneration.cs
More file actions
98 lines (89 loc) · 2.82 KB
/
CodeDomCodeGeneration.cs
File metadata and controls
98 lines (89 loc) · 2.82 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
#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2016 FUJIWARA, Yusuke
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion -- License Terms --
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
namespace MsgPack.Serialization
{
/// <summary>
/// Defines CodeDOM based code generation logic for unit testing.
/// </summary>
internal static class CodeDomCodeGeneration
{
private static int _suffix = 0;
public static void Compile( string code, bool isDebug, out Assembly compiledAssembly, out IList<string> errors, out IList<string> warnings )
{
var assemblyName = "CodeGenerationAssembly" + Interlocked.Increment( ref _suffix );
var assemblyPath = Path.Combine( SerializerDebugging.DumpDirectory ?? Path.GetTempPath(), assemblyName + ".dll" );
using ( var provider = CodeDomProvider.CreateProvider( "C#" ) )
{
var parameters =
new CompilerParameters
{
GenerateInMemory = false,
OutputAssembly = assemblyPath
};
foreach ( var assembly in SerializerDebugging.CodeSerializerDependentAssemblies )
{
parameters.ReferencedAssemblies.Add( ( string ) assembly );
}
var result =
provider.CompileAssemblyFromSource(
parameters,
code
);
errors = BuildCompilationError( result.Errors.OfType<CompilerError>().Where( d => !d.IsWarning ) );
warnings = BuildCompilationError( result.Errors.OfType<CompilerError>().Where( d => d.IsWarning ) );
if ( !errors.Any() )
{
SerializerDebugging.AddCompiledCodeAssembly( result.PathToAssembly );
compiledAssembly = result.CompiledAssembly;
}
else
{
compiledAssembly = null;
}
}
}
private static IList<string> BuildCompilationError( IEnumerable<CompilerError> diagnostics )
{
return
diagnostics.Select(
( diagnostic, i ) =>
String.Format(
CultureInfo.InvariantCulture,
"[{0}]{1}:{2}:(File:{3}, Line:{4}, Column:{5}):{6}",
i,
diagnostic.IsWarning ? "Warn" : "Error",
diagnostic.ErrorNumber,
diagnostic.FileName,
diagnostic.Line,
diagnostic.Column,
diagnostic.ErrorText
)
).ToArray();
}
}
}