-
Notifications
You must be signed in to change notification settings - Fork 109
/
Program.cs
337 lines (294 loc) · 14.1 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Frameworks;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
namespace NuGet.Protocol.Samples
{
public class Program
{
public static async Task Main(string[] args)
{
Console.WriteLine("Listing package versions...");
await ListPackageVersionsAsync();
Console.WriteLine();
Console.WriteLine("Downloading package...");
await DownloadPackageAsync();
Console.WriteLine();
Console.WriteLine("Get package metadata...");
await GetPackageMetadataAsync();
Console.WriteLine();
Console.WriteLine("Searching packages...");
await SearchPackages();
Console.WriteLine();
Console.WriteLine("Creating a package...");
CreatePackage();
Console.WriteLine();
Console.WriteLine("Reading a package...");
ReadPackage();
Console.WriteLine();
Console.WriteLine("Pushing a package...");
await PushPackageAsync();
Console.WriteLine();
Console.WriteLine("Deleting a package...");
await DeletePackageAsync();
}
public static async Task ListPackageVersionsAsync()
{
// This code region is referenced by the NuGet docs. Please update the docs if you rename the region
// or move it to a different file.
#region ListPackageVersions
ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;
SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
FindPackageByIdResource resource = await repository.GetResourceAsync<FindPackageByIdResource>();
IEnumerable<NuGetVersion> versions = await resource.GetAllVersionsAsync(
"Newtonsoft.Json",
cache,
logger,
cancellationToken);
foreach (NuGetVersion version in versions)
{
Console.WriteLine($"Found version {version}");
}
#endregion
}
public static async Task DownloadPackageAsync()
{
// This code region is referenced by the NuGet docs. Please update the docs if you rename the region
// or move it to a different file.
#region DownloadPackage
ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;
SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
FindPackageByIdResource resource = await repository.GetResourceAsync<FindPackageByIdResource>();
string packageId = "Newtonsoft.Json";
NuGetVersion packageVersion = new NuGetVersion("12.0.1");
using MemoryStream packageStream = new MemoryStream();
await resource.CopyNupkgToStreamAsync(
packageId,
packageVersion,
packageStream,
cache,
logger,
cancellationToken);
Console.WriteLine($"Downloaded package {packageId} {packageVersion}");
using PackageArchiveReader packageReader = new PackageArchiveReader(packageStream);
NuspecReader nuspecReader = await packageReader.GetNuspecReaderAsync(cancellationToken);
Console.WriteLine($"Tags: {nuspecReader.GetTags()}");
Console.WriteLine($"Description: {nuspecReader.GetDescription()}");
#endregion
}
public static async Task GetPackageMetadataAsync()
{
// This code region is referenced by the NuGet docs. Please update the docs if you rename the region
// or move it to a different file.
#region GetPackageMetadata
ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;
SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
PackageMetadataResource resource = await repository.GetResourceAsync<PackageMetadataResource>();
IEnumerable<IPackageSearchMetadata> packages = await resource.GetMetadataAsync(
"Newtonsoft.Json",
includePrerelease: true,
includeUnlisted: false,
cache,
logger,
cancellationToken);
foreach (IPackageSearchMetadata package in packages)
{
Console.WriteLine($"Version: {package.Identity.Version}");
Console.WriteLine($"Listed: {package.IsListed}");
Console.WriteLine($"Tags: {package.Tags}");
Console.WriteLine($"Description: {package.Description}");
}
#endregion
}
public static async Task SearchPackages()
{
// This code region is referenced by the NuGet docs. Please update the docs if you rename the region
// or move it to a different file.
#region SearchPackages
ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;
SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
PackageSearchResource resource = await repository.GetResourceAsync<PackageSearchResource>();
SearchFilter searchFilter = new SearchFilter(includePrerelease: true);
IEnumerable<IPackageSearchMetadata> results = await resource.SearchAsync(
"json",
searchFilter,
skip: 0,
take: 20,
logger,
cancellationToken);
foreach (IPackageSearchMetadata result in results)
{
Console.WriteLine($"Found package {result.Identity.Id} {result.Identity.Version}");
}
#endregion
}
public static void CreatePackage()
{
// This code region is referenced by the NuGet docs. Please update the docs if you rename the region
// or move it to a different file.
//
// It is strongly recommended that NuGet packages are created using the official NuGet tooling and not
// using this low level API. There are a variety of characteristics important for a well-formed package
// and the latest version of tooling helps incorporate these best practices.
//
// For more information about creating NuGet packages, see the overview of the package creation workflow
// and the documentation for official pack tooling (e.g. using the dotnet CLI):
// https://docs.microsoft.com/en-us/nuget/create-packages/overview-and-workflow
// https://docs.microsoft.com/en-us/nuget/create-packages/creating-a-package-dotnet-cli
#region CreatePackage
PackageBuilder builder = new PackageBuilder();
builder.Id = "MyPackage";
builder.Version = new NuGetVersion("1.0.0-beta");
builder.Description = "My package created from the API.";
builder.Authors.Add("Sample author");
builder.DependencyGroups.Add(new PackageDependencyGroup(
targetFramework: NuGetFramework.Parse("netstandard1.4"),
packages: new[]
{
new PackageDependency("Newtonsoft.Json", VersionRange.Parse("10.0.1"))
}));
using FileStream outputStream = new FileStream("MyPackage.nupkg", FileMode.Create);
builder.Save(outputStream);
Console.WriteLine($"Saved a package to {outputStream.Name}");
#endregion
}
private static void ReadPackage()
{
// This code region is referenced by the NuGet docs. Please update the docs if you rename the region
// or move it to a different file.
#region ReadPackage
using FileStream inputStream = new FileStream("MyPackage.nupkg", FileMode.Open);
using PackageArchiveReader reader = new PackageArchiveReader(inputStream);
NuspecReader nuspec = reader.NuspecReader;
Console.WriteLine($"ID: {nuspec.GetId()}");
Console.WriteLine($"Version: {nuspec.GetVersion()}");
Console.WriteLine($"Description: {nuspec.GetDescription()}");
Console.WriteLine($"Authors: {nuspec.GetAuthors()}");
Console.WriteLine("Dependencies:");
foreach (var dependencyGroup in nuspec.GetDependencyGroups())
{
Console.WriteLine($" - {dependencyGroup.TargetFramework.GetShortFolderName()}");
foreach (var dependency in dependencyGroup.Packages)
{
Console.WriteLine($" > {dependency.Id} {dependency.VersionRange}");
}
}
Console.WriteLine("Files:");
foreach (var file in reader.GetFiles())
{
Console.WriteLine($" - {file}");
}
#endregion
}
public static async Task UseNuGetProtocolWithAnAuthenticatedFeed()
{
// This code region is referenced by the NuGet docs. Please update the docs if you rename the region
// or move it to a different file.
#region AuthenticatedFeed
ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;
SourceCacheContext cache = new SourceCacheContext();
var sourceUri = "https://contoso.privatefeed/v3/index.json";
var packageSource = new PackageSource(sourceUri)
{
Credentials = new PackageSourceCredential(
source: sourceUri,
username: "myUsername",
passwordText: "myVerySecretPassword",
isPasswordClearText: true,
validAuthenticationTypesText: null)
};
// If the `SourceRepository` is created with a `PackageSource`, the rest of APIs will consume the credentials attached to `PackageSource.Credentials`.
SourceRepository repository = Repository.Factory.GetCoreV3(packageSource);
PackageMetadataResource resource = await repository.GetResourceAsync<PackageMetadataResource>();
IEnumerable<IPackageSearchMetadata> packages = await resource.GetMetadataAsync(
"MyPackage",
includePrerelease: true,
includeUnlisted: false,
cache,
logger,
cancellationToken);
foreach (IPackageSearchMetadata package in packages)
{
Console.WriteLine($"Version: {package.Identity.Version}");
Console.WriteLine($"Listed: {package.IsListed}");
Console.WriteLine($"Tags: {package.Tags}");
Console.WriteLine($"Description: {package.Description}");
}
#endregion
}
private static async Task PushPackageAsync()
{
try
{
// This code region is referenced by the NuGet docs. Please update the docs if you rename the region
// or move it to a different file.
#region PushPackage
ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;
SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
PackageUpdateResource resource = await repository.GetResourceAsync<PackageUpdateResource>();
string apiKey = "my-api-key";
await resource.Push(
"MyPackage.nupkg",
symbolSource: null,
timeoutInSecond: 5 * 60,
disableBuffering: false,
getApiKey: packageSource => apiKey,
getSymbolApiKey: packageSource => null,
noServiceEndpoint: false,
skipDuplicate: false,
symbolPackageUpdateResource: null,
logger);
#endregion
}
catch (Exception e)
{
Console.WriteLine($"Pushing package failed: {e}");
}
}
private static async Task DeletePackageAsync()
{
try
{
// This code region is referenced by the NuGet docs. Please update the docs if you rename the region
// or move it to a different file.
#region DeletePackage
ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;
SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
PackageUpdateResource resource = await repository.GetResourceAsync<PackageUpdateResource>();
string apiKey = "my-api-key";
await resource.Delete(
"MyPackage",
"1.0.0-beta",
getApiKey: packageSource => apiKey,
confirm: packageSource => true,
noServiceEndpoint: false,
logger);
#endregion
}
catch (Exception e)
{
Console.WriteLine($"Deleting package failed: {e}");
}
}
}
}