53ff89c5cd673e0f16bd3674247bb6b74ab7124b
[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 ("/q /c");
62
63                         if (!String.IsNullOrEmpty (command)) {
64                                 scriptFile = Path.GetTempFileName ();
65                                 if (IsRunningOnWindows)
66                                         scriptFile = scriptFile + ".bat";
67                                 using (StreamWriter sw = new StreamWriter (scriptFile)) {
68                                         sw.Write (command);
69                                 }
70                                 commandLine.AppendFileNameIfNotNull (scriptFile);
71                         }
72                         base.AddCommandLineCommands (commandLine);
73                 }
74
75                 protected override int ExecuteTool (string pathToTool,
76                                                     string responseFileCommands,
77                                                     string commandLineCommands)
78                 {
79                         try {
80                                 return base.ExecuteTool (pathToTool, responseFileCommands, commandLineCommands);
81                         } finally {
82                                 if (scriptFile != null)
83                                         DeleteTempFile (scriptFile);
84                         }
85                 }
86
87                 [MonoTODO]
88                 protected override string GenerateFullPathToTool ()
89                 {
90                         return IsRunningOnWindows ? "cmd.exe" : "sh";
91                 }
92                 
93                 protected override string GetWorkingDirectory ()
94                 {
95                         return workingDirectory;
96                 }
97                 
98                 protected override bool HandleTaskExecutionErrors ()
99                 {
100                         if (ExitCode != 0)
101                                 Log.LogError ("Command '{0}' exited with code: {1}.", Command, ExitCode);
102
103                         return ExitCode == 0 || ignoreExitCode;
104                 }
105                 
106                 [MonoTODO]
107                 protected override void LogPathToTool (string toolName,
108                                                        string pathToTool)
109                 {
110                 }
111                 
112                 [MonoTODO]
113                 protected override void LogToolCommand (string message)
114                 {
115                         Log.LogMessage (MessageImportance.Normal, "Executing: " + command);
116                 }
117                 
118                 protected override void LogEventsFromTextOutput (string singleLine, MessageImportance importance)
119                 {
120                         Log.LogMessage (importance, singleLine);
121                 }
122
123                 [MonoTODO]
124                 protected override bool ValidateParameters ()
125                 {
126                         return true;
127                 }
128                 
129                 [Required]
130                 public string Command {
131                         get { return command; }
132                         set {
133                                 command = value;
134                                 if (Path.DirectorySeparatorChar == '/')
135                                         command = command.Replace ("\r\n", "\n");
136                         }
137                 }
138
139                 public bool IgnoreExitCode {
140                         get { return ignoreExitCode; }
141                         set { ignoreExitCode = value; }
142                 }
143
144                 [Output]
145                 public ITaskItem[] Outputs {
146                         get { return outputs; }
147                         set { outputs = value; }
148                 }
149
150                 protected override Encoding StandardErrorEncoding {
151                         get { return base.StandardErrorEncoding; }
152                 }
153                 
154                 protected override MessageImportance StandardErrorLoggingImportance {
155                         get { return base.StandardErrorLoggingImportance; }
156                 }
157
158                 protected override Encoding StandardOutputEncoding {
159                         get { return base.StandardOutputEncoding; }
160                 }
161                 
162                 protected override MessageImportance StandardOutputLoggingImportance {
163                         get { return base.StandardOutputLoggingImportance; }
164                 }
165                 
166                 [MonoTODO]
167                 [Output]
168                 public string StdOutEncoding {
169                         get { return stdOutEncoding; }
170                         set { stdOutEncoding = value; }
171                 }
172                 
173                 [MonoTODO]
174                 [Output]
175                 public string StdErrEncoding {
176                         get { return stdErrEncoding; }
177                         set { stdErrEncoding = value; }
178                 }
179                 
180                 [MonoTODO]
181                 protected override string ToolName {
182                         get { return String.Empty; }
183                 }
184
185                 public string WorkingDirectory {
186                         get { return workingDirectory; }
187                         set { workingDirectory = value; }
188                 }
189
190                 static bool IsRunningOnWindows {
191                         get {
192                                 PlatformID pid = Environment.OSVersion.Platform;
193                                 return ((int) pid != 128 && (int) pid != 4 && (int) pid != 6);
194                         }
195                 }
196
197         }
198 }
199
200 #endif