@@ -217,157 +217,28 @@ public override FindResults FindVersionWithTag(string packageName, string versio
217
217
/** INSTALL APIS **/
218
218
219
219
/// <summary>
220
- /// Installs specific package.
220
+ /// Installs a specific package.
221
221
/// Name: no wildcard support.
222
222
/// Examples: Install "PowerShellGet"
223
- /// Implementation Note: if not prerelease: https://www.powershellgallery.com/api/v2/package/powershellget (Returns latest stable)
224
- /// if prerelease, call into InstallVersion instead.
225
- /// </summary>
226
- public override Stream InstallName ( string packageName , bool includePrerelease , out ErrorRecord errRecord )
227
- {
228
- _cmdletPassedIn . WriteDebug ( "In LocalServerApiCalls::InstallName()" ) ;
229
- FileStream fs = null ;
230
- errRecord = null ;
231
- WildcardPattern pkgNamePattern = new WildcardPattern ( $ "{ packageName } .*", WildcardOptions . IgnoreCase ) ;
232
- NuGetVersion latestVersion = new NuGetVersion ( "0.0.0.0" ) ;
233
- String latestVersionPath = String . Empty ;
234
-
235
- foreach ( string path in Directory . GetFiles ( Repository . Uri . LocalPath ) )
236
- {
237
- string packageFullName = Path . GetFileName ( path ) ;
238
-
239
- if ( ! String . IsNullOrEmpty ( packageFullName ) && pkgNamePattern . IsMatch ( packageFullName ) )
240
- {
241
- _cmdletPassedIn . WriteDebug ( $ "'{ packageName } ' found in '{ path } '") ;
242
- string [ ] packageWithoutName = packageFullName . ToLower ( ) . Split ( new string [ ] { $ "{ packageName . ToLower ( ) } ." } , StringSplitOptions . RemoveEmptyEntries ) ;
243
- string packageVersionAndExtension = packageWithoutName [ 0 ] ;
244
- int extensionDot = packageVersionAndExtension . LastIndexOf ( '.' ) ;
245
- string version = packageVersionAndExtension . Substring ( 0 , extensionDot ) ;
246
- _cmdletPassedIn . WriteDebug ( $ "Parsing version '{ version } ' of package '{ packageName } '") ;
247
- NuGetVersion . TryParse ( version , out NuGetVersion nugetVersion ) ;
248
-
249
- if ( ( ! nugetVersion . IsPrerelease || includePrerelease ) && ( nugetVersion > latestVersion ) )
250
- {
251
- latestVersion = nugetVersion ;
252
- latestVersionPath = path ;
253
- }
254
- }
255
- }
256
-
257
- if ( String . IsNullOrEmpty ( latestVersionPath ) )
258
- {
259
- errRecord = new ErrorRecord (
260
- new LocalResourceEmpty ( $ "'{ packageName } ' is not present in repository") ,
261
- "InstallNameFailure" ,
262
- ErrorCategory . ResourceUnavailable ,
263
- this ) ;
264
-
265
- return fs ;
266
- }
267
-
268
- try
269
- {
270
- _cmdletPassedIn . WriteDebug ( $ "Reading file '{ latestVersionPath } '") ;
271
- fs = new FileStream ( latestVersionPath , FileMode . Open , FileAccess . Read ) ;
272
- if ( fs == null )
273
- {
274
- errRecord = new ErrorRecord (
275
- new LocalResourceEmpty ( "The contents of the package file for specified resource was empty or invalid" ) ,
276
- "InstallNameFailure" ,
277
- ErrorCategory . ResourceUnavailable ,
278
- this ) ;
279
- }
280
- }
281
- catch ( Exception e )
282
- {
283
- errRecord = new ErrorRecord (
284
- exception : e ,
285
- "InstallNameFailure" ,
286
- ErrorCategory . ReadError ,
287
- this ) ;
288
- }
289
-
290
- return fs ;
291
- }
292
-
293
- /// <summary>
294
- /// Installs package with specific name and version.
295
- /// Name: no wildcard support.
296
- /// Version: no wildcard support.
297
- /// Examples: Install "PowerShellGet" -Version "3.0.0.0"
298
- /// Install "PowerShellGet" -Version "3.0.0-beta16"
299
- /// API Call: https://www.powershellgallery.com/api/v2/package/Id/version (version can be prerelease)
223
+ /// Install "PowerShellGet" -Version "3.0.0"
300
224
/// </summary>
301
- public override Stream InstallVersion ( string packageName , string version , out ErrorRecord errRecord )
225
+ public override Stream InstallPackage ( string packageName , string packageVersion , bool includePrerelease , out ErrorRecord errRecord )
302
226
{
303
- _cmdletPassedIn . WriteDebug ( "In LocalServerApiCalls::InstallVersion()" ) ;
304
- errRecord = null ;
305
- FileStream fs = null ;
306
-
307
- // if 4 digits and last is 0, create 3 digit equiv string
308
- // 4 digit version (where last is 0) is always passed in.
309
- NuGetVersion . TryParse ( version , out NuGetVersion pkgVersion ) ;
310
- _cmdletPassedIn . WriteDebug ( $ "Version parsed as '{ pkgVersion } '") ;
311
-
312
- if ( pkgVersion . Revision == 0 )
227
+ Stream results = new MemoryStream ( ) ;
228
+ if ( string . IsNullOrEmpty ( packageVersion ) )
313
229
{
314
- version = pkgVersion . ToNormalizedString ( ) ;
230
+ results = InstallName ( packageName , includePrerelease , out errRecord ) ;
315
231
}
316
-
317
- WildcardPattern pkgNamePattern = new WildcardPattern ( $ "{ packageName } .{ version } .nupkg*", WildcardOptions . IgnoreCase ) ;
318
- String pkgVersionPath = String . Empty ;
319
-
320
- foreach ( string path in Directory . GetFiles ( Repository . Uri . LocalPath ) )
321
- {
322
- string packageFullName = Path . GetFileName ( path ) ;
323
-
324
- if ( ! String . IsNullOrEmpty ( packageFullName ) && pkgNamePattern . IsMatch ( packageFullName ) )
325
- {
326
- _cmdletPassedIn . WriteDebug ( $ "Found match with '{ path } '") ;
327
- pkgVersionPath = path ;
328
- }
232
+ else {
233
+ results = InstallVersion ( packageName , packageVersion , out errRecord ) ;
329
234
}
330
235
331
- if ( String . IsNullOrEmpty ( pkgVersionPath ) )
332
- {
333
- errRecord = new ErrorRecord (
334
- new LocalResourceEmpty ( $ "'{ packageName } ' is not present in repository") ,
335
- "InstallNameFailure" ,
336
- ErrorCategory . ResourceUnavailable ,
337
- this ) ;
338
-
339
- return fs ;
340
- }
341
-
342
- try
343
- {
344
- _cmdletPassedIn . WriteDebug ( $ "Reading file '{ pkgVersionPath } '") ;
345
- fs = new FileStream ( pkgVersionPath , FileMode . Open , FileAccess . Read ) ;
346
-
347
- if ( fs == null )
348
- {
349
- errRecord = new ErrorRecord (
350
- new LocalResourceEmpty ( "The contents of the package file for specified resource was empty or invalid" ) ,
351
- "InstallNameFailure" ,
352
- ErrorCategory . ResourceUnavailable ,
353
- this ) ;
354
- }
355
- }
356
- catch ( Exception e )
357
- {
358
- errRecord = new ErrorRecord (
359
- exception : e ,
360
- "InstallVersionFailure" ,
361
- ErrorCategory . ReadError ,
362
- this ) ;
363
- }
364
-
365
- return fs ;
236
+ return results ;
366
237
}
367
238
368
239
#endregion
369
240
370
- #region LocalRepo Specific Methods
241
+ #region Private Methods
371
242
372
243
/// <summary>
373
244
/// Helper method called by FindName() and FindNameWithTag()
@@ -592,6 +463,155 @@ private FindResults FindTagsHelper(string[] tags, bool includePrerelease, out Er
592
463
return findResponse ;
593
464
}
594
465
466
+ /// <summary>
467
+ /// Installs specific package.
468
+ /// Name: no wildcard support.
469
+ /// Examples: Install "PowerShellGet"
470
+ /// Implementation Note: if not prerelease: https://www.powershellgallery.com/api/v2/package/powershellget (Returns latest stable)
471
+ /// if prerelease, call into InstallVersion instead.
472
+ /// </summary>
473
+ private Stream InstallName ( string packageName , bool includePrerelease , out ErrorRecord errRecord )
474
+ {
475
+ _cmdletPassedIn . WriteDebug ( "In LocalServerApiCalls::InstallName()" ) ;
476
+ FileStream fs = null ;
477
+ errRecord = null ;
478
+ WildcardPattern pkgNamePattern = new WildcardPattern ( $ "{ packageName } .*", WildcardOptions . IgnoreCase ) ;
479
+ NuGetVersion latestVersion = new NuGetVersion ( "0.0.0.0" ) ;
480
+ String latestVersionPath = String . Empty ;
481
+
482
+ foreach ( string path in Directory . GetFiles ( Repository . Uri . LocalPath ) )
483
+ {
484
+ string packageFullName = Path . GetFileName ( path ) ;
485
+
486
+ if ( ! String . IsNullOrEmpty ( packageFullName ) && pkgNamePattern . IsMatch ( packageFullName ) )
487
+ {
488
+ _cmdletPassedIn . WriteDebug ( $ "'{ packageName } ' found in '{ path } '") ;
489
+ string [ ] packageWithoutName = packageFullName . ToLower ( ) . Split ( new string [ ] { $ "{ packageName . ToLower ( ) } ." } , StringSplitOptions . RemoveEmptyEntries ) ;
490
+ string packageVersionAndExtension = packageWithoutName [ 0 ] ;
491
+ int extensionDot = packageVersionAndExtension . LastIndexOf ( '.' ) ;
492
+ string version = packageVersionAndExtension . Substring ( 0 , extensionDot ) ;
493
+ _cmdletPassedIn . WriteDebug ( $ "Parsing version '{ version } ' of package '{ packageName } '") ;
494
+ NuGetVersion . TryParse ( version , out NuGetVersion nugetVersion ) ;
495
+
496
+ if ( ( ! nugetVersion . IsPrerelease || includePrerelease ) && ( nugetVersion > latestVersion ) )
497
+ {
498
+ latestVersion = nugetVersion ;
499
+ latestVersionPath = path ;
500
+ }
501
+ }
502
+ }
503
+
504
+ if ( String . IsNullOrEmpty ( latestVersionPath ) )
505
+ {
506
+ errRecord = new ErrorRecord (
507
+ new LocalResourceEmpty ( $ "'{ packageName } ' is not present in repository") ,
508
+ "InstallNameFailure" ,
509
+ ErrorCategory . ResourceUnavailable ,
510
+ this ) ;
511
+
512
+ return fs ;
513
+ }
514
+
515
+ try
516
+ {
517
+ _cmdletPassedIn . WriteDebug ( $ "Reading file '{ latestVersionPath } '") ;
518
+ fs = new FileStream ( latestVersionPath , FileMode . Open , FileAccess . Read ) ;
519
+ if ( fs == null )
520
+ {
521
+ errRecord = new ErrorRecord (
522
+ new LocalResourceEmpty ( "The contents of the package file for specified resource was empty or invalid" ) ,
523
+ "InstallNameFailure" ,
524
+ ErrorCategory . ResourceUnavailable ,
525
+ this ) ;
526
+ }
527
+ }
528
+ catch ( Exception e )
529
+ {
530
+ errRecord = new ErrorRecord (
531
+ exception : e ,
532
+ "InstallNameFailure" ,
533
+ ErrorCategory . ReadError ,
534
+ this ) ;
535
+ }
536
+
537
+ return fs ;
538
+ }
539
+
540
+ /// <summary>
541
+ /// Installs package with specific name and version.
542
+ /// Name: no wildcard support.
543
+ /// Version: no wildcard support.
544
+ /// Examples: Install "PowerShellGet" -Version "3.0.0.0"
545
+ /// Install "PowerShellGet" -Version "3.0.0-beta16"
546
+ /// API Call: https://www.powershellgallery.com/api/v2/package/Id/version (version can be prerelease)
547
+ /// </summary>
548
+ private Stream InstallVersion ( string packageName , string version , out ErrorRecord errRecord )
549
+ {
550
+ _cmdletPassedIn . WriteDebug ( "In LocalServerApiCalls::InstallVersion()" ) ;
551
+ errRecord = null ;
552
+ FileStream fs = null ;
553
+
554
+ // if 4 digits and last is 0, create 3 digit equiv string
555
+ // 4 digit version (where last is 0) is always passed in.
556
+ NuGetVersion . TryParse ( version , out NuGetVersion pkgVersion ) ;
557
+ _cmdletPassedIn . WriteDebug ( $ "Version parsed as '{ pkgVersion } '") ;
558
+
559
+ if ( pkgVersion . Revision == 0 )
560
+ {
561
+ version = pkgVersion . ToNormalizedString ( ) ;
562
+ }
563
+
564
+ WildcardPattern pkgNamePattern = new WildcardPattern ( $ "{ packageName } .{ version } .nupkg*", WildcardOptions . IgnoreCase ) ;
565
+ String pkgVersionPath = String . Empty ;
566
+
567
+ foreach ( string path in Directory . GetFiles ( Repository . Uri . LocalPath ) )
568
+ {
569
+ string packageFullName = Path . GetFileName ( path ) ;
570
+
571
+ if ( ! String . IsNullOrEmpty ( packageFullName ) && pkgNamePattern . IsMatch ( packageFullName ) )
572
+ {
573
+ _cmdletPassedIn . WriteDebug ( $ "Found match with '{ path } '") ;
574
+ pkgVersionPath = path ;
575
+ }
576
+ }
577
+
578
+ if ( String . IsNullOrEmpty ( pkgVersionPath ) )
579
+ {
580
+ errRecord = new ErrorRecord (
581
+ new LocalResourceEmpty ( $ "'{ packageName } ' is not present in repository") ,
582
+ "InstallNameFailure" ,
583
+ ErrorCategory . ResourceUnavailable ,
584
+ this ) ;
585
+
586
+ return fs ;
587
+ }
588
+
589
+ try
590
+ {
591
+ _cmdletPassedIn . WriteDebug ( $ "Reading file '{ pkgVersionPath } '") ;
592
+ fs = new FileStream ( pkgVersionPath , FileMode . Open , FileAccess . Read ) ;
593
+
594
+ if ( fs == null )
595
+ {
596
+ errRecord = new ErrorRecord (
597
+ new LocalResourceEmpty ( "The contents of the package file for specified resource was empty or invalid" ) ,
598
+ "InstallNameFailure" ,
599
+ ErrorCategory . ResourceUnavailable ,
600
+ this ) ;
601
+ }
602
+ }
603
+ catch ( Exception e )
604
+ {
605
+ errRecord = new ErrorRecord (
606
+ exception : e ,
607
+ "InstallVersionFailure" ,
608
+ ErrorCategory . ReadError ,
609
+ this ) ;
610
+ }
611
+
612
+ return fs ;
613
+ }
614
+
595
615
/// <summary>
596
616
/// Extract metadata from .nupkg package file.
597
617
/// This is called only for packages that are ascertained to be a match for our search criteria.
0 commit comments