2 // Exec.cs: Task that executes commands.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
6 // Ankit Jain (jankit@novell.com)
8 // (C) 2005 Marek Sieradzki
9 // Copyright 2009 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Diagnostics;
38 using System.Threading;
39 using Microsoft.Build.Framework;
40 using Microsoft.Build.Utilities;
42 namespace Microsoft.Build.Tasks {
43 public class Exec : ToolTaskExtension {
48 string stdErrEncoding;
49 string stdOutEncoding;
50 string workingDirectory;
55 ignoreExitCode = false;
58 protected internal override void AddCommandLineCommands (CommandLineBuilderExtension commandLine)
60 if (IsRunningOnWindows)
61 commandLine.AppendSwitch ("/c");
63 if (!String.IsNullOrEmpty (command)) {
64 scriptFile = Path.GetTempFileName ();
65 using (StreamWriter sw = new StreamWriter (scriptFile)) {
68 commandLine.AppendFileNameIfNotNull (scriptFile);
70 base.AddCommandLineCommands (commandLine);
73 protected override int ExecuteTool (string pathToTool,
74 string responseFileCommands,
75 string commandLineCommands)
78 return base.ExecuteTool (pathToTool, responseFileCommands, commandLineCommands);
80 if (scriptFile != null)
81 File.Delete (scriptFile);
86 protected override string GenerateFullPathToTool ()
88 return IsRunningOnWindows ? "cmd.exe" : "sh";
91 protected override string GetWorkingDirectory ()
93 return workingDirectory;
96 protected override bool HandleTaskExecutionErrors ()
98 return ExitCode == 0 || ignoreExitCode;
102 protected override void LogPathToTool (string toolName,
108 protected override void LogToolCommand (string message)
110 Log.LogMessage (MessageImportance.Normal, "Executing: " + command);
113 protected override void LogEventsFromTextOutput (string singleLine, MessageImportance importance)
115 Log.LogMessage (importance, singleLine);
119 protected override bool ValidateParameters ()
125 public string Command {
126 get { return command; }
127 set { command = value; }
130 public bool IgnoreExitCode {
131 get { return ignoreExitCode; }
132 set { ignoreExitCode = value; }
136 public ITaskItem[] Outputs {
137 get { return outputs; }
138 set { outputs = value; }
141 protected override Encoding StandardErrorEncoding {
142 get { return base.StandardErrorEncoding; }
145 protected override MessageImportance StandardErrorLoggingImportance {
146 get { return base.StandardErrorLoggingImportance; }
149 protected override Encoding StandardOutputEncoding {
150 get { return base.StandardOutputEncoding; }
153 protected override MessageImportance StandardOutputLoggingImportance {
154 get { return base.StandardOutputLoggingImportance; }
159 public string StdOutEncoding {
160 get { return stdOutEncoding; }
161 set { stdOutEncoding = value; }
166 public string StdErrEncoding {
167 get { return stdErrEncoding; }
168 set { stdErrEncoding = value; }
172 protected override string ToolName {
173 get { return String.Empty; }
176 public string WorkingDirectory {
177 get { return workingDirectory; }
178 set { workingDirectory = value; }
181 static bool IsRunningOnWindows {
183 PlatformID pid = Environment.OSVersion.Platform;
184 return ((int) pid != 128 && (int) pid != 4 && (int) pid != 6);