Merge pull request #2417 from razzfazz/guard_substr
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Internal / BuildTaskDatabase.cs
index 97a5a4692cf46f34446a26f07ec57736ca26cbfc..3d3b3daa327448b3aa7040bf898047aa806f5832 100644 (file)
@@ -45,6 +45,8 @@ namespace Microsoft.Build.Internal
 
                public static BuildTaskDatabase GetDefaultTaskDatabase (Toolset toolset)
                {
+                       if (toolset == null)
+                               throw new ArgumentNullException ("toolset");
                        BuildTaskDatabase defaults;
                        if (!default_factory.TryGetValue (toolset.ToolsVersion, out defaults)) {
                                defaults = new BuildTaskDatabase (toolset);
@@ -58,12 +60,13 @@ namespace Microsoft.Build.Internal
                        ProjectRootElement root;
                        using (var xml = XmlReader.Create (Path.Combine (toolset.ToolsPath, default_tasks_file)))
                                root = ProjectRootElement.Create (xml);
-                       LoadUsingTasks (null, root);
+                       LoadUsingTasks (null, root.UsingTasks);
                }
                
-               public BuildTaskDatabase (ProjectInstance projectInstance, ProjectRootElement projectRootElement)
+               public BuildTaskDatabase (IBuildEngine engine, ProjectInstance projectInstance)
                {
-                       LoadUsingTasks (projectInstance, projectRootElement);
+                       this.engine = engine;
+                       LoadUsingTasks (projectInstance, projectInstance.UsingTasks);
                }
                
                internal class TaskDescription
@@ -90,35 +93,70 @@ namespace Microsoft.Build.Internal
                        public string AssemblyFile { get; set; }
                        public Assembly LoadedAssembly { get; set; }
                }
-               
+
+               readonly IBuildEngine engine;
                readonly List<TaskAssembly> assemblies = new List<TaskAssembly> ();
                readonly List<TaskDescription> task_descs = new List<TaskDescription> ();
 
                public List<TaskDescription> Tasks {
                        get { return task_descs; }
                }
