I am facing an issue with PowerShell and NuGet. I have created a custom package, which also contains a license file in the lib directory. If I use the following code in an attempt to add a relative file so it would be added as a link (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)
I get an error that a file was not found, despite the proper relative translation.
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
If I use the original, non-relative path, I get a duplicate file error.
I started experimenting with different approaches and did eventually get it to work:
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
Cheers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With