【问题标题】:What's the easiest way to get a package from nuget.org into a private nuget server?将包从 nuget.org 获取到私有 nuget 服务器的最简单方法是什么?
【发布时间】:2015-12-29 20:01:55
【问题描述】:

我们正在考虑运行私有 NuGet 服务器。我知道像 ProGet 这样的产品具有从 NuGet 获取包并将它们放入私有源的功能。如果可能的话,我们对简单的方法更感兴趣。我使用 NuGet.Server 和 Visual Studio 构建并部署了 NuGet 服务器。我可以将 nupkg 文件放入我的包文件夹中,然后从我的私人服务器安装到 Visual Studio 项目中。我不得不手动下载 NuGet 包,然后复制 nupkg 文件。这是唯一的方法吗?似乎某些 NuGet 包不能作为独立包完全下载。我可以通过创建一个虚拟应用程序,从 nuget.org 安装包,然后复制 nupkg 文件来获得一个,但这似乎是做作。

任何运行私有 NuGet 服务器的人有更简洁的方式将包从 nuget.org“复制”到他们的私有服务器中?

TIA

【问题讨论】:

    标签: nuget nuget-package nuget-server


    【解决方案1】:

    过去遇到同样的问题,我用过这个Blog Post by Jon Galloway

    它包含一个“开箱即用”的脚本(现在在 Win7 上仔细检查)

    注意变量,因为默认脚本存储库是 Microsoft 的,并且限制为 500 个 nuget 包。

    另外,在下载软件包时,我注意到它们保存在“.Version.nupkg”中并且不包含完整的软件包名称(发生在 Nuget 官方存储库https://www.nuget.org/api/v2/

    编辑:

    根据您需要选择特定的 nuget 包,我添加了几行用于过滤包

          param(
      [parameter(Mandatory=$TRUE,Position=0)]
      $PackageFilterBy
      )
    # --- settings ---
    $feedUrlBase = "http://go.microsoft.com/fwlink/?LinkID=206669"
    # the rest will be params when converting to funclet
    $latest = $true
    $overwrite = $false
    $top = 500 #use $top = $null to grab all
    $destinationDirectory = join-path ([Environment]::GetFolderPath("MyDocuments")) "NuGetLocal"
    
    # --- locals ---
    $webClient = New-Object System.Net.WebClient
    
    # --- functions ---
    
    # download entries on a page, recursively called for page continuations
    function DownloadEntries {
     param ([string]$feedUrl) 
     $feed = [xml]$webClient.DownloadString($feedUrl)
     $entries =@()
    
    $filter = $PackageFilterBy.Split(',')
    
     foreach($item in $filter ){
     $feed.feed.entry | %{if($_.id -match "$item"){$entries += $_}}
     }
     $progress = 0
    
     foreach ($entry in $entries) {
        $url = $entry.content.src
        $fileName = $entry.properties.id + "." + $entry.properties.version + ".nupkg"
        $saveFileName = join-path $destinationDirectory $fileName
        $pagepercent = ((++$progress)/$entries.Length*100)
        if ((-not $overwrite) -and (Test-Path -path $saveFileName)) 
        {
            write-progress -activity "$fileName already downloaded" `
                           -status "$pagepercent% of current page complete" `
                           -percentcomplete $pagepercent
            continue
        }
        write-progress -activity "Downloading $fileName" `
                       -status "$pagepercent% of current page complete" `
                       -percentcomplete $pagepercent
    
        [int]$trials = 0
        do {
            try {
                $trials +=1
                $webClient.DownloadFile($url, $saveFileName)
                break
            } catch [System.Net.WebException] {
                write-host "Problem downloading $url `tTrial $trials `
                           `n`tException: " $_.Exception.Message
            }
        }
        while ($trials -lt 3)
      }
    
      $link = $feed.feed.link | where { $_.rel.startsWith("next") } | select href
      if ($link -ne $null) {
        # if using a paged url with a $skiptoken like 
        # http:// ... /Packages?$skiptoken='EnyimMemcached-log4net','2.7'
        # remember that you need to escape the $ in powershell with `
        return $link.href
      }
      return $null
    }  
    
    # the NuGet feed uses a fwlink which redirects
    # using this to follow the redirect
    function GetPackageUrl {
     param ([string]$feedUrlBase) 
     $resp = [xml]$webClient.DownloadString($feedUrlBase)
     return $resp.service.GetAttribute("xml:base")
    }
    
    # --- do the actual work ---
    
    # if dest dir doesn't exist, create it
    if (!(Test-Path -path $destinationDirectory)) { 
        New-Item $destinationDirectory -type directory 
    }
    
    # set up feed URL
    $serviceBase = GetPackageUrl($feedUrlBase)
    $feedUrl = $serviceBase + "Packages"
    if($latest) {
        $feedUrl = $feedUrl + "?`$filter=IsLatestVersion eq true"
        if($top -ne $null) {
            $feedUrl = $feedUrl + "&`$orderby=DownloadCount desc&`$top=$top"
        }
    }
    
    while($feedUrl -ne $null) {
        $feedUrl = DownloadEntries $feedUrl
    }
    

    使用脚本会是这样的:.\Script.ps1 jquery,Microsoft

    【讨论】:

    • 嗯...我看了这个。我需要选择特定包的能力。不是 powershell 开发人员,我不确定我是否有能力修改它,以便我可以传递包列表或包的名称以供下载。我必须在 ISE 中加载脚本,看看我是否能弄清楚。不确定这是否是我的答案,但到目前为止我收到的唯一回复。谢谢
    • 也许此编辑将帮助您满足您的需求
    • 我试过你的代码。没有足够的空间来发布错误,但是对于在提要中找到的每个包,我发现似乎是一个错误...有关错误消息的详细信息,请参阅上面的“答案”帖子。我不是 PS 开发人员,所以我不知道代码是怎么回事...
    • 使用 jquery,Microsoft 的参数进行一些调试:write-host Entries: $entries.count = Entries: 51。每个条目我都将输入参数写到 DownloadFile 并得到:DownloadFile to C:\Users\rs02130\Documents\NuGetLocal\..nupkg 所以 $url 和 $saveFileName 是空的。我会继续戳。也许我能想出点什么来。
    • 您说的完全正确,我应该更加谨慎地建议未经测试的代码。已编辑
    猜你喜欢
    • 2022-07-21
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    相关资源
    最近更新 更多