diff --git a/mpt-core/03_msbuild/ICan.cs b/mpt-core/03_msbuild/ICan.cs index 5c77b34..cbdb45c 100644 --- a/mpt-core/03_msbuild/ICan.cs +++ b/mpt-core/03_msbuild/ICan.cs @@ -1,21 +1,23 @@ -using System.Collections.Generic; -using System.Xml; - -public interface IHaveUnderlyingNode +namespace BuildAutomation { - XmlNode UnderlyingNode { get; } -} + using System.Collections.Generic; + using System.Xml; -public interface ICanHaveProperties : IHaveUnderlyingNode -{ -} + public interface IHaveUnderlyingNode + { + XmlNode UnderlyingNode { get; } + } -public interface ICanHaveItems : IHaveUnderlyingNode -{ -} + public interface ICanHaveProperties : IHaveUnderlyingNode + { + } -public interface ICanBeConditional -{ - string Condition { get; set; } -} + public interface ICanHaveItems : IHaveUnderlyingNode + { + } + public interface ICanBeConditional + { + string Condition { get; set; } + } +} diff --git a/mpt-core/03_msbuild/MSBuildFile.cs b/mpt-core/03_msbuild/MSBuildFile.cs index 5ea244a..dfce9b1 100644 --- a/mpt-core/03_msbuild/MSBuildFile.cs +++ b/mpt-core/03_msbuild/MSBuildFile.cs @@ -1,290 +1,293 @@ -using System; -using System.Collections.Generic; -using System.Xml.XPath; -using System.Xml; - -public class MSBuildFile : IDisposable +namespace BuildAutomation { - public const string NamespaceName = "http://schemas.microsoft.com/developer/msbuild/2003"; - XmlDocument doc; - bool bSaveRequired = false; - string filename = null; - List importNodes = new List(); - List targetNodes = new List(); - public XmlDocument UnderlyingObject + using System; + using System.Collections.Generic; + using System.Xml.XPath; + using System.Xml; + + public class MSBuildFile : IDisposable { - get + public const string NamespaceName = "http://schemas.microsoft.com/developer/msbuild/2003"; + XmlDocument doc; + bool bSaveRequired = false; + string filename = null; + List importNodes = new List(); + List targetNodes = new List(); + public XmlDocument UnderlyingObject { - return doc; + get + { + return doc; + } } - } - public string FileName - { - get + public string FileName { - return filename; + get + { + return filename; + } + set + { + filename = value; + } } - set + + public MSBuildFile(XmlDocument d) { - filename = value; + this.doc = d; } - } - - public MSBuildFile(XmlDocument d) - { - this.doc = d; - } - public MSBuildFile(string filename) - { - XmlDocument d = new XmlDocument(); - d.Load(filename); - this.doc = d; - this.filename = filename; - FindAllImports(); - FindAllTargets(); - } - - public void Dispose() - { - if (bSaveRequired && String.IsNullOrWhiteSpace(filename) == false) + public MSBuildFile(string filename) { - doc.Save(filename); + XmlDocument d = new XmlDocument(); + d.Load(filename); + this.doc = d; + this.filename = filename; + FindAllImports(); + FindAllTargets(); } - } - public void AddTarget(MSBuildTarget target) - { - // it does not automatically add the new object to the document tree. - // To add the new object, one must explicitly call one of the node insert methods. - XmlNode n = target.UnderlyingObject; - XPathNavigator locator = doc.CreateNavigator(); - locator.MoveToRoot(); - XmlNode root = locator.UnderlyingObject as XmlNode; - if (locator.MoveToFirstChild() == false) + public void Dispose() { - root.AppendChild(n); + if (bSaveRequired && String.IsNullOrWhiteSpace(filename) == false) + { + doc.Save(filename); + } } - else + + public void AddTarget(MSBuildTarget target) { - while (locator.MoveToNext()) { }; - XmlNode sibling = locator.UnderlyingObject as XmlNode; - root.InsertAfter(n, sibling); + // it does not automatically add the new object to the document tree. + // To add the new object, one must explicitly call one of the node insert methods. + XmlNode n = target.UnderlyingObject; + XPathNavigator locator = doc.CreateNavigator(); + locator.MoveToRoot(); + XmlNode root = locator.UnderlyingObject as XmlNode; + if (locator.MoveToFirstChild() == false) + { + root.AppendChild(n); + } + else + { + while (locator.MoveToNext()) { }; + XmlNode sibling = locator.UnderlyingObject as XmlNode; + root.InsertAfter(n, sibling); + } + bSaveRequired = true; } - bSaveRequired = true; - } - static bool IsAlreadyExists(string import_name, XmlNamespaceManager xmlNamespaceManager, XPathNavigator navigator) - { - var xpath1 = "/ns:Project/ns:Import[@Project='" + import_name + "']"; - XPathExpression expr1 = navigator.Compile(xpath1); - expr1.SetContext(xmlNamespaceManager); - var nodeIterator1 = navigator.Select(expr1); - if (nodeIterator1.Count > 0) + static bool IsAlreadyExists(string import_name, XmlNamespaceManager xmlNamespaceManager, XPathNavigator navigator) { - return true; + var xpath1 = "/ns:Project/ns:Import[@Project='" + import_name + "']"; + XPathExpression expr1 = navigator.Compile(xpath1); + expr1.SetContext(xmlNamespaceManager); + var nodeIterator1 = navigator.Select(expr1); + if (nodeIterator1.Count > 0) + { + return true; + } + return false; } - return false; - } - public MSBuildImport CreateImport() - { - var result = new MSBuildImport(this); - return result; - } + public MSBuildImport CreateImport() + { + var result = new MSBuildImport(this); + return result; + } - public void FindAllImports() - { - var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); - xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); + public void FindAllImports() + { + var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); + xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); - XPathNavigator navigator = doc.CreateNavigator(); - navigator.MoveToRoot(); + XPathNavigator navigator = doc.CreateNavigator(); + navigator.MoveToRoot(); - var xpath = "/ns:Project/ns:Import[@Project]"; - XPathExpression expr = navigator.Compile(xpath); - expr.SetContext(xmlNamespaceManager); + var xpath = "/ns:Project/ns:Import[@Project]"; + XPathExpression expr = navigator.Compile(xpath); + expr.SetContext(xmlNamespaceManager); - var nodeIterator = navigator.Select(expr); - if (nodeIterator.Count == 0) - { - return; - } - while (nodeIterator.MoveNext()) - { - if (nodeIterator.Current is IHasXmlNode) + var nodeIterator = navigator.Select(expr); + if (nodeIterator.Count == 0) { - XmlNode node = ((IHasXmlNode)nodeIterator.Current).GetNode(); - XmlElement element = (XmlElement)node; - MSBuildImport wrapperObject = new MSBuildImport(this, element); - importNodes.Add(wrapperObject); + return; } + while (nodeIterator.MoveNext()) + { + if (nodeIterator.Current is IHasXmlNode) + { + XmlNode node = ((IHasXmlNode)nodeIterator.Current).GetNode(); + XmlElement element = (XmlElement)node; + MSBuildImport wrapperObject = new MSBuildImport(this, element); + importNodes.Add(wrapperObject); + } + } + // see also https://weblogs.asp.net/cazzu/86609 } - // see also https://weblogs.asp.net/cazzu/86609 - } - // locate if there is import of Microsoft.CSharp.targets - public MSBuildImport FindImport(string v) - { - foreach (MSBuildImport item in this.importNodes) + // locate if there is import of Microsoft.CSharp.targets + public MSBuildImport FindImport(string v) { - if (string.Compare(item.Project, v) == 0) + foreach (MSBuildImport item in this.importNodes) { - return item; + if (string.Compare(item.Project, v) == 0) + { + return item; + } } + return null; } - return null; - } - public void InsertImport(MSBuildImport newImport) - { - if (FindImport(newImport.Project) != null) + public void InsertImport(MSBuildImport newImport) { - return; - } + if (FindImport(newImport.Project) != null) + { + return; + } - // запомнить у себя - this.importNodes.Add(newImport); + // запомнить у себя + this.importNodes.Add(newImport); - // у тебя в руках узел, но оне вставленный в XML-документ - XmlElement newXmlElement = newImport.UnderlyingObject; + // у тебя в руках узел, но оне вставленный в XML-документ + XmlElement newXmlElement = newImport.UnderlyingObject; - XPathNavigator navigator = doc.CreateNavigator(); - navigator.MoveToRoot(); + XPathNavigator navigator = doc.CreateNavigator(); + navigator.MoveToRoot(); - XmlNode root = (XmlNode)navigator.UnderlyingObject; - XmlNode lc = root.LastChild; - lc.AppendChild(newXmlElement); + XmlNode root = (XmlNode)navigator.UnderlyingObject; + XmlNode lc = root.LastChild; + lc.AppendChild(newXmlElement); - bSaveRequired = true; - } + bSaveRequired = true; + } - public void InsertImportAfter(MSBuildImport existingImport, MSBuildImport newImport) - { - // запомнить у себя - this.importNodes.Add(newImport); - // вставить в нижележащий слой - XmlElement existingElement = existingImport.UnderlyingObject; - XmlElement newElement = newImport.UnderlyingObject; - existingElement.ParentNode.InsertAfter(newElement, existingElement); - - bSaveRequired = true; - } + public void InsertImportAfter(MSBuildImport existingImport, MSBuildImport newImport) + { + // запомнить у себя + this.importNodes.Add(newImport); + // вставить в нижележащий слой + XmlElement existingElement = existingImport.UnderlyingObject; + XmlElement newElement = newImport.UnderlyingObject; + existingElement.ParentNode.InsertAfter(newElement, existingElement); + + bSaveRequired = true; + } - public MSBuildTarget CreateTarget() - { - var result = new MSBuildTarget(this); - return result; - } + public MSBuildTarget CreateTarget() + { + var result = new MSBuildTarget(this); + return result; + } - public void FindAllTargets() - { - var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); - xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); + public void FindAllTargets() + { + var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); + xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); - XPathNavigator navigator = doc.CreateNavigator(); - navigator.MoveToRoot(); + XPathNavigator navigator = doc.CreateNavigator(); + navigator.MoveToRoot(); - var xpath = "/ns:Project/ns:Target[@Name]"; - XPathExpression expr = navigator.Compile(xpath); - expr.SetContext(xmlNamespaceManager); + var xpath = "/ns:Project/ns:Target[@Name]"; + XPathExpression expr = navigator.Compile(xpath); + expr.SetContext(xmlNamespaceManager); - var nodeIterator = navigator.Select(expr); - if (nodeIterator.Count == 0) - { - return; - } - while (nodeIterator.MoveNext()) - { - if (nodeIterator.Current is IHasXmlNode) + var nodeIterator = navigator.Select(expr); + if (nodeIterator.Count == 0) + { + return; + } + while (nodeIterator.MoveNext()) { - XmlNode node = ((IHasXmlNode)nodeIterator.Current).GetNode(); - XmlElement element = (XmlElement)node; - MSBuildTarget wrapperObject = new MSBuildTarget(this, element); - targetNodes.Add(wrapperObject); + if (nodeIterator.Current is IHasXmlNode) + { + XmlNode node = ((IHasXmlNode)nodeIterator.Current).GetNode(); + XmlElement element = (XmlElement)node; + MSBuildTarget wrapperObject = new MSBuildTarget(this, element); + targetNodes.Add(wrapperObject); + } } + // see also https://weblogs.asp.net/cazzu/86609 } - // see also https://weblogs.asp.net/cazzu/86609 - } - public MSBuildTarget FindTarget(string v) - { - foreach (MSBuildTarget item in this.targetNodes) + public MSBuildTarget FindTarget(string v) { - if (string.Compare(item.Name, v) == 0) + foreach (MSBuildTarget item in this.targetNodes) { - return item; + if (string.Compare(item.Name, v) == 0) + { + return item; + } } + return null; } - return null; - } - public void InsertTarget(MSBuildTarget newTarget) - { - if (FindTarget(newTarget.Name) != null) + public void InsertTarget(MSBuildTarget newTarget) { - return; - } - - // запомнить у себя - this.targetNodes.Add(newTarget); + if (FindTarget(newTarget.Name) != null) + { + return; + } - // у тебя в руках узел, но оне вставленный в XML-документ - XmlElement newXmlElement = newTarget.UnderlyingObject; + // запомнить у себя + this.targetNodes.Add(newTarget); - XPathNavigator navigator = doc.CreateNavigator(); - navigator.MoveToRoot(); + // у тебя в руках узел, но оне вставленный в XML-документ + XmlElement newXmlElement = newTarget.UnderlyingObject; - XmlNode root = (XmlNode)navigator.UnderlyingObject; - XmlNode project = root.LastChild; - project.AppendChild(newXmlElement); + XPathNavigator navigator = doc.CreateNavigator(); + navigator.MoveToRoot(); - bSaveRequired = true; - } + XmlNode root = (XmlNode)navigator.UnderlyingObject; + XmlNode project = root.LastChild; + project.AppendChild(newXmlElement); - public void EnsureTargetExists(string targetName) - { - if (FindTarget(targetName) != null) - { - return; + bSaveRequired = true; } - MSBuildTarget targ = this.CreateTarget(); - targ.Name = targetName; - InsertTarget(targ); - - bSaveRequired = true; - } - public void AddAfterTarget(string precedingTarget, string followingTarget) - { - MSBuildTarget preceding = FindTarget(precedingTarget); - if (preceding == null) - { - throw new ApplicationException($"Target {precedingTarget} not found"); - } - MSBuildTarget following = FindTarget(followingTarget); - if (following == null) + public void EnsureTargetExists(string targetName) { - throw new ApplicationException($"Target {followingTarget} not found"); - } - preceding.AddAfterTarget(following.Name); + if (FindTarget(targetName) != null) + { + return; + } + MSBuildTarget targ = this.CreateTarget(); + targ.Name = targetName; + InsertTarget(targ); - bSaveRequired = true; - } + bSaveRequired = true; + } - public void AddDependOnTarget(string precedingTarget, string followingTarget) - { - MSBuildTarget preceding = FindTarget(precedingTarget); - if (preceding == null) + public void AddAfterTarget(string precedingTarget, string followingTarget) { - throw new ApplicationException($"Target {precedingTarget} not found"); + MSBuildTarget preceding = FindTarget(precedingTarget); + if (preceding == null) + { + throw new ApplicationException($"Target {precedingTarget} not found"); + } + MSBuildTarget following = FindTarget(followingTarget); + if (following == null) + { + throw new ApplicationException($"Target {followingTarget} not found"); + } + preceding.AddAfterTarget(following.Name); + + bSaveRequired = true; } - MSBuildTarget following = FindTarget(followingTarget); - if (following == null) + + public void AddDependOnTarget(string precedingTarget, string followingTarget) { - throw new ApplicationException($"Target {followingTarget} not found"); - } - preceding.AddDependOnTarget(following.Name); + MSBuildTarget preceding = FindTarget(precedingTarget); + if (preceding == null) + { + throw new ApplicationException($"Target {precedingTarget} not found"); + } + MSBuildTarget following = FindTarget(followingTarget); + if (following == null) + { + throw new ApplicationException($"Target {followingTarget} not found"); + } + preceding.AddDependOnTarget(following.Name); - bSaveRequired = true; + bSaveRequired = true; + } } } diff --git a/mpt-core/03_msbuild/MSBuildImport.cs b/mpt-core/03_msbuild/MSBuildImport.cs index 222d633..6977111 100644 --- a/mpt-core/03_msbuild/MSBuildImport.cs +++ b/mpt-core/03_msbuild/MSBuildImport.cs @@ -1,44 +1,47 @@ -using System; -using System.Collections.Generic; -using System.Xml; - -public class MSBuildImport +namespace BuildAutomation { - MSBuildFile file; - XmlElement uo; + using System; + using System.Collections.Generic; + using System.Xml; - public XmlElement UnderlyingObject + public class MSBuildImport { - get + MSBuildFile file; + XmlElement uo; + + public XmlElement UnderlyingObject { - return uo; + get + { + return uo; + } } - } - - public string Project { get { return uo.Attributes["Project"].Value; } set { uo.SetAttribute("Project", value); } } - - public MSBuildImport(MSBuildFile f, XmlElement el) - { - this.file = f; - uo = el; - } - public MSBuildImport(MSBuildFile f) - { - this.file = f; - // string element = ""; - uo = (XmlElement)file.UnderlyingObject.CreateNode(XmlNodeType.Element, "Import", MSBuildFile.NamespaceName); - } + public string Project { get { return uo.Attributes["Project"].Value; } set { uo.SetAttribute("Project", value); } } - /*void SetProject(string value) - { - if (uo.HasAttribute("Project))" + + public MSBuildImport(MSBuildFile f, XmlElement el) { - uo.Attributes["Project"].Value = value; + this.file = f; + uo = el; } - else + + public MSBuildImport(MSBuildFile f) { - uo.SetAttribute("Project", value); + this.file = f; + // string element = ""; + uo = (XmlElement)file.UnderlyingObject.CreateNode(XmlNodeType.Element, "Import", MSBuildFile.NamespaceName); } - }*/ + + /*void SetProject(string value) + { + if (uo.HasAttribute("Project))" + + { + uo.Attributes["Project"].Value = value; + } + else + { + uo.SetAttribute("Project", value); + } + }*/ + } } diff --git a/mpt-core/03_msbuild/MSBuildItem.cs b/mpt-core/03_msbuild/MSBuildItem.cs index 5f23feb..69dca37 100644 --- a/mpt-core/03_msbuild/MSBuildItem.cs +++ b/mpt-core/03_msbuild/MSBuildItem.cs @@ -1,16 +1,18 @@ -using System; -using System.Xml; - -public class MSBuildItem +namespace BuildAutomation { - XmlElement uo; - - public XmlElement UnderlyingObject { get { return uo; } } + using System; + using System.Xml; - public MSBuildItem(MSBuildItemGroup parent) + public class MSBuildItem { - XmlDocument doc = parent.UnderlyingObject.OwnerDocument; - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "UndefilnedItemName", MSBuildFile.NamespaceName); + XmlElement uo; + + public XmlElement UnderlyingObject { get { return uo; } } + + public MSBuildItem(MSBuildItemGroup parent) + { + XmlDocument doc = parent.UnderlyingObject.OwnerDocument; + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "UndefilnedItemName", MSBuildFile.NamespaceName); + } } } - diff --git a/mpt-core/03_msbuild/MSBuildItemGroup.cs b/mpt-core/03_msbuild/MSBuildItemGroup.cs index 1fef884..3fad03a 100644 --- a/mpt-core/03_msbuild/MSBuildItemGroup.cs +++ b/mpt-core/03_msbuild/MSBuildItemGroup.cs @@ -1,32 +1,34 @@ -using System; -using System.Collections.Generic; -using System.Xml; - -public class MSBuildItemGroup +namespace BuildAutomation { - XmlElement uo; + using System; + using System.Collections.Generic; + using System.Xml; - public XmlElement UnderlyingObject { get { return uo; } } + public class MSBuildItemGroup + { + XmlElement uo; - IEnumerable Items { get; } + public XmlElement UnderlyingObject { get { return uo; } } - //ICanHaveItems parent; + IEnumerable Items { get; } - public MSBuildItemGroup(ICanHaveItems parent) - { - //this.parent = parent; - XmlDocument doc = parent.UnderlyingNode.OwnerDocument; - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "UndefilnedItemName", MSBuildFile.NamespaceName); - } + //ICanHaveItems parent; - public MSBuildItem CreateItem() - { - MSBuildItem res = new MSBuildItem(this); - return res; - } - public void AppendItem(MSBuildItem item) - { - throw new NotImplementedException(); + public MSBuildItemGroup(ICanHaveItems parent) + { + //this.parent = parent; + XmlDocument doc = parent.UnderlyingNode.OwnerDocument; + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "UndefilnedItemName", MSBuildFile.NamespaceName); + } + + public MSBuildItem CreateItem() + { + MSBuildItem res = new MSBuildItem(this); + return res; + } + public void AppendItem(MSBuildItem item) + { + throw new NotImplementedException(); + } } } - diff --git a/mpt-core/03_msbuild/MSBuildProperty.cs b/mpt-core/03_msbuild/MSBuildProperty.cs index 50b7aa5..65a0655 100644 --- a/mpt-core/03_msbuild/MSBuildProperty.cs +++ b/mpt-core/03_msbuild/MSBuildProperty.cs @@ -1,43 +1,46 @@ -using System; -using System.Xml; - -public class MSBuildProperty +namespace BuildAutomation { - XmlElement uo; + using System; + using System.Xml; - public XmlElement UnderlyingObject { get { return uo; } } + public class MSBuildProperty + { + XmlElement uo; - public string Name { get { return uo.LocalName; } set { SetName(value); } } - public string Value { get { return uo.InnerText; } set { uo.InnerText = value; } } + public XmlElement UnderlyingObject { get { return uo; } } - public MSBuildProperty(MSBuildPropertyGroup parent) - { - XmlDocument doc = parent.UnderlyingObject.OwnerDocument; - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "UndefilnedPropertyName", MSBuildFile.NamespaceName); - } + public string Name { get { return uo.LocalName; } set { SetName(value); } } + public string Value { get { return uo.InnerText; } set { uo.InnerText = value; } } - void SetName(string name) - { - // replace underlaying object to change it's name - XmlElement oldItem = uo; - XmlDocument doc = oldItem.OwnerDocument; - // replace name - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, MSBuildFile.NamespaceName); - // what about node's text content ? - uo.InnerText = oldItem.InnerText; - // copy attributes - foreach (XmlAttribute a in oldItem.Attributes) - { - uo.Attributes.Append((XmlAttribute)a.CloneNode(true)); - } - // copy childs - for (XmlNode child = oldItem.FirstChild; child != null; child = child.NextSibling) + public MSBuildProperty(MSBuildPropertyGroup parent) { - uo.AppendChild(child.CloneNode(true)); + XmlDocument doc = parent.UnderlyingObject.OwnerDocument; + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "UndefilnedPropertyName", MSBuildFile.NamespaceName); } - if (oldItem.ParentNode != null) + + void SetName(string name) { - oldItem.ParentNode.ReplaceChild(uo, oldItem); + // replace underlaying object to change it's name + XmlElement oldItem = uo; + XmlDocument doc = oldItem.OwnerDocument; + // replace name + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, MSBuildFile.NamespaceName); + // what about node's text content ? + uo.InnerText = oldItem.InnerText; + // copy attributes + foreach (XmlAttribute a in oldItem.Attributes) + { + uo.Attributes.Append((XmlAttribute)a.CloneNode(true)); + } + // copy childs + for (XmlNode child = oldItem.FirstChild; child != null; child = child.NextSibling) + { + uo.AppendChild(child.CloneNode(true)); + } + if (oldItem.ParentNode != null) + { + oldItem.ParentNode.ReplaceChild(uo, oldItem); + } } } } diff --git a/mpt-core/03_msbuild/MSBuildPropertyGroup.cs b/mpt-core/03_msbuild/MSBuildPropertyGroup.cs index adf41d8..d71e4d7 100644 --- a/mpt-core/03_msbuild/MSBuildPropertyGroup.cs +++ b/mpt-core/03_msbuild/MSBuildPropertyGroup.cs @@ -1,55 +1,58 @@ -using System; -using System.Collections.Generic; -using System.Xml; - -public class MSBuildPropertyGroup : ICanBeConditional +namespace BuildAutomation { - XmlElement uo; - List properties = new List(); - - public XmlElement UnderlyingObject { get { return uo; } } - public IEnumerable Properties { get { return properties; } } - - public string Condition { get { return GetCondition(); } set { SetCondition(value); } } - - public MSBuildPropertyGroup(ICanHaveProperties parent) - { - //this.parent = parent; - XmlDocument doc = parent.UnderlyingNode.OwnerDocument; - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "PropertyGroup", MSBuildFile.NamespaceName); - } - - string GetCondition() - { - if (uo.HasAttribute("Condition") == false) return null; - return uo.Attributes["Condition"].Value; - } - - void SetCondition(string value) - { - uo.SetAttribute("Condition", value); - } - - public MSBuildProperty CreateProperty() - { - MSBuildProperty res = new MSBuildProperty(this); - return res; - } - - public void AppendProperty(MSBuildProperty item) - { - // insert on this level - this.properties.Add(item); - // insert on underlaying level - XmlNode tn = item.UnderlyingObject; - uo.AppendChild(tn); - } + using System; + using System.Collections.Generic; + using System.Xml; - public void AddProperty(string name, string val) + public class MSBuildPropertyGroup : ICanBeConditional { - MSBuildProperty prop = this.CreateProperty(); - prop.Name = name; - prop.Value = val; - this.AppendProperty(prop); + XmlElement uo; + List properties = new List(); + + public XmlElement UnderlyingObject { get { return uo; } } + public IEnumerable Properties { get { return properties; } } + + public string Condition { get { return GetCondition(); } set { SetCondition(value); } } + + public MSBuildPropertyGroup(ICanHaveProperties parent) + { + //this.parent = parent; + XmlDocument doc = parent.UnderlyingNode.OwnerDocument; + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "PropertyGroup", MSBuildFile.NamespaceName); + } + + string GetCondition() + { + if (uo.HasAttribute("Condition") == false) return null; + return uo.Attributes["Condition"].Value; + } + + void SetCondition(string value) + { + uo.SetAttribute("Condition", value); + } + + public MSBuildProperty CreateProperty() + { + MSBuildProperty res = new MSBuildProperty(this); + return res; + } + + public void AppendProperty(MSBuildProperty item) + { + // insert on this level + this.properties.Add(item); + // insert on underlaying level + XmlNode tn = item.UnderlyingObject; + uo.AppendChild(tn); + } + + public void AddProperty(string name, string val) + { + MSBuildProperty prop = this.CreateProperty(); + prop.Name = name; + prop.Value = val; + this.AppendProperty(prop); + } } -} +} \ No newline at end of file diff --git a/mpt-core/03_msbuild/MSBuildTarget.cs b/mpt-core/03_msbuild/MSBuildTarget.cs index 3cdeb77..ba90b80 100644 --- a/mpt-core/03_msbuild/MSBuildTarget.cs +++ b/mpt-core/03_msbuild/MSBuildTarget.cs @@ -1,111 +1,114 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Xml; - -public class MSBuildTarget : ICanHaveProperties, ICanHaveItems +namespace BuildAutomation { - MSBuildFile file; - XmlElement uo; - List propertyGroups = new List(); - List tasks = new List(); - List items = new List(); - - public string Name { get { return uo.Attributes["Name"].Value; } set { uo.SetAttribute("Name", value); } } - public IEnumerable Tasks { get { return tasks; } } - public XmlElement UnderlyingObject { get { return uo; } } - public XmlNode UnderlyingNode { get { return UnderlyingObject; } } - public IEnumerable PropertyGroups {get { return propertyGroups; } } - public IEnumerable Items { get { return items; } } + using System; + using System.Collections.Generic; + using System.Text; + using System.Xml; - public MSBuildTarget(MSBuildFile f) + public class MSBuildTarget : ICanHaveProperties, ICanHaveItems { - this.file = f; - uo = (XmlElement)file.UnderlyingObject.CreateNode(XmlNodeType.Element, "Target", MSBuildFile.NamespaceName); - } + MSBuildFile file; + XmlElement uo; + List propertyGroups = new List(); + List tasks = new List(); + List items = new List(); - public MSBuildTarget(MSBuildFile f, XmlElement el) - { - this.file = f; - uo = el; - } + public string Name { get { return uo.Attributes["Name"].Value; } set { uo.SetAttribute("Name", value); } } + public IEnumerable Tasks { get { return tasks; } } + public XmlElement UnderlyingObject { get { return uo; } } + public XmlNode UnderlyingNode { get { return UnderlyingObject; } } + public IEnumerable PropertyGroups { get { return propertyGroups; } } + public IEnumerable Items { get { return items; } } - public MSBuildTask CreateTask() - { - MSBuildTask res = new MSBuildTask(this); - return res; - } + public MSBuildTarget(MSBuildFile f) + { + this.file = f; + uo = (XmlElement)file.UnderlyingObject.CreateNode(XmlNodeType.Element, "Target", MSBuildFile.NamespaceName); + } - public MSBuildPropertyGroup CreatePropertyGroup() - { - MSBuildPropertyGroup res = new MSBuildPropertyGroup(this); - return res; - } + public MSBuildTarget(MSBuildFile f, XmlElement el) + { + this.file = f; + uo = el; + } - public void AppendTask(MSBuildTask task) - { - // insert on this level - this.tasks.Add(task); - // insert on underlaying level - XmlNode tn = task.UnderlyingObject; - uo.AppendChild(tn); - } + public MSBuildTask CreateTask() + { + MSBuildTask res = new MSBuildTask(this); + return res; + } - public void AppendPropertyGroup(MSBuildPropertyGroup item) - { - // insert on this level - this.propertyGroups.Add(item); - // insert on underlaying level - XmlNode tn = item.UnderlyingObject; - uo.AppendChild(tn); - } + public MSBuildPropertyGroup CreatePropertyGroup() + { + MSBuildPropertyGroup res = new MSBuildPropertyGroup(this); + return res; + } - public void AppendItem(MSBuildItem item) - { - // insert on this level - this.items.Add(item); - // insert on underlaying level - XmlNode tn = item.UnderlyingObject; - uo.AppendChild(tn); - } + public void AppendTask(MSBuildTask task) + { + // insert on this level + this.tasks.Add(task); + // insert on underlaying level + XmlNode tn = task.UnderlyingObject; + uo.AppendChild(tn); + } - public readonly char[] TargetSeparators = new char[] { ';' }; + public void AppendPropertyGroup(MSBuildPropertyGroup item) + { + // insert on this level + this.propertyGroups.Add(item); + // insert on underlaying level + XmlNode tn = item.UnderlyingObject; + uo.AppendChild(tn); + } - void AppendListOfTargets(string attributeName, string nameToAdd) - { - string attr = uo.GetAttribute(attributeName); - string[] targets = attr.Split(TargetSeparators); - for (int i = 0; i < targets.Length; i++) + public void AppendItem(MSBuildItem item) { - if (string.Compare(targets[i], nameToAdd) == 0) - { - return; - } + // insert on this level + this.items.Add(item); + // insert on underlaying level + XmlNode tn = item.UnderlyingObject; + uo.AppendChild(tn); } - StringBuilder res = new StringBuilder(); - for (int i = 0; i < targets.Length; i++) + + public readonly char[] TargetSeparators = new char[] { ';' }; + + void AppendListOfTargets(string attributeName, string nameToAdd) { - if (i > 0) + string attr = uo.GetAttribute(attributeName); + string[] targets = attr.Split(TargetSeparators); + for (int i = 0; i < targets.Length; i++) + { + if (string.Compare(targets[i], nameToAdd) == 0) + { + return; + } + } + StringBuilder res = new StringBuilder(); + for (int i = 0; i < targets.Length; i++) + { + if (i > 0) + { + res.Append(TargetSeparators); + } + res.Append(targets[i]); + } + if (res.Length > 0) { res.Append(TargetSeparators); } - res.Append(targets[i]); + res.Append(nameToAdd); + uo.SetAttribute(attributeName, res.ToString()); } - if (res.Length > 0) + + public void AddAfterTarget(string name) { - res.Append(TargetSeparators); + AppendListOfTargets("AfterTargets", name); } - res.Append(nameToAdd); - uo.SetAttribute(attributeName, res.ToString()); - } - public void AddAfterTarget(string name) - { - AppendListOfTargets("AfterTargets", name); - } - - public void AddDependOnTarget(string name) - { - AppendListOfTargets("DependsOnTargets", name); + public void AddDependOnTarget(string name) + { + AppendListOfTargets("DependsOnTargets", name); + } } -} +} \ No newline at end of file diff --git a/mpt-core/03_msbuild/MSBuildTask.cs b/mpt-core/03_msbuild/MSBuildTask.cs index 005715d..b7c1e5e 100644 --- a/mpt-core/03_msbuild/MSBuildTask.cs +++ b/mpt-core/03_msbuild/MSBuildTask.cs @@ -1,120 +1,123 @@ -using System; -using System.Collections.Generic; -using System.Xml; - -public class MSBuildTask : ICanBeConditional +namespace BuildAutomation { - MSBuildTarget parent; - XmlElement uo; - List parameters = new List(); - List resultProperties = new List(); - List resultItems = new List(); - public XmlNode UnderlyingObject + using System; + using System.Collections.Generic; + using System.Xml; + + public class MSBuildTask : ICanBeConditional { - get + MSBuildTarget parent; + XmlElement uo; + List parameters = new List(); + List resultProperties = new List(); + List resultItems = new List(); + public XmlNode UnderlyingObject { - return uo; + get + { + return uo; + } } - } - public string Condition { get { return GetCondition(); } set { SetCondition(value); } } + public string Condition { get { return GetCondition(); } set { SetCondition(value); } } - public string Name { get { return uo.LocalName; } set { SetName(value); } } - - public MSBuildTask(MSBuildTarget p) - { - string name = "NoXmlElementName"; - this.parent = p; - XmlDocument doc = parent.UnderlyingObject.OwnerDocument; - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, MSBuildFile.NamespaceName); - } + public string Name { get { return uo.LocalName; } set { SetName(value); } } - void SetName(string name) - { - // replace underlaying object to change it's name - XmlElement oldItem = uo; - XmlDocument doc = oldItem.OwnerDocument; - // replace name - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, MSBuildFile.NamespaceName); - // what about node's text content ? - uo.InnerText = oldItem.InnerText; - // copy attributes - foreach (XmlAttribute a in oldItem.Attributes) + public MSBuildTask(MSBuildTarget p) { - uo.Attributes.Append((XmlAttribute)a.CloneNode(true)); + string name = "NoXmlElementName"; + this.parent = p; + XmlDocument doc = parent.UnderlyingObject.OwnerDocument; + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, MSBuildFile.NamespaceName); } - // copy childs - for (XmlNode child = oldItem.FirstChild; child != null; child = child.NextSibling) + + void SetName(string name) { - uo.AppendChild(child.CloneNode(true)); + // replace underlaying object to change it's name + XmlElement oldItem = uo; + XmlDocument doc = oldItem.OwnerDocument; + // replace name + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, MSBuildFile.NamespaceName); + // what about node's text content ? + uo.InnerText = oldItem.InnerText; + // copy attributes + foreach (XmlAttribute a in oldItem.Attributes) + { + uo.Attributes.Append((XmlAttribute)a.CloneNode(true)); + } + // copy childs + for (XmlNode child = oldItem.FirstChild; child != null; child = child.NextSibling) + { + uo.AppendChild(child.CloneNode(true)); + } + if (oldItem.ParentNode != null) + { + oldItem.ParentNode.ReplaceChild(uo, oldItem); + } } - if (oldItem.ParentNode != null) + + public MSBuildTaskParameter CreateParameter() { - oldItem.ParentNode.ReplaceChild(uo, oldItem); + MSBuildTaskParameter res = new MSBuildTaskParameter(this); + return res; } - } - - public MSBuildTaskParameter CreateParameter() - { - MSBuildTaskParameter res = new MSBuildTaskParameter(this); - return res; - } - public MSBuildTaskResultProperty CreateResultProperty() - { - MSBuildTaskResultProperty res = new MSBuildTaskResultProperty(this); - return res; - } + public MSBuildTaskResultProperty CreateResultProperty() + { + MSBuildTaskResultProperty res = new MSBuildTaskResultProperty(this); + return res; + } - public MSBuildTaskResultItem CreateResultItem() - { - MSBuildTaskResultItem res = new MSBuildTaskResultItem(this); - return res; - } + public MSBuildTaskResultItem CreateResultItem() + { + MSBuildTaskResultItem res = new MSBuildTaskResultItem(this); + return res; + } - public void AppendParameter(MSBuildTaskParameter attribute) - { - // insert on this level - this.parameters.Add(attribute); - // insert on underlaying level - XmlAttribute tn = attribute.UnderlyingObject; - uo.SetAttributeNode(tn); - } + public void AppendParameter(MSBuildTaskParameter attribute) + { + // insert on this level + this.parameters.Add(attribute); + // insert on underlaying level + XmlAttribute tn = attribute.UnderlyingObject; + uo.SetAttributeNode(tn); + } - public void AppendResultProperty(MSBuildTaskResultProperty subNode) - { - // insert on this level - this.resultProperties.Add(subNode); - // insert on underlaying level - XmlNode tn = subNode.UnderlyingObject; - uo.AppendChild(tn); - } + public void AppendResultProperty(MSBuildTaskResultProperty subNode) + { + // insert on this level + this.resultProperties.Add(subNode); + // insert on underlaying level + XmlNode tn = subNode.UnderlyingObject; + uo.AppendChild(tn); + } - public void AppendResultItem(MSBuildTaskResultItem subNode) - { - // insert on this level - this.resultItems.Add(subNode); - // insert on underlaying level - XmlNode tn = subNode.UnderlyingObject; - uo.AppendChild(tn); - } + public void AppendResultItem(MSBuildTaskResultItem subNode) + { + // insert on this level + this.resultItems.Add(subNode); + // insert on underlaying level + XmlNode tn = subNode.UnderlyingObject; + uo.AppendChild(tn); + } - public void AddParameter(string name, string val) - { - MSBuildTaskParameter parameter = this.CreateParameter(); - parameter.Name = name; - parameter.Value = val; - this.AppendParameter(parameter); - } + public void AddParameter(string name, string val) + { + MSBuildTaskParameter parameter = this.CreateParameter(); + parameter.Name = name; + parameter.Value = val; + this.AppendParameter(parameter); + } - string GetCondition() - { - if (uo.HasAttribute("Condition") == false) return null; - return uo.Attributes["Condition"].Value; - } + string GetCondition() + { + if (uo.HasAttribute("Condition") == false) return null; + return uo.Attributes["Condition"].Value; + } - void SetCondition(string value) - { - uo.SetAttribute("Condition", value); + void SetCondition(string value) + { + uo.SetAttribute("Condition", value); + } } -} +} \ No newline at end of file diff --git a/mpt-core/03_msbuild/MSBuildTaskParameter.cs b/mpt-core/03_msbuild/MSBuildTaskParameter.cs index d88b6a8..91fd6d3 100644 --- a/mpt-core/03_msbuild/MSBuildTaskParameter.cs +++ b/mpt-core/03_msbuild/MSBuildTaskParameter.cs @@ -1,41 +1,44 @@ -using System; -using System.Xml; - -public class MSBuildTaskParameter +namespace BuildAutomation { - // https://github.com/mono/mono/blob/master/mcs/class/referencesource/System.Xml/System/Xml/Dom/XmlAttribute.cs - XmlAttribute uo; - MSBuildTask parent; + using System; + using System.Xml; - public XmlAttribute UnderlyingObject + public class MSBuildTaskParameter { - get + // https://github.com/mono/mono/blob/master/mcs/class/referencesource/System.Xml/System/Xml/Dom/XmlAttribute.cs + XmlAttribute uo; + MSBuildTask parent; + + public XmlAttribute UnderlyingObject { - return uo; + get + { + return uo; + } } - } - public string Name { get { return uo.LocalName; } set { SetName(value); } } - public string Value { get { return uo.Value; } set { uo.Value = value; } } + public string Name { get { return uo.LocalName; } set { SetName(value); } } + public string Value { get { return uo.Value; } set { uo.Value = value; } } - public MSBuildTaskParameter(MSBuildTask p) - { - string name = "NoAttributeNameGiven"; - this.parent = p; - XmlDocument doc = parent.UnderlyingObject.OwnerDocument; - uo = (XmlAttribute)doc.CreateNode(XmlNodeType.Attribute, name, null/*MSBuildFile.NamespaceName*/); - } + public MSBuildTaskParameter(MSBuildTask p) + { + string name = "NoAttributeNameGiven"; + this.parent = p; + XmlDocument doc = parent.UnderlyingObject.OwnerDocument; + uo = (XmlAttribute)doc.CreateNode(XmlNodeType.Attribute, name, null/*MSBuildFile.NamespaceName*/); + } - void SetName(string name) - { - // replace underlaying object to change it's name - XmlAttribute oldAttr = uo; - XmlDocument doc = oldAttr.OwnerDocument; - uo = (XmlAttribute)doc.CreateNode(XmlNodeType.Attribute, name, null/*MSBuildFile.NamespaceName*/); - uo.Value = oldAttr.Value; - if (oldAttr.ParentNode != null) + void SetName(string name) { - oldAttr.ParentNode.ReplaceChild(uo, oldAttr); + // replace underlaying object to change it's name + XmlAttribute oldAttr = uo; + XmlDocument doc = oldAttr.OwnerDocument; + uo = (XmlAttribute)doc.CreateNode(XmlNodeType.Attribute, name, null/*MSBuildFile.NamespaceName*/); + uo.Value = oldAttr.Value; + if (oldAttr.ParentNode != null) + { + oldAttr.ParentNode.ReplaceChild(uo, oldAttr); + } } } -} +} \ No newline at end of file diff --git a/mpt-core/03_msbuild/MSBuildTaskResultItem.cs b/mpt-core/03_msbuild/MSBuildTaskResultItem.cs index 767b39f..5a0d555 100644 --- a/mpt-core/03_msbuild/MSBuildTaskResultItem.cs +++ b/mpt-core/03_msbuild/MSBuildTaskResultItem.cs @@ -1,50 +1,53 @@ -using System; -using System.Xml; - -public class MSBuildTaskResultItem +namespace BuildAutomation { - // https://github.com/mono/mono/blob/master/mcs/class/referencesource/System.Xml/System/Xml/Dom/XmlAttribute.cs - XmlElement uo; - MSBuildTask parent; + using System; + using System.Xml; - public XmlElement UnderlyingObject + public class MSBuildTaskResultItem { - get + // https://github.com/mono/mono/blob/master/mcs/class/referencesource/System.Xml/System/Xml/Dom/XmlAttribute.cs + XmlElement uo; + MSBuildTask parent; + + public XmlElement UnderlyingObject { - return uo; + get + { + return uo; + } } - } - - public string TaskParameter { get { return uo.Attributes["TaskParameter"].Value; } set { uo.SetAttribute("TaskParameter", value); } } - public string ItemName { get { return uo.Attributes["ItemName"].Value; } set { uo.SetAttribute("ItemName", value); } } - public MSBuildTaskResultItem(MSBuildTask p) - { - this.parent = p; - XmlDocument doc = parent.UnderlyingObject.OwnerDocument; - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "Output", MSBuildFile.NamespaceName); - } + public string TaskParameter { get { return uo.Attributes["TaskParameter"].Value; } set { uo.SetAttribute("TaskParameter", value); } } + public string ItemName { get { return uo.Attributes["ItemName"].Value; } set { uo.SetAttribute("ItemName", value); } } - void SetName(string name) - { - // replace underlaying object to change it's name - XmlElement oldItem = uo; - XmlDocument doc = oldItem.OwnerDocument; - // replace name - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, MSBuildFile.NamespaceName); - uo.Value = oldItem.Value; - // copy attributes - foreach (XmlAttribute a in oldItem.Attributes) + public MSBuildTaskResultItem(MSBuildTask p) { - uo.Attributes.Append((XmlAttribute)a.CloneNode(true)); + this.parent = p; + XmlDocument doc = parent.UnderlyingObject.OwnerDocument; + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "Output", MSBuildFile.NamespaceName); } - // copy childs - for (XmlNode child = oldItem.FirstChild; child != null; child = child.NextSibling) + + void SetName(string name) { - uo.AppendChild(child.CloneNode(true)); + // replace underlaying object to change it's name + XmlElement oldItem = uo; + XmlDocument doc = oldItem.OwnerDocument; + // replace name + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, MSBuildFile.NamespaceName); + uo.Value = oldItem.Value; + // copy attributes + foreach (XmlAttribute a in oldItem.Attributes) + { + uo.Attributes.Append((XmlAttribute)a.CloneNode(true)); + } + // copy childs + for (XmlNode child = oldItem.FirstChild; child != null; child = child.NextSibling) + { + uo.AppendChild(child.CloneNode(true)); + } + // what about node's text content ? + //uo.Value = oldItem.Value; + oldItem.ParentNode.ReplaceChild(uo, oldItem); } - // what about node's text content ? - //uo.Value = oldItem.Value; - oldItem.ParentNode.ReplaceChild(uo, oldItem); } } diff --git a/mpt-core/03_msbuild/MSBuildTaskResultProperty.cs b/mpt-core/03_msbuild/MSBuildTaskResultProperty.cs index 761c89c..4f30d0e 100644 --- a/mpt-core/03_msbuild/MSBuildTaskResultProperty.cs +++ b/mpt-core/03_msbuild/MSBuildTaskResultProperty.cs @@ -1,50 +1,53 @@ -using System; -using System.Xml; - -public class MSBuildTaskResultProperty +namespace BuildAutomation { - // https://github.com/mono/mono/blob/master/mcs/class/referencesource/System.Xml/System/Xml/Dom/XmlAttribute.cs - XmlElement uo; - MSBuildTask parent; + using System; + using System.Xml; - public XmlElement UnderlyingObject + public class MSBuildTaskResultProperty { - get + // https://github.com/mono/mono/blob/master/mcs/class/referencesource/System.Xml/System/Xml/Dom/XmlAttribute.cs + XmlElement uo; + MSBuildTask parent; + + public XmlElement UnderlyingObject { - return uo; + get + { + return uo; + } } - } - - public string TaskParameter { get { return uo.Attributes["TaskParameter"].Value; } set { uo.Attributes["TaskParameter"].Value = value; } } - public string Value { get { return uo.Value; } set { uo.Value = value; } } - public MSBuildTaskResultProperty(MSBuildTask p) - { - this.parent = p; - XmlDocument doc = parent.UnderlyingObject.OwnerDocument; - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "Output", MSBuildFile.NamespaceName); - } + public string TaskParameter { get { return uo.Attributes["TaskParameter"].Value; } set { uo.Attributes["TaskParameter"].Value = value; } } + public string Value { get { return uo.Value; } set { uo.Value = value; } } - void SetName(string name) - { - // replace underlaying object to change it's name - XmlElement oldItem = uo; - XmlDocument doc = oldItem.OwnerDocument; - // replace name - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, MSBuildFile.NamespaceName); - uo.Value = oldItem.Value; - // copy attributes - foreach (XmlAttribute a in oldItem.Attributes) + public MSBuildTaskResultProperty(MSBuildTask p) { - uo.Attributes.Append((XmlAttribute)a.CloneNode(true)); + this.parent = p; + XmlDocument doc = parent.UnderlyingObject.OwnerDocument; + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "Output", MSBuildFile.NamespaceName); } - // copy childs - for (XmlNode child = oldItem.FirstChild; child != null; child = child.NextSibling) + + void SetName(string name) { - uo.AppendChild(child.CloneNode(true)); + // replace underlaying object to change it's name + XmlElement oldItem = uo; + XmlDocument doc = oldItem.OwnerDocument; + // replace name + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, MSBuildFile.NamespaceName); + uo.Value = oldItem.Value; + // copy attributes + foreach (XmlAttribute a in oldItem.Attributes) + { + uo.Attributes.Append((XmlAttribute)a.CloneNode(true)); + } + // copy childs + for (XmlNode child = oldItem.FirstChild; child != null; child = child.NextSibling) + { + uo.AppendChild(child.CloneNode(true)); + } + // what about node's text content ? + //uo.Value = oldItem.Value; + oldItem.ParentNode.ReplaceChild(uo, oldItem); } - // what about node's text content ? - //uo.Value = oldItem.Value; - oldItem.ParentNode.ReplaceChild(uo, oldItem); } } diff --git a/mpt-core/04_CSProj/CSharpLibraryProject.cs b/mpt-core/04_CSProj/CSharpLibraryProject.cs index ce142d5..196d5a1 100644 --- a/mpt-core/04_CSProj/CSharpLibraryProject.cs +++ b/mpt-core/04_CSProj/CSharpLibraryProject.cs @@ -1,134 +1,201 @@ -using System; - -public class CSharpLibraryProject : IDisposable +namespace BuildAutomation { - MSBuildFile uo; - - ConfigurationHashList configurations = null; - public ConfigurationHashList Configurations { get { return configurations; } } - - public string FileName { get { return uo.FileName; } set { uo.FileName = value; } } + using System; + using System.Collections.Generic; + using System.IO; + using System.Xml; + using CWDev.SLNTools.Core; - public CSharpLibraryProject(string csproj_file) + public class CSharpLibraryProject : IDisposable { - configurations = new ConfigurationHashList(this); - uo = new MSBuildFile(csproj_file); - } + MSBuildFile uo; - public void Dispose() - { - uo.Dispose(); - } + ConfigurationHashList configurations = null; + public ConfigurationHashList Configurations { get { return configurations; } } - public void InjectProjectImport(string import_name) - { - // construct new import - MSBuildImport newImport = uo.CreateImport(); - newImport.Project = import_name; - // insert import - MSBuildImport existingImport = uo.FindImport("$(MSBuildBinPath)\\Microsoft.CSharp.targets"); - if (existingImport == null) - { - uo.InsertImport(newImport); - } - else + public string FileName { get { return uo.FileName; } set { uo.FileName = value; } } + + public CSharpLibraryProject(string csproj_file) { - uo.InsertImportAfter(existingImport, newImport); + configurations = new ConfigurationHashList(this); + uo = new MSBuildFile(csproj_file); } - } - public void InjectVersioning(string versionPropertyName) - { - /* - - - - - - - */ - MSBuildTarget targ = uo.CreateTarget(); - targ.Name = "MyAssemblyVersion"; + public void Dispose() { - MSBuildTask task = targ.CreateTask(); - task.Name = "MakeDir"; - task.AddParameter("Directories", "$(IntermediateOutputPath)"); - targ.AppendTask(task); + uo.Dispose(); } + + public void InjectProjectImport(string import_name) { - MSBuildPropertyGroup group = targ.CreatePropertyGroup(); - group.Condition = " '$(" + versionPropertyName + ")' == '' "; - group.AddProperty(versionPropertyName, "1.0.0.0"); - targ.AppendPropertyGroup(group); + // construct new import + MSBuildImport newImport = uo.CreateImport(); + newImport.Project = import_name; + // insert import + MSBuildImport existingImport = uo.FindImport("$(MSBuildBinPath)\\Microsoft.CSharp.targets"); + if (existingImport == null) + { + uo.InsertImport(newImport); + } + else + { + uo.InsertImportAfter(existingImport, newImport); + } } + + public void InjectVersioning(string versionPropertyName) { - MSBuildTask task = targ.CreateTask(); - task.Name = "AssemblyInfo"; - task.AddParameter("CodeLanguage", "CS"); + /* + + + + + + + */ + MSBuildTarget targ = uo.CreateTarget(); + targ.Name = "MyAssemblyVersion"; + { + MSBuildTask task = targ.CreateTask(); + task.Name = "MakeDir"; + task.AddParameter("Directories", "$(IntermediateOutputPath)"); + targ.AppendTask(task); + } + { + MSBuildPropertyGroup group = targ.CreatePropertyGroup(); + group.Condition = " '$(" + versionPropertyName + ")' == '' "; + group.AddProperty(versionPropertyName, "1.0.0.0"); + targ.AppendPropertyGroup(group); + } + { + MSBuildTask task = targ.CreateTask(); + task.Name = "AssemblyInfo"; + task.AddParameter("CodeLanguage", "CS"); - task.AddParameter("AssemblyVersion", "$(" + versionPropertyName + ")"); // System.Reflection.AssemblyVersion - task.AddParameter("AssemblyFileVersion", "$(" + versionPropertyName + ")"); // System.Reflection.AssemblyFileVersion - task.AddParameter("AssemblyInformationalVersion", "$(" + versionPropertyName + ")"); // System.Reflection.AssemblyInformationalVersion + task.AddParameter("AssemblyVersion", "$(" + versionPropertyName + ")"); // System.Reflection.AssemblyVersion + task.AddParameter("AssemblyFileVersion", "$(" + versionPropertyName + ")"); // System.Reflection.AssemblyFileVersion + task.AddParameter("AssemblyInformationalVersion", "$(" + versionPropertyName + ")"); // System.Reflection.AssemblyInformationalVersion - task.AddParameter("OutputFile", "$(IntermediateOutputPath)AssemblyVersion.Generated.cs"); - { - MSBuildTaskResultItem resultItem = task.CreateResultItem(); - resultItem.TaskParameter = "OutputFile"; - resultItem.ItemName = "Compile"; - task.AppendResultItem(resultItem); + task.AddParameter("OutputFile", "$(IntermediateOutputPath)AssemblyVersion.Generated.cs"); + { + MSBuildTaskResultItem resultItem = task.CreateResultItem(); + resultItem.TaskParameter = "OutputFile"; + resultItem.ItemName = "Compile"; + task.AppendResultItem(resultItem); + } + targ.AppendTask(task); } - targ.AppendTask(task); + uo.EnsureTargetExists("BeforeBuild"); + uo.InsertTarget(targ); + uo.AddDependOnTarget("BeforeBuild", targ.Name); } - uo.EnsureTargetExists("BeforeBuild"); - uo.InsertTarget(targ); - uo.AddDependOnTarget("BeforeBuild", targ.Name); - } - // http://stackoverflow.com/questions/30943342/how-to-use-internalsvisibleto-attribute-with-strongly-named-assembly - public void InjectInternalsVisibleTo(string assemblyName, string assemblyPublicKey) - { - MSBuildTarget targ = uo.CreateTarget(); - targ.Name = "MyInsertInternalsTo"; + // http://stackoverflow.com/questions/30943342/how-to-use-internalsvisibleto-attribute-with-strongly-named-assembly + public void InjectInternalsVisibleTo(string assemblyName, string assemblyPublicKey) { - MSBuildTask task = targ.CreateTask(); // '$(SignAssembly)' == 'true' - task.Name = "AssemblyInfo"; - task.AddParameter("CodeLanguage", "CS"); + MSBuildTarget targ = uo.CreateTarget(); + targ.Name = "MyInsertInternalsTo"; + { + MSBuildTask task = targ.CreateTask(); // '$(SignAssembly)' == 'true' + task.Name = "AssemblyInfo"; + task.AddParameter("CodeLanguage", "CS"); - task.Condition = "'$(SignAssembly)' == 'true'"; - task.AddParameter("InternalsVisibleTo", assemblyName + ", PublicKey=" + assemblyPublicKey); - task.AddParameter("OutputFile", "$(IntermediateOutputPath)" + assemblyName + ".IVT.Generated.cs"); + task.Condition = "'$(SignAssembly)' == 'true'"; + task.AddParameter("InternalsVisibleTo", assemblyName + ", PublicKey=" + assemblyPublicKey); + task.AddParameter("OutputFile", "$(IntermediateOutputPath)" + assemblyName + ".IVT.Generated.cs"); + { + MSBuildTaskResultItem resultItem = task.CreateResultItem(); + resultItem.TaskParameter = "OutputFile"; + resultItem.ItemName = "Compile"; + task.AppendResultItem(resultItem); + } + targ.AppendTask(task); + } { - MSBuildTaskResultItem resultItem = task.CreateResultItem(); - resultItem.TaskParameter = "OutputFile"; - resultItem.ItemName = "Compile"; - task.AppendResultItem(resultItem); + MSBuildTask task = targ.CreateTask(); // '$(SignAssembly)' == 'false' + task.Name = "AssemblyInfo"; + task.AddParameter("CodeLanguage", "CS"); + + task.Condition = "'$(SignAssembly)' != 'true'"; + task.AddParameter("InternalsVisibleTo", assemblyName); + task.AddParameter("OutputFile", "$(IntermediateOutputPath)" + assemblyName + ".IVT.Generated.cs"); + { + MSBuildTaskResultItem resultItem = task.CreateResultItem(); + resultItem.TaskParameter = "OutputFile"; + resultItem.ItemName = "Compile"; + task.AppendResultItem(resultItem); + } + targ.AppendTask(task); } - targ.AppendTask(task); + uo.EnsureTargetExists("BeforeBuild"); + uo.InsertTarget(targ); + uo.AddDependOnTarget("BeforeBuild", targ.Name); } + + public IEnumerable References { - MSBuildTask task = targ.CreateTask(); // '$(SignAssembly)' == 'false' - task.Name = "AssemblyInfo"; - task.AddParameter("CodeLanguage", "CS"); + get + { + string filename = uo.FileName; + if (!File.Exists(filename)) + { + throw new FileNotFoundException($"Cannot detect references of project '{filename}' because the project file cannot be found."); + } + var docManaged = new XmlDocument(); + docManaged.Load(filename); + + var xmlManager = new XmlNamespaceManager(docManaged.NameTable); + xmlManager.AddNamespace("prefix", "http://schemas.microsoft.com/developer/msbuild/2003"); - task.Condition = "'$(SignAssembly)' != 'true'"; - task.AddParameter("InternalsVisibleTo", assemblyName); - task.AddParameter("OutputFile", "$(IntermediateOutputPath)" + assemblyName + ".IVT.Generated.cs"); + foreach (XmlNode xmlNode in docManaged.SelectNodes(@"//prefix:Reference", xmlManager)) + { + string referenceInclude = xmlNode.Attributes.GetNamedItem("Include").InnerText; + string referencePackage = xmlNode.SelectSingleNode(@"prefix:Package", xmlManager)?.InnerText.Trim(); // TODO handle null + yield return new ReferencedAssembly( + referenceInclude, + referencePackage); + } + } + } + public IEnumerable Dependencies + { + get { - MSBuildTaskResultItem resultItem = task.CreateResultItem(); - resultItem.TaskParameter = "OutputFile"; - resultItem.ItemName = "Compile"; - task.AppendResultItem(resultItem); + throw new NotImplementedException(); + /* + var docManaged = this.uo.UnderlyingObject; + + var xmlManager = new XmlNamespaceManager(docManaged.NameTable); + xmlManager.AddNamespace("prefix", "http://schemas.microsoft.com/developer/msbuild/2003"); + + foreach (XmlNode xmlNode in docManaged.SelectNodes(@"//prefix:ProjectReference", xmlManager)) + { + // SelectSingleNode - Selects the first XmlNode that matches the XPath expression. + var nodeProject = xmlNode.SelectSingleNode(@"prefix:Project", xmlManager); + if (nodeProject == null) + { + Console.WriteLine($"Unexpected syntax of reference {xmlNode.OuterXml}"); + continue; + } + string dependencyGuid = nodeProject.InnerText == null ? string.Empty : nodeProject.InnerText.Trim(); // TODO handle null + var nodeName = xmlNode.SelectSingleNode(@"prefix:Name", xmlManager); + string dependencyName = nodeName.InnerText == null ? string.Empty : nodeName.InnerText.Trim(); // TODO handle null + yield return FindProjectInContainer( + dependencyGuid, + "Cannot find one of the dependency of project '{0}'.\nProject guid: {1}\nDependency guid: {2}\nDependency name: {3}\nReference found in: ProjectReference node of file '{4}'", + m_projectName, + r_projectGuid, + dependencyGuid, + dependencyName, + this.FullPath); + } + */ } - targ.AppendTask(task); } - uo.EnsureTargetExists("BeforeBuild"); - uo.InsertTarget(targ); - uo.AddDependOnTarget("BeforeBuild", targ.Name); } - -} +} \ No newline at end of file diff --git a/mpt-core/04_CSProj/Configuration.cs b/mpt-core/04_CSProj/Configuration.cs index 34320ca..1e76702 100644 --- a/mpt-core/04_CSProj/Configuration.cs +++ b/mpt-core/04_CSProj/Configuration.cs @@ -1,123 +1,125 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Diagnostics; - -public class Configuration +namespace BuildAutomation { - CSharpLibraryProject r_container; + using System; + using System.Collections.Generic; + using System.Collections.ObjectModel; + using System.Diagnostics; - public Configuration(CSharpLibraryProject r_container, Configuration item) + public class Configuration { - this.r_container = r_container; - this.Name = item.Name; - } + CSharpLibraryProject r_container; - public string Name - { - get + public Configuration(CSharpLibraryProject r_container, Configuration item) { - Debug.Assert(r_container != null); - throw new NotImplementedException(); + this.r_container = r_container; + this.Name = item.Name; } - set + + public string Name { - throw new NotImplementedException(); + get + { + Debug.Assert(r_container != null); + throw new NotImplementedException(); + } + set + { + throw new NotImplementedException(); + } } - } - public string GetAssemblyName() - { - throw new NotImplementedException(); + public string GetAssemblyName() + { + throw new NotImplementedException(); + } } -} -public class ConfigurationHashList : KeyedCollection -{ - private CSharpLibraryProject r_container = null; - // - // Properties - // - public CSharpLibraryProject Container + public class ConfigurationHashList : KeyedCollection { - get + private CSharpLibraryProject r_container = null; + // + // Properties + // + public CSharpLibraryProject Container { - return this.r_container; + get + { + return this.r_container; + } } - } - // - // Constructors - // - public ConfigurationHashList(CSharpLibraryProject container) : base(StringComparer.InvariantCultureIgnoreCase) - { - if (container == null) + // + // Constructors + // + public ConfigurationHashList(CSharpLibraryProject container) : base(StringComparer.InvariantCultureIgnoreCase) { - throw new ArgumentNullException("container"); + if (container == null) + { + throw new ArgumentNullException("container"); + } + this.r_container = container; } - this.r_container = container; - } - public ConfigurationHashList(CSharpLibraryProject container, IEnumerable items) : this(container) - { - this.AddRange(items); - } + public ConfigurationHashList(CSharpLibraryProject container, IEnumerable items) : this(container) + { + this.AddRange(items); + } - // - // Methods - // - public void AddRange(IEnumerable items) - { - if (items != null) + // + // Methods + // + public void AddRange(IEnumerable items) { - foreach (var current in items) + if (items != null) { - base.Add(current); + foreach (var current in items) + { + base.Add(current); + } } } - } - public Configuration FindByName(string name) - { - Configuration result; - foreach (var current in this) + public Configuration FindByName(string name) { - if (string.Compare(current.Name, name, StringComparison.InvariantCultureIgnoreCase) == 0) + Configuration result; + foreach (var current in this) { - result = current; - return result; + if (string.Compare(current.Name, name, StringComparison.InvariantCultureIgnoreCase) == 0) + { + result = current; + return result; + } } + result = null; + return result; } - result = null; - return result; - } - protected override string GetKeyForItem(Configuration item) - { - return item.Name; - } + protected override string GetKeyForItem(Configuration item) + { + return item.Name; + } - protected override void InsertItem(int index, Configuration item) - { - base.InsertItem(index, new Configuration(this.r_container, item)); - } + protected override void InsertItem(int index, Configuration item) + { + base.InsertItem(index, new Configuration(this.r_container, item)); + } - protected override void SetItem(int index, Configuration item) - { - base.SetItem(index, new Configuration(this.r_container, item)); - } + protected override void SetItem(int index, Configuration item) + { + base.SetItem(index, new Configuration(this.r_container, item)); + } - public void Sort() - { - this.Sort((Configuration p1, Configuration p2) => StringComparer.InvariantCultureIgnoreCase.Compare(p1.Name, p2.Name)); - } + public void Sort() + { + this.Sort((Configuration p1, Configuration p2) => StringComparer.InvariantCultureIgnoreCase.Compare(p1.Name, p2.Name)); + } - public void Sort(Comparison comparer) - { - var list = new List(this); - list.Sort(comparer); - base.Clear(); - this.AddRange(list); + public void Sort(Comparison comparer) + { + var list = new List(this); + list.Sort(comparer); + base.Clear(); + this.AddRange(list); + } } } - diff --git a/mpt-core/04_CSProj/ProjectTools.cs b/mpt-core/04_CSProj/ProjectTools.cs index eb73b98..dd0b43e 100644 --- a/mpt-core/04_CSProj/ProjectTools.cs +++ b/mpt-core/04_CSProj/ProjectTools.cs @@ -1,330 +1,347 @@ -using System; -using System.Text.RegularExpressions; -using System.IO; -using System.Collections.Generic; -using System.Xml.Linq; -using System.Xml.XPath; -using System.Xml; - -public class ProjectTools +namespace BuildAutomation { - public static void DumpFiles(string projectFilename, string baseDirectory) + using System; + using System.Text.RegularExpressions; + using System.IO; + using System.Collections.Generic; + using System.Xml.Linq; + using System.Xml.XPath; + using System.Xml; + + public class ProjectTools { - List files = new List(); - files.Add(projectFilename); - var stream = new MemoryStream(File.ReadAllBytes(projectFilename)); // cache file in memoty - var document = XDocument.Load(stream); - var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); - xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); - IEnumerable listOfSourceFiles = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:Compile[@Include]", xmlNamespaceManager); - foreach (var el in listOfSourceFiles) - { - files.Add(el.Attribute("Include").Value); - } - IEnumerable listOfContentFiles = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:Content[@Include]", xmlNamespaceManager); - foreach (var el in listOfContentFiles) - { - files.Add(el.Attribute("Include").Value); - } - IEnumerable listOfResourceFiles = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:EmbeddedResource[@Include]", xmlNamespaceManager); - foreach (var el in listOfResourceFiles) - { - files.Add(el.Attribute("Include").Value); - } - string separator = new string(Path.DirectorySeparatorChar, 1); - string projDir = (new FileInfo(projectFilename)).Directory.FullName; - foreach (string relativeName in files) + public static void DumpFiles(string projectFilename, string baseDirectory) { - var correctedRelativeName = relativeName.Replace("\\", separator); - string fullFileName = Path.Combine(projDir, correctedRelativeName); - fullFileName = new FileInfo(fullFileName).FullName; - string shortName; - if (fullFileName.StartsWith(baseDirectory, StringComparison.InvariantCulture)) + List files = new List(); + files.Add(projectFilename); + var stream = new MemoryStream(File.ReadAllBytes(projectFilename)); // cache file in memoty + var document = XDocument.Load(stream); + var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); + xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); + IEnumerable listOfSourceFiles = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:Compile[@Include]", xmlNamespaceManager); + foreach (var el in listOfSourceFiles) { - shortName = fullFileName.Substring(baseDirectory.Length); - if (shortName.StartsWith(separator, StringComparison.InvariantCulture)) - { - shortName = shortName.Substring(1); - } + files.Add(el.Attribute("Include").Value); + } + IEnumerable listOfContentFiles = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:Content[@Include]", xmlNamespaceManager); + foreach (var el in listOfContentFiles) + { + files.Add(el.Attribute("Include").Value); } - else + IEnumerable listOfResourceFiles = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:EmbeddedResource[@Include]", xmlNamespaceManager); + foreach (var el in listOfResourceFiles) { - shortName = fullFileName; + files.Add(el.Attribute("Include").Value); + } + string separator = new string(Path.DirectorySeparatorChar, 1); + string projDir = (new FileInfo(projectFilename)).Directory.FullName; + foreach (string relativeName in files) + { + var correctedRelativeName = relativeName.Replace("\\", separator); + string fullFileName = Path.Combine(projDir, correctedRelativeName); + fullFileName = new FileInfo(fullFileName).FullName; + string shortName; + if (fullFileName.StartsWith(baseDirectory, StringComparison.InvariantCulture)) + { + shortName = fullFileName.Substring(baseDirectory.Length); + if (shortName.StartsWith(separator, StringComparison.InvariantCulture)) + { + shortName = shortName.Substring(1); + } + } + else + { + shortName = fullFileName; + } + Console.WriteLine(shortName); } - Console.WriteLine(shortName); } - } - public static void DumpRefs(string projectFilename, string baseDirectory) - { - var stream = new MemoryStream(File.ReadAllBytes(projectFilename)); // cache file in memoty - var document = XDocument.Load(stream); - var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); - xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); - IEnumerable listOfSourceFiles = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:Reference[@Include]", xmlNamespaceManager); - foreach (var el in listOfSourceFiles) + public static void DumpRefs(string projectFilename, string baseDirectory) { - string strAssemblyReference = el.Attribute("Include").Value; - Console.WriteLine(strAssemblyReference); - // TODO: process hint paths + var stream = new MemoryStream(File.ReadAllBytes(projectFilename)); // cache file in memoty + var document = XDocument.Load(stream); + var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); + xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); + IEnumerable listOfSourceFiles = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:Reference[@Include]", xmlNamespaceManager); + foreach (var el in listOfSourceFiles) + { + string strAssemblyReference = el.Attribute("Include").Value; + Console.WriteLine(strAssemblyReference); + // TODO: process hint paths + } } - } - public static void DumpProjRefs(string projectFilename, string baseDirectory) - { - List files = new List(); - var stream = new MemoryStream(File.ReadAllBytes(projectFilename)); // cache file in memoty - var document = XDocument.Load(stream); - var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); - xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); - IEnumerable listOfSourceFiles = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:ProjectReference[@Include]", xmlNamespaceManager); - foreach (var el in listOfSourceFiles) - { - files.Add(el.Attribute("Include").Value); - } - string separator = new string(Path.DirectorySeparatorChar, 1); - string projDir = (new FileInfo(projectFilename)).Directory.FullName; - foreach (string relativeName in files) + public static void DumpProjRefs(string projectFilename, string baseDirectory) { - var correctedRelativeName = relativeName.Replace("\\", separator); - string fullFileName = Path.Combine(projDir, correctedRelativeName); - fullFileName = new FileInfo(fullFileName).FullName; - string shortName; - if (fullFileName.StartsWith(baseDirectory, StringComparison.InvariantCulture)) + List files = new List(); + var stream = new MemoryStream(File.ReadAllBytes(projectFilename)); // cache file in memoty + var document = XDocument.Load(stream); + var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); + xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); + IEnumerable listOfSourceFiles = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:ProjectReference[@Include]", xmlNamespaceManager); + foreach (var el in listOfSourceFiles) { - shortName = fullFileName.Substring(baseDirectory.Length); - if (shortName.StartsWith(separator, StringComparison.InvariantCulture)) - { - shortName = shortName.Substring(1); - } + files.Add(el.Attribute("Include").Value); } - else + string separator = new string(Path.DirectorySeparatorChar, 1); + string projDir = (new FileInfo(projectFilename)).Directory.FullName; + foreach (string relativeName in files) { - shortName = fullFileName; + var correctedRelativeName = relativeName.Replace("\\", separator); + string fullFileName = Path.Combine(projDir, correctedRelativeName); + fullFileName = new FileInfo(fullFileName).FullName; + string shortName; + if (fullFileName.StartsWith(baseDirectory, StringComparison.InvariantCulture)) + { + shortName = fullFileName.Substring(baseDirectory.Length); + if (shortName.StartsWith(separator, StringComparison.InvariantCulture)) + { + shortName = shortName.Substring(1); + } + } + else + { + shortName = fullFileName; + } + Console.WriteLine(shortName); } - Console.WriteLine(shortName); } - } - public static System.Text.Encoding GetEncoding(string filePath) - { - System.Text.Encoding enc = null; - using (var file = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) + public static System.Text.Encoding GetEncoding(string filePath) { - if (file.CanSeek) + System.Text.Encoding enc = null; + using (var file = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { - byte[] bom = new byte[4]; // Get the byte-order mark, if there is one - file.Read(bom, 0, 4); - if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) // utf-8 + if (file.CanSeek) { - enc = System.Text.Encoding.UTF8; + byte[] bom = new byte[4]; // Get the byte-order mark, if there is one + file.Read(bom, 0, 4); + if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) // utf-8 + { + enc = System.Text.Encoding.UTF8; + } + else + if (bom[0] == 0xfe && bom[1] == 0xff) // utf-16 and ucs-2 + { + enc = System.Text.Encoding.Unicode; + } + else + if ((bom[0] == 0xff && bom[1] == 0xfe) || // ucs-2le, ucs-4le, and ucs-16le + (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff)) // ucs-4 + { + enc = System.Text.Encoding.Unicode; + } + else + { + enc = System.Text.Encoding.ASCII; + } + + // Now reposition the file cursor back to the start of the file + file.Seek(0, System.IO.SeekOrigin.Begin); } else - if (bom[0] == 0xfe && bom[1] == 0xff) // utf-16 and ucs-2 { - enc = System.Text.Encoding.Unicode; + // The file cannot be randomly accessed, so you need to decide what to set the default to + // based on the data provided. If you're expecting data from a lot of older applications, + // default your encoding to Encoding.ASCII. If you're expecting data from a lot of newer + // applications, default your encoding to Encoding.Unicode. Also, since binary files are + // single byte-based, so you will want to use Encoding.ASCII, even though you'll probably + // never need to use the encoding then since the Encoding classes are really meant to get + // strings from the byte array that is the file. + + enc = System.Text.Encoding.ASCII; } - else - if ((bom[0] == 0xff && bom[1] == 0xfe) || // ucs-2le, ucs-4le, and ucs-16le - (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff)) // ucs-4 + } + return enc; + } + + public static void RemoveWarningsAsErrors(string csproj_file, string as_unified_patch) + { + System.Text.Encoding enc = GetEncoding(csproj_file); + var old_version = File.ReadAllText(csproj_file, enc); + var start_of_line = @"( |\t)*"; + var end_of_line = @"(\n\r|\n|\r)"; + var r = new Regex(start_of_line + "true" + end_of_line); + var collection = r.Matches(old_version); + Match[] mc = new Match[collection.Count]; + collection.CopyTo(mc, 0); + var new_version = old_version; + for (int i = mc.Length - 1; i >= 0; --i) + { + Match m = mc[i]; + new_version = new_version.Remove(m.Index, m.Length); + } + if (string.Compare(old_version, new_version) != 0) + { + if (as_unified_patch == null) { - enc = System.Text.Encoding.Unicode; + File.WriteAllText(csproj_file, new_version, enc); + Console.WriteLine("\tchanged"); } else { - enc = System.Text.Encoding.ASCII; + throw new NotImplementedException(); } + } + } - // Now reposition the file cursor back to the start of the file - file.Seek(0, System.IO.SeekOrigin.Begin); + public static void RemoveSigning(string csproj_file, string as_unified_patch) + { + bool bRequiresSave = false; + + var stream = new MemoryStream(File.ReadAllBytes(csproj_file)); // cache file in memoty + var document = XDocument.Load(stream); + var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); + xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); + + // remove SignAssembly + var itemsToRemove1 = new List(); + IEnumerable listOfElements1 = document.XPathSelectElements("/ns:Project/ns:PropertyGroup/ns:SignAssembly", xmlNamespaceManager); + foreach (var el in listOfElements1) + { + itemsToRemove1.Add(el); } - else + foreach (var item in itemsToRemove1) { - // The file cannot be randomly accessed, so you need to decide what to set the default to - // based on the data provided. If you're expecting data from a lot of older applications, - // default your encoding to Encoding.ASCII. If you're expecting data from a lot of newer - // applications, default your encoding to Encoding.Unicode. Also, since binary files are - // single byte-based, so you will want to use Encoding.ASCII, even though you'll probably - // never need to use the encoding then since the Encoding classes are really meant to get - // strings from the byte array that is the file. + Console.WriteLine($"SignAssembly removed {item.ToString()}"); + item.Remove(); + bRequiresSave = true; + } - enc = System.Text.Encoding.ASCII; + // remove AssemblyOriginatorKeyFile + var itemsToRemove2 = new List(); + IEnumerable listOfElements2 = document.XPathSelectElements("/ns:Project/ns:PropertyGroup/ns:AssemblyOriginatorKeyFile", xmlNamespaceManager); + foreach (var el in listOfElements2) + { + itemsToRemove2.Add(el); + } + foreach (var item in itemsToRemove2) + { + Console.WriteLine($"AssemblyOriginatorKeyFile removed {item.ToString()}"); + item.Remove(); + bRequiresSave = true; } - } - return enc; - } - public static void RemoveWarningsAsErrors(string csproj_file, string as_unified_patch) - { - System.Text.Encoding enc = GetEncoding(csproj_file); - var old_version = File.ReadAllText(csproj_file, enc); - var start_of_line = @"( |\t)*"; - var end_of_line = @"(\n\r|\n|\r)"; - var r = new Regex(start_of_line + "true" + end_of_line); - var collection = r.Matches(old_version); - Match[] mc = new Match[collection.Count]; - collection.CopyTo(mc, 0); - var new_version = old_version; - for (int i = mc.Length - 1; i >= 0; --i) - { - Match m = mc[i]; - new_version = new_version.Remove(m.Index, m.Length); - } - if (string.Compare(old_version, new_version) != 0) - { - if (as_unified_patch == null) + // remove .snk file link + var itemsToRemove3 = new List(); + IEnumerable listOfReferences3 = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:None[@Include]", xmlNamespaceManager); + foreach (var el in listOfReferences3) { - File.WriteAllText(csproj_file, new_version, enc); - Console.WriteLine("\tchanged"); + string filepath = el.Attribute("Include").Value; + foreach (var item in itemsToRemove2) + { + if (string.Compare(item.Value, filepath) == 0) + { + itemsToRemove3.Add(el); + } + } } - else + foreach (var item in itemsToRemove3) { - throw new NotImplementedException(); + Console.WriteLine($"Item removed {item.ToString()}"); + item.Remove(); + bRequiresSave = true; + } + if (bRequiresSave) + { + document.Save(csproj_file); } } - } - - public static void RemoveSigning(string csproj_file, string as_unified_patch) - { - bool bRequiresSave = false; - - var stream = new MemoryStream(File.ReadAllBytes(csproj_file)); // cache file in memoty - var document = XDocument.Load(stream); - var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); - xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); - // remove SignAssembly - var itemsToRemove1 = new List(); - IEnumerable listOfElements1 = document.XPathSelectElements("/ns:Project/ns:PropertyGroup/ns:SignAssembly", xmlNamespaceManager); - foreach (var el in listOfElements1) - { - itemsToRemove1.Add(el); - } - foreach (var item in itemsToRemove1) + public static Dictionary GetMainOutputs(string csproj_file) { - Console.WriteLine($"SignAssembly removed {item.ToString()}"); - item.Remove(); - bRequiresSave = true; + Dictionary res = new Dictionary(); + // TODO: open XML file, read attributes, construct relative path + return res; } - // remove AssemblyOriginatorKeyFile - var itemsToRemove2 = new List(); - IEnumerable listOfElements2 = document.XPathSelectElements("/ns:Project/ns:PropertyGroup/ns:AssemblyOriginatorKeyFile", xmlNamespaceManager); - foreach (var el in listOfElements2) + protected static bool RemoveReference(XDocument document, string reference_name) { - itemsToRemove2.Add(el); - } - foreach (var item in itemsToRemove2) - { - Console.WriteLine($"AssemblyOriginatorKeyFile removed {item.ToString()}"); - item.Remove(); - bRequiresSave = true; - } + bool bWasRemoved = false; + var newname = new AssemblyNameInGAC(reference_name); + var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); + xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); - // remove .snk file link - var itemsToRemove3 = new List(); - IEnumerable listOfReferences3 = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:None[@Include]", xmlNamespaceManager); - foreach (var el in listOfReferences3) - { - string filepath = el.Attribute("Include").Value; - foreach (var item in itemsToRemove2) + // remove ProjectReference + var itemsToRemove2 = new List(); + IEnumerable listOfReferences2 = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:ProjectReference[@Include]", xmlNamespaceManager); + foreach (var el in listOfReferences2) { - if (string.Compare(item.Value, filepath) == 0) + string assRef = el.Attribute("Include").Value; + if (string.Compare(newname.Name, assRef) == 0) { - itemsToRemove3.Add(el); + itemsToRemove2.Add(el); } } - } - foreach (var item in itemsToRemove3) - { - Console.WriteLine($"Item removed {item.ToString()}"); - item.Remove(); - bRequiresSave = true; - } - if (bRequiresSave) - { - document.Save(csproj_file); - } - } - - public static Dictionary GetMainOutputs(string csproj_file) - { - Dictionary res = new Dictionary(); - // TODO: open XML file, read attributes, construct relative path - return res; - } - - public static void ReplaceReference(string csproj_file, string reference_name, bool force) - { - bool bWasRemoved = false; - bool bRequiresSave = false; - var newname = new AssemblyNameInGAC(reference_name); - var stream = new MemoryStream(File.ReadAllBytes(csproj_file)); // cache file in memoty - var document = XDocument.Load(stream); - var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); - xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); - - // remove ProjectReference - var itemsToRemove2 = new List(); - IEnumerable listOfReferences2 = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:ProjectReference[@Include]", xmlNamespaceManager); - foreach (var el in listOfReferences2) - { - string assRef = el.Attribute("Include").Value; - if (string.Compare(newname.Name, assRef) == 0) + foreach (var item in itemsToRemove2) { - itemsToRemove2.Add(el); + Console.WriteLine($"ProjectReference removed {item.ToString()}"); + item.Remove(); + bWasRemoved = true; } - } - foreach (var item in itemsToRemove2) - { - Console.WriteLine($"ProjectReference removed {item.ToString()}"); - item.Remove(); - bWasRemoved = true; - } - var itemsToRemove = new List(); - IEnumerable listOfReferences = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:Reference[@Include]", xmlNamespaceManager); - foreach (var el in listOfReferences) - { - string assRef = el.Attribute("Include").Value; - var name = new AssemblyNameInGAC(assRef); - if (string.Compare(newname.Name, name.Name) == 0) + var itemsToRemove = new List(); + IEnumerable listOfReferences = document.XPathSelectElements("/ns:Project/ns:ItemGroup/ns:Reference[@Include]", xmlNamespaceManager); + foreach (var el in listOfReferences) { - itemsToRemove.Add(el); + string assRef = el.Attribute("Include").Value; + var name = new AssemblyNameInGAC(assRef); + if (string.Compare(newname.Name, name.Name) == 0) + { + itemsToRemove.Add(el); + } + } + foreach (var item in itemsToRemove) + { + Console.WriteLine($"Reference removed {item.ToString()}"); + item.Remove(); + bWasRemoved = true; } - } - foreach (var item in itemsToRemove) - { - Console.WriteLine($"Reference removed {item.ToString()}"); - item.Remove(); - bWasRemoved = true; - } - XElement group = document.XPathSelectElement("/ns:Project/ns:ItemGroup", xmlNamespaceManager); - if (group == null) - { - // insert new item group - throw new NotImplementedException(); - } - if (bWasRemoved) - { - bRequiresSave = true; + return bWasRemoved; } - // insert new reference - if (force || bWasRemoved) + public static bool RemoveReference(string csproj_file, string reference_name) { - XNamespace ns = document.Root.Name.Namespace; - var newEl = new XElement(ns + "Reference"); - newEl.Add(new XAttribute("Include", newname.Generate())); - group.Add(newEl); - bRequiresSave = true; + var stream = new MemoryStream(File.ReadAllBytes(csproj_file)); // cache file in memoty + var document = XDocument.Load(stream); + return RemoveReference(document, reference_name); } - if (bRequiresSave) + public static void ReplaceReference(string csproj_file, string reference_name, bool force) { - document.Save(csproj_file); + var stream = new MemoryStream(File.ReadAllBytes(csproj_file)); // cache file in memoty + var document = XDocument.Load(stream); + + bool bWasRemoved = RemoveReference(csproj_file, reference_name); + bool bRequiresSave = bWasRemoved; + + var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); + xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); + XElement group = document.XPathSelectElement("/ns:Project/ns:ItemGroup", xmlNamespaceManager); + if (group == null) + { + // insert new item group + throw new NotImplementedException(); + } + + // insert new reference + if (force || bWasRemoved) + { + XNamespace ns = document.Root.Name.Namespace; + var newEl = new XElement(ns + "Reference"); + var newname = new AssemblyNameInGAC(reference_name); + newEl.Add(new XAttribute("Include", newname.Generate())); + group.Add(newEl); + bRequiresSave = true; + } + + if (bRequiresSave) + { + document.Save(csproj_file); + } } } } diff --git a/mpt-core/04_CSProj/ReferencedAssembly.cs b/mpt-core/04_CSProj/ReferencedAssembly.cs new file mode 100644 index 0000000..244fc7b --- /dev/null +++ b/mpt-core/04_CSProj/ReferencedAssembly.cs @@ -0,0 +1,37 @@ +namespace BuildAutomation +{ + using System; + + public class ReferencedAssembly + { + string m_Specification; + string m_Package; + public ReferencedAssembly() + { + } + public ReferencedAssembly(string spec) + { + m_Specification = spec; + } + public ReferencedAssembly(string spec, string package) + { + m_Specification = spec; + m_Package = package; + } + public string AssemblyName + { + get + { + return m_Specification; + } + } + public string Package + { + get + { + return m_Package; + } + } + } +} + diff --git a/mpt-core/mpt-core.csproj b/mpt-core/mpt-core.csproj index 88c77a7..30d6ca8 100644 --- a/mpt-core/mpt-core.csproj +++ b/mpt-core/mpt-core.csproj @@ -71,6 +71,7 @@ + diff --git a/mpt-core/sln/SolutionTools.cs b/mpt-core/sln/SolutionTools.cs index 9e6d4ae..692447e 100644 --- a/mpt-core/sln/SolutionTools.cs +++ b/mpt-core/sln/SolutionTools.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using BuildAutomation; using CWDev.SLNTools.Core; public class SolutionTools diff --git a/mpt-csproj/Program.cs b/mpt-csproj/Program.cs index c282a0b..2d1e2f2 100644 --- a/mpt-csproj/Program.cs +++ b/mpt-csproj/Program.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using Mono.Options; +using BuildAutomation; namespace mptcsproj { @@ -17,6 +18,12 @@ enum ExitCode : int HelpOrVersion = 5, Exception = 6, } + enum Action : int + { + Change = 0, + Create = 1, + Delete = 2, + } public static int Main(string[] args) { try @@ -53,7 +60,7 @@ static ExitCode MainProcessing(string[] args) string base_dir = null; string dir = null; string reference_name = null; - bool bForceReferenceAppending = false; + Action referenceAction = Action.Change; string import_name = null; string version_string = null; string friend_assembly_name = null; @@ -92,9 +99,11 @@ static ExitCode MainProcessing(string[] args) { "remove-warnings-as-errors", b => remove_warnings_as_errors = b }, { "remove-signing", b => remove_signing = b }, // replace reference(s) in .csproj files - { "replace-reference=", str => reference_name = str }, + { "remove-reference=", str => { reference_name = str; referenceAction = Action.Delete; } }, + // replace reference(s) in .csproj files + { "replace-reference=", str => { reference_name = str; referenceAction = Action.Change; } }, // insert reference(s) in .csproj files - { "inject-reference=", str => { reference_name = str; bForceReferenceAppending = true; } }, + { "inject-reference=", str => { reference_name = str; referenceAction = Action.Create; } }, // insert project import into .csproj files { "inject-import=", str => { import_name = str; } }, // insert task for versioning into .csproj files @@ -246,11 +255,19 @@ static ExitCode MainProcessing(string[] args) } if (reference_name != null) { - Console.WriteLine($"Replacing reference {reference_name}"); + Console.WriteLine($"Processing reference {reference_name}"); foreach (var csproj_file in listOfCsproj) { Console.WriteLine($"in file {csproj_file}"); - ProjectTools.ReplaceReference(csproj_file, reference_name, bForceReferenceAppending); + if (referenceAction == Action.Delete) + { + ProjectTools.RemoveReference(csproj_file, reference_name); + } + else + { + bool bForceReferenceAppending = referenceAction == Action.Create; + ProjectTools.ReplaceReference(csproj_file, reference_name, bForceReferenceAppending); + } } } if (import_name != null) @@ -328,6 +345,7 @@ public static void ShowHelp() Console.WriteLine("\tmpt-csproj --remove-warnings-as-errors --dir=work --recursive"); Console.WriteLine("\tmpt-csproj --remove-warnings-as-errors --dir=work --recursive --as-unified-patch my.patch"); Console.WriteLine("\t\tremoves xml element true"); + Console.WriteLine("\tmpt-csproj --remove-reference=\"MyDll\""); Console.WriteLine("\tmpt-csproj --replace-reference=\"MyDll,Version,Culture,PubKeyToken\""); Console.WriteLine("\t\treplaces the reference for MyDll of given version"); Console.WriteLine("\tmpt-csproj --inject-reference=\"MyDll,Version,Culture,PubKeyToken\"");