Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a nuget source is already configured

I want to add a nuget package feed that is hosted on our TFS instance to all of our developer's workstations. The problem that I have is that if the source has already been added, I get an error stating The name specified has already been added to the list of available package sources. Please provide a unique name.

What I want to do is check if a nuget source has already been registered on the machine before I run the code to add the source. Checking the documentation for nuget.exe I tried to use the List operation along with the Name and Source but I just get the same result as if I just run nuget sources

All of these commands:

nuget sources list -Source $myURL
nuget sources list -Name $myName
nuget sources

Return the same result:

Registered Sources:

  1.  nuget.org [Enabled]
      https://api.nuget.org/v3/index.json
  2.  myPowershellFeed [Enabled]
    https://myURL.myDomain.org

I am using Powershell to run these commands and came up with a workaround, but ideally I am hoping there is a nuget.exe command line option that will get this info for me.

like image 841
Brandon McClure Avatar asked Oct 16 '18 20:10

Brandon McClure


People also ask

How do I know if a NuGet package is installed?

go to the Project or Solution in question. right click, Manage NuGet Packages... on the left, you will see 'Installed Packages' click on this and you will see the list.

What is the default NuGet package source?

The default is %userprofile%\. nuget\packages (Windows) or ~/. nuget/packages (Mac/Linux). A relative path can be used in project-specific nuget.

What is a NuGet source?

NuGet (pronounced "New Get") is a package manager designed to enable developers to share reusable code. It is a software as a service solution whose client app is free and open-source. The Outercurve Foundation initially created it under the name NuPack.


2 Answers

In PowerShell v5, you have access to the PackageManagement module. This includes a NuGet package provider:

$nuget = Get-PackageProvider -Name NuGet

Alongside this, you can access all your sources:

$nuget | Get-PackageSource

By default, this will only have nuget.org, but with your added source(s), you will see them from the result of this command as well. As a bonus, because it's a powershell command, it returns objects instead of strings, so you can do the following:

Get-PackageSource -Name myPowershellFeed |
    Format-List -Property * -Force

To address your Q&A:

if (-not $(Get-PackageSource -Name myPowershellFeed -ProviderName NuGet -ErrorAction Ignore))
{
    # add the packagesource
like image 92
Maximilian Burszley Avatar answered Oct 07 '22 05:10

Maximilian Burszley


You can use the follow line:

$nugetHasMyUrlSource =!!(nuget source | ? { $_ -like "*$myUrl"})

Or even encapsulate it in a function:

function HasNugetSource ($url){
    return !!(nuget source | ? { $_ -like "*$url"});
}
like image 34
Matheus Valiente Souza Avatar answered Oct 07 '22 05:10

Matheus Valiente Souza