以下代码来自名为 CodeAssassin.WixWebProjectReferences 的包。它在安装和卸载时向项目文件添加和删除以下导入标签。该软件包不需要任何依赖项。
<Import Project="..\packages\CodeAssassin.WixWebProjectReferences.1.0\tools\CodeAssassin.WixWebProjectReferences.targets" />
下载包并使用NuGetPackageExplorer打开它以了解它是如何完成的。
下面是来自 install.ps1 和 uninstall.ps1 的代码(它们仅在 NuGet 包的内容文件夹非空时执行)。
(我找不到任何 powershell 高亮显示,所以我改用 php,但它并不完美。)
安装.ps1
param (
$InstallPath,
$ToolsPath,
$Package,
$Project
)
$TargetsFile = 'CodeAssassin.WixWebProjectReferences.targets'
$TargetsPath = $ToolsPath | Join-Path -ChildPath $TargetsFile
Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
$MSBProject = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($Project.FullName) |
Select-Object -First 1
$ProjectUri = New-Object -TypeName Uri -ArgumentList "file://$($Project.FullName)"
$TargetUri = New-Object -TypeName Uri -ArgumentList "file://$TargetsPath"
$RelativePath = $ProjectUri.MakeRelativeUri($TargetUri) -replace '/','\'
$ExistingImports = $MSBProject.Xml.Imports |
Where-Object { $_.Project -like "*\$TargetsFile" }
if ($ExistingImports) {
$ExistingImports |
ForEach-Object {
$MSBProject.Xml.RemoveChild($_) | Out-Null
}
}
$MSBProject.Xml.AddImport($RelativePath) | Out-Null
$Project.Save()
卸载.ps1
param (
$InstallPath,
$ToolsPath,
$Package,
$Project
)
$TargetsFile = 'CodeAssassin.WixWebProjectReferences.targets'
Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
$MSBProject = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($Project.FullName) |
Select-Object -First 1
$ExistingImports = $MSBProject.Xml.Imports |
Where-Object { $_.Project -like "*\$TargetsFile" }
if ($ExistingImports) {
$ExistingImports |
ForEach-Object {
$MSBProject.Xml.RemoveChild($_) | Out-Null
}
$Project.Save()
}
将一些文件复制到输出路径的示例目标文件
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<ItemGroup>
<Files Include="..\packages\Insert_Path_To_Your_Package_Folder_Here\bin\*" />
</ItemGroup>
<Target Name="Insert_Name_of_Your_Target_Here" AfterTargets="AfterBuild">
<Copy SourceFiles="@(Files)" DestinationFolder="$(TargetDir)\bin\" SkipUnchangedFiles="true" />
</Target>
</Project>