删除 NuGet 源(如果存在)

问题描述 投票:0回答:2

我正在尝试检测源是否已添加到配置文件中。如果已添加,我想将其删除。棘手的部分是我最初不知道源的名称,但知道路径。

使用

nuget sources list -configfile C:\Temp\NuGet.Config
我能够识别已添加的源,但这会返回一个字符串。

如何根据源路径判断源是否已添加,并提取源的名称以便将其删除?

一些伪代码:

nuget sources add -name "example.com" -source "https://nuget.example.com/nuget" -username "example" -password "example" -storepasswordincleartext -configfile C:\Temp\NuGet.Config

if source exists with path 'https://nuget.example.com/nuget':
    remove source
powershell nuget
2个回答
1
投票

所以我有同样的问题,dotnet CLI 不支持基于源删除,这有点痛苦。

我是这样解决的:

$feedUrl = "<YourFeedUrl>"
$sources = dotnet nuget list source
$sourceName = ""
$sources -split "`n" | %{
    if($_ -match '^\s+\d+\.\s+(.*)\s+\[Enabled\]$'){
        $sourceName = $matches[1]
    } elseif ($_ -match "^\s+$feedUrl$") {
        dotnet nuget remove source $sourceName
    }
}

此脚本按行分割输出。然后,它检查该行是否与源名称行的格式匹配,在这种情况下,它会保存该名称。然后,如果它遇到与提要 URL 匹配的行,它将删除具有先前保存的名称的源。

这依赖于

dotnet nuget list source -format detailed
,这是默认设置,在我的例子中至少输出一个这样格式化的列表:

Registered Sources:
  1.  nuget.org [Enabled]
      https://api.nuget.org/v3/index.json
  2.  Microsoft Visual Studio Offline Packages [Enabled]
      C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\

请注意,如果将来输出发生变化,或者您的 dotnet CLI 版本输出不同的内容,您可能需要调整正则表达式。


0
投票

Dotnet 不支持通过 url 删除源,并且别名可以不同。 所以对于它

命令的先决条件

Dotnet list 源提供了这种类型的输出,否则下面的 grep 命令将会改变

注册来源:

  1. nuget.org [已启用] https://api.nuget.org/v3/index.json
  2. Microsoft Visual Studio 离线包。 [启用]

建议的解决方案:(shell脚本)

1.使用 grep 命令来获取严格匹配大小写的名称,并删除所有存在的源名称

2.然后添加必要的源名称

#通过源名称删除所有源

sources=$(dotnet nuget list source | grep '\[Enabled\]' | awk '{print $2}')
    
echo "$sources" | xargs -I % dotnet nuget remove source %

#删除后添加必要的源

Dotnet nuget add source < source-url > -n < source-name >
© www.soinside.com 2019 - 2024. All rights reserved.