[System] Fix a few type members on WatchOS
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / Exec.cs
index f3fb35e9163139001d7899d6db10bcdc61097203..fe1f00bfa5f46861467f6c1d89c198df635b5679 100644 (file)
@@ -3,8 +3,10 @@
 //
 // Author:
 //   Marek Sieradzki (marek.sieradzki@gmail.com)
+//   Ankit Jain (jankit@novell.com)
 //
 // (C) 2005 Marek Sieradzki
+// Copyright 2009 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-#if NET_2_0
-
 using System;
 using System.Collections;
 using System.Collections.Specialized;
 using System.Diagnostics;
 using System.IO;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Threading;
 using Microsoft.Build.Framework;
 using Microsoft.Build.Utilities;
@@ -46,80 +47,63 @@ namespace Microsoft.Build.Tasks {
                string          stdErrEncoding;
                string          stdOutEncoding;
                string          workingDirectory;
-               
-               Process         process;
-               int             executionTime;
+               string scriptFile;
+
+               Func<string, bool> errorMatcher, warningMatcher;
                
                public Exec ()
                {
                        ignoreExitCode = false;
                }
-
-               /*public override bool Execute ()
-               {
-                       StringCollection temporaryOutputs = new StringCollection ();
-                       string line = null;
-                       string[] commandTable = command.Split (null, 2);
-                       string filename = commandTable [0];
-                       string arguments = "";
-                       if (commandTable.Length == 2)
-                               arguments = commandTable [1];
                
-                       if (workingDirectory != null)
-                               process.StartInfo.WorkingDirectory = workingDirectory;
-                       process.StartInfo.FileName = filename;
-                       process.StartInfo.Arguments = arguments;
-                       process.StartInfo.RedirectStandardOutput = true;
-                       process.StartInfo.RedirectStandardError = true;
-                       process.StartInfo.UseShellExecute = false;
-                       
-                       try {
-                               process.Start ();
-                               process.WaitForExit ();
-
-                               //exitCode = process.ExitCode;
-                               while ((line = process.StandardOutput.ReadLine ()) != null)
-                                       temporaryOutputs.Add (line);
-                               outputs = new ITaskItem [temporaryOutputs.Count];
-                               int i  = 0;
-                               foreach (string s in temporaryOutputs)
-                                       outputs [i++] = new TaskItem (s);
-                       }
-                       catch (Exception ex) {
-                               Log.LogErrorFromException (ex);
-                               return false;
+               protected internal override void AddCommandLineCommands (CommandLineBuilderExtension commandLine)
+               {
+                       if (IsRunningOnWindows)
+                               commandLine.AppendSwitch ("/q /c");
+
+                       if (!String.IsNullOrEmpty (command)) {
+                               scriptFile = Path.GetTempFileName ();
+                               if (IsRunningOnWindows)
+                                       scriptFile = scriptFile + ".bat";
+                               using (StreamWriter sw = new StreamWriter (scriptFile)) {
+                                       sw.Write (command);
+                               }
+                               commandLine.AppendFileNameIfNotNull (scriptFile);
                        }
-                       
-                       if (exitCode != 0 && ignoreExitCode == false)
-                               return false;
-                       else
-                               return true;
-               }*/
-               
-               [MonoTODO]
+                       base.AddCommandLineCommands (commandLine);
+               }
+
                protected override int ExecuteTool (string pathToTool,
                                                    string responseFileCommands,
                                                    string commandLineCommands)
                {
-                       return base.ExecuteTool (GenerateFullPathToTool (), String.Empty, String.Empty);
+                       try {
+                               errorMatcher = GetTryMatchRegexFunc (CustomErrorRegularExpression, true);
+                               warningMatcher = GetTryMatchRegexFunc (CustomWarningRegularExpression, false);
+                               return base.ExecuteTool (pathToTool, responseFileCommands, commandLineCommands);
+                       } finally {
+                               if (scriptFile != null)
+                                       DeleteTempFile (scriptFile);
+                       }
                }
-               
+
                [MonoTODO]
                protected override string GenerateFullPathToTool ()
                {
-                       return command;
+                       return IsRunningOnWindows ? "cmd.exe" : "sh";
                }
                
