VS模板没有安装Nuget包

时间:2017-02-23 18:48:34

标签: visual-studio templates nuget vsix project-template

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

我按照此处列出的步骤进行了操作:https://docs.microsoft.com/en-us/nuget/visual-studio-extensibility/visual-studio-templates

以下是我的设置:

  • 我的vsix项目中的.nupkg个文件。如果我解压缩vsix,我会在Packages文件夹中看到它们。 (我在我原来的解决方案中从packages文件夹中复制了这些.nupkg文件。)

  • 我已在我的source.extension.vsixmanifest文件(在vsix项目中找到)中列出了每个Nuget包作为资产。

以下是一个例子:

<Asset Type="AutoMapper.5.2.0.nupkg" d:Source="File" Path="Packages\AutoMapper.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的引用都被破坏。

2 个答案:

答案 0 :(得分:2)

事实证明,您必须从模板中删除所有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)
}

答案 1 :(得分:1)

文件source.extension.vsixmanifest中的Assets部分应为:

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

您所遵循的说明似乎已过时。您可以参考similar issue了解详细信息。