Fix issue with VB projects
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / Exec.cs
index fde1d445ad5d7b887697a993604a56f4500bfb36..fe1f00bfa5f46861467f6c1d89c198df635b5679 100644 (file)
 // 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;
@@ -49,6 +48,8 @@ namespace Microsoft.Build.Tasks {
                string          stdOutEncoding;
                string          workingDirectory;
                string scriptFile;
+
+               Func<string, bool> errorMatcher, warningMatcher;
                
                public Exec ()
                {
@@ -58,10 +59,12 @@ namespace Microsoft.Build.Tasks {
                protected internal override void AddCommandLineCommands (CommandLineBuilderExtension commandLine)
                {
                        if (IsRunningOnWindows)
-                               commandLine.AppendSwitch ("/c");
+                               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);
                                }
@@ -75,6 +78,8 @@ namespace Microsoft.Build.Tasks {
                                                    string commandLineCommands)
                {
                        try {
+                               errorMatcher = GetTryMatchRegexFunc (CustomErrorRegularExpression, true);
+                               warningMatcher = GetTryMatchRegexFunc (CustomWarningRegularExpression, false);
                                return base.ExecuteTool (pathToTool, responseFileCommands, commandLineCommands);
                        } finally {
                                if (scriptFile != null)
@@ -113,9 +118,44 @@ namespace Microsoft.Build.Tasks {
                        Log.LogMessage (MessageImportance.Normal, "Executing: " + command);
                }
                
-               protected override void LogEventsFromTextOutput (string singleLine, MessageImportance importance)
+               protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance)
                {
-                       Log.LogMessage (importance, singleLine);
+                       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]
@@ -127,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 {
@@ -156,6 +200,12 @@ namespace Microsoft.Build.Tasks {
                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]
@@ -190,5 +240,3 @@ namespace Microsoft.Build.Tasks {
 
        }
 }
-
-#endif