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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
// CustomCommand.cs
//
// Author:
// Lluis Sanchez Gual <lluis@novell.com>
//
// Copyright (c) 2007 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
using System;
using System.IO;
using System.ComponentModel;
using MonoDevelop.Core.Serialization;
using MonoDevelop.Core;
using MonoDevelop.Core.Execution;
using MonoDevelop.Core.StringParsing;
using System.Collections.Generic;
using MonoDevelop.Core.ProgressMonitoring;
using System.Threading.Tasks;
using System.Threading;
namespace MonoDevelop.Projects
{
public class CustomCommand
{
[ItemProperty ()]
CustomCommandType type;
[ItemProperty (DefaultValue = "")]
string name = "";
[ItemProperty ()]
string command;
[ItemProperty ()]
string workingdir;
[ItemProperty (DefaultValue = false)]
bool externalConsole;
[ItemProperty (DefaultValue = false)]
bool pauseExternalConsole;
[ItemProperty("EnvironmentVariables", SkipEmpty = true)]
[ItemProperty("Variable", Scope = "item")]
[ItemProperty("name", Scope = "key")]
[ItemProperty("value", Scope = "value")]
Dictionary<string, string> environmentVariables = new Dictionary<string, string> ();
public Dictionary<string, string> EnvironmentVariables {
get { return environmentVariables; }
}
public CustomCommandType Type {
get { return type; }
set { type = value; }
}
public string Command {
get { return command; }
set { command = value ?? string.Empty; }
}
public string WorkingDir {
get { return workingdir; }
set { workingdir = value; }
}
public string Name {
get { return name; }
set { name = value; }
}
public bool ExternalConsole {
get { return externalConsole; }
set { externalConsole = value; }
}
public bool PauseExternalConsole {
get { return pauseExternalConsole; }
set { pauseExternalConsole = value; }
}
public string TypeLabel {
get {
switch (type) {
case CustomCommandType.BeforeBuild: return GettextCatalog.GetString ("Before Build");
case CustomCommandType.Build: return GettextCatalog.GetString ("Build");
case CustomCommandType.AfterBuild: return GettextCatalog.GetString ("After Build");
case CustomCommandType.BeforeExecute: return GettextCatalog.GetString ("Before Execute");
case CustomCommandType.Execute: return GettextCatalog.GetString ("Execute");
case CustomCommandType.AfterExecute: return GettextCatalog.GetString ("After Execute");
case CustomCommandType.BeforeClean: return GettextCatalog.GetString ("Before Clean");
case CustomCommandType.Clean: return GettextCatalog.GetString ("Clean");
case CustomCommandType.AfterClean: return GettextCatalog.GetString ("After Clean");
case CustomCommandType.Custom: return GettextCatalog.GetString ("Custom Command");
default: return type.ToString ();
}
}
}
public string GetCommandFile (WorkspaceObject entry, ConfigurationSelector configuration)
{
string exe, args;
StringTagModel tagSource = GetTagModel (entry, configuration);
ParseCommand (tagSource, out exe, out args);
return exe;
}
public string GetCommandArgs (WorkspaceObject entry, ConfigurationSelector configuration)
{
string exe, args;
StringTagModel tagSource = GetTagModel (entry, configuration);
ParseCommand (tagSource, out exe, out args);
return args;
}
public FilePath GetCommandWorkingDir (WorkspaceObject entry, ConfigurationSelector configuration)
{
StringTagModel tagSource = GetTagModel (entry, configuration);
if (string.IsNullOrEmpty (workingdir))
return entry.BaseDirectory;
FilePath dir = StringParserService.Parse (workingdir, tagSource);
return dir.ToAbsolute (entry.BaseDirectory);
}
public CustomCommand Clone ()
{
CustomCommand cmd = new CustomCommand ();
cmd.command = command;
cmd.workingdir = workingdir;
cmd.name = name;
cmd.externalConsole = externalConsole;
cmd.pauseExternalConsole = pauseExternalConsole;
cmd.type = type;
foreach (var variable in environmentVariables) {
cmd.environmentVariables.Add (variable.Key, variable.Value);
}
return cmd;
}
public bool Equals (CustomCommand cmd)
{
if (command != cmd.command || workingdir != cmd.workingdir || externalConsole != cmd.externalConsole || pauseExternalConsole != cmd.pauseExternalConsole || type != cmd.type)
return false;
if (environmentVariables.Count != cmd.environmentVariables.Count)
return false;
foreach (var e in environmentVariables) {
string val;
if (!cmd.environmentVariables.TryGetValue (e.Key, out val) || e.Value != val)
return false;
}
return true;
}
StringTagModel GetTagModel (WorkspaceObject entry, ConfigurationSelector configuration)
{
if (entry is SolutionFolderItem)
return ((SolutionFolderItem)entry).GetStringTagModel (configuration);
else if (entry is WorkspaceItem)
return ((WorkspaceItem)entry).GetStringTagModel ();
else
return new StringTagModel ();
}
void ParseCommand (StringTagModel tagSource, out string cmd, out string args)
{
ParseCommand (out cmd, out args);
cmd = StringParserService.Parse (cmd, tagSource);
args = StringParserService.Parse (args, tagSource);
}
internal void ParseCommand (out string cmd, out string args)
{
if (command.Length > 0 && command [0] == '"') {
int n = command.IndexOf ('"', 1);
if (n != -1) {
cmd = command.Substring (1, n - 1);
args = command.Substring (n + 1).Trim ();
} else {
cmd = command;
args = string.Empty;
}
} else {
int i = command.IndexOf (' ');
if (i != -1) {
cmd = command.Substring (0, i);
args = command.Substring (i + 1).Trim ();
} else {
cmd = command;
args = string.Empty;
}
}
}
public ProcessExecutionCommand CreateExecutionCommand (WorkspaceObject entry, ConfigurationSelector configuration)
{
if (string.IsNullOrEmpty (command))
throw new UserException (GettextCatalog.GetString ("Invalid custom command for '{0}' step: the path to the command to execute has not been provided.", TypeLabel));
string exe, args;
StringTagModel tagSource = GetTagModel (entry, configuration);
ParseCommand (tagSource, out exe, out args);
//if the executable name matches an executable in the project directory, use that, for back-compat
//else fall back and let the execution handler handle it via PATH, working directory, etc.
if (!Path.IsPathRooted (exe)) {
string localPath = ((FilePath) exe).ToAbsolute (entry.BaseDirectory).FullPath;
if (File.Exists (localPath))
exe = localPath;
}
ProcessExecutionCommand cmd = Runtime.ProcessService.CreateCommand (exe);
cmd.Arguments = args;
FilePath workingDir = this.workingdir;
if (!workingDir.IsNullOrEmpty)
workingDir = StringParserService.Parse (workingDir, tagSource);
cmd.WorkingDirectory = workingDir.IsNullOrEmpty
? entry.BaseDirectory
: workingDir.ToAbsolute (entry.BaseDirectory);
if (environmentVariables != null) {
var vars = new Dictionary<string, string> ();
foreach (var v in environmentVariables)
vars [v.Key] = StringParserService.Parse (v.Value, tagSource);
cmd.EnvironmentVariables = vars;
}
return cmd;
}
public Task<bool> Execute (ProgressMonitor monitor, WorkspaceObject entry, ConfigurationSelector configuration)
{
return Execute (monitor, entry, null, configuration);
}
public bool CanExecute (WorkspaceObject entry, ExecutionContext context, ConfigurationSelector configuration)
{
if (string.IsNullOrEmpty (command))
return false;
if (context == null)
return true;
var cmd = CreateExecutionCommand (entry, configuration);
return context.ExecutionHandler.CanExecute (cmd);
}
public async Task<bool> Execute (ProgressMonitor monitor, WorkspaceObject entry, ExecutionContext context,
ConfigurationSelector configuration)
{
ProcessExecutionCommand cmd = CreateExecutionCommand (entry, configuration);
monitor.Log.WriteLine (GettextCatalog.GetString ("Executing: {0} {1}", cmd.Command, cmd.Arguments));
if (!Directory.Exists (cmd.WorkingDirectory)) {
monitor.ReportError (GettextCatalog.GetString ("Custom command working directory does not exist"), null);
return false;
}
ProcessAsyncOperation oper = null;
OperationConsole console = null;
var result = true;
try {
if (context != null) {
if (externalConsole)
console = context.ExternalConsoleFactory.CreateConsole (!pauseExternalConsole, monitor.CancellationToken);
else
console = context.ConsoleFactory.CreateConsole (monitor.CancellationToken);
oper = context.ExecutionHandler.Execute (cmd, console);
} else {
if (externalConsole) {
console = ExternalConsoleFactory.Instance.CreateConsole (!pauseExternalConsole, monitor.CancellationToken);
oper = Runtime.ProcessService.StartConsoleProcess (cmd.Command, cmd.Arguments,
cmd.WorkingDirectory, console, null);
} else {
oper = Runtime.ProcessService.StartProcess (cmd.Command, cmd.Arguments,
cmd.WorkingDirectory, monitor.Log, monitor.Log, null, false).ProcessAsyncOperation;
}
}
using (var stopper = monitor.CancellationToken.Register (oper.Cancel)) {
await oper.Task;
}
if (oper.ExitCode != 0) {
monitor.ReportError (GettextCatalog.GetString ("Custom command failed (exit code: {0})", oper.ExitCode), null);
}
} catch (Win32Exception w32ex) {
monitor.ReportError (GettextCatalog.GetString ("Failed to execute custom command '{0}': {1}",
cmd.Command, w32ex.Message), null);
return false;
} catch (Exception ex) {
LoggingService.LogError ("Command execution failed", ex);
throw new UserException (GettextCatalog.GetString ("Command execution failed: {0}", ex.Message));
} finally {
result = oper != null && oper.ExitCode == 0;
if (console != null) {
console.Dispose ();
}
}
return result;
}
}
}
|