Dotnetcore安装nuget包时不能使用content中的文件

问题:用NUGET打包了一个asp.netcore的项目,试图安装到另一个asp.netcore项目中,除了自动添加引用外,还希望自动释放一些文件以供修改。这些操作以前在netframe中是正常的,脚本如下

@@@code

<?xml version="1.0"?>

<package>

<metadata>

<id>Q.AspNetCore.Tpl</id>

<version>1.0.7</version>

<title>Q.AspNetCore.Tpl</title>

<authors>Zhangwenxiang</authors>

<owners>Zhangwenxiang</owners>

<licenseUrl>http://www.qoushui.com</licenseUrl>

<projectUrl>http://www.qoushui.com</projectUrl>

<iconUrl>http://www.qoushui.com</iconUrl>

<requireLicenseAcceptance>false</requireLicenseAcceptance>

<description>Q.AspNetCore.Tpl</description>

<releaseNotes>整理DOTNETCORE一般需要的文件</releaseNotes>

<copyright>zhangwenxiang</copyright>

<tags>Q.AspNetCore.Tpl</tags>

       

<dependencies>

<group>

<dependency id="AspNetCoreRateLimit" version="3.0.0" />

<dependency id="CSRedisCore" version="3.0.60" />

<dependency id="QCommon.TimeJob" version="1.0.9" />

<dependency id="System.Text.Encoding.CodePages" version="4.5.1" />

</group>

</dependencies>

</metadata>

<files>

<file src="binRelease*Q.AspNetCore.Tpl.dll" target="lib" />

<file src="*.cs" target="Content" />

<file src="Models*.cs" target="ContentModels" />

<file src="Controllers*.cs" target="ContentControllers" />

<file src="readme.txt" target="" />

<file src="tools*" target="tools" />

</files>

</package>

@@#

参考

说解决了,但根据他的方法,我并没有解决:

https://q.cnblogs.com/q/114299/

https://github.com/NuGet/Home/issues/6548

老外最后也只能出个提示,让你手工复制

参考

https://weblog.west-wind.com/posts/2018/Jan/29/Distributing-Content-and-Showing-a-ReadMe-file-in-a-NET-Core-Nuget-Package

     

       

链接文件(没试过,估计是以前netframe直接添加文件,更新包会覆盖可能修改过的文件的原因,所以搞了个LINK),参考

http://blog.stashev.com/linking-a-file-from-a-nuget-package/

       

弹出readme,功能正常,参考

https://weblog.west-wind.com/posts/2018/Jan/29/Distributing-Content-and-Showing-a-ReadMe-file-in-a-NET-Core-Nuget-Package

       

PS操纵项目文件参考

https://blog.csdn.net/weixin_34417200/article/details/85998228

       

       

PS的工作原理参考https://everydaylifein.net/netframework/running-powershell-scripts-during-nuget-package-installation-and-removal.html

     

       

最后没办法,添加了init.ps1用于复制文件,代码如下,该方法要求VS必须以管理员身份运行,但它工作也不是很稳定(只在首次安装时才执行)

@@@code

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

#$installPath = "C:Userszwx.nugetpackagesq.aspnetcore.tpl1.0.9"

#$projectItemPath = "D:CodeCardE云平台Y.OPSY.OPS.ClientProgram.cs"

Write-Host "hello"

$projectItemPath = " "

$projectPath = " "

if ($project)

{

    $projectItemPath = $project.ProjectItems.Item("Program.cs").Properties.Item("FullPath").Value

    $projectPath = [System.IO.Path]::GetDirectoryName($projectItemPath)

}else{

#$installPath = "C:Userszwx.nugetpackagesq.aspnetcore.tpl1.0.9 ools"

#$projectItemPath = "D:CodeCardE云平台Y.OPSY.OPS.ClientProgram.cs"

       

    $projectPath = [System.IO.Path]::GetDirectoryName($installPath)

    $installPath = Split-Path -Parent $MyInvocation.MyCommand.Definition

$x =New-Object -TypeName System.IO.DirectoryInfo -ArgumentList $installPath

$installPath = $x.Parent.FullName

}

       

Write-Host "从 $installPath 中复制文件至 $projectPath"

#Write-Host $installPath

#Write-Host $projectPath

#Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject

       

$contentPath = [System.IO.Path]::Combine($installPath,"content")

$len = $contentPath.Length+1;

ForEach($item in get-ChildItem $contentPath -Recurse)

