forked from chocolatey-community/chocolatey-oneget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChocolateyPackageProvider.cs
376 lines (321 loc) · 17.1 KB
/
ChocolateyPackageProvider.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using OneGet.Sdk;
namespace OneGet
{
/// <summary>
/// A Package provider for OneGet.
///
/// Important notes:
/// - Required Methods: Not all methods are required; some package providers do not support some features. If the methods isn't used or implemented it should be removed (or commented out)
/// - Error Handling: Avoid throwing exceptions from these methods. To properly return errors to the user, use the request.Error(...) method to notify the user of an error conditionm and then return.
/// - Communicating with the HOST and CORE: each method takes a Request (in reality, an alias for System.Object), which can be used in one of two ways:
/// - use the c# 'dynamic' keyword, and call functions on the object directly.
/// - use the <code><![CDATA[ .As<Request>() ]]></code> extension method to strongly-type it to the Request type (which calls upon the duck-typer to generate a strongly-typed wrapper). The strongly-typed wrapper also implements several helper functions to make using the request object easier.
///
/// TODO: Give this class a proper name
/// </summary>
public class ChocolateyPackageProvider
{
/// <summary>
/// The features that this package supports.
/// TODO: fill in the feature strings for this provider
/// </summary>
protected static Dictionary<string, string[]> Features = new Dictionary<string, string[]> {
// specify the extensions that your provider uses for its package files (if you have any)
{ Constants.Features.SupportedExtensions, new[]{"nupkg"}},
// you can list the URL schemes that you support searching for packages with
{ Constants.Features.SupportedSchemes, new [] {"http", "https", "file"}},
#if FOR_EXAMPLE
// add this if you want to 'hide' your provider by default.
{ Constants.Features.AutomationOnly, Constants.FeaturePresent },
// you can list the magic signatures (bytes at the beginning of a file) that we can use
// to peek and see if a given file is yours.
{ Constants.Features.MagicSignatures, Constants.Signatures.ZipVariants},
#endif
};
/// <summary>
/// Returns the name of the Provider.
/// TODO: Test and verify that we want "choco" vs "chocolatey"
/// </summary>
/// <returns>The name of this provider</returns>
public string PackageProviderName
{
get { return "Choco"; }
}
/// <summary>
/// Returns the version of the Provider.
/// </summary>
/// <returns>The version of this provider </returns>
public string ProviderVersion
{
get
{
return "1.0.0.0";
}
}
/// <summary>
/// This is just here as to give us some possibility of knowing when an unexception happens...
/// At the very least, we'll write it to the system debug channel, so a developer can find it if they are looking for it.
/// </summary>
public void OnUnhandledException(string methodName, Exception exception)
{
Debug.WriteLine("Unexpected Exception thrown in '{0}::{1}' -- {2}\\{3}\r\n{4}", PackageProviderName, methodName, exception.GetType().Name, exception.Message, exception.StackTrace);
}
/// <summary>
/// Performs one-time initialization of the $provider.
/// </summary>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
public void InitializeProvider(Request request)
{
// TODO: improve this debug message that tells what's going on.
request.Debug("Calling '{0}::InitializeProvider'", PackageProviderName);
// TODO: add any one-time initialization code here, or remove this method
}
/// <summary>
/// Returns a collection of strings to the client advertizing features this provider supports.
/// </summary>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
public void GetFeatures(Request request)
{
// TODO: improve this debug message that tells what's going on.
request.Debug("Calling '{0}::GetFeatures' ", PackageProviderName);
foreach (var feature in Features)
{
request.Yield(feature);
}
}
/// <summary>
/// Returns dynamic option definitions to the HOST
///
/// example response:
/// request.YieldDynamicOption( "MySwitch", OptionType.String.ToString(), false);
///
/// </summary>
/// <param name="category">The category of dynamic options that the HOST is interested in</param>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
public void GetDynamicOptions(string category, Request request)
{
// TODO: improve this debug message that tells us what's going on.
request.Debug("Calling '{0}::GetDynamicOptions' {1}", PackageProviderName, category);
switch ((category ?? string.Empty).ToLowerInvariant())
{
case "install":
// TODO: put the options supported for install/uninstall/getinstalledpackages
break;
case "provider":
// TODO: put the options used with this provider (so far, not used by OneGet?).
break;
case "source":
// TODO: put any options for package sources
break;
case "package":
// TODO: put any options used when searching for packages
break;
default:
request.Debug("Unknown category for '{0}::GetDynamicOptions': {1}", PackageProviderName, category);
break;
}
}
/// <summary>
/// Resolves and returns Package Sources to the client.
///
/// Specified sources are passed in via the request object (<c>request.GetSources()</c>).
///
/// Sources are returned using <c>request.YieldPackageSource(...)</c>
/// </summary>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
public void ResolvePackageSources(Request request)
{
// TODO: improve this debug message that tells us what's going on.
request.Debug("Calling '{0}::ResolvePackageSources'", PackageProviderName);
// TODO: resolve package sources
if (request.Sources.Any())
{
// the system is requesting sources that match the values passed.
// if the value passed can be a legitimate source, but is not registered, return a package source marked unregistered.
}
else
{
// the system is requesting all the registered sources
}
}
/// <summary>
/// This is called when the user is adding (or updating) a package source
/// </summary>
/// <param name="name">The name of the package source. If this parameter is null or empty the PROVIDER should use the location as the name (if the PROVIDER actually stores names of package sources)</param>
/// <param name="location">The location (ie, directory, URL, etc) of the package source. If this is null or empty, the PROVIDER should use the name as the location (if valid)</param>
/// <param name="trusted">A boolean indicating that the user trusts this package source. Packages returned from this source should be marked as 'trusted'</param>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
public void AddPackageSource(string name, string location, bool trusted, Request request)
{
// TODO: improve this debug message that tells us what's going on.
request.Debug("Calling '{0}::AddPackageSource' '{1}','{2}','{3}'", PackageProviderName, name, location, trusted);
// TODO: support user-defined package sources OR remove this method
}
/// <summary>
/// Removes/Unregisters a package source
/// </summary>
/// <param name="name">The name or location of a package source to remove.</param>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
public void RemovePackageSource(string name, Request request)
{
// TODO: improve this debug message that tells us what's going on.
request.Debug("Calling '{0}::RemovePackageSource' '{1}'", PackageProviderName, name);
// TODO: support user-defined package sources OR remove this method
}
/// <summary>
/// Searches package sources given name and version information
///
/// Package information must be returned using <c>request.YieldPackage(...)</c> function.
/// </summary>
/// <param name="name">a name or partial name of the package(s) requested</param>
/// <param name="requiredVersion">A specific version of the package. Null or empty if the user did not specify</param>
/// <param name="minimumVersion">A minimum version of the package. Null or empty if the user did not specify</param>
/// <param name="maximumVersion">A maximum version of the package. Null or empty if the user did not specify</param>
/// <param name="id">if this is greater than zero (and the number should have been generated using <c>StartFind(...)</c>, the core is calling this multiple times to do a batch search request. The operation can be delayed until <c>CompleteFind(...)</c> is called</param>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
public void FindPackage(string name, string requiredVersion, string minimumVersion, string maximumVersion, int id, Request request)
{
// TODO: improve this debug message that tells us what's going on.
request.Debug("Calling '{0}::FindPackage' '{1}','{2}','{3}','{4}'", PackageProviderName, requiredVersion, minimumVersion, maximumVersion, id);
// TODO: find package by name (and version) or id...
}
/// <summary>
/// Finds packages given a locally-accessible filename
///
/// Package information must be returned using <c>request.YieldPackage(...)</c> function.
/// </summary>
/// <param name="file">the full path to the file to determine if it is a package</param>
/// <param name="id">if this is greater than zero (and the number should have been generated using <c>StartFind(...)</c>, the core is calling this multiple times to do a batch search request. The operation can be delayed until <c>CompleteFind(...)</c> is called</param>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
public void FindPackageByFile(string file, int id, Request request)
{
// TODO: improve this debug message that tells us what's going on.
request.Debug("Calling '{0}::FindPackageByFile' '{1}','{2}'", PackageProviderName, file, id);
// TODO: implement searching for a package by analyzing the package file, or remove this method
}
/// <summary>
/// Finds packages given a URI.
///
/// The function is responsible for downloading any content required to make this work
///
/// Package information must be returned using <c>request.YieldPackage(...)</c> function.
/// </summary>
/// <param name="uri">the URI the client requesting a package for.</param>
/// <param name="id">if this is greater than zero (and the number should have been generated using <c>StartFind(...)</c>, the core is calling this multiple times to do a batch search request. The operation can be delayed until <c>CompleteFind(...)</c> is called</param>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
public void FindPackageByUri(Uri uri, int id, Request request)
{
// TODO: improve this debug message that tells us what's going on.
request.Debug("Calling '{0}::FindPackageByUri' '{1}','{2}'", PackageProviderName, uri, id);
// TODO: implement searching for a package by it's unique uri (or remove this method)
}
/// <summary>
/// Downloads a remote package file to a local location.
/// </summary>
/// <param name="fastPackageReference"></param>
/// <param name="location"></param>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
public void DownloadPackage(string fastPackageReference, string location, Request request)
{
// TODO: improve this debug message that tells us what's going on.
request.Debug("Calling '{0}::DownloadPackage' '{1}','{2}'", PackageProviderName, fastPackageReference, location);
// TODO: actually download the package ...
}
/// <summary>
/// Returns package references for all the dependent packages
/// </summary>
/// <param name="fastPackageReference"></param>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
public void GetPackageDependencies(string fastPackageReference, Request request)
{
// TODO: improve this debug message that tells us what's going on.
request.Debug("Calling '{0}::GetPackageDependencies' '{1}'", PackageProviderName, fastPackageReference);
// TODO: check dependencies
}
/// <summary>
/// Installs a given package.
/// </summary>
/// <param name="fastPackageReference">A provider supplied identifier that specifies an exact package</param>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
public void InstallPackage(string fastPackageReference, Request request)
{
// TODO: improve this debug message that tells us what's going on.
request.Debug("Calling '{0}::InstallPackage' '{1}'", PackageProviderName, fastPackageReference);
// TODO: Install the package
}
/// <summary>
/// Uninstalls a package
/// </summary>
/// <param name="fastPackageReference"></param>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
public void UninstallPackage(string fastPackageReference, Request request)
{
// TODO: improve this debug message that tells us what's going on.
request.Debug("Calling '{0}::UninstallPackage' '{1}'", PackageProviderName, fastPackageReference);
// TODO: Uninstall the package
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
public void GetInstalledPackages(string name, Request request)
{
// TODO: improve this debug message that tells us what's going on.
request.Debug("Calling '{0}::GetInstalledPackages' '{1}'", PackageProviderName, name);
// TODO: list all installed packages
}
/// <summary>
///
/// </summary>
/// <param name="fastPackageReference"></param>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
public void GetPackageDetails(string fastPackageReference, Request request)
{
// TODO: improve this debug message that tells us what's going on.
request.Debug("Calling '{0}::GetPackageDetails' '{1}'", PackageProviderName, fastPackageReference);
// TODO: This method is for fetching details that are more expensive than FindPackage* (if you don't need that, remove this method)
}
/// <summary>
/// Initializes a batch search request.
/// </summary>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
/// <returns></returns>
public int StartFind(Request request)
{
// TODO: improve this debug message that tells us what's going on.
request.Debug("Calling '{0}::StartFind'", PackageProviderName);
// TODO: batch search implementation
return default(int);
}
/// <summary>
/// Finalizes a batch search request.
/// </summary>
/// <param name="id"></param>
/// <param name="request">An object passed in from the CORE that contains functions that can be used to interact with the CORE and HOST</param>
/// <returns></returns>
public void CompleteFind(int id, Request request)
{
// TODO: improve this debug message that tells us what's going on.
request.Debug("Calling '{0}::CompleteFind' '{1}'", PackageProviderName, id);
// TODO: batch search implementation
}
}
}