VS模板未安装Nuget软件包



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

我遵循此处概述的步骤:https://learn.microsoft.com/en-us/nuget/visual-studio-extensenibility/visual-studio-templates。

这是我设置的:

  • 我的VSIX项目中的.nupkg文件。如果我解压缩VSIX,我会在包装夹中看到它们。(我从我制作的原始解决方案中的包装文件夹中复制了这些.nupkg文件。)

  • 我已将每个Nuget软件包列为source.extension.vsixmanifest文件中的资产(在VSIX项目中找到)。

这是一个示例:

<Asset Type="AutoMapper.5.2.0.nupkg" d:Source="File" Path="PackagesAutoMapper.5.2.0.nupkg" d:VsixSubPath="Packages" />
  • 在我的子项目.VSTEMPLATE文件中,我添加了Nuget向导,并列出了该模板所需的软件包。

这是我的一个子项目之一的一个示例(第一个向导是我的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>

我可以告诉我,我正在按照说明。

但是,当我运行模板时,解决方案级别文件夹没有包装夹,并且我所有基于Nuget的参考文献都被打破了。

事实证明,您必须将所有Nuget伪像从模板中消失。(好像从未安装。)Nuget向导将正确安装。

我上面提到的链接将其列为"最佳实践",而不是"如果您不这样做,它将不起作用",就像它的确一样。

这是我在构建引擎中运行的脚本,可以从我的工作项目中删除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)
}

文件源的资产部分。

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

您遵循的说明似乎过时了。您可以参考类似的问题以获取详细信息。

最新更新