{

if($item -is [System.IO.FileInfo]){

       

$targetPath=[System.IO.Path]::Combine($projectPath,$item.FullName.Substring($len))

if([System.IO.File]::Exists($targetPath)){

continue;

}

$tmpPath = [System.IO.Path]::GetDirectoryName($targetPath)

if(![System.IO.Directory]::Exists($tmpPath)){

[System.IO.Directory]::CreateDirectory($tmpPath)

}

       

[System.IO.File]::Copy($item.FullName,$targetPath,0)

        $info = $item.FullName.Substring($len)

Write-Host "copy file: $info"

}

       

}

@@#

效果图

       

       

不工作的时候切换到PM的控制台,会发现它试图工作,可手工执行,参考

https://docs.microsoft.com/zh-cn/nuget/tools/package-manager-console

       

附另一种build target的做法,会在项目中添加文件链接,生成时自动复制

参考nuget中的EMGU.CV包

@@@code

<?xml version="1.0" encoding="utf-8"?>

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>

<EmguCvLinkTarget>WindowsForms</EmguCvLinkTarget>

<EmguCvBuildX86 Condition="'$(Platform)'=='AnyCPU' OR '$(Platform)'=='x86'">True</EmguCvBuildX86>

<EmguCvBuildX64 Condition="'$(Platform)'=='AnyCPU' OR '$(Platform)'=='x64'">True</EmguCvBuildX64>

<EmguCvNativeFileX86>$(MSBuildThisFileDirectory)x86cvextern.dll</EmguCvNativeFileX86>

<EmguCvNativeFileX64>$(MSBuildThisFileDirectory)x64cvextern.dll</EmguCvNativeFileX64>

<EmguCvDeployMessage Condition="'$(EmguCvBuildX86)'=='True' AND Exists('$(EmguCvNativeFileX86)')">$(EmguCvDeployMessage)x86 </EmguCvDeployMessage>

<EmguCvErrorMessage Condition="'$(EmguCvBuildX86)'=='True' AND !Exists('$(EmguCvNativeFileX86)')">This package do not contain necessary binary for $(EmguCvLinkTarget). X86 is targeted, but file $(EmguCvNativeFileX86) is missing.</EmguCvErrorMessage>

<EmguCvDeployMessage Condition="'$(EmguCvBuildX64)'=='True' AND Exists('$(EmguCvNativeFileX64)')">$(EmguCvDeployMessage)x64 </EmguCvDeployMessage>

<EmguCvErrorMessage Condition="'$(EmguCvBuildX64)'=='True' AND !Exists('$(EmguCvNativeFileX64)')">This package do not contain necessary binary for $(EmguCvLinkTarget). X64 is targeted, but file $(EmguCvNativeFileX64) is missing.</EmguCvErrorMessage>

</PropertyGroup>

<ItemGroup Condition="'$(EmguCvBuildX64)'=='True' AND Exists('$(EmguCvNativeFileX64)')">

<None Include="$(MSBuildThisFileDirectory)x64*.dll">

<Link>x64\%(RecursiveDir)%(Filename)%(Extension)</Link>

<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

</None>

</ItemGroup>

<ItemGroup Condition="'$(EmguCvBuildX86)'=='True' AND Exists('$(EmguCvNativeFileX86)')">

<None Include="$(MSBuildThisFileDirectory)x86*.dll">

<Link>x86\%(RecursiveDir)%(Filename)%(Extension)</Link>

<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

</None>

</ItemGroup>

<Target Name="EmguCVPackageBuildImports" BeforeTargets="PrepareForBuild">

<Error Condition="'$(EmguCvLinkTarget)'==''" Text="Emgu CV is not able to detect the project type, unloading and reloading the project may fix this problem. If you are still having difficulty, please send an email to support@emgu.com" />

<Message Condition="'$(EmguCvLinkTarget)'!=''" Text="Emgu CV nuget package compiling against $(EmguCvLinkTarget)" Importance="High" />

<Error Text="'$(EmguCvErrorMessage)'" Condition="'$(EmguCvErrorMessage)'!=''" />

<Message Text="Emgu CV compiling with $(EmguCvDeployMessage)binary" Condition="'$(EmguCvDeployMessage)'!=''" Importance="High" />

<Message Text="No native binary is deployed by the Emgu CV project." Condition="'$(EmguCvDeployMessage)'==''" Importance="High" />

</Target>

</Project>

@@#

    

原文地址:https://www.cnblogs.com/QinQouShui/p/11008659.html