Skip to content

Commit

Permalink
Changes and fixes in handling of .NET reference assemblies (#3)
Browse files Browse the repository at this point in the history
* Added automatic searching for .NET referenced assembly directory.
* Show error messages instead of crash if a reference cannot be found.
* Also update dependencies.
  • Loading branch information
lwchkg authored May 13, 2017
1 parent b9c89fa commit 82420e6
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 16 deletions.
4 changes: 4 additions & 0 deletions DotNetEditor/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
<assemblyIdentity name="System.Composition.Hosting" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.0.30.0" newVersion="1.0.30.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
18 changes: 14 additions & 4 deletions DotNetEditor/CodeRunner/CSCodeRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,20 @@ protected override Compilation GetCompilation()
uniqueImports.Add("mscorlib.dll");
uniqueImports.Add("Microsoft.CSharp.dll");

string dllPath = GetReferenceAssembliesPath();
IEnumerable<MetadataReference> references =
uniqueImports.Select(import => MetadataReference.CreateFromFile(
System.IO.Path.Combine(dllPath, import)));
List<MetadataReference> references = new List<MetadataReference>();
try
{
string dllPath = GetReferenceAssembliesPath();
foreach (string import in uniqueImports)
{
references.Add(MetadataReference.CreateFromFile(System.IO.Path.Combine(
dllPath, import)));
}
}
catch (Exception e)
{
throw new CodeRunnerException(e.Message, e);
}

var options = new CSharpCompilationOptions(OutputKind.ConsoleApplication);

Expand Down
46 changes: 41 additions & 5 deletions DotNetEditor/CodeRunner/CodeRunnerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,38 @@ void dumpConsoleBuffer(TextWriterWithConsoleColor.TextWriterWithConsoleColor buf
buffer.ForEachPart(addConsoleColoredText);
}

// Return: whether a the code runs successfully without error.
// Get the path to the optimally selected version of the .NET reference assemblies. If no
// matches are found, throw DirectoryNotFoundException.
protected string GetReferenceAssembliesPath()
{
return @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1";
string basepath = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86,
Environment.SpecialFolderOption.DoNotVerify),
"Reference Assemblies",
"Microsoft",
"Framework",
".NETFramework");

// List of .NET reference assembly directories, with glob wildcards characters.
string[] globPatternList = { "v4.6.*", "v4.*" };

// Try to match the first pattern. If no match, try the second pattern, etc.
foreach (string pattern in globPatternList)
{
string[] directories = System.IO.Directory.GetDirectories(basepath, pattern);
if (directories.Any())
{
// If multiple versions are found, return the path to the latest version.
return directories.Max();
}
}

// If no .NET reference assembly is found, throw an exception.
string dotNetDeveloperPackURL =
"https://www.microsoft.com/en-us/download/details.aspx?id=49978";
throw new System.IO.DirectoryNotFoundException(
String.Format("No .NET 4.x reference assemblies is found. Please install .NET Framework 4.6.1 Developer Pack at {0}",
dotNetDeveloperPackURL));
}

protected abstract Compilation GetCompilation();
Expand All @@ -130,7 +158,16 @@ private string DiagToString(Diagnostic diag)
public bool Run()
{
_outputArea.Clear();
Compilation compilation = GetCompilation();
Compilation compilation;
try
{
compilation = GetCompilation();
}
catch (CodeRunnerException e)
{
addResult(Environment.NewLine + e.Message, "Unable to compile", errorFgColor);
return false;
}

var stream = new System.IO.MemoryStream();
var emitResult = compilation.Emit(stream);
Expand Down Expand Up @@ -227,8 +264,7 @@ public bool Run()

if (hasError)
{
addResult(exStore.InnerException.Message,
"Runtime error");//, errorColor)
addResult(exStore.InnerException.Message, "Runtime error", errorFgColor);
}

return !hasError;
Expand Down
16 changes: 16 additions & 0 deletions DotNetEditor/CodeRunner/CodeRunnerException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2017 Leung Wing-chung. All rights reserved.
// Use of this source code is governed by a GPLv3 license that can be found in
// the LICENSE file.

using System;

namespace DotNetEditor.CodeRunner
{
[Serializable]
class CodeRunnerException : Exception
{
public CodeRunnerException() { }
public CodeRunnerException(string message) : base(message) { }
public CodeRunnerException(string message, Exception e) : base(message, e) { }
}
}
18 changes: 14 additions & 4 deletions DotNetEditor/CodeRunner/VBCodeRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,20 @@ protected override Compilation GetCompilation()
uniqueImports.Add("mscorlib.dll");
uniqueImports.Add("Microsoft.VisualBasic.dll");

string dllPath = GetReferenceAssembliesPath();
IEnumerable<MetadataReference> references =
uniqueImports.Select(import => MetadataReference.CreateFromFile(
System.IO.Path.Combine(dllPath, import)));
List<MetadataReference> references = new List<MetadataReference>();
try
{
string dllPath = GetReferenceAssembliesPath();
foreach (string import in uniqueImports)
{
references.Add(MetadataReference.CreateFromFile(System.IO.Path.Combine(
dllPath, import)));
}
}
catch (Exception e)
{
throw new CodeRunnerException(e.Message, e);
}

var compilationOptions = new VisualBasicCompilationOptions(
OutputKind.ConsoleApplication,
Expand Down
5 changes: 3 additions & 2 deletions DotNetEditor/DotNetEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@
<HintPath>..\packages\System.Threading.Thread.4.3.0\lib\net46\System.Threading.Thread.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
<Reference Include="System.ValueTuple, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.3.1\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Windows.Forms" />
Expand Down Expand Up @@ -202,6 +202,7 @@
<Compile Include="AvalonUtils\AvalonRichTextBox.cs" />
<Compile Include="AvalonUtils\PixelSnapper.cs" />
<Compile Include="CodeRunner\CodeRunnerBase.cs" />
<Compile Include="CodeRunner\CodeRunnerException.cs" />
<Compile Include="CodeRunner\CSCodeRunner.cs" />
<Compile Include="ConsoleColorScheme.cs" />
<Compile Include="ConsoleUtil.cs" />
Expand Down
2 changes: 1 addition & 1 deletion DotNetEditor/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<package id="System.Threading.Tasks" version="4.3.0" targetFramework="net461" />
<package id="System.Threading.Tasks.Parallel" version="4.3.0" targetFramework="net461" />
<package id="System.Threading.Thread" version="4.3.0" targetFramework="net461" />
<package id="System.ValueTuple" version="4.3.0" targetFramework="net461" />
<package id="System.ValueTuple" version="4.3.1" targetFramework="net461" />
<package id="System.Xml.ReaderWriter" version="4.3.0" targetFramework="net461" />
<package id="System.Xml.XDocument" version="4.3.0" targetFramework="net461" />
<package id="System.Xml.XmlDocument" version="4.3.0" targetFramework="net461" />
Expand Down

0 comments on commit 82420e6

Please sign in to comment.