diff --git a/mpt-core/03_msbuild/MSBuildFile.cs b/mpt-core/03_msbuild/MSBuildFile.cs index 355af36..5ea244a 100644 --- a/mpt-core/03_msbuild/MSBuildFile.cs +++ b/mpt-core/03_msbuild/MSBuildFile.cs @@ -234,8 +234,56 @@ public void InsertTarget(MSBuildTarget newTarget) XPathNavigator navigator = doc.CreateNavigator(); navigator.MoveToRoot(); - XmlElement root = (XmlElement)navigator.UnderlyingObject; - root.AppendChild(newXmlElement); + XmlNode root = (XmlNode)navigator.UnderlyingObject; + XmlNode project = root.LastChild; + project.AppendChild(newXmlElement); + + bSaveRequired = true; + } + + public void EnsureTargetExists(string targetName) + { + if (FindTarget(targetName) != null) + { + return; + } + 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) + { + throw new ApplicationException($"Target {followingTarget} not found"); + } + preceding.AddAfterTarget(following.Name); + + bSaveRequired = true; + } + + public void AddDependOnTarget(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) + { + throw new ApplicationException($"Target {followingTarget} not found"); + } + preceding.AddDependOnTarget(following.Name); bSaveRequired = true; } diff --git a/mpt-core/03_msbuild/MSBuildItem.cs b/mpt-core/03_msbuild/MSBuildItem.cs index 905528d..5f23feb 100644 --- a/mpt-core/03_msbuild/MSBuildItem.cs +++ b/mpt-core/03_msbuild/MSBuildItem.cs @@ -10,7 +10,7 @@ public class MSBuildItem public MSBuildItem(MSBuildItemGroup parent) { XmlDocument doc = parent.UnderlyingObject.OwnerDocument; - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "UndefilnedItemName", doc.NamespaceURI); + 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 1d4ad15..1fef884 100644 --- a/mpt-core/03_msbuild/MSBuildItemGroup.cs +++ b/mpt-core/03_msbuild/MSBuildItemGroup.cs @@ -16,7 +16,7 @@ public MSBuildItemGroup(ICanHaveItems parent) { //this.parent = parent; XmlDocument doc = parent.UnderlyingNode.OwnerDocument; - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "UndefilnedItemName", doc.NamespaceURI); + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "UndefilnedItemName", MSBuildFile.NamespaceName); } public MSBuildItem CreateItem() diff --git a/mpt-core/03_msbuild/MSBuildProperty.cs b/mpt-core/03_msbuild/MSBuildProperty.cs index 4b6486c..50b7aa5 100644 --- a/mpt-core/03_msbuild/MSBuildProperty.cs +++ b/mpt-core/03_msbuild/MSBuildProperty.cs @@ -8,12 +8,12 @@ public class MSBuildProperty public XmlElement UnderlyingObject { 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 Value { get { return uo.InnerText; } set { uo.InnerText = value; } } public MSBuildProperty(MSBuildPropertyGroup parent) { XmlDocument doc = parent.UnderlyingObject.OwnerDocument; - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "UndefilnedPropertyName", doc.NamespaceURI); + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "UndefilnedPropertyName", MSBuildFile.NamespaceName); } void SetName(string name) @@ -22,8 +22,9 @@ void SetName(string name) XmlElement oldItem = uo; XmlDocument doc = oldItem.OwnerDocument; // replace name - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, doc.NamespaceURI); - uo.Value = oldItem.Value; + 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) { @@ -34,8 +35,9 @@ void SetName(string name) { uo.AppendChild(child.CloneNode(true)); } - // what about node's text content ? - //uo.Value = oldItem.Value; - oldItem.ParentNode.ReplaceChild(uo, oldItem); + 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 6262e32..adf41d8 100644 --- a/mpt-core/03_msbuild/MSBuildPropertyGroup.cs +++ b/mpt-core/03_msbuild/MSBuildPropertyGroup.cs @@ -16,7 +16,7 @@ public MSBuildPropertyGroup(ICanHaveProperties parent) { //this.parent = parent; XmlDocument doc = parent.UnderlyingNode.OwnerDocument; - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "PropertyGroup", doc.NamespaceURI); + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "PropertyGroup", MSBuildFile.NamespaceName); } string GetCondition() diff --git a/mpt-core/03_msbuild/MSBuildTarget.cs b/mpt-core/03_msbuild/MSBuildTarget.cs index a6d8bde..3cdeb77 100644 --- a/mpt-core/03_msbuild/MSBuildTarget.cs +++ b/mpt-core/03_msbuild/MSBuildTarget.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Text; using System.Xml; public class MSBuildTarget : ICanHaveProperties, ICanHaveItems @@ -10,7 +11,7 @@ public class MSBuildTarget : ICanHaveProperties, ICanHaveItems List tasks = new List(); List items = new List(); - public string Name { get { return uo.Attributes["Name"].Value; } set { uo.Attributes["Name"].Value = value; } } + 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; } } @@ -67,4 +68,44 @@ public void AppendItem(MSBuildItem item) XmlNode tn = item.UnderlyingObject; uo.AppendChild(tn); } + + public readonly char[] TargetSeparators = new char[] { ';' }; + + void AppendListOfTargets(string attributeName, string nameToAdd) + { + 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(nameToAdd); + uo.SetAttribute(attributeName, res.ToString()); + } + + public void AddAfterTarget(string name) + { + AppendListOfTargets("AfterTargets", name); + } + + public void AddDependOnTarget(string name) + { + AppendListOfTargets("DependsOnTargets", name); + } } diff --git a/mpt-core/03_msbuild/MSBuildTask.cs b/mpt-core/03_msbuild/MSBuildTask.cs index 71146d0..005715d 100644 --- a/mpt-core/03_msbuild/MSBuildTask.cs +++ b/mpt-core/03_msbuild/MSBuildTask.cs @@ -26,7 +26,7 @@ public MSBuildTask(MSBuildTarget p) string name = "NoXmlElementName"; this.parent = p; XmlDocument doc = parent.UnderlyingObject.OwnerDocument; - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, doc.NamespaceURI); + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, MSBuildFile.NamespaceName); } void SetName(string name) @@ -35,8 +35,9 @@ void SetName(string name) XmlElement oldItem = uo; XmlDocument doc = oldItem.OwnerDocument; // replace name - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, doc.NamespaceURI); - uo.Value = oldItem.Value; + 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) { @@ -47,9 +48,10 @@ void SetName(string name) { uo.AppendChild(child.CloneNode(true)); } - // what about node's text content ? - //uo.Value = oldItem.Value; - oldItem.ParentNode.ReplaceChild(uo, oldItem); + if (oldItem.ParentNode != null) + { + oldItem.ParentNode.ReplaceChild(uo, oldItem); + } } public MSBuildTaskParameter CreateParameter() diff --git a/mpt-core/03_msbuild/MSBuildTaskParameter.cs b/mpt-core/03_msbuild/MSBuildTaskParameter.cs index bb88a73..d88b6a8 100644 --- a/mpt-core/03_msbuild/MSBuildTaskParameter.cs +++ b/mpt-core/03_msbuild/MSBuildTaskParameter.cs @@ -23,7 +23,7 @@ public MSBuildTaskParameter(MSBuildTask p) string name = "NoAttributeNameGiven"; this.parent = p; XmlDocument doc = parent.UnderlyingObject.OwnerDocument; - uo = (XmlAttribute)doc.CreateNode(XmlNodeType.Attribute, name, doc.NamespaceURI); + uo = (XmlAttribute)doc.CreateNode(XmlNodeType.Attribute, name, null/*MSBuildFile.NamespaceName*/); } void SetName(string name) @@ -31,8 +31,11 @@ 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, doc.NamespaceURI); + uo = (XmlAttribute)doc.CreateNode(XmlNodeType.Attribute, name, null/*MSBuildFile.NamespaceName*/); uo.Value = oldAttr.Value; - oldAttr.ParentNode.ReplaceChild(uo, oldAttr); + if (oldAttr.ParentNode != null) + { + oldAttr.ParentNode.ReplaceChild(uo, oldAttr); + } } } diff --git a/mpt-core/03_msbuild/MSBuildTaskResultItem.cs b/mpt-core/03_msbuild/MSBuildTaskResultItem.cs index fae1689..767b39f 100644 --- a/mpt-core/03_msbuild/MSBuildTaskResultItem.cs +++ b/mpt-core/03_msbuild/MSBuildTaskResultItem.cs @@ -15,14 +15,14 @@ public XmlElement UnderlyingObject } } - public string TaskParameter { get { return uo.Attributes["TaskParameter"].Value; } set { uo.Attributes["TaskParameter"].Value = value; } } - public string ItemName { get { return uo.Attributes["ItemName"].Value; } set { uo.Attributes["ItemName"].Value = value; } } + 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", doc.NamespaceURI); + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "Output", MSBuildFile.NamespaceName); } void SetName(string name) @@ -31,7 +31,7 @@ void SetName(string name) XmlElement oldItem = uo; XmlDocument doc = oldItem.OwnerDocument; // replace name - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, doc.NamespaceURI); + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, MSBuildFile.NamespaceName); uo.Value = oldItem.Value; // copy attributes foreach (XmlAttribute a in oldItem.Attributes) diff --git a/mpt-core/03_msbuild/MSBuildTaskResultProperty.cs b/mpt-core/03_msbuild/MSBuildTaskResultProperty.cs index ddc50bf..761c89c 100644 --- a/mpt-core/03_msbuild/MSBuildTaskResultProperty.cs +++ b/mpt-core/03_msbuild/MSBuildTaskResultProperty.cs @@ -22,7 +22,7 @@ public MSBuildTaskResultProperty(MSBuildTask p) { this.parent = p; XmlDocument doc = parent.UnderlyingObject.OwnerDocument; - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "Output", doc.NamespaceURI); + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, "Output", MSBuildFile.NamespaceName); } void SetName(string name) @@ -31,7 +31,7 @@ void SetName(string name) XmlElement oldItem = uo; XmlDocument doc = oldItem.OwnerDocument; // replace name - uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, doc.NamespaceURI); + uo = (XmlElement)doc.CreateNode(XmlNodeType.Element, name, MSBuildFile.NamespaceName); uo.Value = oldItem.Value; // copy attributes foreach (XmlAttribute a in oldItem.Attributes) diff --git a/mpt-core/04_CSProj/CSharpLibraryProject.cs b/mpt-core/04_CSProj/CSharpLibraryProject.cs index d521ea7..ce142d5 100644 --- a/mpt-core/04_CSProj/CSharpLibraryProject.cs +++ b/mpt-core/04_CSProj/CSharpLibraryProject.cs @@ -84,7 +84,9 @@ public void InjectVersioning(string versionPropertyName) } targ.AppendTask(task); } + 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 @@ -95,19 +97,38 @@ public void InjectInternalsVisibleTo(string assemblyName, string assemblyPublicK { 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"); + { + MSBuildTaskResultItem resultItem = task.CreateResultItem(); + resultItem.TaskParameter = "OutputFile"; + resultItem.ItemName = "Compile"; + task.AppendResultItem(resultItem); + } targ.AppendTask(task); } { 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); } + uo.EnsureTargetExists("BeforeBuild"); uo.InsertTarget(targ); + uo.AddDependOnTarget("BeforeBuild", targ.Name); } + } diff --git a/mpt-core/AssemblyName/PublicKeyUtils.cs b/mpt-core/AssemblyName/PublicKeyUtils.cs index ad2c9ae..a05dd82 100644 --- a/mpt-core/AssemblyName/PublicKeyUtils.cs +++ b/mpt-core/AssemblyName/PublicKeyUtils.cs @@ -29,7 +29,7 @@ public static string GetPublicKeyStringFromContainer(string AssemblyKeyContainer { byte[] publicKey2 = GetPublicKey(AssemblyKeyContainerName); string output2 = ByteArrayToString(publicKey2); - return ", PublicKey=" + output2; + return output2; } public static string GetPublicKeyStringFromFilename(string AssemblyOriginatorKeyFile) @@ -37,6 +37,6 @@ public static string GetPublicKeyStringFromFilename(string AssemblyOriginatorKey byte[] snk = File.ReadAllBytes(AssemblyOriginatorKeyFile); byte[] publicKey1 = GetPublicKey(snk); string output1 = ByteArrayToString(publicKey1); - return ", PublicKey=" + output1; + return output1; } } diff --git a/mpt-csproj/Program.cs b/mpt-csproj/Program.cs index 36449f2..c282a0b 100644 --- a/mpt-csproj/Program.cs +++ b/mpt-csproj/Program.cs @@ -21,7 +21,7 @@ public static int Main(string[] args) { try { - return MainProcessing(args); + return (int)MainProcessing(args); } catch (Exception ex) { @@ -39,7 +39,7 @@ public static int Main(string[] args) } return (int)ExitCode.Exception; } - public static int MainProcessing(string[] args) + static ExitCode MainProcessing(string[] args) { var verbose = (string)null; var remove_warnings_as_errors = (string)null; @@ -131,7 +131,7 @@ public static int MainProcessing(string[] args) if (!bFile && !bDir) { Console.WriteLine($"unknown parameter {strUnparsedParameter}"); - Environment.Exit((int)ExitCode.WrongUsage); + return ExitCode.WrongUsage; } } if (String.IsNullOrWhiteSpace(dir) == false) @@ -154,7 +154,7 @@ public static int MainProcessing(string[] args) } if (listOfCsproj.Count == 0) { - return (int)ExitCode.NoInputFileSpecified; + return ExitCode.NoInputFileSpecified; } if (list_outputs != null) { @@ -279,6 +279,10 @@ public static int MainProcessing(string[] args) } if (friend_assembly_name != null) { + if (String.IsNullOrWhiteSpace(AssemblyOriginatorKeyFile) && String.IsNullOrWhiteSpace(AssemblyKeyContainerName)) + { + return ExitCode.WrongUsage; + } Console.WriteLine($"Injecting version property {version_string}"); foreach (var csproj_file in listOfCsproj) { @@ -295,11 +299,11 @@ public static int MainProcessing(string[] args) { publicKey = PublicKeyUtils.GetPublicKeyStringFromContainer(AssemblyKeyContainerName); } - file.InjectInternalsVisibleTo(import_name, publicKey); + file.InjectInternalsVisibleTo(friend_assembly_name, publicKey); } } } - return (int)ExitCode.Success; + return ExitCode.Success; } public static void ShowVersion() { diff --git a/mpt-csproj/mpt-csproj.csproj b/mpt-csproj/mpt-csproj.csproj index 5643798..2bc6745 100644 --- a/mpt-csproj/mpt-csproj.csproj +++ b/mpt-csproj/mpt-csproj.csproj @@ -35,7 +35,7 @@ - +