-               [MonoTODO]
                protected override string GetWorkingDirectory ()
                {
-                       return Environment.CurrentDirectory;
+                       return workingDirectory;
                }
                
-               [MonoTODO]
                protected override bool HandleTaskExecutionErrors ()
                {
-                       return true;
+                       if (ExitCode != 0)
+                               Log.LogError ("Command '{0}' exited with code: {1}.", Command, ExitCode);
+
+                       return ExitCode == 0 || ignoreExitCode;
                }
                
                [MonoTODO]
@@ -131,8 +115,49 @@ namespace Microsoft.Build.Tasks {
                [MonoTODO]
                protected override void LogToolCommand (string message)
                {
+                       Log.LogMessage (MessageImportance.Normal, "Executing: " + command);
                }
                
+               protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance)
+               {
+                       if (IgnoreStandardErrorWarningFormat ||
+                               (!errorMatcher (singleLine) && !warningMatcher (singleLine)))
+                               Log.LogMessage (messageImportance, singleLine);
+               }
+
+               // @is_error_type - log as errors, else warnings
+               Func<string, bool> GetTryMatchRegexFunc (string regex_str, bool is_error_type)
+               {
+                       bool is_bad = false;
+                       Regex regex = null;
+                       return (singleLine) => {
+                               if (String.IsNullOrEmpty (regex_str) || is_bad)
+                                       return false;
+
+                               try {
+                                       if (regex == null)
+                                               regex = new Regex (regex_str, RegexOptions.Compiled);
+                               } catch (ArgumentException ae) {
+                                       Log.LogError ("The regular expression specified for '{0}' is invalid : {1}",
+                                                       is_error_type ? "errors" : "warnings", ae.Message);
+                                       Log.LogMessage (MessageImportance.Low, "The regular expression specified for '{0}' is invalid : {1}",
+                                                       is_error_type ? "errors" : "warnings", ae.ToString ());
+
+                                       is_bad = true;
+                                       return false;
+                               }
+
+                               if (!regex.Match (singleLine).Success)
+                                       return false;
+
+                               if (is_error_type)
+                                       Log.LogError (singleLine);
+                               else
+                                       Log.LogWarning (singleLine);
+                               return true;
+                       };
+               }
+
                [MonoTODO]
                protected override bool ValidateParameters ()
                {
@@ -142,7 +167,11 @@ namespace Microsoft.Build.Tasks {
                [Required]
                public string Command {
                        get { return command; }
-                       set { command = value; }
+                       set {
+                               command = value;
+                               if (Path.DirectorySeparatorChar == '/')
+                                       command = command.Replace ("\r\n", "\n");
+                       }
                }
 
                public bool IgnoreExitCode {
@@ -157,7 +186,7 @@ namespace Microsoft.Build.Tasks {
                }
 
                protected override Encoding StandardErrorEncoding {
-                       get { return Console.Error.Encoding; }
+                       get { return base.StandardErrorEncoding; }
                }
                
                protected override MessageImportance StandardErrorLoggingImportance {
@@ -165,12 +194,18 @@ namespace Microsoft.Build.Tasks {
                }
 
                protected override Encoding StandardOutputEncoding {
-                       get { return Console.Out.Encoding; }
+                       get { return base.StandardOutputEncoding; }
                }
                
                protected override MessageImportance StandardOutputLoggingImportance {
                        get { return base.StandardOutputLoggingImportance; }
                }
+
+               public bool IgnoreStandardErrorWarningFormat { get; set; }
+
+               public string CustomErrorRegularExpression { get; set; }
+
+               public string CustomWarningRegularExpression { get; set; }
                
                [MonoTODO]
                [Output]
@@ -195,7 +230,13 @@ namespace Microsoft.Build.Tasks {
                        get { return workingDirectory; }
                        set { workingDirectory = value; }
                }
+
+               static bool IsRunningOnWindows {
+                       get {
+                               PlatformID pid = Environment.OSVersion.Platform;
+                               return ((int) pid != 128 && (int) pid != 4 && (int) pid != 6);
+                       }
+               }
+
        }
 }
-
-#endif