forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoslynScriptPersistentEngine.cs
More file actions
62 lines (49 loc) · 1.9 KB
/
RoslynScriptPersistentEngine.cs
File metadata and controls
62 lines (49 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Common.Logging;
using ScriptCs.Contracts;
namespace ScriptCs.Engine.Roslyn
{
public class RoslynScriptPersistentEngine : RoslynScriptCompilerEngine
{
private readonly IFileSystem _fileSystem;
private const string RoslynAssemblyNameCharacter = "ℛ";
public RoslynScriptPersistentEngine(IScriptHostFactory scriptHostFactory, ILog logger, IFileSystem fileSystem)
: base(scriptHostFactory, logger)
{
_fileSystem = fileSystem;
}
protected override bool ShouldCompile()
{
var dllPath = GetDllTargetPath();
return !_fileSystem.FileExists(dllPath);
}
protected override Assembly LoadAssembly(byte[] exeBytes, byte[] pdbBytes)
{
this.Logger.DebugFormat("Writing assembly to {0}.", FileName);
if (!_fileSystem.DirectoryExists(CacheDirectory))
{
_fileSystem.CreateDirectory(CacheDirectory, true);
}
var dllPath = GetDllTargetPath();
_fileSystem.WriteAllBytes(dllPath, exeBytes);
Logger.DebugFormat("Loading assembly {0}.", dllPath);
// the assembly is automatically loaded into the AppDomain when compiled
// just need to find and return it
return AppDomain.CurrentDomain.GetAssemblies().LastOrDefault(x => x.FullName.StartsWith(RoslynAssemblyNameCharacter));
}
protected override Assembly LoadAssemblyFromCache()
{
var dllPath = GetDllTargetPath();
return Assembly.LoadFrom(dllPath);
}
private string GetDllTargetPath()
{
var dllName = FileName.Replace(Path.GetExtension(FileName), ".dll");
var dllPath = Path.Combine(CacheDirectory, dllName);
return dllPath;
}
}
}