e5d2118d5ec557808d33472412b99371ad0e4f79
[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 //   Ankit Jain (jankit@novell.com)
7 //
8 // (C) 2005 Marek Sieradzki
9 // Copyright 2009 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 #if NET_2_0
31
32 using System;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Diagnostics;
36 using System.IO;
37 using System.Text;
38 using System.Threading;
39 using Microsoft.Build.Framework;
40 using Microsoft.Build.Utilities;
41
42 namespace Microsoft.Build.Tasks {
43         public class Exec : ToolTaskExtension {
44         
45                 string          command;
46                 bool            ignoreExitCode;
47                 ITaskItem[]     outputs;
48                 string          stdErrEncoding;
49                 string          stdOutEncoding;
50                 string          workingDirectory;
51                 string scriptFile;
52                 
53                 public Exec ()
54                 {
55                         ignoreExitCode = false;
56                 }
57                 
58                 protected internal override void AddCommandLineCommands (CommandLineBuilderExtension commandLine)
59                 {
60                         if (IsRunningOnWindows)
61                                 commandLine.AppendSwitch ("/c");
62
63                         if (!String.IsNullOrEmpty (command)) {
64                                 scriptFile = Path.GetTempFileName ();
65                                 using (StreamWriter sw = new StreamWriter (scriptFile)) {
66                                         sw.Write (command);
67                                 }
68                                 commandLine.AppendFileNameIfNotNull (scriptFile);
69                         }
70                         base.AddCommandLineCommands (commandLine);
71                 }
72
73                 protected override int ExecuteTool (string pathToTool,
74                                                     string responseFileCommands,
75                                                     string commandLineCommands)
76                 {
77                         try {
78                                 return base.ExecuteTool (pathToTool, responseFileCommands, commandLineCommands);
79                         } finally {
80                                 if (scriptFile != null)
81                                         File.Delete (scriptFile);
82                         }
83                 }
84
85                 [MonoTODO]
86                 protected override string GenerateFullPathToTool ()
87                 {
88                         return IsRunningOnWindows ? "cmd.exe" : "sh";
89                 }
90                 
91                 protected override string GetWorkingDirectory ()
92                 {
93                         return workingDirectory;
94                 }
95                 
96                 protected override bool HandleTaskExecutionErrors ()
97                 {
98                         return ExitCode == 0 || ignoreExitCode;
99                 }
100                 
101                 [MonoTODO]
102                 protected override void LogPathToTool (string toolName,
103                                                        string pathToTool)
104                 {
105                 }
106                 
107                 [MonoTODO]
108                 protected override void LogToolCommand (string message)
109                 {
110                         Log.LogMessage (MessageImportance.Normal, "Executing: " + command);
111                 }
112                 
113                 protected override void LogEventsFromTextOutput (string singleLine, MessageImportance importance)
114                 {
115                         Log.LogMessage (importance, singleLine);
116                 }
117
118                 [MonoTODO]
119                 protected override bool ValidateParameters ()
120                 {
121                         return true;
122                 }
123                 
124                 [Required]
125                 public string Command {
126                         get { return command; }
127                         set { command = value; }
128                 }
129
130                 public bool IgnoreExitCode {
131                         get { return ignoreExitCode; }
132                         set { ignoreExitCode = value; }
133                 }
134
135                 [Output]
136                 public ITaskItem[] Outputs {
137                         get { return outputs; }
138                         set { outputs = value; }
139                 }
140
141                 protected override Encoding StandardErrorEncoding {
142                         get { return base.StandardErrorEncoding; }
143                 }
144                 
145                 protected override MessageImportance StandardErrorLoggingImportance {
146                         get { return base.StandardErrorLoggingImportance; }
147                 }
148
149                 protected override Encoding StandardOutputEncoding {
150                         get { return base.StandardOutputEncoding; }
151                 }
152                 
153                 protected override MessageImportance StandardOutputLoggingImportance {
154                         get { return base.StandardOutputLoggingImportance; }
155                 }
156                 
157                 [MonoTODO]
158                 [Output]
159                 public string StdOutEncoding {
160                         get { return stdOutEncoding; }
161                         set { stdOutEncoding = value; }
162                 }
163                 
164                 [MonoTODO]
165                 [Output]
166                 public string StdErrEncoding {
167                         get { return stdErrEncoding; }
168                         set { stdErrEncoding = value; }
169                 }
170                 
171                 [MonoTODO]
172                 protected override string ToolName {
173                         get { return String.Empty; }
174                 }
175
176                 public string WorkingDirectory {
177                         get { return workingDirectory; }
178                         set { workingDirectory = value; }
179                 }
180
181                 static bool IsRunningOnWindows {
182                         get {
183                                 PlatformID pid = Environment.OSVersion.Platform;
184                                 return ((int) pid != 128 && (int) pid != 4 && (int) pid != 6);
185                         }
186                 }
187
188         }
189 }
190
191 #endif