簡體   English   中英

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

這是我設置的內容:

  • 我的vsix項目中的.nupkg文件。 如果我解壓縮vsix,我會在Packages文件夾中看到它們。 (我在我原來的解決方案中從packages文件夾中復制了這些.nupkg文件。)

  • 我列出了每一個NuGet包作為一種資產在我source.extension.vsixmanifest文件(在VSIX項目中找到)。

這是一個例子:

<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的引用都被破壞。

事實證明,您必須從模板中刪除所有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)
}

文件source.extension.vsixmanifest中的Assets部分應該是:

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

您遵循的說明似乎已過時。 您可以參考類似的問題以獲取詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM