forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharpPersistentEngine.cs
More file actions
58 lines (48 loc) · 1.74 KB
/
CSharpPersistentEngine.cs
File metadata and controls
58 lines (48 loc) · 1.74 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
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using ScriptCs.Contracts;
namespace ScriptCs.Engine.Roslyn
{
public class CSharpPersistentEngine : CSharpScriptCompilerEngine
{
private readonly IFileSystem _fileSystem;
private readonly ILog _log;
public CSharpPersistentEngine(IScriptHostFactory scriptHostFactory, ILogProvider logProvider, IFileSystem fileSystem)
: base(scriptHostFactory, logProvider)
{
Guard.AgainstNullArgument("logProvider", logProvider);
_log = logProvider.ForCurrentType();
_fileSystem = fileSystem;
}
protected override bool ShouldCompile()
{
var dllPath = GetDllTargetPath();
return !_fileSystem.FileExists(dllPath);
}
protected override Assembly LoadAssembly(byte[] exeBytes, byte[] pdbBytes)
{
_log.DebugFormat("Writing assembly to {0}.", FileName);
if (!_fileSystem.DirectoryExists(CacheDirectory))
{
_fileSystem.CreateDirectory(CacheDirectory, true);
}
var dllPath = GetDllTargetPath();
_fileSystem.WriteAllBytes(dllPath, exeBytes);
_log.DebugFormat("Loading assembly {0}.", dllPath);
return LoadAssemblyFromCache();
}
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;
}
}
}