@@ -32,7 +32,6 @@ namespace Microsoft.PackageManagement.Providers.Internal.Bootstrap {
3232 using Directory = System . IO . Directory ;
3333 using ErrorCategory = PackageManagement . Internal . ErrorCategory ;
3434 using File = System . IO . File ;
35- using System . IO . Compression ;
3635
3736 public class BootstrapProvider {
3837 private static readonly Dictionary < string , string [ ] > _features = new Dictionary < string , string [ ] > {
@@ -324,127 +323,12 @@ private bool InstallProviderFromInstaller(Package provider, Link link, string fa
324323 return false ;
325324 }
326325
327- case Iso19770_2 . MediaType . NuGetPackage :
328- return InstallNugetPackage ( provider , link , fastPath , request ) ;
329-
330326 default :
331327 request . Warning ( "Provider '{0}' with link '{1}' has unknown media type '{2}'." , provider . Name , link . HRef , link . MediaType ) ;
332328 return false ;
333329 }
334330 }
335331
336- private bool InstallNugetPackage ( Package provider , Link link , string fastPath , BootstrapRequest request )
337- {
338- // download the nuget package
339- string downloadedNupkg = request . DownloadAndValidateFile ( provider . _swidtag ) ;
340-
341- if ( downloadedNupkg != null )
342- {
343- // extracted folder
344- string extractedFolder = String . Concat ( FilesystemExtensions . GenerateTemporaryFileOrDirectoryNameInTempDirectory ( ) ) ;
345-
346- try
347- {
348- //unzip the file
349- ZipFile . ExtractToDirectory ( downloadedNupkg , extractedFolder ) ;
350-
351- if ( Directory . Exists ( extractedFolder ) )
352- {
353- string versionFolder = Path . Combine ( request . DestinationPath ( request ) , provider . Name , provider . Version ) ;
354- // tool folder is where we find things like nuget.exe
355- string toolFolder = Path . Combine ( extractedFolder , "tools" ) ;
356- string libFolder = Path . Combine ( extractedFolder , "lib" ) ;
357-
358- // create the directory version folder if not exist
359- if ( ! Directory . Exists ( versionFolder ) )
360- {
361- Directory . CreateDirectory ( versionFolder ) ;
362- }
363-
364- // copy the tools directory
365- if ( Directory . Exists ( toolFolder ) )
366- {
367- string destinationToolFolder = Path . Combine ( versionFolder , "tools" ) ;
368-
369- if ( ! Directory . Exists ( destinationToolFolder ) )
370- {
371- Directory . CreateDirectory ( destinationToolFolder ) ;
372- }
373-
374- foreach ( string child in Directory . EnumerateFiles ( toolFolder ) )
375- {
376- try
377- {
378- // try copy and overwrite
379- File . Copy ( child , Path . Combine ( destinationToolFolder , Path . GetFileName ( child ) ) , true ) ;
380- }
381- catch ( Exception e )
382- {
383- request . Debug ( e . StackTrace ) ;
384- if ( ! ( e is UnauthorizedAccessException || e is IOException ) )
385- {
386- // something wrong, delete the version folder
387- versionFolder . TryHardToDelete ( ) ;
388- return false ;
389- }
390-
391- // otherwise this means the file is just being used. so just moves on to copy other files
392- }
393- }
394- }
395-
396- // copy files from lib
397- if ( Directory . Exists ( libFolder ) )
398- {
399- // check that the lib folder has at most 1 dll
400- if ( Directory . EnumerateFiles ( libFolder ) . Count ( file => String . Equals ( Path . GetExtension ( file ) , ".dll" , StringComparison . OrdinalIgnoreCase ) ) > 1 )
401- {
402- request . Warning ( String . Format ( CultureInfo . CurrentCulture , Resources . Messages . MoreThanOneDllExists , provider . Name ) ) ;
403- return false ;
404- }
405-
406- foreach ( string child in Directory . EnumerateFiles ( libFolder ) )
407- {
408- try
409- {
410- File . Copy ( child , Path . Combine ( versionFolder , Path . GetFileName ( child ) ) , true ) ;
411- }
412- catch ( Exception e )
413- {
414- request . Debug ( e . StackTrace ) ;
415- if ( ! ( e is UnauthorizedAccessException || e is IOException ) )
416- {
417- // something wrong, delete the version folder
418- versionFolder . TryHardToDelete ( ) ;
419- return false ;
420- }
421-
422- // otherwise this means the file is just being used. so just moves on to copy other files
423- }
424- }
425- }
426-
427- // target file name is the assembly provider
428- string targetFile = Path . Combine ( versionFolder , Path . GetFileName ( link . Attributes [ Iso19770_2 . Discovery . TargetFilename ] ) ) ;
429-
430- if ( File . Exists ( targetFile ) )
431- {
432- request . Verbose ( Resources . Messages . InstalledPackage , provider . Name , targetFile ) ;
433- request . YieldFromSwidtag ( provider , fastPath ) ;
434- return true ;
435- }
436- }
437- }
438- finally
439- {
440- downloadedNupkg . TryHardToDelete ( ) ;
441- extractedFolder . TryHardToDelete ( ) ;
442- }
443- }
444-
445- return false ;
446- }
447-
448332 private bool InstallPackageFile ( Package provider , string fastPath , BootstrapRequest request ) {
449333 // we can download and verify this package and get the core to install it.
450334 var file = request . DownloadAndValidateFile ( provider . _swidtag ) ;
@@ -522,7 +406,6 @@ private bool InstallAssemblyProvider(Package provider, Link link, string fastPat
522406 return false ;
523407 }
524408
525-
526409 string targetFilename = fastPath ;
527410 string file = fastPath ;
528411
@@ -532,7 +415,7 @@ private bool InstallAssemblyProvider(Package provider, Link link, string fastPat
532415 targetFilename = link . Attributes [ Iso19770_2 . Discovery . TargetFilename ] ;
533416
534417 // download the file
535- file = request . DownloadAndValidateFile ( provider . _swidtag ) ;
418+ file = request . DownloadAndValidateFile ( provider . _swidtag ) ;
536419
537420 }
538421
@@ -551,22 +434,23 @@ private bool InstallAssemblyProvider(Package provider, Link link, string fastPat
551434 //... providername\version\.dll
552435 var versionFolder = Path . Combine ( request . DestinationPath ( request ) , provider . Name , provider . Version ) ;
553436
554- if ( ! Directory . Exists ( versionFolder ) ) {
555- //we create it
437+ // if version folder exists, remove it
438+ if ( Directory . Exists ( versionFolder ) )
439+ {
440+ RemoveDirectory ( versionFolder ) ;
441+ }
442+
443+ // create the directory if we successfully deleted it
444+ if ( ! Directory . Exists ( versionFolder ) )
445+ {
556446 Directory . CreateDirectory ( versionFolder ) ;
557447 }
558448
559449 var targetFile = Path . Combine ( versionFolder , targetFilename ) ;
560-
450+
561451 if ( file != null ) {
562452 try
563453 {
564- // looks good! let's keep it
565- if ( File . Exists ( targetFile ) ) {
566- request . Debug ( "Removing old file '{0}'" , targetFile ) ;
567- targetFile . TryHardToDelete ( ) ;
568- }
569-
570454 // is that file still there?
571455 if ( File . Exists ( targetFile ) ) {
572456 request . Error ( ErrorCategory . InvalidOperation , fastPath , Constants . Messages . UnableToRemoveFile , targetFile ) ;
@@ -575,7 +459,16 @@ private bool InstallAssemblyProvider(Package provider, Link link, string fastPat
575459
576460 request . Debug ( "Copying file '{0}' to '{1}'" , file , targetFile ) ;
577461 try {
578- File . Copy ( file , targetFile ) ;
462+ if ( File . Exists ( file ) )
463+ {
464+ // if this is a file
465+ File . Copy ( file , targetFile ) ;
466+ }
467+ else if ( Directory . Exists ( file ) )
468+ {
469+ // if this is a directory, copy items over
470+ CopyDirectory ( file , versionFolder ) ;
471+ }
579472 }
580473 catch ( Exception ex ) {
581474 request . Debug ( ex . StackTrace ) ;
@@ -599,11 +492,61 @@ private bool InstallAssemblyProvider(Package provider, Link link, string fastPat
599492 file . TryHardToDelete ( ) ;
600493 }
601494 }
602- }
495+ }
603496
604497 return false ;
605498 }
606499
500+ private void RemoveDirectory ( string directoryFolder )
501+ {
502+ // remove all files
503+ foreach ( var fileToBeRemoved in Directory . EnumerateFiles ( directoryFolder ) )
504+ {
505+ fileToBeRemoved . TryHardToDelete ( ) ;
506+ }
507+
508+ // remove all subdirectories
509+ foreach ( var folderToBeRemoved in Directory . EnumerateDirectories ( directoryFolder ) )
510+ {
511+ RemoveDirectory ( folderToBeRemoved ) ;
512+ }
513+
514+ try
515+ {
516+ // now try to remove the directory
517+ Directory . Delete ( directoryFolder ) ;
518+ }
519+ catch { }
520+ }
521+
522+ private void CopyDirectory ( string sourceFolder , string destinationFolder )
523+ {
524+ // check that source and destination folders exist
525+ if ( ! sourceFolder . DirectoryExists ( ) || ! destinationFolder . DirectoryExists ( ) )
526+ {
527+ return ;
528+ }
529+
530+ // copy the files over
531+ foreach ( var file in Directory . EnumerateFiles ( sourceFolder ) )
532+ {
533+ File . Copy ( file , Path . Combine ( destinationFolder , Path . GetFileName ( file ) ) , true ) ;
534+ }
535+
536+ // copy the directories over
537+ foreach ( var directory in Directory . EnumerateDirectories ( sourceFolder ) )
538+ {
539+ var destinationDirName = Path . Combine ( destinationFolder , Path . GetFileName ( directory ) ) ;
540+
541+ if ( ! Directory . Exists ( destinationDirName ) )
542+ {
543+ Directory . CreateDirectory ( destinationDirName ) ;
544+ }
545+
546+ CopyDirectory ( directory , destinationDirName ) ;
547+ }
548+ }
549+
607550 private void InstallPackageFromFile ( string fastPath , BootstrapRequest request )
608551 {
609552 var filePath = new Uri ( fastPath ) . LocalPath ;
0 commit comments