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 ("/q /c");
63 if (!String.IsNullOrEmpty (command)) {
64 scriptFile = Path.GetTempFileName ();
65 if (IsRunningOnWindows)
66 scriptFile = scriptFile + ".bat";
67 using (StreamWriter sw = new StreamWriter (scriptFile)) {
70 commandLine.AppendFileNameIfNotNull (scriptFile);
72 base.AddCommandLineCommands (commandLine);
75 protected override int ExecuteTool (string pathToTool,
76 string responseFileCommands,
77 string commandLineCommands)
80 return base.ExecuteTool (pathToTool, responseFileCommands, commandLineCommands);
82 if (scriptFile != null)
83 DeleteTempFile (scriptFile);
88 protected override string GenerateFullPathToTool ()
90 return IsRunningOnWindows ? "cmd.exe" : "sh";
93 protected override string GetWorkingDirectory ()
95 return workingDirectory;
98 protected override bool HandleTaskExecutionErrors ()
101 Log.LogError ("Command '{0}' exited with code: {1}.", Command, ExitCode);
103 return ExitCode == 0 || ignoreExitCode;
107 protected override void LogPathToTool (string toolName,
113 protected override void LogToolCommand (string message)
115 Log.LogMessage (MessageImportance.Normal, "Executing: " + command);
118 protected override void LogEventsFromTextOutput (string singleLine, MessageImportance importance)
120 Log.LogMessage (importance, singleLine);
124 protected override bool ValidateParameters ()
130 public string Command {
131 get { return command; }
134 if (Path.DirectorySeparatorChar == '/')
135 command = command.Replace ("\r\n", "\n");
139 public bool IgnoreExitCode {
140 get { return ignoreExitCode; }
141 set { ignoreExitCode = value; }
145 public ITaskItem[] Outputs {
146 get { return outputs; }
147 set { outputs = value; }
150 protected override Encoding StandardErrorEncoding {
151 get { return base.StandardErrorEncoding; }
154 protected override MessageImportance StandardErrorLoggingImportance {
155 get { return base.StandardErrorLoggingImportance; }
158 protected override Encoding StandardOutputEncoding {
159 get { return base.StandardOutputEncoding; }
162 protected override MessageImportance StandardOutputLoggingImportance {
163 get { return base.StandardOutputLoggingImportance; }
168 public string StdOutEncoding {
169 get { return stdOutEncoding; }
170 set { stdOutEncoding = value; }
175 public string StdErrEncoding {
176 get { return stdErrEncoding; }
177 set { stdErrEncoding = value; }
181 protected override string ToolName {
182 get { return String.Empty; }
185 public string WorkingDirectory {
186 get { return workingDirectory; }
187 set { workingDirectory = value; }
190 static bool IsRunningOnWindows {
192 PlatformID pid = Environment.OSVersion.Platform;
193 return ((int) pid != 128 && (int) pid != 4 && (int) pid != 6);