VS 模板未安装 Nuget 包

VS Template not installing Nuget Packages

我正在为我的公司构建一个 Visual Studio 2015 Web API 2 模板。它是一个多项目模板。我已经完成了所有设置并且工作正常,除了 Nuget 包。他们不会安装。

我按照此处概述的步骤操作:https://docs.microsoft.com/en-us/nuget/visual-studio-extensibility/visual-studio-templates

这是我的设置:

这是一个例子:

<Asset Type="AutoMapper.5.2.0.nupkg" d:Source="File" Path="Packages\AutoMapper.5.2.0.nupkg" d:VsixSubPath="Packages" />

这是我的一个子项目的示例(第一个向导是我的 vsix 项目):

<WizardExtension>
    <Assembly>MyVsixProject, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=52156a0ac017d515</Assembly>
    <FullClassName>MyVsixProject.WizardImplementation</FullClassName>
</WizardExtension>
<WizardExtension>
    <Assembly>NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
    <FullClassName>NuGet.VisualStudio.TemplateWizard</FullClassName>
</WizardExtension>
<WizardData>
    <packages repository="extension" repositoryId="MyVsixProject.8b0b584a-d7ab-4608-e317-84e1aa773a01">
        <package id="AutoMapper" version="5.2.0" />            
        <package id="EntityFramework" version="6.1.3" />
        <package id="Microsoft.Net.Compilers" version="1.3.2" />
        <package id="SimpleInjector" version="3.2.0" />            
    </packages>
</WizardData>

据我所知,我正在按照说明进行操作。

但是,当我 运行 模板时,解决方案级文件夹没有 packages 文件夹,我所有基于 nuget 的引用都被破坏了。

文件 source.extension.vsixmanifest 中的资产部分应该是:

<Asset d:Source="File" Type="AutoMapper.5.2.0.nupkg" Path="Packages\AutoMapper.5.2.0.nupkg" d:VsixSubPath="Packages" />

您遵循的说明似乎已过时。详情可参考

事实证明,您必须从模板中删除所有 Nuget 工件。 (就好像它从未安装过一样。)然后 Nuget 向导将正确安装。

我上面提到的 link 将其列为 "Best Practice" 而不是 "It will not work if you don't do it",就像它确实是的那样。

这是我在我的构建引擎中 运行 用于从我的工作项目中删除 nuget 内容的脚本:

# DO NOT CALL THIS DIRECTLY!  IT SHOULD ONLY BE CALLED BY THE BUILD SYSTEM.
# IF YOU CALL THIS DIRECTLY then the projects will have all of their nuget references removed.
#
# For the template to be able to install the nuget stuff, it needs it to be in a state as if
# it has never been installed.  This script removes:
#     • The packages.config files
#     • Removes <None Include="packages.config" /> from any csproj files
#     • Compiler references that are added by nuget
#     • Removes any references from csproj files that have a hint path that contains '..\packages\

# Find all packages.config files and delete them
get-childitem ./ -include packages.config -recurse | foreach ($_) {remove-item $_.fullname -force}

# Find all .csproj files 
$csProjFiles = get-childitem ./ -include *.csproj -recurse 

# Remove the packages.config include from the csproj files.
$csProjFiles | foreach ($_) {$currentFile = $_; (get-content $_) | Where-Object {$_ -notmatch 'Include="packages.config"'} | Set-Content $currentFile -force}

# Remove any compiler references added by the compiler nuget packages.
$csProjFiles | foreach ($_) {$currentFile = $_; (get-content $_) | Where-Object {$_ -notmatch 'build\Microsoft.Net.Compilers.props'} | Set-Content $currentFile -force}
$csProjFiles | foreach ($_) {$currentFile = $_; (get-content $_) | Where-Object {$_ -notmatch 'build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'} | Set-Content $currentFile -force}

# Remove any references that have a hint path that contains '..\packages\'
foreach ($csProjFile in $csProjFiles){
    [xml] $csProjFileXml = get-content $csProjFile;
    foreach ($itemGroup in $csProjFileXml.Project.ItemGroup) {  
        foreach ($reference in $itemGroup.Reference) {
            foreach ($hintPath in $reference.HintPath){
                if ($hintPath -like '*..\packages\*'){
                    $itemGroup.RemoveChild($reference)
                }           
            }           
        }
    }
    $csProjFileXml.Save($csProjFile)
}