GenerateTemporaryAssemblyTask.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / XamlBuildTask / Microsoft / Build / Tasks / Xaml / GenerateTemporaryAssemblyTask.cs / 1407647 / GenerateTemporaryAssemblyTask.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------

namespace Microsoft.Build.Tasks.Xaml 
{
    using System; 
    using System.CodeDom; 
    using System.CodeDom.Compiler;
    using System.Collections; 
    using System.Collections.Generic;
    using System.IO;
    using System.Xaml;
    using System.Xaml.Schema; 
    using System.Xml;
    using System.Xml.Linq; 
    using Microsoft.Build.Framework; 
    using Microsoft.Build.Utilities;
    using System.Reflection; 
    using System.Globalization;
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime;
 
    [Fx.Tag.XamlVisible(true)]
    public class GenerateTemporaryAssemblyTask : Task 
    { 
        const string MSBuildNamespace = "http://schemas.microsoft.com/developer/msbuild/2003";
 
        public GenerateTemporaryAssemblyTask()
        {
        }
 
        [Required]
        public string AssemblyName 
        { get; set; } 

        [Required] 
        public string OutputPath
        { get; set; }

        [Required] 
        public string CurrentProject
        { get; set; } 
 
        [Fx.Tag.KnownXamlExternal]
        [Required] 
        public ITaskItem[] SourceCodeFiles { get; set; }

        [Required]
        public string CompileTargetName 
        { get; set; }
 
        [Fx.Tag.KnownXamlExternal] 
        public ITaskItem[] ReferencePaths
        { get; set; } 

        [Required]
        public string ApplicationMarkupTypeName
        { get; set; } 

        public override bool Execute() 
        { 
            bool retVal;
            try 
            {
                XDocument projectDocument = XDocument.Load(this.CurrentProject);
                if (projectDocument != null)
                { 
                    XElement projectElement = projectDocument.Element(XName.Get("Project", MSBuildNamespace));
                    if (projectElement != null) 
                    { 
                        RemoveItemsByName(projectElement, ApplicationMarkupTypeName);
                        RemoveItemsByName(projectElement, "Reference"); 
                        AddNewItems(projectElement, "Compile", SourceCodeFiles);
                        AddNewItems(projectElement, "ReferencePath", ReferencePaths);

                        RemovePropertyByName(projectElement, "OutputType"); 
                        RemovePropertyByName(projectElement, "AssemblyName");
                        AddNewProperties(projectElement, 
                            new ProjectProperty[] { 
                                new ProjectProperty() { Name = "OutputType", Value = "Library" },
                                new ProjectProperty() {Name = "AssemblyName", Value = AssemblyName }, 
                                new ProjectProperty() { Name = "Utf8Output", Value = "true", Condition = "'$(Utf8Output)' == ''" }
                            });
                    }
                } 

                string filename = Path.ChangeExtension(Path.GetRandomFileName(), ".tmp_proj"); 
                projectDocument.Save(filename); 
                Hashtable globalProperties = new Hashtable();
                globalProperties["IntermediateOutputPath"] = this.OutputPath; 
                globalProperties["AssemblyName"] = this.AssemblyName;
                globalProperties["OutputType"] = "Library";
                retVal = base.BuildEngine.BuildProjectFile(filename, new string[] { this.CompileTargetName }, globalProperties, null);
                File.Delete(filename); 

                return retVal; 
            } 
            catch (Exception e)
            { 
                if (Fx.IsFatal(e))
                {
                    throw;
                } 

                // Log unknown errors that do not originate from the task. 
                // Assumes that all known errors are logged when the exception is thrown. 
                XamlBuildTaskServices.LogException(this, e.Message);
                retVal = false; 
            }
            return retVal;
        }
 
        void RemoveItemsByName(XElement project, string itemName)
        { 
            if (!string.IsNullOrEmpty(itemName)) 
            {
                IEnumerable itemGroups = project.Elements(XName.Get("ItemGroup", MSBuildNamespace)); 
                itemGroups.Elements(XName.Get(itemName, MSBuildNamespace)).Remove();
            }
        }
 
