forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecuteScriptCommand.cs
More file actions
95 lines (82 loc) · 2.87 KB
/
ExecuteScriptCommand.cs
File metadata and controls
95 lines (82 loc) · 2.87 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Common.Logging;
using System.Reflection;
namespace ScriptCs.Command
{
internal class ExecuteScriptCommand : IScriptCommand
{
private readonly string _script;
private readonly IFileSystem _fileSystem;
private readonly IScriptExecutor _scriptExecutor;
private readonly IScriptPackResolver _scriptPackResolver;
private readonly IAssemblyName _assemblyName;
private readonly ILog _logger;
public ExecuteScriptCommand(string script,
string[] scriptArgs,
IFileSystem fileSystem,
IScriptExecutor scriptExecutor,
IScriptPackResolver scriptPackResolver,
ILog logger,
IAssemblyName assemblyName)
{
_script = script;
ScriptArgs = scriptArgs;
_fileSystem = fileSystem;
_scriptExecutor = scriptExecutor;
_scriptPackResolver = scriptPackResolver;
_logger = logger;
_assemblyName = assemblyName;
}
public string[] ScriptArgs { get; private set; }
public CommandResult Execute()
{
try
{
var assemblyPaths = Enumerable.Empty<string>();
var workingDirectory = _fileSystem.GetWorkingDirectory(_script);
if (workingDirectory != null)
{
assemblyPaths = GetAssemblyPaths(workingDirectory);
}
_scriptExecutor.Execute(_script, ScriptArgs, assemblyPaths, _scriptPackResolver.GetPacks());
return CommandResult.Success;
}
catch (Exception ex)
{
_logger.Error(ex.Message);
return CommandResult.Error;
}
}
private IEnumerable<string> GetAssemblyPaths(string workingDirectory)
{
var binFolder = Path.Combine(workingDirectory, "bin");
if (!_fileSystem.DirectoryExists(binFolder))
_fileSystem.CreateDirectory(binFolder);
var assemblyPaths =
_fileSystem.EnumerateFiles(binFolder, "*.dll")
.Union(_fileSystem.EnumerateFiles(binFolder, "*.exe"))
.Where(IsManagedAssembly)
.ToList();
foreach (var path in assemblyPaths.Select(Path.GetFileName))
{
_logger.DebugFormat("Found assembly reference: {0}", path);
}
return assemblyPaths;
}
private bool IsManagedAssembly(string path)
{
try
{
_assemblyName.GetAssemblyName(path);
}
catch (BadImageFormatException)
{
return false;
}
return true;
}
}
}