如何使用PowerShell将许可证文件添加为自定义NuGet包的一部分

时间:2013-06-11 21:57:54

标签: visual-studio powershell nuget

我遇到了PowerShell和NuGet的问题。我创建了一个自定义包,它还包含lib目录中的许可文件。如果我在尝试添加相关文件时使用以下代码,那么它将被添加为链接(install.ps1):

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

function PathToUri([string] $path)
{
    return new-object Uri('file://' + $path.Replace("%","%25").Replace("#","%23").Replace("$","%24").Replace("+","%2B").Replace(",","%2C").Replace("=","%3D").Replace("@","%40").Replace("~","%7E").Replace("^","%5E"))
}

function UriToPath([System.Uri] $uri)
{
    return [System.Uri]::UnescapeDataString( $uri.ToString() ).Replace([System.IO.Path]::AltDirectorySeparatorChar, [System.IO.Path]::DirectorySeparatorChar)
}

$licensePath = PathToUri( "$installPath\lib\Aspose.Pdf.lic" )
Write-Host "Lice $licensePath"
$projectPath = PathToUri( $project.FullName )
Write-Host "Proj $projectPath"
$relativePath = UriToPath( $projectPath.MakeRelativeUri($licensePath) )
Write-Host "Rele $relativePath"

$project.ProjectItems.AddFromFile($relativePath)

我收到一个错误,即找不到文件,尽管有正确的相对翻译。

Lice file:///T:/ConsoleApplication2/packages/Aspose.PDF.7.4.0/lib/Aspose.Pdf.lic
Proj file:///T:/ConsoleApplication2/ConsoleApplication2/ConsoleApplication2.csproj
Rele ..\packages\Aspose.PDF.7.4.0\lib\Aspose.Pdf.lic
Exception calling "AddFromFile" with "1" argument(s): "Cannot add the link because the source file '..\packages\Aspose.PDF.7.4.0\lib\Aspose.Pdf.lic' cannot be found."
At T:\ConsoleApplication2\packages\Aspose.PDF.7.4.0\tools\Install.ps1:20 char:1
+ $project.ProjectItems.AddFromFile($relativePath)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

如果我使用原始的非相对路径,则会出现重复的文件错误。

1 个答案:

答案 0 :(得分:2)

我开始尝试不同的方法,最终让它发挥作用:

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

pushd $project.Properties.Item("FullPath").Value
$project.ProjectItems.AddFromFile( "$installPath\lib\Aspose.Pdf.lic" )
$project.ProjectItems.Item("Aspose.Pdf.Lic").Properties.Item("CopyToOutputDirectory").Value = 2
popd

干杯。