        void AddNewItems(XElement project, string itemName, ITaskItem[] items)
        { 
            if (!string.IsNullOrEmpty(itemName) && items != null) 
            {
                XElement newItemGroup = new XElement(XName.Get("ItemGroup", MSBuildNamespace)); 
                project.Add(newItemGroup);
                foreach (ITaskItem item in items)
                {
                    XElement newElement = new XElement(XName.Get(itemName, MSBuildNamespace)); 
                    XAttribute include = new XAttribute("Include", item.ItemSpec);
                    newElement.Add(include); 
                    newItemGroup.Add(newElement); 
                }
            } 
        }

        void RemovePropertyByName(XElement project, string propertyName)
        { 
            if (!string.IsNullOrEmpty(propertyName))
            { 
                IEnumerable itemGroups = project.Elements(XName.Get("PropertyGroup", MSBuildNamespace)); 
                itemGroups.Elements(XName.Get(propertyName, MSBuildNamespace)).Remove();
            } 
        }

        void AddNewProperties(XElement project, IEnumerable properties)
        { 
            if (properties != null)
            { 
                XElement newPropertyGroup = new XElement(XName.Get("PropertyGroup", MSBuildNamespace)); 
                project.Add(newPropertyGroup);
                foreach (ProjectProperty prop in properties) 
                {
                    if (!string.IsNullOrEmpty(prop.Name) && prop.Value != null)
                    {
                        XElement newElement = new XElement(XName.Get(prop.Name, MSBuildNamespace)); 
                        newElement.Value = prop.Value;
                        if (!string.IsNullOrEmpty(prop.Condition)) 
                        { 
                            newElement.SetAttributeValue(XName.Get("Condition", string.Empty), prop.Condition);
                        } 
                        newPropertyGroup.Add(newElement);
                    }
                }
            } 
        }
 
        class ProjectProperty 
        {
            public string Name 
            { get; set; }
            public string Value
            { get; set; }
            public string Condition 
            { get; set; }
        } 
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------

namespace Microsoft.Build.Tasks.Xaml 
{
    using System; 
    using System.CodeDom; 
    using System.CodeDom.Compiler;
    using System.Collections; 
    using System.Collections.Generic;
    using System.IO;
    using System.Xaml;
    using System.Xaml.Schema; 
    using System.Xml;
    using System.Xml.Linq; 
    using Microsoft.Build.Framework; 
    using Microsoft.Build.Utilities;
    using System.Reflection; 
    using System.Globalization;
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime;
 
    [Fx.Tag.XamlVisible(true)]
    public class GenerateTemporaryAssemblyTask : Task 
    { 
        const string MSBuildNamespace = "http://schemas.microsoft.com/developer/msbuild/2003";
 
        public GenerateTemporaryAssemblyTask()
        {
        }
 
        [Required]
        public string AssemblyName 
        { get; set; } 

        [Required] 
        public string OutputPath
        { get; set; }

        [Required] 
        public string CurrentProject
        { get; set; } 
 
        [Fx.Tag.KnownXamlExternal]
        [Required] 
        public ITaskItem[] SourceCodeFiles { get; set; }

        [Required]
        public string CompileTargetName 
        { get; set; }
 
        [Fx.Tag.KnownXamlExternal] 
        public ITaskItem[] ReferencePaths
        { get; set; } 

        [Required]
        public string ApplicationMarkupTypeName
        { get; set; } 

