-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error handling and assembly resolving improvements
- Loading branch information
Showing
11 changed files
with
194 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 81 additions & 77 deletions
158
Source/Nake/AssemblyResolver.cs → Source/Nake/RoslynAssemblyResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,81 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Nake | ||
{ | ||
class AssemblyResolver | ||
{ | ||
public static volatile bool Resolve = false; | ||
|
||
static readonly List<RoslynAssembly> roslynAssemblies; | ||
static readonly ConcurrentDictionary<string, Assembly> resolvedAssemblies = new ConcurrentDictionary<string, Assembly>(); | ||
|
||
static readonly string currentDir; | ||
|
||
static AssemblyResolver() | ||
{ | ||
currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | ||
|
||
roslynAssemblies = new List<RoslynAssembly> | ||
{ | ||
new RoslynAssembly("Roslyn.Compilers", "Roslyn.Compilers.Common"), | ||
new RoslynAssembly("Roslyn.Compilers.CSharp", "Roslyn.Compilers.CSharp") | ||
}; | ||
} | ||
|
||
public static void Register() | ||
{ | ||
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly; | ||
} | ||
|
||
public static void Unregister() | ||
{ | ||
AppDomain.CurrentDomain.AssemblyResolve -= ResolveAssembly; | ||
} | ||
|
||
static Assembly ResolveAssembly(object sender, ResolveEventArgs args) | ||
{ | ||
if (!Resolve) | ||
return null; | ||
|
||
return resolvedAssemblies.GetOrAdd(args.Name, name => | ||
{ | ||
var assemblyName = new AssemblyName(args.Name); | ||
|
||
var roslynAssembly = roslynAssemblies | ||
.Find(x => x.AssemblyName == assemblyName.Name); | ||
|
||
Unregister(); | ||
|
||
return roslynAssembly.Load(); | ||
}); | ||
} | ||
|
||
class RoslynAssembly | ||
{ | ||
public readonly string AssemblyName; | ||
readonly string assemblyPath; | ||
|
||
public RoslynAssembly(string assemblyName, string packageName) | ||
{ | ||
AssemblyName = assemblyName; | ||
|
||
assemblyPath = Path.Combine(currentDir, | ||
string.Format(@"Roslyn\{0}.1.2.20906.2\lib\net45\{1}.dll", packageName, assemblyName)); | ||
} | ||
|
||
public Assembly Load() | ||
{ | ||
return Assembly.LoadFrom(assemblyPath); | ||
} | ||
} | ||
} | ||
} | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Nake | ||
{ | ||
class RoslynAssemblyResolver | ||
{ | ||
public static volatile bool Resolve = false; | ||
|
||
static readonly List<RoslynAssembly> roslynAssemblies; | ||
|
||
static readonly ConcurrentDictionary<string, Lazy<Assembly>> resolvedAssemblies = | ||
new ConcurrentDictionary<string, Lazy<Assembly>>(); | ||
|
||
static readonly string currentDir; | ||
|
||
static RoslynAssemblyResolver() | ||
{ | ||
currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | ||
|
||
roslynAssemblies = new List<RoslynAssembly> | ||
{ | ||
new RoslynAssembly("Roslyn.Compilers", "Roslyn.Compilers.Common"), | ||
new RoslynAssembly("Roslyn.Compilers.CSharp", "Roslyn.Compilers.CSharp") | ||
}; | ||
} | ||
|
||
public static void Register() | ||
{ | ||
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly; | ||
} | ||
|
||
public static void Unregister() | ||
{ | ||
AppDomain.CurrentDomain.AssemblyResolve -= ResolveAssembly; | ||
} | ||
|
||
static Assembly ResolveAssembly(object sender, ResolveEventArgs args) | ||
{ | ||
return Resolve ? resolvedAssemblies.GetOrAdd(args.Name, LoadRoslynAssembly).Value : null; | ||
} | ||
|
||
static Lazy<Assembly> LoadRoslynAssembly(string name) | ||
{ | ||
return new Lazy<Assembly>(() => | ||
{ | ||
var assemblyName = new AssemblyName(name); | ||
|
||
var roslynAssembly = roslynAssemblies | ||
.Find(x => x.AssemblyName == assemblyName.Name); | ||
|
||
Unregister(); | ||
|
||
return roslynAssembly.Load(); | ||
}); | ||
} | ||
|
||
class RoslynAssembly | ||
{ | ||
public readonly string AssemblyName; | ||
readonly string assemblyPath; | ||
|
||
public RoslynAssembly(string assemblyName, string packageName) | ||
{ | ||
AssemblyName = assemblyName; | ||
|
||
assemblyPath = Path.Combine(currentDir, | ||
string.Format(@"Roslyn\{0}.1.2.20906.2\lib\net45\{1}.dll", packageName, assemblyName)); | ||
} | ||
|
||
public Assembly Load() | ||
{ | ||
return Assembly.LoadFrom(assemblyPath); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.