Skip to content

Commit 16a97b8

Browse files
Implement public constructors that exist in parent so our providers can slip right into existing code that uses the old CodeDomProviders. (#134)
1 parent e614826 commit 16a97b8

3 files changed

Lines changed: 81 additions & 7 deletions

File tree

src/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/CSharpCodeProvider.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.CodeDom.Compiler;
6+
using System.Collections.Generic;
67
using System.ComponentModel;
78

89
namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatform {
@@ -37,6 +38,13 @@ public CSharpCodeProvider(IProviderOptions providerOptions = null) {
3738
_providerOptions = providerOptions == null ? CompilationUtil.CSC2 : providerOptions;
3839
}
3940

41+
/// <summary>
42+
/// Creates an instance using the given IDictionary to create IProviderOptions
43+
/// </summary>
44+
/// <param name="providerOptions"></param>
45+
public CSharpCodeProvider(IDictionary<string, string> providerOptions)
46+
: this(CompilationUtil.CreateProviderOptions(providerOptions, CompilationUtil.CSC2)) { }
47+
4048
/// <summary>
4149
/// Gets an instance of the .NET Compiler Platform C# code compiler.
4250
/// </summary>

src/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/Util/CompilationUtil.cs

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,56 @@ static CompilationUtil()
3030

3131
public static IProviderOptions VBC2 { get; }
3232

33+
internal static IProviderOptions CreateProviderOptions(IDictionary<string, string> options, IProviderOptions baseOptions)
34+
{
35+
Dictionary<string, string> allOptions = null;
36+
37+
// Copy the base options
38+
ProviderOptions providerOpts = new ProviderOptions(baseOptions);
39+
40+
// Update as necessary. Case-sensitive.
41+
foreach (var option in options)
42+
{
43+
if (String.IsNullOrWhiteSpace(option.Key))
44+
continue;
45+
46+
switch (option.Key)
47+
{
48+
case "CompilerFullPath":
49+
providerOpts.CompilerFullPath = option.Value;
50+
break;
51+
52+
case "CompilerServerTimeToLive":
53+
if (Int32.TryParse(option.Value, out int newTTL))
54+
providerOpts.CompilerServerTimeToLive = newTTL;
55+
break;
56+
57+
case "CompilerVersion":
58+
providerOpts.CompilerVersion = option.Value;
59+
break;
60+
61+
case "WarnAsError":
62+
if (Boolean.TryParse(option.Value, out bool warnAsError))
63+
providerOpts.WarnAsError = warnAsError;
64+
break;
65+
66+
case "AllOptions":
67+
allOptions = allOptions ?? new Dictionary<string, string>(providerOpts.AllOptions);
68+
allOptions.Remove(option.Key);
69+
allOptions.Add(option.Key, option.Value);
70+
break;
71+
72+
default:
73+
break;
74+
}
75+
}
76+
77+
if (allOptions != null)
78+
providerOpts.AllOptions = allOptions;
79+
80+
return providerOpts;
81+
}
82+
3383
public static IProviderOptions GetProviderOptionsFor(string fileExt)
3484
{
3585
//
@@ -48,10 +98,15 @@ public static IProviderOptions GetProviderOptionsFor(string fileExt)
4898
if (String.IsNullOrEmpty(compilerFullPath))
4999
compilerFullPath = CompilerDefaultPath();
50100

51-
if (fileExt.Equals(".cs", StringComparison.InvariantCultureIgnoreCase))
52-
compilerFullPath = Path.Combine(compilerFullPath, "csc.exe");
53-
else if (fileExt.Equals(".vb", StringComparison.InvariantCultureIgnoreCase))
54-
compilerFullPath = Path.Combine(compilerFullPath, "vbc.exe");
101+
if (!String.IsNullOrWhiteSpace(fileExt))
102+
{
103+
// If we have a file extension, try to infer the compiler to use
104+
// TODO: Should we also check compilerFullPath to assert it is a Directory and not a file?
105+
if (fileExt.Equals(".cs", StringComparison.InvariantCultureIgnoreCase) || fileExt.Equals("cs", StringComparison.InvariantCultureIgnoreCase))
106+
compilerFullPath = Path.Combine(compilerFullPath, "csc.exe");
107+
else if (fileExt.Equals(".vb", StringComparison.InvariantCultureIgnoreCase) || fileExt.Equals("vb", StringComparison.InvariantCultureIgnoreCase))
108+
compilerFullPath = Path.Combine(compilerFullPath, "vbc.exe");
109+
}
55110

56111

57112
//
@@ -61,7 +116,8 @@ public static IProviderOptions GetProviderOptionsFor(string fileExt)
61116
string ttlstr = Environment.GetEnvironmentVariable("VBCSCOMPILER_TTL");
62117
if (String.IsNullOrEmpty(ttlstr))
63118
options.TryGetValue("CompilerServerTTL", out ttlstr);
64-
if (!Int32.TryParse(ttlstr, out ttl)) {
119+
if (!Int32.TryParse(ttlstr, out ttl))
120+
{
65121
ttl = DefaultCompilerServerTTL;
66122

67123
if (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("DEV_ENVIRONMENT")) ||
@@ -82,7 +138,8 @@ public static IProviderOptions GetProviderOptionsFor(string fileExt)
82138
// WarnAsError - default false.
83139
//
84140
bool warnAsError = false;
85-
if (options.TryGetValue("WarnAsError", out string sWAE)) {
141+
if (options.TryGetValue("WarnAsError", out string sWAE))
142+
{
86143
Boolean.TryParse(sWAE, out warnAsError); // Failure to parse sets to 'false'
87144
}
88145

@@ -97,7 +154,8 @@ public static IProviderOptions GetProviderOptionsFor(string fileExt)
97154
useAspNetSettings = true;
98155
}
99156

100-
ProviderOptions providerOptions = new ProviderOptions() {
157+
ProviderOptions providerOptions = new ProviderOptions()
158+
{
101159
CompilerFullPath = compilerFullPath,
102160
CompilerServerTimeToLive = ttl,
103161
CompilerVersion = compilerVersion,

src/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/VBCodeProvider.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.CodeDom.Compiler;
6+
using System.Collections.Generic;
67
using System.ComponentModel;
78

89
namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatform {
@@ -37,6 +38,13 @@ public VBCodeProvider(IProviderOptions providerOptions = null) {
3738
_providerOptions = providerOptions == null ? CompilationUtil.VBC2 : providerOptions;
3839
}
3940

41+
/// <summary>
42+
/// Creates an instance using the given IDictionary to create IProviderOptions
43+
/// </summary>
44+
/// <param name="providerOptions"></param>
45+
public VBCodeProvider(IDictionary<string, string> providerOptions)
46+
: this(CompilationUtil.CreateProviderOptions(providerOptions, CompilationUtil.VBC2)) { }
47+
4048
/// <summary>
4149
/// Gets an instance of the .NET Compiler Platform VB code compiler.
4250
/// </summary>

0 commit comments

Comments
 (0)