nugetnuget-server

Finding missing nuget packages in local repository


I have a local nuget repository (Nuget Server). I need to upload packages from Nuget.Org to my local repository for offline building.

How can I list all packages missing for some solution? I need it to list all missing packages and thier depedecies (if it's missing too).

Is there a way to do it with nuget command line, other tools, powershell script (any other way)?


Solution

  • Found the solution. The powershell command Find-Package can list the package and it's depedencies. Next we can use the same command on each of the required packages to verify they exists in the target nuget reposiroy.

    The following powershell script uses that command. It will enumerate the required packages for the specfied package (in nuget.org repository). And than check each package existence in the target repository. It will write to CSV files:

    For example, set $inputPackage to "BenchmarkDotNet", replace $inputPackage with you own repository, and the script will write all the missing packages (including BenchmarkDotNet itself if it's missing) that Benchmark dot net uses and are missing in your repository.

    $inputPackage = "<Package name>"
    $inputPackage = "<target package source url>" #"local nuget repository url"
    
    Write-Output "Enumerating needed packages"
    $packages = Find-Package -Source https://api.nuget.org/v3/index.json -Name $inputPackage -IncludeDependencies
    Write-Output "Writing needed packages to c:\temp\RequiredPackages.csv"
    $packages | Export-Csv .\RequiredPackages.csv
    
    #$packages = Import-Csv c:\temp\RequiredPackages.csv
    
    Write-Output "Checking package existance in local nuget repository"
    $missingPackages = @()
    foreach($package in $packages)
    {
        try {
            Find-Package -Source $targetNugetRepository -Name $package.Name -RequiredVersion $package.Version -ErrorAction Stop
        }
        catch {
            $missingPackages += $package
        }
    }
    
    $missingPackages | Export-Csv -Path .\MissingPackages.csv
    
    Write-Output "Done"
    

    The above sciprt can be used manually on the top level packages a solution needs (Or can be automated to read the projects files first).