Lie*_*ero 7 powershell command-line nuget
如何在visual studio外使用powershell或命令行检查指定包源(nuget服务器)中是否存在具有特定版本的nuget包?
我有私人NuGet服务器,我想推送自己的包.我在TFS构建期间自动创建了包.我想念的是检查包是否先前上传并发布到NuGet服务器.
我不知道您的私有 NuGet 服务器是否实现了与 nuget.org 相同的服务器 Api,但我会假设它确实如此。https://docs.microsoft.com/en-us/nuget/api/registration-base-url-resource描述了可用于检查包是否已发布并获取其版本的 API。与nuget list命令不同,它通过包 ID 执行精确匹配。
例如,假设您想知道 AjaxMin 包是否已发布。执行nuget list ajaxmin不是很有帮助:
C:\> nuget.exe list ajaxmin
BundleTransformer.MicrosoftAjax 1.10.0
Bundler.NET 1.5.7
CodeSlice.Web.Baler.Extensions.AjaxMinifier 0.2.0
combres 2.3.0.4
combres.log4net 2.3.0.4
combres.mvc 2.3.0.4
Delegate.AjaxMinBuilder 1.1.2
DotLessMinification 0.42.1
AjaxMin 5.14.5506.26202
NUglify 1.5.1
NUglify 1.5.13
Pvc.Ajaxmin 0.0.2
RequestReduce 1.8.76
WebMarkupMin.Core 2.6.0
WebMarkupMin.MsAjax 2.6.0
Web.Optimization.Bundles.AjaxMin 0.0.8
Web.Optimization.Bundles.YUI 0.0.8
C:\>
Run Code Online (Sandbox Code Playgroud)
是的,您可以过滤输出,但我觉得工作量太大。您可以通过使用服务器 Api 更精确。观察:
C:\> $url = 'https://api.nuget.org/v3/registration3-gz-semver2/ajaxmin/index.json'
C:\> $res = Invoke-RestMethod $url
C:\> $res.items.items.catalogEntry.version
4.12.4057.21792
4.13.4076.28499
4.19.4141.18463
4.30.4295.16112
4.36.4337.24224
4.37.4345.34101
4.39.4362.37207
4.40.4369.35534
4.41.4378.21434
4.42.4387.23950
4.43.4392.20083
4.44.4396.18853
4.45.4416.14249
4.46.4422.26284
4.47.4452.34008
4.48.4489.28432
4.49.4503.16524
4.50.4504.34801
4.51.4507.18296
4.52.4518.14738
4.53.4526.21623
4.54.4533.37029
4.55.4545.19776
4.56.4560.33404
4.58.4566.27257
4.59.4576.13504
4.60.4609.17023
4.61.4617.31171
4.62.4618.15628
4.63.4630.14654
4.64.4630.17932
4.66.4633.35991
4.67.4639.17289
4.68.4663.23906
4.69.4665.24361
4.70.4668.12892
4.71.4679.26350
4.72.4679.35523
4.73.4685.17669
4.74.4698.25434
4.75.4713.17606
4.76.4714.20550
4.77.4723.25304
4.78.4724.23869
4.80.4763.16598
4.81.4769.14860
4.82.4784.14537
4.83.4785.14876
4.84.4790.14417
4.85.4828.21154
4.86.4836.34222
4.89.4861.30057
4.90.4864.13402
4.91.4875.26882
4.92.4896.13361
4.93.4902.12739
4.94.4916.15482
4.95.4924.12392
4.96.4941.15389
4.97.4951.28483
5.0.5007.14585
5.1.5007.23730
5.2.5021.15814
5.3.5068.16463
5.4.5085.25629
5.5.5091.22839
5.6.5100.19204
5.7.5124.21499
5.8.5172.27710
5.9.5229.26438
5.10.5260.16959
5.11.5295.12309
5.12.5436.22734
5.13.5463.15282
5.14.5506.26202
C:\>
Run Code Online (Sandbox Code Playgroud)
我们得到了包的所有版本。现在,如果您想检查某个事先已知的版本是否已发布 - 您可以:
C:\> $url = 'https://api.nuget.org/v3/registration3-gz-semver2/ajaxmin/5.14.5506.26202.json'
C:\> $res = Invoke-RestMethod $url
C:\> $res
@id : https://api.nuget.org/v3/registration3-gz-semver2/ajaxmin/5.14.5506.26202.json
@type : {Package, http://schema.nuget.org/catalog#Permalink}
catalogEntry : https://api.nuget.org/v3/catalog0/data/2018.10.06.23.54.33/ajaxmin.5.14.5506.26202.json
listed : True
packageContent : https://api.nuget.org/v3-flatcontainer/ajaxmin/5.14.5506.26202/ajaxmin.5.14.5506.26202.nupkg
published : 2015-01-28T22:45:45.577+00:00
registration : https://api.nuget.org/v3/registration3-gz-semver2/ajaxmin/index.json
@context : @{@vocab=http://schema.nuget.org/schema#; xsd=http://www.w3.org/2001/XMLSchema#; catalogEntry=; registration=; packageContent=; published=}
C:\>
Run Code Online (Sandbox Code Playgroud)
现在如果我们要求一个不存在的版本会发生什么?开始了:
C:\> $url = 'https://api.nuget.org/v3/registration3-gz-semver2/ajaxmin/5.14.5506.26196.json'
C:\> $res = Invoke-RestMethod $url
Invoke-RestMethod : BlobNotFoundThe specified blob does not exist.
RequestId:cd1579e6-801e-007d-5eba-64ac9d000000
Time:2019-09-06T13:53:54.6832811Z
At line:1 char:8
+ $res = Invoke-RestMethod $url
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
C:\>
Run Code Online (Sandbox Code Playgroud)
要求一个不存在的包裹怎么样?让我们搜索ajaxmi(没有'n'):
C:\> $url = 'https://api.nuget.org/v3/registration3-gz-semver2/ajaxmi/index.json'
C:\> $res = Invoke-RestMethod $url
Invoke-RestMethod : BlobNotFoundThe specified blob does not exist.
RequestId:23d96274-b01e-0018-3bba-641dc0000000
Time:2019-09-06T13:55:35.5288968Z
At line:1 char:8
+ $res = Invoke-RestMethod $url
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
C:\>
Run Code Online (Sandbox Code Playgroud)
您可以从 PowerShell 轻松调用此 CMD 脚本,下面是示例。您可以直接$LastExitCode确定如何继续,这意味着0您可以发布:
@echo off
SetLocal EnableDelayedExpansion EnableExtensions
pushd "%~dp0"
set "pkg_name=%~1"
set "pkg_version=%~2"
call nuget list %pkg_name% -AllVersions -Prerelease|findstr /i /r /c:"^%pkg_name% %pkg_version%$" -N>nul 2>&1
if not "%errorlevel%"=="0" (
echo This package can be published
exit /b 0
) else (
echo This package has already been published.
exit /b 1
)
Run Code Online (Sandbox Code Playgroud)
C:\stuff>.\check_nupkg.bat "lolspeak" "1.0.0"
This package has already been published.
C:\stuff>check_nupkg.bat "lolspeak" "11.0.0"
This package can be published
| 归档时间: |
|
| 查看次数: |
4570 次 |
| 最近记录: |