2007-02-02 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / Exec.cs
1 //
2 // Exec.cs: Task that executes commands.
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2005 Marek Sieradzki
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27
28 #if NET_2_0
29
30 using System;
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Diagnostics;
34 using System.IO;
35 using System.Text;
36 using System.Threading;
37 using Microsoft.Build.Framework;
38 using Microsoft.Build.Utilities;
39
40 namespace Microsoft.Build.Tasks {
41         public class Exec : ToolTaskExtension {
42         
43                 string          command;
44                 bool            ignoreExitCode;
45                 ITaskItem[]     outputs;
46                 string          stdErrEncoding;
47                 string          stdOutEncoding;
48                 string          workingDirectory;
49                 
50                 //Process               process;
51                 //int           executionTime;
52                 
53                 public Exec ()
54                 {
55                         ignoreExitCode = false;
56                 }
57                 
58                 // FIXME: what does that method do?
59                 [MonoTODO]
60                 protected internal override void AddCommandLineCommands (CommandLineBuilderExtension commandLine)
61                 {
62                         base.AddCommandLineCommands (commandLine);
63                 }
64                 
65                 /*public override bool Execute ()
66                 {
67                         StringCollection temporaryOutputs = new StringCollection ();
68                         string line = null;
69                         string[] commandTable = command.Split (null, 2);
70                         string filename = commandTable [0];
71                         string arguments = "";
72                         if (commandTable.Length == 2)
73                                 arguments = commandTable [1];
74                 
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;
82                         
83                         try {
84                                 process.Start ();
85                                 process.WaitForExit ();
86
87                                 //exitCode = process.ExitCode;
88                                 while ((line = process.StandardOutput.ReadLine ()) != null)
89                                         temporaryOutputs.Add (line);
90                                 outputs = new ITaskItem [temporaryOutputs.Count];
91                                 int i  = 0;
92                                 foreach (string s in temporaryOutputs)
93                                         outputs [i++] = new TaskItem (s);
94                         }
95                         catch (Exception ex) {
96                                 Log.LogErrorFromException (ex);
97                                 return false;
98                         }
99                         
100                         if (exitCode != 0 && ignoreExitCode == false)
101                                 return false;
102                         else
103                                 return true;
104                 }*/
105                 
106                 // FIXME: we need to write another ExecuteTool and RealExecute that will collect std output and
107                 // make it available through Outputs property
108                 
109                 [MonoTODO]
110                 protected override int ExecuteTool (string pathToTool,
111                                                     string responseFileCommands,
112                                                     string commandLineCommands)
113                 {
114                         return base.ExecuteTool (GenerateFullPathToTool (), String.Empty, String.Empty);
115                 }
116                 
117                 [MonoTODO]
118                 protected override string GenerateFullPathToTool ()
119                 {
120                         return command;
121                 }
122                 
123                 [MonoTODO]
124                 protected override string GetWorkingDirectory ()
125                 {
126                         return Environment.CurrentDirectory;
127                 }
128                 
129                 [MonoTODO]
130                 protected override bool HandleTaskExecutionErrors ()
131                 {
132                         return true;
133                 }
134                 
135                 [MonoTODO]
136                 protected override void LogPathToTool (string toolName,
137                                                        string pathToTool)
138                 {
139                 }
140                 
141                 [MonoTODO]
142                 protected override void LogToolCommand (string message)
143                 {
144                 }
145                 
146                 [MonoTODO]
147                 protected override bool ValidateParameters ()
148                 {
149                         return true;
150                 }
151                 
152                 [Required]
153                 public string Command {
154                         get { return command; }
155                         set { command = value; }
156                 }
157
158                 public bool IgnoreExitCode {
159                         get { return ignoreExitCode; }
160                         set { ignoreExitCode = value; }
161                 }
162
163                 [Output]
164                 public ITaskItem[] Outputs {
165                         get { return outputs; }
166                         set { outputs = value; }
167                 }
168
169                 protected override Encoding StandardErrorEncoding {
170                         get { return base.StandardErrorEncoding; }
171                 }
172                 
173                 protected override MessageImportance StandardErrorLoggingImportance {
174                         get { return base.StandardErrorLoggingImportance; }
175                 }
176
177                 protected override Encoding StandardOutputEncoding {
178                         get { return base.StandardOutputEncoding; }
179                 }
180                 
181                 protected override MessageImportance StandardOutputLoggingImportance {
182                         get { return base.StandardOutputLoggingImportance; }
183                 }
184                 
185                 [MonoTODO]
186                 [Output]
187                 public string StdOutEncoding {
188                         get { return stdOutEncoding; }
189                         set { stdOutEncoding = value; }
190                 }
191                 
192                 [MonoTODO]
193                 [Output]
194                 public string StdErrEncoding {
195                         get { return stdErrEncoding; }
196                         set { stdErrEncoding = value; }
197                 }
198                 
199                 [MonoTODO]
200                 protected override string ToolName {
201                         get { return String.Empty; }
202                 }
203
204                 public string WorkingDirectory {
205                         get { return workingDirectory; }
206                         set { workingDirectory = value; }
207                 }
208         }
209 }
210
211 #endif