src/Microsoft.PackageManagement.NuGetProvider/NugetLightClient.cs
/// <summary>
/// Download a package from a file repository that matches the given version and name and install it on the local system.
/// </summary>
/// <param name="packageName">Package name</param>
/// <param name="version">Package version</param>
/// <param name="request">An object passed in from the PackageManagement platform that contains APIs that can be used to interact with it </param>  
/// <param name="source">Package source</param>
/// <param name="sourceFilePath">File source path pointing to the package to be installed</param>
/// <param name="progressTracker">progress tracker to help keep track of progressid, start and end of the progress</param>
/// <returns>PackageItem object</returns>
Download a package from a file repository that matches the given version and name and install it on the local system.
Download a package from a file repository that matches the given version and name and install it on the local system .
internal static PackageItem InstallPackageLocal(
            string packageName, 
            string version,
            NuGetRequest request, 
            PackageSource source,             
            string sourceFilePath,
            ProgressTracker progressTracker
            )
        {
            request.Debug(Messages.DebugInfoCallMethod, "NuGetClient", "InstallPackageLocal");
            string tempSourceFilePath = null;
            string tempSourceDirectory = null;
            string directoryToDeleteWhenFailed = String.Empty;
            bool needToDelete = false;
            try 
            {
                string destinationFilePath = request.Destination;
                request.Verbose(string.Format(CultureInfo.InvariantCulture, "InstallPackageLocal' - name='{0}', version='{1}',destination='{2}'", packageName, version, destinationFilePath));
                request.Debug(sourceFilePath);
                if (string.IsNullOrWhiteSpace(sourceFilePath))
                {
                    throw new ArgumentNullException(sourceFilePath);
                }
                if (!File.Exists(sourceFilePath)) {
                    throw new FileNotFoundException(sourceFilePath);
                }
                if (!Directory.Exists(destinationFilePath)) {
                    Directory.CreateDirectory(destinationFilePath);
                    directoryToDeleteWhenFailed = destinationFilePath;
                }
                tempSourceFilePath = FileUtility.GetTempFileFullPath(fileExtension: NuGetConstant.PackageExtension);
                File.Copy(sourceFilePath, tempSourceFilePath, true);
                request.Progress(progressTracker.ProgressID, progressTracker.StartPercent, string.Format(CultureInfo.CurrentCulture, Messages.Unzipping));
                tempSourceDirectory = PackageUtility.DecompressFile(tempSourceFilePath);
                string installedFolder = FileUtility.MakePackageDirectoryName(request.ExcludeVersion.Value, destinationFilePath, packageName, version);
                if (string.IsNullOrWhiteSpace(directoryToDeleteWhenFailed))
                {
                    directoryToDeleteWhenFailed = installedFolder;
                }
                request.Progress(progressTracker.ProgressID, progressTracker.ConvertPercentToProgress(0.7), string.Format(CultureInfo.CurrentCulture, Messages.CopyUnzippedFiles, installedFolder));
                FileUtility.CopyDirectory(tempSourceDirectory, installedFolder, true);
                var nupkgFilePath = Path.Combine(installedFolder, FileUtility.MakePackageFileName(request.ExcludeVersion.Value, packageName, version, NuGetConstant.PackageExtension));
                if (!File.Exists(nupkgFilePath))
                {
                    File.Copy(sourceFilePath, nupkgFilePath);
                }
                request.Progress(progressTracker.ProgressID, progressTracker.ConvertPercentToProgress(0.85), string.Format(CultureInfo.CurrentCulture, Messages.ReadingManifest));
                string nuspec = Path.Combine(installedFolder, packageName) + NuGetConstant.ManifestExtension;
                PackageBase package = PackageUtility.ProcessNuspec(nuspec);
                var pkgItem = new PackageItem {
                    Package = package,
                    PackageSource = source,
                    FastPath = request.MakeFastPath(source, package.Id, package.Version),
                    FullPath = installedFolder
                };
                var nuspecFilePath = Path.Combine(installedFolder, packageName + NuGetConstant.ManifestExtension);
                if (File.Exists(nuspecFilePath))
                {
                    FileUtility.DeleteFile(nuspecFilePath, false);
                }
                request.Debug(Messages.DebugInfoReturnCall, "NuGetClient", "InstallPackageLocal");
                request.Progress(progressTracker.ProgressID, progressTracker.EndPercent, string.Format(CultureInfo.CurrentCulture, Messages.FinishInstalling, packageName));
                return pkgItem;
            } catch (Exception ex) {
                needToDelete = true;
                ex.Dump(request);
                request.WriteError(ErrorCategory.InvalidResult, packageName, Resources.Messages.PackageFailedToInstallReason, packageName, ex.Message);
                throw;
            } finally {
                if (needToDelete && Directory.Exists(directoryToDeleteWhenFailed))
                {
                    FileUtility.DeleteDirectory(directoryToDeleteWhenFailed, true, isThrow: false);
                }
                FileUtility.DeleteFile(tempSourceFilePath, isThrow:false);
                FileUtility.DeleteDirectory(tempSourceDirectory, recursive: true, isThrow: false);
            }
        }
