@@ -255,32 +255,48 @@ private FindResults FindNameHelper(string packageName, string[] tags, bool inclu
255
255
FindResults findResponse = new FindResults ( ) ;
256
256
errRecord = null ;
257
257
258
- WildcardPattern pkgNamePattern = new WildcardPattern ( $ "{ packageName } .*", WildcardOptions . IgnoreCase ) ;
259
258
NuGetVersion latestVersion = new NuGetVersion ( "0.0.0.0" ) ;
260
259
String latestVersionPath = String . Empty ;
261
260
string actualPkgName = packageName ;
262
261
262
+ // this regex pattern matches packageName followed by a version (4 digit or 3 with prerelease word)
263
+ string regexPattern = $ "{ packageName } " + @".\d+\.\d+\.\d+(?:-\w+|.\d)*.nupkg" ;
264
+ Regex rx = new Regex ( regexPattern , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
265
+ _cmdletPassedIn . WriteDebug ( $ "package file name pattern to be searched for is: { regexPattern } ") ;
266
+
263
267
foreach ( string path in Directory . GetFiles ( Repository . Uri . LocalPath ) )
264
268
{
265
269
string packageFullName = Path . GetFileName ( path ) ;
270
+ MatchCollection matches = rx . Matches ( packageFullName ) ;
271
+ if ( matches . Count == 0 )
272
+ {
273
+ continue ;
274
+ }
266
275
267
- if ( ! String . IsNullOrEmpty ( packageFullName ) && pkgNamePattern . IsMatch ( packageFullName ) )
276
+ Match match = matches [ 0 ] ;
277
+
278
+ GroupCollection groups = match . Groups ;
279
+ if ( groups . Count == 0 )
268
280
{
269
- NuGetVersion nugetVersion = GetInfoFromFileName ( packageFullName : packageFullName , packageName : packageName , actualName : out actualPkgName , out errRecord ) ;
270
- _cmdletPassedIn . WriteDebug ( $ "Version parsed as ' { nugetVersion } '" ) ;
281
+ continue ;
282
+ }
271
283
272
- if ( errRecord != null )
273
- {
274
- return findResponse ;
275
- }
284
+ Capture group = groups [ 0 ] ;
276
285
277
- if ( ( ! nugetVersion . IsPrerelease || includePrerelease ) && ( nugetVersion > latestVersion ) )
286
+ NuGetVersion nugetVersion = GetInfoFromFileName ( packageFullName : packageFullName , packageName : packageName , actualName : out actualPkgName , out errRecord ) ;
287
+ _cmdletPassedIn . WriteDebug ( $ "Version parsed as '{ nugetVersion } '") ;
288
+
289
+ if ( errRecord != null )
290
+ {
291
+ return findResponse ;
292
+ }
293
+
294
+ if ( ( ! nugetVersion . IsPrerelease || includePrerelease ) && ( nugetVersion > latestVersion ) )
295
+ {
296
+ if ( nugetVersion > latestVersion )
278
297
{
279
- if ( nugetVersion > latestVersion )
280
- {
281
- latestVersion = nugetVersion ;
282
- latestVersionPath = path ;
283
- }
298
+ latestVersion = nugetVersion ;
299
+ latestVersionPath = path ;
284
300
}
285
301
}
286
302
}
@@ -371,29 +387,46 @@ private FindResults FindVersionHelper(string packageName, string version, string
371
387
return findResponse ;
372
388
}
373
389
374
- WildcardPattern pkgNamePattern = new WildcardPattern ( $ "{ packageName } .*", WildcardOptions . IgnoreCase ) ;
390
+ // this regex pattern matches packageName followed by the requested version
391
+ string regexPattern = $ "{ packageName } .{ requiredVersion . ToNormalizedString ( ) } " + @".nupkg" ;
392
+ Regex rx = new Regex ( regexPattern , RegexOptions . Compiled | RegexOptions . IgnoreCase ) ;
393
+ _cmdletPassedIn . WriteDebug ( $ "pattern is: { regexPattern } ") ;
375
394
string pkgPath = String . Empty ;
376
395
string actualPkgName = String . Empty ;
396
+
377
397
foreach ( string path in Directory . GetFiles ( Repository . Uri . LocalPath ) )
378
398
{
379
399
string packageFullName = Path . GetFileName ( path ) ;
380
- if ( ! String . IsNullOrEmpty ( packageFullName ) && pkgNamePattern . IsMatch ( packageFullName ) )
400
+ MatchCollection matches = rx . Matches ( packageFullName ) ;
401
+ if ( matches . Count == 0 )
381
402
{
382
- NuGetVersion nugetVersion = GetInfoFromFileName ( packageFullName : packageFullName , packageName : packageName , actualName : out actualPkgName , out errRecord ) ;
383
- _cmdletPassedIn . WriteDebug ( $ "' { packageName } ' version parsed as ' { nugetVersion } '" ) ;
403
+ continue ;
404
+ }
384
405
385
- if ( errRecord != null )
386
- {
387
- return findResponse ;
388
- }
406
+ Match match = matches [ 0 ] ;
389
407
390
- if ( nugetVersion == requiredVersion )
391
- {
392
- _cmdletPassedIn . WriteDebug ( "Found matching version" ) ;
393
- string pkgFullName = $ "{ actualPkgName } .{ nugetVersion . ToString ( ) } .nupkg";
394
- pkgPath = Path . Combine ( Repository . Uri . LocalPath , pkgFullName ) ;
395
- break ;
396
- }
408
+ GroupCollection groups = match . Groups ;
409
+ if ( groups . Count == 0 )
410
+ {
411
+ continue ;
412
+ }
413
+
414
+ Capture group = groups [ 0 ] ;
415
+
416
+ NuGetVersion nugetVersion = GetInfoFromFileName ( packageFullName : packageFullName , packageName : packageName , actualName : out actualPkgName , out errRecord ) ;
417
+ _cmdletPassedIn . WriteDebug ( $ "Version parsed as '{ nugetVersion } '") ;
418
+
419
+ if ( errRecord != null )
420
+ {
421
+ return findResponse ;
422
+ }
423
+
424
+ if ( nugetVersion == requiredVersion )
425
+ {
426
+ _cmdletPassedIn . WriteDebug ( "Found matching version" ) ;
427
+ string pkgFullName = $ "{ actualPkgName } .{ nugetVersion . ToString ( ) } .nupkg";
428
+ pkgPath = Path . Combine ( Repository . Uri . LocalPath , pkgFullName ) ;
429
+ break ;
397
430
}
398
431
}
399
432
0 commit comments