2 // Exec.cs: Task that executes commands.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Diagnostics;
36 using System.Threading;
37 using Microsoft.Build.Framework;
38 using Microsoft.Build.Utilities;
40 namespace Microsoft.Build.Tasks {
41 public class Exec : ToolTaskExtension {
46 string stdErrEncoding;
47 string stdOutEncoding;
48 string workingDirectory;
55 ignoreExitCode = false;
58 // FIXME: what does that method do?
60 protected internal override void AddCommandLineCommands (CommandLineBuilderExtension commandLine)
62 base.AddCommandLineCommands (commandLine);
65 /*public override bool Execute ()
67 StringCollection temporaryOutputs = new StringCollection ();
69 string[] commandTable = command.Split (null, 2);
70 string filename = commandTable [0];
71 string arguments = "";
72 if (commandTable.Length == 2)
73 arguments = commandTable [1];
75 if (workingDirectory != null)
76 process.StartInfo.WorkingDirectory = workingDirectory;
77 process.StartInfo.FileName = filename;
78 process.StartInfo.Arguments = arguments;
79 process.StartInfo.RedirectStandardOutput = true;
80 process.StartInfo.RedirectStandardError = true;
81 process.StartInfo.UseShellExecute = false;
85 process.WaitForExit ();
87 //exitCode = process.ExitCode;
88 while ((line = process.StandardOutput.ReadLine ()) != null)
89 temporaryOutputs.Add (line);
90 outputs = new ITaskItem [temporaryOutputs.Count];
92 foreach (string s in temporaryOutputs)
93 outputs [i++] = new TaskItem (s);
95 catch (Exception ex) {
96 Log.LogErrorFromException (ex);
100 if (exitCode != 0 && ignoreExitCode == false)
106 // FIXME: we need to write another ExecuteTool and RealExecute that will collect std output and
107 // make it available through Outputs property
110 protected override int ExecuteTool (string pathToTool,
111 string responseFileCommands,
112 string commandLineCommands)
114 return base.ExecuteTool (GenerateFullPathToTool (), String.Empty, String.Empty);
118 protected override string GenerateFullPathToTool ()
124 protected override string GetWorkingDirectory ()
126 return Environment.CurrentDirectory;
130 protected override bool HandleTaskExecutionErrors ()
136 protected override void LogPathToTool (string toolName,
142 protected override void LogToolCommand (string message)
147 protected override bool ValidateParameters ()
153 public string Command {
154 get { return command; }
155 set { command = value; }
158 public bool IgnoreExitCode {
159 get { return ignoreExitCode; }
160 set { ignoreExitCode = value; }
164 public ITaskItem[] Outputs {
165 get { return outputs; }
166 set { outputs = value; }
169 protected override Encoding StandardErrorEncoding {
170 get { return base.StandardErrorEncoding; }
173 protected override MessageImportance StandardErrorLoggingImportance {
174 get { return base.StandardErrorLoggingImportance; }
177 protected override Encoding StandardOutputEncoding {
178 get { return base.StandardOutputEncoding; }
181 protected override MessageImportance StandardOutputLoggingImportance {
182 get { return base.StandardOutputLoggingImportance; }
187 public string StdOutEncoding {
188 get { return stdOutEncoding; }
189 set { stdOutEncoding = value; }
194 public string StdErrEncoding {
195 get { return stdErrEncoding; }
196 set { stdErrEncoding = value; }
200 protected override string ToolName {
201 get { return String.Empty; }
204 public string WorkingDirectory {
205 get { return workingDirectory; }
206 set { workingDirectory = value; }