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
88 lines (72 loc) · 2.75 KB
/
ExecuteScriptCommand.cs
File metadata and controls
88 lines (72 loc) · 2.75 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
using System;
using System.IO;
using System.Linq;
using Common.Logging;
using ScriptCs.Contracts;
namespace ScriptCs.Command
{
internal class ExecuteScriptCommand : IScriptCommand
{
private readonly IScriptPackResolver _scriptPackResolver;
private readonly IAssemblyResolver _assemblyResolver;
private readonly IScriptExecutor _scriptExecutor;
private readonly IFileSystem _fileSystem;
private readonly string _script;
private readonly ILog _logger;
public ExecuteScriptCommand(string script,
string[] scriptArgs,
IFileSystem fileSystem,
IScriptExecutor scriptExecutor,
IScriptPackResolver scriptPackResolver,
ILog logger,
IAssemblyResolver assemblyResolver)
{
_script = script;
_fileSystem = fileSystem;
ScriptArgs = scriptArgs;
_scriptExecutor = scriptExecutor;
_scriptPackResolver = scriptPackResolver;
_logger = logger;
_assemblyResolver = assemblyResolver;
}
public string[] ScriptArgs { get; private set; }
public CommandResult Execute()
{
try
{
var assemblyPaths = Enumerable.Empty<string>();
var workingDirectory = _fileSystem.GetWorkingDirectory(_script);
if (workingDirectory != null)
{
assemblyPaths = _assemblyResolver.GetAssemblyPaths(workingDirectory);
}
_scriptExecutor.Initialize(assemblyPaths, _scriptPackResolver.GetPacks(), ScriptArgs);
var result = _scriptExecutor.Execute(_script, ScriptArgs);
_scriptExecutor.Terminate();
if (result == null) return CommandResult.Error;
if (result.CompileExceptionInfo != null)
{
_logger.Error(result.CompileExceptionInfo.SourceException.Message);
_logger.Debug(result.CompileExceptionInfo.SourceException);
return CommandResult.Error;
}
if (result.ExecuteExceptionInfo != null)
{
_logger.Error(result.ExecuteExceptionInfo.SourceException);
return CommandResult.Error;
}
return CommandResult.Success;
}
catch (FileNotFoundException fnfex)
{
_logger.ErrorFormat("{0} - {1}", fnfex.Message, fnfex.FileName);
return CommandResult.Error;
}
catch (Exception ex)
{
_logger.Error(ex.Message);
return CommandResult.Error;
}
}
}
}