Skip to content

Commit

Permalink
second iteration of debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
ArsenShnurkov committed Jan 21, 2017
1 parent 86afe4f commit 2f3dcbe
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 37 deletions.
52 changes: 50 additions & 2 deletions mpt-core/03_msbuild/MSBuildFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion mpt-core/03_msbuild/MSBuildItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

2 changes: 1 addition & 1 deletion mpt-core/03_msbuild/MSBuildItemGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 9 additions & 7 deletions mpt-core/03_msbuild/MSBuildProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
{
Expand All @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion mpt-core/03_msbuild/MSBuildPropertyGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
43 changes: 42 additions & 1 deletion mpt-core/03_msbuild/MSBuildTarget.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

public class MSBuildTarget : ICanHaveProperties, ICanHaveItems
Expand All @@ -10,7 +11,7 @@ public class MSBuildTarget : ICanHaveProperties, ICanHaveItems
List<MSBuildTask> tasks = new List<MSBuildTask>();
List<MSBuildItem> items = new List<MSBuildItem>();

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<MSBuildTask> Tasks { get { return tasks; } }
public XmlElement UnderlyingObject { get { return uo; } }
public XmlNode UnderlyingNode { get { return UnderlyingObject; } }
Expand Down Expand Up @@ -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);
}
}
14 changes: 8 additions & 6 deletions mpt-core/03_msbuild/MSBuildTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
{
Expand All @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions mpt-core/03_msbuild/MSBuildTaskParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ 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)
{
// 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);
}
}
}
8 changes: 4 additions & 4 deletions mpt-core/03_msbuild/MSBuildTaskResultItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions mpt-core/03_msbuild/MSBuildTaskResultProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions mpt-core/04_CSProj/CSharpLibraryProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

}
4 changes: 2 additions & 2 deletions mpt-core/AssemblyName/PublicKeyUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ 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)
{
byte[] snk = File.ReadAllBytes(AssemblyOriginatorKeyFile);
byte[] publicKey1 = GetPublicKey(snk);
string output1 = ByteArrayToString(publicKey1);
return ", PublicKey=" + output1;
return output1;
}
}
Loading

0 comments on commit 2f3dcbe

Please sign in to comment.