New test.
[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                 [MonoTODO]
107                 protected override int ExecuteTool (string pathToTool,
108                                                     string responseFileCommands,
109                                                     string commandLineCommands)
110                 {
111                         return base.ExecuteTool (GenerateFullPathToTool (), String.Empty, String.Empty);
112                 }
113                 
114                 [MonoTODO]
115                 protected override string GenerateFullPathToTool ()
116                 {
117                         return command;
118                 }
119                 
120                 [MonoTODO]
121                 protected override string GetWorkingDirectory ()
122                 {
123                         return Environment.CurrentDirectory;
124                 }
125                 
126                 [MonoTODO]
127                 protected override bool HandleTaskExecutionErrors ()
128                 {
129                         return true;
130                 }
131                 
132                 [MonoTODO]
133                 protected override void LogPathToTool (string toolName,
134                                                        string pathToTool)
135                 {
136                 }
137                 
138                 [MonoTODO]
139                 protected override void LogToolCommand (string message)
140                 {
141                 }
142                 
143                 [MonoTODO]
144                 protected override bool ValidateParameters ()
145                 {
146                         return true;
147                 }
148                 
149                 [Required]
150                 public string Command {
151                         get { return command; }
152                         set { command = value; }
153                 }
154
155                 public bool IgnoreExitCode {
156                         get { return ignoreExitCode; }
157                         set { ignoreExitCode = value; }
158                 }
159
160                 [Output]
161                 public ITaskItem[] Outputs {
162                         get { return outputs; }
163                         set { outputs = value; }
164                 }
165
166                 protected override Encoding StandardErrorEncoding {
167                         get { return Console.Error.Encoding; }
168                 }
169                 
170                 protected override MessageImportance StandardErrorLoggingImportance {
171                         get { return base.StandardErrorLoggingImportance; }
172                 }
173
174                 protected override Encoding StandardOutputEncoding {
175                         get { return Console.Out.Encoding; }
176                 }
177                 
178                 protected override MessageImportance StandardOutputLoggingImportance {
179                         get { return base.StandardOutputLoggingImportance; }
180                 }
181                 
182                 [MonoTODO]
183                 [Output]
184                 public string StdOutEncoding {
185                         get { return stdOutEncoding; }
186                         set { stdOutEncoding = value; }
187                 }
188                 
189                 [MonoTODO]
190                 [Output]
191                 public string StdErrEncoding {
192                         get { return stdErrEncoding; }
193                         set { stdErrEncoding = value; }
194                 }
195                 
196                 [MonoTODO]
197                 protected override string ToolName {
198                         get { return String.Empty; }
199                 }
200
201                 public string WorkingDirectory {
202                         get { return workingDirectory; }
203                         set { workingDirectory = value; }
204                 }
205         }
206 }
207
208 #endif