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 ;
55using System . CodeDom ;
66using System . CodeDom . Compiler ;
77using System . Collections . Generic ;
1212using System . Security . Permissions ;
1313using System . Security . Principal ;
1414using System . Text ;
15- using static Microsoft . CodeDom . Providers . DotNetCompilerPlatform . Constants . CustomCompilerParameters ;
1615
1716namespace 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 }
0 commit comments