Merge pull request #4453 from lambdageek/bug-49721
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Evaluation / Project.cs
index ff5d0e6a2a24e7c011c9e409b511c5442d64413d..e4263aa9de1c6ba6bc994e8facf7998cf05d02b1 100644 (file)
@@ -79,27 +79,27 @@ namespace Microsoft.Build.Evaluation
        + "{data.Items.Count} #Targets={data.Targets.Count}")]
        public class Project
        {
-               public Project (XmlReader xml)
-                       : this (ProjectRootElement.Create (xml))
+               public Project (XmlReader xmlReader)
+                       : this (ProjectRootElement.Create (xmlReader))
                {
                }
 
-               public Project (XmlReader xml, IDictionary<string, string> globalProperties,
+               public Project (XmlReader xmlReader, IDictionary<string, string> globalProperties,
                                              string toolsVersion)
-                       : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion)
+                       : this (ProjectRootElement.Create (xmlReader), globalProperties, toolsVersion)
                {
                }
 
-               public Project (XmlReader xml, IDictionary<string, string> globalProperties,
+               public Project (XmlReader xmlReader, IDictionary<string, string> globalProperties,
                                              string toolsVersion, ProjectCollection projectCollection)
-                       : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion, projectCollection)
+                       : this (ProjectRootElement.Create (xmlReader), globalProperties, toolsVersion, projectCollection)
                {
                }
 
-               public Project (XmlReader xml, IDictionary<string, string> globalProperties,
+               public Project (XmlReader xmlReader, IDictionary<string, string> globalProperties,
                                              string toolsVersion, ProjectCollection projectCollection,
                                              ProjectLoadSettings loadSettings)
-                       : this (ProjectRootElement.Create (xml), globalProperties, toolsVersion, projectCollection, loadSettings)
+                       : this (ProjectRootElement.Create (xmlReader), globalProperties, toolsVersion, projectCollection, loadSettings)
                {
                }
 
@@ -382,12 +382,12 @@ namespace Microsoft.Build.Evaluation
 
                public bool Build ()
                {
-                       return Build (Xml.DefaultTargets.Split (target_sep, StringSplitOptions.RemoveEmptyEntries));
+                       return Build (GetDefaultTargets (Xml));
                }
 
                public bool Build (IEnumerable<ILogger> loggers)
                {
-                       return Build (Xml.DefaultTargets.Split (target_sep, StringSplitOptions.RemoveEmptyEntries), loggers);
+                       return Build (GetDefaultTargets (Xml), loggers);
                }
 
                public bool Build (string target)
@@ -402,7 +402,7 @@ namespace Microsoft.Build.Evaluation
 
                public bool Build (ILogger logger)
                {
-                       return Build (Xml.DefaultTargets.Split (target_sep, StringSplitOptions.RemoveEmptyEntries), new ILogger [] {logger});
+                       return Build (GetDefaultTargets (Xml), new ILogger [] {logger});
                }
 
                public bool Build (string[] targets, IEnumerable<ILogger> loggers)
@@ -412,7 +412,7 @@ namespace Microsoft.Build.Evaluation
 
                public bool Build (IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers)
                {
-                       return Build (Xml.DefaultTargets.Split (target_sep, StringSplitOptions.RemoveEmptyEntries), loggers, remoteLoggers);
+                       return Build (GetDefaultTargets (Xml), loggers, remoteLoggers);
                }
 
                public bool Build (string target, IEnumerable<ILogger> loggers)
@@ -434,6 +434,41 @@ namespace Microsoft.Build.Evaluation
                        return Build (new string [] { target }, loggers, remoteLoggers);
                }
 
+               // FIXME: this is a duplicate code between Project and ProjectInstance
+               static readonly char [] item_target_sep = {';'};
+               
+               string [] GetDefaultTargets (ProjectRootElement xml)
+               {
+                       var ret = GetDefaultTargets (xml, true, true);
+                       return ret.Any () ? ret : GetDefaultTargets (xml, false, true);
+               }
+               
+               string [] GetDefaultTargets (ProjectRootElement xml, bool fromAttribute, bool checkImports)
+               {
+                       if (fromAttribute) {
+                               var ret = xml.DefaultTargets.Split (item_target_sep, StringSplitOptions.RemoveEmptyEntries).Select (s => s.Trim ()).ToArray ();
+                               if (checkImports && ret.Length == 0) {
+                                       foreach (var imp in this.raw_imports) {
+                                               ret = GetDefaultTargets (imp.ImportedProject, true, false);
+                                               if (ret.Any ())
+                                                       break;
+                                       }
+                               }
+                               return ret;
+                       } else {
+                               if (xml.Targets.Any ())
+                                       return new String [] { xml.Targets.First ().Name };
+                               if (checkImports) {
+                                       foreach (var imp in this.raw_imports) {
+                                               var ret = GetDefaultTargets (imp.ImportedProject, false, false);
+                                               if (ret.Any ())
+                                                       return ret;
+                                       }
+                               }
+                               return new string [0];
+                       }
+               }
+
                public ProjectInstance CreateProjectInstance ()
                {
                        var ret = new ProjectInstance (Xml, GlobalProperties, ToolsVersion, ProjectCollection);
@@ -682,11 +717,7 @@ namespace Microsoft.Build.Evaluation
 
                public bool SkipEvaluation { get; set; }
 
-               #if NET_4_5
                public
-               #else
-               internal
-               #endif
                IDictionary<string, ProjectTargetInstance> Targets {
                        get { return targets; }
                }