3

我面临 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

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

4

1 回答 1

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

干杯。

于 2013-06-11T22:43:22.210 回答