public DummyCircularReferenceTask(string references, string buildFileXml) { m_References = references; XmlDocument doc = new XmlDocument(); doc.LoadXml(buildFileXml); Project = new Project(doc, Level.Info, 1); Project.Execute(); // this loads targets }
/// <summary> /// Processes the framework nodes of the given platform node. /// </summary> /// <param name="platformNode">An <see cref="XmlNode" /> representing the platform on which NAnt is running.</param> private void ProcessFrameworks(XmlNode platformNode) { // determine the framework family name string frameworkFamily = PlatformHelper.IsMono ? "mono" : "net"; // determine the version of the current runtime framework string frameworkClrVersion = Environment.Version.ToString(3); // determine default targetframework string defaultTargetFramework = GetXmlAttributeValue(platformNode, "default"); // deals with xml info from the config file, not build document. foreach (XmlNode frameworkNode in platformNode.SelectNodes("nant:framework", NamespaceManager)) { // skip special elements like comments, pis, text, etc. if (!(frameworkNode.NodeType == XmlNodeType.Element)) { continue; } string name = null; bool isRuntimeFramework = false; try { // get framework attributes name = GetXmlAttributeValue(frameworkNode, "name"); string family = GetXmlAttributeValue(frameworkNode, "family"); string clrVersion = GetXmlAttributeValue(frameworkNode, "clrversion"); // check if we're processing the current runtime framework if (family == frameworkFamily && clrVersion == frameworkClrVersion) { isRuntimeFramework = true; } // get framework-specific project node XmlNode projectNode = frameworkNode.SelectSingleNode("nant:project", NamespaceManager); if (projectNode == null) { throw new BuildException("<project> node has not been defined."); } string tempBuildFile = Path.GetTempFileName(); XmlTextWriter writer = null; Project frameworkProject = null; try { // write project to file writer = new XmlTextWriter(tempBuildFile, Encoding.UTF8); writer.WriteStartDocument(true); writer.WriteRaw(projectNode.OuterXml); writer.Flush(); writer.Close(); // use StreamReader to load build file from to avoid // having location information as part of the error // messages using (StreamReader sr = new StreamReader(new FileStream(tempBuildFile, FileMode.Open, FileAccess.Read, FileShare.Write), Encoding.UTF8)) { XmlDocument projectDoc = new XmlDocument(); projectDoc.Load(sr); // create and execute project frameworkProject = new Project(projectDoc, Level.None, 0, (XmlNode) null); frameworkProject.BaseDirectory = AppDomain.CurrentDomain.BaseDirectory; frameworkProject.Execute(); } } finally { if (writer != null) { writer.Close(); } if (File.Exists(tempBuildFile)) { File.Delete(tempBuildFile); } } string description = frameworkProject.ExpandProperties( GetXmlAttributeValue(frameworkNode, "description"), Location.UnknownLocation); string version = frameworkProject.ExpandProperties( GetXmlAttributeValue(frameworkNode, "version"), Location.UnknownLocation); string runtimeEngine = frameworkProject.ExpandProperties( GetXmlAttributeValue(frameworkNode, "runtimeengine"), Location.UnknownLocation); string frameworkDir = frameworkProject.ExpandProperties( GetXmlAttributeValue(frameworkNode, "frameworkdirectory"), Location.UnknownLocation); string frameworkAssemblyDir = frameworkProject.ExpandProperties( GetXmlAttributeValue(frameworkNode, "frameworkassemblydirectory"), Location.UnknownLocation); string sdkDir = GetXmlAttributeValue(frameworkNode, "sdkdirectory"); try { sdkDir = frameworkProject.ExpandProperties(sdkDir, Location.UnknownLocation); } catch (BuildException) { // do nothing with this exception as a framework is still // considered valid if the sdk directory is not available // or not configured correctly } // create new FrameworkInfo instance, this will throw an // an exception if the framework is not valid FrameworkInfo info = new FrameworkInfo(name, family, description, new Version(version), new Version(clrVersion), frameworkDir, sdkDir, frameworkAssemblyDir, runtimeEngine, frameworkProject); // get framework-specific environment nodes XmlNodeList environmentNodes = frameworkNode.SelectNodes("nant:environment/nant:env", NamespaceManager); // process framework environment nodes info.EnvironmentVariables = ProcessFrameworkEnvironmentVariables( environmentNodes, info); // process framework task assemblies info.TaskAssemblies.Project = frameworkProject; info.TaskAssemblies.NamespaceManager = NamespaceManager; info.TaskAssemblies.Parent = frameworkProject; // avoid warnings by setting the parent of the fileset info.TaskAssemblies.ID = "internal-task-assemblies"; // avoid warnings by assigning an id XmlNode taskAssembliesNode = frameworkNode.SelectSingleNode( "nant:task-assemblies", NamespaceManager); if (taskAssembliesNode != null) { info.TaskAssemblies.Initialize(taskAssembliesNode, frameworkProject.Properties, info); } // framework is valid, so add it to framework dictionary Project.Frameworks.Add(info.Name, info); if (isRuntimeFramework) { // framework matches current runtime, so set it as // current target framework Project.RuntimeFramework = Project.TargetFramework = info; } } catch (Exception ex) { if (isRuntimeFramework) { // current runtime framework is not correctly configured // in NAnt configuration file throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1063"), name), ex); } else { if (name != null && name == defaultTargetFramework) { Project.Log(Level.Warning, ResourceUtils.GetString("NA1181"), name, ex.Message); Project.Log(Level.Debug, ex.ToString()); Project.Log(Level.Warning, ""); } else { Project.Log(Level.Verbose, ResourceUtils.GetString("NA1182"), name, ex.Message); Project.Log(Level.Debug, ex.ToString()); Project.Log(Level.Verbose, ""); } } } } if (Project.RuntimeFramework == null) { // information about the current runtime framework should // be added to the NAnt configuration file throw new BuildException(string.Format(CultureInfo.InvariantCulture, ResourceUtils.GetString("NA1062"), frameworkFamily, frameworkClrVersion)); } if (defaultTargetFramework != null && defaultTargetFramework != "auto") { if (Project.Frameworks.ContainsKey(defaultTargetFramework)) { Project.TargetFramework = Project.Frameworks[defaultTargetFramework]; } else { Project.Log(Level.Warning, ResourceUtils.GetString("NA1178"), defaultTargetFramework, Project.RuntimeFramework.Name); Project.Log(Level.Warning, ""); } } }
/// <summary> /// Executes the project and returns the console output as a string. /// </summary> /// <param name="p">The project to execute.</param> /// <returns> /// The console output. /// </returns> /// <remarks> /// Any exception that is thrown as part of the execution of the /// <see cref="Project" /> is wrapped in a <see cref="TestBuildException" />. /// </remarks> public static string ExecuteProject(Project p) { using (ConsoleCapture c = new ConsoleCapture()) { string output = null; try { p.Execute(); } catch (Exception e) { output = c.Close(); throw new TestBuildException("Error Executing Project", output, e); } finally { if(output == null) { output = c.Close(); } } return output; } }
private void PerformInit() { // get framework-specific project node XmlNode projectNode = _frameworkNode.SelectSingleNode("nant:project", NamespaceManager); if (projectNode == null) throw new ArgumentException("No <project> node is defined."); // create XmlDocument from project node XmlDocument projectDoc = new XmlDocument(); projectDoc.LoadXml(projectNode.OuterXml); // create and execute project Project frameworkProject = new Project(projectDoc); frameworkProject.BaseDirectory = AppDomain.CurrentDomain.BaseDirectory; frameworkProject.Execute(); XmlNode runtimeNode = _frameworkNode.SelectSingleNode ("runtime", NamespaceManager); if (runtimeNode != null) { _runtime = new Runtime (); _runtime.Parent = _runtime.Project = frameworkProject; _runtime.NamespaceManager = NamespaceManager; _runtime.Initialize(runtimeNode, frameworkProject.Properties, this); } string sdkDir = GetXmlAttributeValue(_frameworkNode, "sdkdirectory"); try { sdkDir = frameworkProject.ExpandProperties(sdkDir, Location.UnknownLocation); } catch (BuildException) { // do nothing with this exception as a framework is still // considered valid if the sdk directory is not available // or not configured correctly } // the sdk directory does not actually have to exist for a // framework to be considered valid if (sdkDir != null && Directory.Exists(sdkDir)) _sdkDirectory = new DirectoryInfo(sdkDir); _project = frameworkProject; _status = InitStatus.Initialized; }