Skip to content

Commit 707c08c

Browse files
JinhuafeiHongGit
authored andcommitted
Revert "Adding the ability to define a custom (local) path to Roslyn (#25)" (#34)
* Revert "add support for non web proj (#32)" This reverts commit b9e1127. * Revert "include commit link (#31)" This reverts commit 30f97fb. * Revert "Adding the ability to define a custom (local) path to Roslyn (#25) (#26)" This reverts commit e79aa12.
1 parent b9e1127 commit 707c08c

File tree

4 files changed

+43
-71
lines changed

4 files changed

+43
-71
lines changed

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

+14-47
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System;
4+
using System;
55
using System.CodeDom;
66
using System.CodeDom.Compiler;
77
using System.Collections.Generic;
@@ -12,21 +12,18 @@
1212
using System.Security.Permissions;
1313
using System.Security.Principal;
1414
using System.Text;
15-
using static Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Constants.CustomCompilerParameters;
1615

1716
namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatform {
1817
internal abstract class Compiler : ICodeCompiler {
1918
private readonly CodeDomProvider _codeDomProvider;
2019
private readonly ICompilerSettings _compilerSettings;
20+
private string _compilerFullPath = null;
2121
private const string CLR_PROFILING_SETTING = "COR_ENABLE_PROFILING";
2222
private const string DISABLE_PROFILING = "0";
2323

24-
// Needs to be initialized using InitializeCompilerFullPath where the CompilerParameters are available.
25-
private string _compilerFullPath = null;
26-
2724
public Compiler(CodeDomProvider codeDomProvider, ICompilerSettings compilerSettings) {
28-
_codeDomProvider = codeDomProvider;
29-
_compilerSettings = compilerSettings;
25+
this._codeDomProvider = codeDomProvider;
26+
this._compilerSettings = compilerSettings;
3027
}
3128

3229
public CompilerResults CompileAssemblyFromDom(CompilerParameters options, CodeCompileUnit compilationUnit) {
@@ -50,8 +47,6 @@ public CompilerResults CompileAssemblyFromDomBatch(CompilerParameters options, C
5047
throw new ArgumentNullException("compilationUnits");
5148
}
5249

53-
InitializeCompilerFullPath(options);
54-
5550
try {
5651
var sources = compilationUnits.Select(c => {
5752
var writer = new StringWriter();
@@ -87,8 +82,6 @@ public CompilerResults CompileAssemblyFromFileBatch(CompilerParameters options,
8782
throw new ArgumentNullException("fileNames");
8883
}
8984

90-
InitializeCompilerFullPath(options);
91-
9285
try {
9386
// Try opening the files to make sure they exists. This will throw an exception
9487
// if it doesn't
@@ -124,8 +117,6 @@ public CompilerResults CompileAssemblyFromSourceBatch(CompilerParameters options
124117
throw new ArgumentNullException("sources");
125118
}
126119

127-
InitializeCompilerFullPath(options);
128-
129120
try {
130121
return FromSourceBatch(options, sources);
131122
}
@@ -138,41 +129,17 @@ protected abstract string FileExtension {
138129
get;
139130
}
140131

141-
protected void InitializeCompilerFullPath(CompilerParameters options = null) {
142-
if (string.IsNullOrEmpty(_compilerFullPath)) {
143-
if (options != null) {
144-
// Determining whether the custom compiler path parameter is provided.
145-
var customCompilerPathParameter = options.CompilerOptions.Split('/').FirstOrDefault(p => p.StartsWith(CustomCompilerPath));
146-
if (!string.IsNullOrEmpty(customCompilerPathParameter)) {
147-
if (!customCompilerPathParameter.Contains(":")) {
148-
throw new ArgumentException($"There's no value defined for the \"{CustomCompilerPath}\" compiler parameter!");
149-
}
132+
protected virtual string CompilerName {
133+
get {
134+
if (null == _compilerFullPath) {
135+
_compilerFullPath = _compilerSettings.CompilerFullPath;
150136

151-
// Removing trailing space (when this is not the last parameter) and extracting value.
152-
var customCompilerPath = customCompilerPathParameter.TrimEnd(' ').Split(':')[1];
153-
154-
if (string.IsNullOrEmpty(customCompilerPath)) {
155-
throw new ArgumentException($"The value of the \"{CustomCompilerPath}\" compiler parameter can't be empty!");
156-
}
157-
158-
// Extracting the name of the compiler executable from the default path.
159-
var compilerExecutable = _compilerSettings.CompilerFullPath.Substring(_compilerSettings.CompilerFullPath.LastIndexOf('\\'));
160-
161-
// And finally, we're able to construct the complete custom path to the compiler executable.
162-
// If the custom path contains spaces, then it has to be surrounded by quotes, which we don't need now.
163-
_compilerFullPath = CompilationSettingsHelper.CompilerFullPath($"{customCompilerPath.Trim('"')}\\{compilerExecutable}");
164-
165-
// Removing the custom parameter, as the compiler can't process it.
166-
options.CompilerOptions = options.CompilerOptions.Replace($"/{CustomCompilerPath}:{customCompilerPath}", "");
167-
}
168-
// Falling back to the default behavior.
169-
else _compilerFullPath = _compilerSettings.CompilerFullPath;
137+
// Try opening the file to make sure the compiler exist. This will throw an exception
138+
// if it doesn't
139+
using (var str = File.OpenRead(_compilerFullPath)) { }
170140
}
171-
else _compilerFullPath = _compilerSettings.CompilerFullPath;
172141

173-
// Try opening the file to make sure that the compiler exists.
174-
// This will throw an exception if it doesn't.
175-
using (var str = File.OpenRead(_compilerFullPath)) { }
142+
return _compilerFullPath;
176143
}
177144
}
178145

@@ -315,7 +282,7 @@ private CompilerResults FromFileBatch(CompilerParameters options, string[] fileN
315282
}
316283

317284
Compile(options,
318-
_compilerFullPath,
285+
CompilerName,
319286
args,
320287
ref outputFile,
321288
ref retValue);
@@ -333,7 +300,7 @@ private CompilerResults FromFileBatch(CompilerParameters options, string[] fileN
333300
replacedArgs = true;
334301
var outputLine = string.Format("{0}>{1} {2}",
335302
Environment.CurrentDirectory,
336-
_compilerFullPath,
303+
CompilerName,
337304
trueArgs);
338305
results.Output.Add(outputLine);
339306
}

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

-16
This file was deleted.

src/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
<ItemGroup>
5555
<Compile Include="AppSettings.cs" />
5656
<Compile Include="Compiler.cs" />
57-
<Compile Include="Constants.cs" />
5857
<Compile Include="CSharpCompiler.cs" />
5958
<Compile Include="CSharpCodeProvider.cs">
6059
<SubType>Component</SubType>

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

+29-7
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,17 @@ public CompilerSettings(string compilerFullPath, int compilerServerTimeToLive) {
2121
_compilerServerTimeToLive = compilerServerTimeToLive;
2222
}
2323

24-
string ICompilerSettings.CompilerFullPath => _compilerFullPath;
24+
string ICompilerSettings.CompilerFullPath {
25+
get {
26+
return _compilerFullPath;
27+
}
28+
}
2529

26-
int ICompilerSettings.CompilerServerTimeToLive => _compilerServerTimeToLive;
30+
int ICompilerSettings.CompilerServerTimeToLive {
31+
get{
32+
return _compilerServerTimeToLive;
33+
}
34+
}
2735
}
2836

2937
internal static class CompilationSettingsHelper {
@@ -86,14 +94,28 @@ static CompilationSettingsHelper() {
8694
}
8795
}
8896

89-
public static ICompilerSettings CSC2 => _csc;
97+
public static ICompilerSettings CSC2 {
98+
get {
99+
return _csc;
100+
}
101+
}
90102

91-
public static ICompilerSettings VBC2 => _vb;
103+
public static ICompilerSettings VBC2 {
104+
get {
105+
return _vb;
106+
}
107+
}
92108

93-
public static string CompilerFullPath(string relativePath) =>
94-
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath);
109+
private static string CompilerFullPath(string relativePath) {
110+
string compilerFullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath);
111+
return compilerFullPath;
112+
}
95113

96-
private static bool IsDebuggerAttached => IsDebuggerPresent() || Debugger.IsAttached;
114+
private static bool IsDebuggerAttached {
115+
get {
116+
return IsDebuggerPresent() || Debugger.IsAttached;
117+
}
118+
}
97119

98120
[DllImport("kernel32.dll")]
99121
private extern static bool IsDebuggerPresent();

0 commit comments

Comments
 (0)