0

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)?

YanivR
  • 61
  • 5
  • Run `restore` for your Solution and specify as package source only your local NuGet server - now NuGet should report all missing packages as it cannot download them – mu88 Aug 22 '22 at 06:50
  • Thanks for the reply. I need the report to include all missing packages and thier depedendencies (if they are missing too in my local repo). I don't think that restore will be able to do that since it doesn't know about the dependencies (since the package is missing) – YanivR Aug 22 '22 at 07:37
  • Then you're screwed which NuGet server are you using? I have some experience with BaGet and Artifactory - both offer mirroring packages from nuget.org. If your NuGet server offers the same, you could temporarily enable mirroring, run restore (to fetch all direct and transitive dependencies) and disable mirroring – mu88 Aug 22 '22 at 07:54
  • Interesting idea. I will check it. The problem is that each package that we add to the local artifactory needs to be approved first. Maybe there is a way to do that automatically. – YanivR Aug 22 '22 at 09:16

1 Answers1

0

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:

  • RequiredPackages.csv - The packages that are needed for the specified package
  • MissingPackages.csv - The missing packages in the target nuget repository

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).

YanivR
  • 61
  • 5