internal static PackageItem InstallPackageLocal ( string packageName , string version , NuGetRequest request , PackageSource source , string sourceFilePath , ProgressTracker progressTracker ) { request . Debug ( Messages . DebugInfoCallMethod , " " , " " ) ; string tempSourceFilePath = null ; string tempSourceDirectory = null ; string directoryToDeleteWhenFailed = String . Empty ; bool needToDelete = false ; try { string destinationFilePath = request . Destination ; request . Verbose ( string . Format ( CultureInfo . InvariantCulture , " " , packageName , version , destinationFilePath ) ) ; request . Debug ( sourceFilePath ) ; if ( string . IsNullOrWhiteSpace ( sourceFilePath ) ) { throw new ArgumentNullException ( sourceFilePath ) ; } if ( ! File . Exists ( sourceFilePath ) ) { throw new FileNotFoundException ( sourceFilePath ) ; } if ( ! Directory . Exists ( destinationFilePath ) ) { Directory . CreateDirectory ( destinationFilePath ) ; directoryToDeleteWhenFailed = destinationFilePath ; } tempSourceFilePath = FileUtility . GetTempFileFullPath ( fileExtension : NuGetConstant . PackageExtension ) ; File . Copy ( sourceFilePath , tempSourceFilePath , true ) ; request . Progress ( progressTracker . ProgressID , progressTracker . StartPercent , string . Format ( CultureInfo . CurrentCulture , Messages . Unzipping ) ) ; tempSourceDirectory = PackageUtility . DecompressFile ( tempSourceFilePath ) ; string installedFolder = FileUtility . MakePackageDirectoryName ( request . ExcludeVersion . Value , destinationFilePath , packageName , version ) ; if ( string . IsNullOrWhiteSpace ( directoryToDeleteWhenFailed ) ) { directoryToDeleteWhenFailed = installedFolder ; } request . Progress ( progressTracker . ProgressID , progressTracker . ConvertPercentToProgress ( 0.7 ) , string . Format ( CultureInfo . CurrentCulture , Messages . CopyUnzippedFiles , installedFolder ) ) ; FileUtility . CopyDirectory ( tempSourceDirectory , installedFolder , true ) ; var nupkgFilePath = Path . Combine ( installedFolder , FileUtility . MakePackageFileName ( request . ExcludeVersion . Value , packageName , version , NuGetConstant . PackageExtension ) ) ; if ( ! File . Exists ( nupkgFilePath ) ) { File . Copy ( sourceFilePath , nupkgFilePath ) ; } request . Progress ( progressTracker . ProgressID , progressTracker . ConvertPercentToProgress ( 0.85 ) , string . Format ( CultureInfo . CurrentCulture , Messages . ReadingManifest ) ) ; string nuspec = Path . Combine ( installedFolder , packageName ) + NuGetConstant . ManifestExtension ; PackageBase package = PackageUtility . ProcessNuspec ( nuspec ) ; var pkgItem = new PackageItem { Package = package , PackageSource = source , FastPath = request . MakeFastPath ( source , package . Id , package . Version ) , FullPath = installedFolder } ; var nuspecFilePath = Path . Combine ( installedFolder , packageName + NuGetConstant . ManifestExtension ) ; if ( File . Exists ( nuspecFilePath ) ) { FileUtility . DeleteFile ( nuspecFilePath , false ) ; } request . Debug ( Messages . DebugInfoReturnCall , " " , " " ) ; request . Progress ( progressTracker . ProgressID , progressTracker . EndPercent , string . Format ( CultureInfo . CurrentCulture , Messages . FinishInstalling , packageName ) ) ; return pkgItem ; } catch ( Exception ex ) { needToDelete = true ; ex . Dump ( request ) ; request . WriteError ( ErrorCategory . InvalidResult , packageName , Resources . Messages . PackageFailedToInstallReason , packageName , ex . Message ) ; throw ; } finally { if ( needToDelete && Directory . Exists ( directoryToDeleteWhenFailed ) ) { FileUtility . DeleteDirectory ( directoryToDeleteWhenFailed , true , isThrow : false ) ; } FileUtility . DeleteFile ( tempSourceFilePath , isThrow : false ) ; FileUtility . DeleteDirectory ( tempSourceDirectory , recursive : true , isThrow : false ) ; } }
Download a package from a file repository that matches the given version and name and install it on the local system.
Download a package from a file repository that matches the given version and name and install it on the local system .
//Create the destination directory if it does not exist //Make a temp folder in the user appdata temp directory  //Copy over the source file from  the folder repository to the temp folder //Unzip it //Get a package directory under the destination path to store the package // if we did not set the directory before, then the destinationFilePath already exists, so we should not delete it //File folder format of the Nuget packages looks like the following after installed: //Jquery.2.0.1 //  - JQuery.2.0.1.nupkg //  - contents and other stuff // unzipping should take most of the time (assuming 70%) //Copy the unzipped files to under the package installed folder // copying should take another 15% // copy the nupkg file if it's not in //Read the package manifest and return the package object // Delete the nuspec file //Get a package file path // the error will be package "packageName" failed to install because : "reason"
None
packageName
string
None
version
string
None
request
NuGetRequest
An object passed in from the PackageManagement platform that contains APIs that can be used to interact with it
An object passed in from the PackageManagement platform that contains APIs that can be used to interact with it
source
PackageSource
None
sourceFilePath
string
File source path pointing to the package to be installed
File source path pointing to the package to be installed
progressTracker
ProgressTracker
progress tracker to help keep track of progressid, start and end of the progress
progress tracker to help keep track of progressid start and end of the progress