        public override bool Execute() 
        { 
            bool retVal;
            try 
            {
                XDocument projectDocument = XDocument.Load(this.CurrentProject);
                if (projectDocument != null)
                { 
                    XElement projectElement = projectDocument.Element(XName.Get("Project", MSBuildNamespace));
                    if (projectElement != null) 
                    { 
                        RemoveItemsByName(projectElement, ApplicationMarkupTypeName);
                        RemoveItemsByName(projectElement, "Reference"); 
                        AddNewItems(projectElement, "Compile", SourceCodeFiles);
                        AddNewItems(projectElement, "ReferencePath", ReferencePaths);

                        RemovePropertyByName(projectElement, "OutputType"); 
                        RemovePropertyByName(projectElement, "AssemblyName");
                        AddNewProperties(projectElement, 
                            new ProjectProperty[] { 
                                new ProjectProperty() { Name = "OutputType", Value = "Library" },
                                new ProjectProperty() {Name = "AssemblyName", Value = AssemblyName }, 
                                new ProjectProperty() { Name = "Utf8Output", Value = "true", Condition = "'$(Utf8Output)' == ''" }
                            });
                    }
                } 

                string filename = Path.ChangeExtension(Path.GetRandomFileName(), ".tmp_proj"); 
                projectDocument.Save(filename); 
                Hashtable globalProperties = new Hashtable();
                globalProperties["IntermediateOutputPath"] = this.OutputPath; 
                globalProperties["AssemblyName"] = this.AssemblyName;
                globalProperties["OutputType"] = "Library";
                retVal = base.BuildEngine.BuildProjectFile(filename, new string[] { this.CompileTargetName }, globalProperties, null);
                File.Delete(filename); 

                return retVal; 
            } 
            catch (Exception e)
            { 
                if (Fx.IsFatal(e))
                {
                    throw;
                } 

                // Log unknown errors that do not originate from the task. 
                // Assumes that all known errors are logged when the exception is thrown. 
                XamlBuildTaskServices.LogException(this, e.Message);
                retVal = false; 
            }
            return retVal;
        }
 
        void RemoveItemsByName(XElement project, string itemName)
        { 
            if (!string.IsNullOrEmpty(itemName)) 
            {
                IEnumerable itemGroups = project.Elements(XName.Get("ItemGroup", MSBuildNamespace)); 
                itemGroups.Elements(XName.Get(itemName, MSBuildNamespace)).Remove();
            }
        }
 
        void AddNewItems(XElement project, string itemName, ITaskItem[] items)
        { 
            if (!string.IsNullOrEmpty(itemName) && items != null) 
            {
                XElement newItemGroup = new XElement(XName.Get("ItemGroup", MSBuildNamespace)); 
                project.Add(newItemGroup);
                foreach (ITaskItem item in items)
                {
                    XElement newElement = new XElement(XName.Get(itemName, MSBuildNamespace)); 
                    XAttribute include = new XAttribute("Include", item.ItemSpec);
                    newElement.Add(include); 
                    newItemGroup.Add(newElement); 
                }
            } 
        }

        void RemovePropertyByName(XElement project, string propertyName)
        { 
            if (!string.IsNullOrEmpty(propertyName))
            { 
                IEnumerable itemGroups = project.Elements(XName.Get("PropertyGroup", MSBuildNamespace)); 
                itemGroups.Elements(XName.Get(propertyName, MSBuildNamespace)).Remove();
            } 
        }

        void AddNewProperties(XElement project, IEnumerable properties)
        { 
            if (properties != null)
            { 
                XElement newPropertyGroup = new XElement(XName.Get("PropertyGroup", MSBuildNamespace)); 
                project.Add(newPropertyGroup);
                foreach (ProjectProperty prop in properties) 
                {
                    if (!string.IsNullOrEmpty(prop.Name) && prop.Value != null)
                    {
                        XElement newElement = new XElement(XName.Get(prop.Name, MSBuildNamespace)); 
                        newElement.Value = prop.Value;
                        if (!string.IsNullOrEmpty(prop.Condition)) 
                        { 
                            newElement.SetAttributeValue(XName.Get("Condition", string.Empty), prop.Condition);
                        } 
                        newPropertyGroup.Add(newElement);
                    }
                }
            } 
        }
 
        class ProjectProperty 
        {
            public string Name 
            { get; set; }
            public string Value
            { get; set; }
            public string Condition 
            { get; set; }
        } 
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK