forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptCsExe.cs
More file actions
169 lines (145 loc) · 5.55 KB
/
ScriptCsExe.cs
File metadata and controls
169 lines (145 loc) · 5.55 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
namespace ScriptCs.Tests.Acceptance.Support
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
public static class ScriptCsExe
{
private static readonly bool isMono = Type.GetType("Mono.Runtime") != null;
public static string BinFolder
{
get { return "scriptcs_bin"; }
}
public static string DllCacheFolder
{
get { return ".scriptcs_cache"; }
}
public static string PackagesFile
{
get { return "scriptcs_packages.config"; }
}
public static string PackagesFolder
{
get { return "scriptcs_packages"; }
}
public static string NugetFile
{
get { return "scriptcs_nuget.config"; }
}
public static string Run(IEnumerable<string> args, ScenarioDirectory directory)
{
return Run(null, true, args, Enumerable.Empty<string>(), directory);
}
public static string Run(IEnumerable<string> args, bool debug, ScenarioDirectory directory)
{
return Run(null, debug, args, Enumerable.Empty<string>(), directory);
}
public static string Run(string scriptName, ScenarioDirectory directory)
{
return Run(scriptName, true, Enumerable.Empty<string>(), Enumerable.Empty<string>(), directory);
}
public static string Run(string scriptName, bool debug, ScenarioDirectory directory)
{
return Run(scriptName, debug, Enumerable.Empty<string>(), Enumerable.Empty<string>(), directory);
}
public static string Run(string scriptName, bool debug, IEnumerable<string> args, ScenarioDirectory directory)
{
return Run(scriptName, debug, args, Enumerable.Empty<string>(), directory);
}
public static string Run(
string scriptName,
bool debug,
IEnumerable<string> args,
IEnumerable<string> scriptArgs,
ScenarioDirectory directory)
{
var debugArgs =
debug &&
!args.Select(arg => arg.Trim().ToUpperInvariant()).Contains("-DEBUG") &&
!args.Select(arg => arg.Trim().ToUpperInvariant()).Contains("-D")
? new[] { "--debug" }
: new string[0];
return Execute(
(scriptName == null ? Enumerable.Empty<string>() : new[] { scriptName }).Concat(debugArgs).Concat(args),
scriptArgs,
directory);
}
public static string Install(string package, ScenarioDirectory directory)
{
using (var writer = new StreamWriter(directory.Map(NugetFile), false))
{
writer.Write(
@"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<clear />
<add key=""Local"" value=""" + Path.GetFullPath(Path.Combine("Support", "Gallery")) + @""" />
</packageSources>
<activePackageSource>
<add key=""All"" value=""(Aggregate source)"" />
</activePackageSource>
</configuration>"
);
writer.Flush();
}
return Execute(new[] { "install", package }, Enumerable.Empty<string>(), directory);
}
public static string Save(ScenarioDirectory directory)
{
return Execute(new[] { "install", "--save" }, Enumerable.Empty<string>(), directory);
}
public static string Clean(ScenarioDirectory directory)
{
return Execute(new[] { "install", "--clean" }, Enumerable.Empty<string>(), directory);
}
private static string Execute(
IEnumerable<string> args, IEnumerable<string> scriptArgs, ScenarioDirectory directory)
{
var commandArgs = new List<string>(args);
var scriptArgsArray = scriptArgs.ToArray();
if (scriptArgsArray.Any())
{
commandArgs.Add("--");
commandArgs.AddRange(scriptArgsArray);
}
#if DEBUG
var config = "Debug";
#else
var config = "Release";
#endif
var exe = Path.GetFullPath(
Path.Combine("..", "..", "..", "..", "..", "src", "ScriptCs", "bin", config, "net461", "scriptcs.exe"));
var info = new ProcessStartInfo
{
FileName = isMono
? "mono"
: exe,
Arguments = isMono
? string.Concat(exe, " ", string.Join(" ", commandArgs))
: string.Join(" ", commandArgs),
WorkingDirectory = directory.Name,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
StandardErrorEncoding = Encoding.UTF8,
StandardOutputEncoding = Encoding.UTF8
};
var result = info.Run(Path.GetFileName(directory.Name) + ".log");
if (result.Item1 != 0)
{
var message = string.Format(
CultureInfo.InvariantCulture,
"scriptcs.exe exited with code {0}. The output was: {1}",
result.Item1.ToString(CultureInfo.InvariantCulture),
result.Item2);
throw new ScriptCsException(message);
}
return result.Item2;
}
}
}