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) { root.AppendChild(n); } else { while (locator.MoveToNext()) { } ; XmlNode sibling = locator.UnderlyingObject as XmlNode; root.InsertAfter(n, sibling); } bSaveRequired = true; }
public void FindAllTargets() { var xmlNamespaceManager = new XmlNamespaceManager(new NameTable()); xmlNamespaceManager.AddNamespace("ns", MSBuildFile.NamespaceName); XPathNavigator navigator = doc.CreateNavigator(); navigator.MoveToRoot(); 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) { 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 }
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 void InjectVersioning(string versionPropertyName) { /* * <Target Name="MyAssemblyVersion" Outputs="@(GeneratedVersion)"> * <MakeDir Directories="$(IntermediateOutputPath)" /> * <AssemblyInfo CodeLanguage="CS" * AssemblyCompany="MyCompanyName" * AssemblyCopyright="Copyright $(CompanyName), All rights reserved." * AssemblyVersion="12.34.56.78" * AssemblyFileVersion="3.3.3.3" * OutputFile="$(IntermediateOutputPath)file1.cs"> * <Output TaskParameter="OutputFile" ItemName="Compile" /> * </AssemblyInfo> * </Target> */ MSBuildTarget targ = this.UnderlyingObject.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("OutputFile", "$(IntermediateOutputPath)AssemblyVersion.Generated.cs"); { MSBuildTaskResultItem resultItem = task.CreateResultItem(); resultItem.TaskParameter = "OutputFile"; resultItem.ItemName = "Compile"; task.AppendResultItem(resultItem); } targ.AppendTask(task); } this.UnderlyingObject.EnsureTargetExists("BeforeBuild"); this.UnderlyingObject.InsertTarget(targ); this.UnderlyingObject.AddDependOnTarget("BeforeBuild", targ.Name); }
public void EnsureTargetExists(string targetName) { if (FindTarget(targetName) != null) { return; } MSBuildTarget targ = this.CreateTarget(); targ.Name = targetName; InsertTarget(targ); 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; }
// http://stackoverflow.com/questions/30943342/how-to-use-internalsvisibleto-attribute-with-strongly-named-assembly public void InjectInternalsVisibleTo(string assemblyName, string assemblyPublicKey) { MSBuildTarget targ = this.UnderlyingObject.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"); { 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); } this.UnderlyingObject.EnsureTargetExists("BeforeBuild"); this.UnderlyingObject.InsertTarget(targ); this.UnderlyingObject.AddDependOnTarget("BeforeBuild", targ.Name); }
public void InsertTarget(MSBuildTarget newTarget) { if (FindTarget(newTarget.Name) != null) { return; } // запомнить у себя this.targetNodes.Add(newTarget); // у тебя в руках узел, но оне вставленный в XML-документ XmlElement newXmlElement = newTarget.UnderlyingObject; XPathNavigator navigator = doc.CreateNavigator(); navigator.MoveToRoot(); XmlNode root = (XmlNode)navigator.UnderlyingObject; XmlNode project = root.LastChild; project.AppendChild(newXmlElement); bSaveRequired = true; }
public MSBuildTarget CreateTarget() { var result = new MSBuildTarget(this); return(result); }
public static XmlElement GetKeyForItem(MSBuildTarget item) { return(item.UnderlyingObject); }