powershell - 如何让 nuget (powershell) 在 objective-c sproj 文件中插入 <DependentUpon> 元素

标签 powershell nuget nuget-package envdte csproj

我正在为 Windows 服务编写一个 nuget 包,这个想法是我的同事可以创建一个 Windows 服务安装该包,并且将为他们设置所有默认日志记录设置、entlib 库和其他内部任务。

除了让我发疯的一件事外,我已经完成了几乎所有工作。

在我的 nuget 目录的内容文件夹中,我有 Service.cs 和 Service.Designer.cs,它们被添加到 objective-c sproj 但它们没有关联。

当我查看 csproj 文件时,我看到:

<Compile Include="Service.cs">
  <SubType>Component</SubType>
</Compile>
<Compile Include="Service.Designer.cs" />

但我想看:
<Compile Include="Service.cs">
  <SubType>Component</SubType>
</Compile>
<Compile Include="Service.Designer.cs">
  <DependentUpon>Service.cs</DependentUpon>
</Compile>

任何想法,很确定它会涉及 install.ps 脚本,但我的 powershell 技能不存在?

顺便说一句,nuget 可以用来删除/覆盖文件吗?到目前为止,它似乎只是跳过。

最佳答案

经过大约 2 天的 powershell 和 msbuild hell 之后,我终于得到了一个可行的解决方案。见下文。调用 project.Save() 的警告是至关重要的 - 如果没有这个,您将收到“检测到冲突的文件修改”警告。如果您先调用 project.Save() ,您只会在完成后被要求重新加载解决方案。

param($installPath, $toolsPath, $package, $project)

#save the project file first - this commits the changes made by nuget before this     script runs.
$project.Save()

#Load the csproj file into an xml object
$xml = [XML] (gc $project.FullName)

#grab the namespace from the project element so your xpath works.
$nsmgr = New-Object System.Xml.XmlNamespaceManager -ArgumentList $xml.NameTable
$nsmgr.AddNamespace('a',$xml.Project.GetAttribute("xmlns"))

#link the service designer to the service.cs
$node = $xml.Project.SelectSingleNode("//a:Compile[@Include='Service.Designer.cs']", $nsmgr)
$depUpon = $xml.CreateElement("DependentUpon", $xml.Project.GetAttribute("xmlns"))
$depUpon.InnerXml = "Service.cs"
$node.AppendChild($depUpon)

#link the settings file to the settings.designer.cs 
$settings = $xml.Project.SelectSingleNode("//a:None[@Include='ServiceSettings.settings']", $nsmgr)
$generator = $xml.CreateElement("Generator", $xml.Project.GetAttribute("xmlns"))
$generator.InnerXml = "SettingsSingleFileGenerator"
$settings.AppendChild($generator)

$LastGenOutput = $xml.CreateElement("LastGenOutput", $xml.Project.GetAttribute("xmlns"))
$LastGenOutput.InnerXml = "ServiceSettings.Designer.cs"
$settings.AppendChild($LastGenOutput)

#set the settings designer to be autogen
$settingsDesigner = $xml.Project.SelectSingleNode("//a:Compile[@Include='ServiceSettings.Designer.cs']", $nsmgr)
$autoGen = $xml.CreateElement("AutoGen", $xml.Project.GetAttribute("xmlns"))
$autoGen.InnerXml = "True"

$DesignTimeSharedInput = $xml.CreateElement("DesignTimeSharedInput", $xml.Project.GetAttribute("xmlns"))
$DesignTimeSharedInput.InnerXml = "True"

$AGDependentUpon = $xml.CreateElement("DependentUpon", $xml.Project.GetAttribute("xmlns"))
$AGDependentUpon.InnerXml = "ServiceSettings.settings"

$settingsDesigner.AppendChild($autoGen)
$settingsDesigner.AppendChild($DesignTimeSharedInput)
$settingsDesigner.AppendChild($AGDependentUpon)

#fix up the project installer.    
$projectInstallerRes = $xml.Project.SelectSingleNode("//a:EmbeddedResource[@Include='ProjectInstaller.resx']", $nsmgr)
$projectInstallerResDepUpon = $xml.CreateElement("DependentUpon", $xml.Project.GetAttribute("xmlns"))
$projectInstallerResDepUpon.InnerXml = "ProjectInstaller.cs"
$projectInstallerRes.AppendChild($projectInstallerResDepUpon)

$projectInstallerDesigner = $xml.Project.SelectSingleNode("//a:Compile[@Include='ProjectInstaller.Designer.cs']", $nsmgr)
$projectInstallerDesignerDepUpon = $xml.CreateElement("DependentUpon", $xml.Project.GetAttribute("xmlns"))
$projectInstallerDesignerDepUpon.InnerXml = "ProjectInstaller.cs"
$projectInstallerDesigner.AppendChild($projectInstallerDesignerDepUpon)

#delete the bundled program.cs file.
$prog = $xml.Project.SelectSingleNode("//a:Compile[@Include='Program.cs']", $nsmgr)
$prog.SelectSingleNode("..").RemoveChild($prog)

#delete the bundled service1 file
$oldServiceFile = $xml.Project.SelectSingleNode("//a:Compile[@Include='Service1.cs']", $nsmgr)
$oldServiceFile.SelectSingleNode("..").RemoveChild($oldServiceFile)

$oldServiceDesignerFile = $xml.Project.SelectSingleNode("//a:Compile[@Include='Service1.Designer.cs']", $nsmgr)
$oldServiceDesignerFile.SelectSingleNode("..").RemoveChild($oldServiceDesignerFile)

#save the changes.
$xml.Save($project.FullName)

无耻自堵:I'll do a full write up of the solution and issues I encountered on my blog cianm.com
希望这可以节省一些人的时间。

关于powershell - 如何让 nuget (powershell) 在 objective-c sproj 文件中插入 <DependentUpon> 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10842567/

相关文章:

windows - Json.net 无法在 visual studio 2012 中安装

.net - Toast 通知不适用于 Windows 秋季创作者更新

powershell - 为什么 PowerShell 重定向 >> 更改文本内容的格式?

powershell - 在命令行中找不到 Cmdlet,但在 ISE 中可用

.net - "Detected package downgrade"警告是什么意思?

visual-studio - 调试和发布 nuget 包本地存储库

powershell - 如何在包管理器控制台中将 "Update-Package"更改为以前的版本?

powershell - 在Powershell中从左到右抓取文件中每行的前10个字符

.net - Nuget:NU1001 无法解析依赖项 *。 (错误:...在 100000 毫秒后超时。)

asp.net-mvc - Nuget 包 : Use Different MVC Version When Available