-               
-               void LoadUsingTasks (ProjectInstance projectInstance, ProjectRootElement project)
+
+               // FIXME: my guess is the tasks does not have to be loaded entirely but only requested tasks must be loaded at invocation time.
+               void LoadUsingTasks (ProjectInstance projectInstance, IEnumerable<ProjectUsingTaskElement> usingTasks)
                {
                        Func<string,bool> cond = s => projectInstance != null ? projectInstance.EvaluateCondition (s) : Convert.ToBoolean (s);
-                       foreach (var ut in project.UsingTasks) {
-                               var ta = assemblies.FirstOrDefault (a => a.AssemblyFile.Equals (ut.AssemblyFile, StringComparison.OrdinalIgnoreCase) || a.AssemblyName.Equals (ut.AssemblyName, StringComparison.OrdinalIgnoreCase));
+                       Func<string,string> expand = s => projectInstance != null ? projectInstance.ExpandString (s) : s;
+                       foreach (var ut in usingTasks) {
+                               var aName = expand (ut.AssemblyName);
+                               var aFile = expand (ut.AssemblyFile);
+                               if (string.IsNullOrEmpty (aName) && string.IsNullOrEmpty (aFile)) {
+                                       var errorNoAssembly = string.Format ("Task '{0}' does not specify either of AssemblyName or AssemblyFile.", ut.TaskName);
+                                       engine.LogWarningEvent (new BuildWarningEventArgs (null, null, projectInstance.FullPath, ut.Location.Line, ut.Location.Column, 0, 0, errorNoAssembly, null, null));
+                                       continue;
+                               }
+                               var ta = assemblies.FirstOrDefault (a => a.AssemblyFile.Equals (aFile, StringComparison.OrdinalIgnoreCase) || a.AssemblyName.Equals (aName, StringComparison.OrdinalIgnoreCase));
                                if (ta == null) {
-                                       ta = new TaskAssembly () { AssemblyName = ut.AssemblyName, AssemblyFile = ut.AssemblyFile };
-                                       ta.LoadedAssembly = ta.AssemblyName != null ? Assembly.Load (ta.AssemblyName) : Assembly.LoadFile (ta.AssemblyFile);
+                                       var path = Path.GetDirectoryName (string.IsNullOrEmpty (ut.Location.File) ? projectInstance.FullPath : ut.Location.File);
+                                       ta = new TaskAssembly () { AssemblyName = aName, AssemblyFile = aFile };
+                                       try {
+                                               ta.LoadedAssembly = !string.IsNullOrEmpty (ta.AssemblyName) ? Assembly.Load (ta.AssemblyName) : Assembly.LoadFile (Path.Combine (path, ta.AssemblyFile));
+                                       } catch {
+                                               var errorNotLoaded = string.Format ("For task '{0}' Specified assembly '{1}' was not found", ut.TaskName, string.IsNullOrEmpty (ta.AssemblyName) ? Path.Combine (path, ta.AssemblyFile) : ta.AssemblyName);
+                                               engine.LogWarningEvent (new BuildWarningEventArgs (null, null, projectInstance.FullPath, ut.Location.Line, ut.Location.Column, 0, 0, errorNotLoaded, null, null));
+                                               continue;
+                                       }
                                        assemblies.Add (ta);
                                }
                                var pg = ut.ParameterGroup == null ? null : ut.ParameterGroup.Parameters.Select (p => new TaskPropertyInfo (p.Name, Type.GetType (p.ParameterType), cond (p.Output), cond (p.Required)))
                                        .ToDictionary (p => p.Name);
-                               var task = new TaskDescription () {
+                               
+
+                               Type type = null;
+                               string error = null;
+                               TaskDescription task = new TaskDescription () {
                                        TaskAssembly = ta,
                                        Name = ut.TaskName,
-                                       TaskFactoryType = string.IsNullOrEmpty (ut.TaskFactory) ? null : LoadTypeFrom (ta.LoadedAssembly, ut.TaskName, ut.TaskFactory),
-                                       TaskType = string.IsNullOrEmpty (ut.TaskFactory) ? LoadTypeFrom (ta.LoadedAssembly, ut.TaskName, ut.TaskName) : null,
                                        TaskFactoryParameters = pg,
-                                       TaskBody = ut.TaskBody != null && cond (ut.TaskBody.Condition) ? ut.TaskBody.Evaluate : null,
+                                       TaskBody = ut.TaskBody != null && cond (ut.TaskBody.Condition) ? ut.TaskBody.TaskBody : null,
                                        };
-                               task_descs.Add (task);
+                               if (string.IsNullOrEmpty (ut.TaskFactory)) {
+                                       type = LoadTypeFrom (ta.LoadedAssembly, ut.TaskName, ut.TaskName);
+                                       if (type == null)
+                                               error = string.Format ("For task '{0}' Specified type '{1}' was not found in assembly '{2}'", ut.TaskName, ut.TaskName, ta.LoadedAssembly.FullName);
+                                       else
+                                               task.TaskType = type;
+                               } else {
+                                       type = LoadTypeFrom (ta.LoadedAssembly, ut.TaskName, ut.TaskFactory);
+                                       if (type == null)
+                                               error = string.Format ("For task '{0}' Specified factory type '{1}' was not found in assembly '{2}'", ut.TaskName, ut.TaskFactory, ta.LoadedAssembly.FullName);
+                                       else
+                                               task.TaskFactoryType = type;
+                               }
+                               if (error != null)
+                                       engine.LogWarningEvent (new BuildWarningEventArgs (null, null, projectInstance.FullPath, ut.Location.Line, ut.Location.Column, 0, 0, error, null, null));
+                               else
+                                       task_descs.Add (task);
                        }
                }
                
@@ -127,8 +165,6 @@ namespace Microsoft.Build.Internal
                        Type type = a.GetType (possiblyShortTypeName, false, true);
                        if (possiblyShortTypeName.IndexOf ('.') < 0)
                                type = a.GetTypes ().FirstOrDefault (t => t.Name == possiblyShortTypeName);
-                       if (type == null)
-                               throw new InvalidOperationException (string.Format ("For task '{0}' Specified type '{1}' was not found in assembly '{2}'", taskName, possiblyShortTypeName, a.FullName));
                        return type;
                }
        }