Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Adding the ability to define a custom (local) path to Roslyn (#25)" #34

Merged
merged 3 commits into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 14 additions & 47 deletions src/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/Compiler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
Expand All @@ -12,21 +12,18 @@
using System.Security.Permissions;
using System.Security.Principal;
using System.Text;
using static Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Constants.CustomCompilerParameters;

namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatform {
internal abstract class Compiler : ICodeCompiler {
private readonly CodeDomProvider _codeDomProvider;
private readonly ICompilerSettings _compilerSettings;
private string _compilerFullPath = null;
private const string CLR_PROFILING_SETTING = "COR_ENABLE_PROFILING";
private const string DISABLE_PROFILING = "0";

// Needs to be initialized using InitializeCompilerFullPath where the CompilerParameters are available.
private string _compilerFullPath = null;

public Compiler(CodeDomProvider codeDomProvider, ICompilerSettings compilerSettings) {
_codeDomProvider = codeDomProvider;
_compilerSettings = compilerSettings;
this._codeDomProvider = codeDomProvider;
this._compilerSettings = compilerSettings;
}

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

InitializeCompilerFullPath(options);

try {
var sources = compilationUnits.Select(c => {
var writer = new StringWriter();
Expand Down Expand Up @@ -87,8 +82,6 @@ public CompilerResults CompileAssemblyFromFileBatch(CompilerParameters options,
throw new ArgumentNullException("fileNames");
}

InitializeCompilerFullPath(options);

try {
// Try opening the files to make sure they exists. This will throw an exception
// if it doesn't
Expand Down Expand Up @@ -124,8 +117,6 @@ public CompilerResults CompileAssemblyFromSourceBatch(CompilerParameters options
throw new ArgumentNullException("sources");
}

InitializeCompilerFullPath(options);

try {
return FromSourceBatch(options, sources);
}
Expand All @@ -138,41 +129,17 @@ protected abstract string FileExtension {
get;
}

protected void InitializeCompilerFullPath(CompilerParameters options = null) {
if (string.IsNullOrEmpty(_compilerFullPath)) {
if (options != null) {
// Determining whether the custom compiler path parameter is provided.
var customCompilerPathParameter = options.CompilerOptions.Split('/').FirstOrDefault(p => p.StartsWith(CustomCompilerPath));
if (!string.IsNullOrEmpty(customCompilerPathParameter)) {
if (!customCompilerPathParameter.Contains(":")) {
throw new ArgumentException($"There's no value defined for the \"{CustomCompilerPath}\" compiler parameter!");
}
protected virtual string CompilerName {
get {
if (null == _compilerFullPath) {
_compilerFullPath = _compilerSettings.CompilerFullPath;

// Removing trailing space (when this is not the last parameter) and extracting value.
var customCompilerPath = customCompilerPathParameter.TrimEnd(' ').Split(':')[1];

if (string.IsNullOrEmpty(customCompilerPath)) {
throw new ArgumentException($"The value of the \"{CustomCompilerPath}\" compiler parameter can't be empty!");
}

// Extracting the name of the compiler executable from the default path.
var compilerExecutable = _compilerSettings.CompilerFullPath.Substring(_compilerSettings.CompilerFullPath.LastIndexOf('\\'));

// And finally, we're able to construct the complete custom path to the compiler executable.
// If the custom path contains spaces, then it has to be surrounded by quotes, which we don't need now.
_compilerFullPath = CompilationSettingsHelper.CompilerFullPath($"{customCompilerPath.Trim('"')}\\{compilerExecutable}");

// Removing the custom parameter, as the compiler can't process it.
options.CompilerOptions = options.CompilerOptions.Replace($"/{CustomCompilerPath}:{customCompilerPath}", "");
}
// Falling back to the default behavior.
else _compilerFullPath = _compilerSettings.CompilerFullPath;
// Try opening the file to make sure the compiler exist. This will throw an exception
// if it doesn't
using (var str = File.OpenRead(_compilerFullPath)) { }
}
else _compilerFullPath = _compilerSettings.CompilerFullPath;

// Try opening the file to make sure that the compiler exists.
// This will throw an exception if it doesn't.
using (var str = File.OpenRead(_compilerFullPath)) { }
return _compilerFullPath;
}
}

Expand Down Expand Up @@ -315,7 +282,7 @@ private CompilerResults FromFileBatch(CompilerParameters options, string[] fileN
}

Compile(options,
_compilerFullPath,
CompilerName,
args,
ref outputFile,
ref retValue);
Expand All @@ -333,7 +300,7 @@ private CompilerResults FromFileBatch(CompilerParameters options, string[] fileN
replacedArgs = true;
var outputLine = string.Format("{0}>{1} {2}",
Environment.CurrentDirectory,
_compilerFullPath,
CompilerName,
trueArgs);
results.Output.Add(outputLine);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
<ItemGroup>
<Compile Include="AppSettings.cs" />
<Compile Include="Compiler.cs" />
<Compile Include="Constants.cs" />
<Compile Include="CSharpCompiler.cs" />
<Compile Include="CSharpCodeProvider.cs">
<SubType>Component</SubType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ public CompilerSettings(string compilerFullPath, int compilerServerTimeToLive) {
_compilerServerTimeToLive = compilerServerTimeToLive;
}

string ICompilerSettings.CompilerFullPath => _compilerFullPath;
string ICompilerSettings.CompilerFullPath {
get {
return _compilerFullPath;
}
}

int ICompilerSettings.CompilerServerTimeToLive => _compilerServerTimeToLive;
int ICompilerSettings.CompilerServerTimeToLive {
get{
return _compilerServerTimeToLive;
}
}
}

internal static class CompilationSettingsHelper {
Expand Down Expand Up @@ -80,14 +88,28 @@ static CompilationSettingsHelper() {
}
}

public static ICompilerSettings CSC2 => _csc;
public static ICompilerSettings CSC2 {
get {
return _csc;
}
}

public static ICompilerSettings VBC2 => _vb;
public static ICompilerSettings VBC2 {
get {
return _vb;
}
}

public static string CompilerFullPath(string relativePath) =>
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath);
private static string CompilerFullPath(string relativePath) {
string compilerFullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath);
return compilerFullPath;
}

private static bool IsDebuggerAttached => IsDebuggerPresent() || Debugger.IsAttached;
private static bool IsDebuggerAttached {
get {
return IsDebuggerPresent() || Debugger.IsAttached;
}
}

[DllImport("kernel32.dll")]
private extern static bool IsDebuggerPresent();
Expand Down