src/Microsoft.PackageManagement.NuGetProvider/NugetLightClient.cs
/// <summary>
/// Download a single package to destination without checking for dependencies
/// </summary>
/// <param name="pkgItem"></param>
/// <param name="progressTracker"></param>
/// <param name="request"></param>
/// <param name="destLocation"></param>
/// <returns></returns>
Download a single package to destination without checking for dependencies
Download a single package to destination without checking for dependencies
internal static bool DownloadSinglePackage(PackageItem pkgItem, NuGetRequest request, string destLocation, ProgressTracker progressTracker)
        {
            if (string.IsNullOrWhiteSpace(pkgItem.PackageFilename) || pkgItem.PackageSource == null || pkgItem.PackageSource.Location == null
            || (pkgItem.PackageSource.IsSourceAFile && pkgItem.Package == null))
            {
                request.WriteError(ErrorCategory.ObjectNotFound, pkgItem.Id, Constants.Messages.UnableToResolvePackage, pkgItem.Id);
                return false;
            }
            bool force = request.GetOptionValue("Force") != null;
            destLocation = Path.Combine(destLocation, pkgItem.PackageFilename);
            if (File.Exists(destLocation))
            {
                if (!force)
                {
                    request.Verbose(Constants.Messages.SkippedDownloadedPackage, pkgItem.Id);
                    request.YieldPackage(pkgItem, pkgItem.PackageSource.Name);
                    return true;
                }
                FileUtility.DeleteFile(destLocation, isThrow: false);
                if (File.Exists(destLocation))
                {
                    request.WriteError(ErrorCategory.ResourceUnavailable, destLocation, Constants.Messages.UnableToOverwriteExistingFile, destLocation);
                    return false;
                }
            }
            bool downloadSuccessful = false;
            try
            {
                if (pkgItem.PackageSource.Repository == null)
                {
                    return false;
                }
                if (pkgItem.PackageSource.Repository.IsFile)
                {
                    using (var input = File.OpenRead(pkgItem.Package.FullFilePath))
                    {
                        using (var output = new FileStream(destLocation, FileMode.Create, FileAccess.Write, FileShare.Read))
                        {
                            input.CopyTo(output);
                        }
                    }
                }
                else
                {
                    string append = String.Format(CultureInfo.InvariantCulture, "/package/{0}/{1}", pkgItem.Id, pkgItem.Version);
                    string httpquery = PathUtility.UriCombine(pkgItem.PackageSource.Repository.Source, append);
                    downloadSuccessful = NuGetClient.DownloadPackage(pkgItem.Id, pkgItem.Version, destLocation,
                        string.IsNullOrWhiteSpace(pkgItem.Package.ContentSrcUrl) ? httpquery : pkgItem.Package.ContentSrcUrl, request, pkgItem.PackageSource, progressTracker);
                }
            }
            catch (Exception ex)
            {
                ex.Dump(request);
                return false;
            }
            if (downloadSuccessful)
            {
                request.Verbose(Resources.Messages.SuccessfullyDownloaded, pkgItem.Id);
                request.YieldPackage(pkgItem, pkgItem.PackageSource.Name, Path.GetDirectoryName(destLocation));
                return true;
            }
            return false;
        }
internal static bool DownloadSinglePackage ( PackageItem pkgItem , NuGetRequest request , string destLocation , ProgressTracker progressTracker ) { if ( string . IsNullOrWhiteSpace ( pkgItem . PackageFilename ) || pkgItem . PackageSource == null || pkgItem . PackageSource . Location == null || ( pkgItem . PackageSource . IsSourceAFile && pkgItem . Package == null ) ) { request . WriteError ( ErrorCategory . ObjectNotFound , pkgItem . Id , Constants . Messages . UnableToResolvePackage , pkgItem . Id ) ; return false ; } bool force = request . GetOptionValue ( " " ) != null ; destLocation = Path . Combine ( destLocation , pkgItem . PackageFilename ) ; if ( File . Exists ( destLocation ) ) { if ( ! force ) { request . Verbose ( Constants . Messages . SkippedDownloadedPackage , pkgItem . Id ) ; request . YieldPackage ( pkgItem , pkgItem . PackageSource . Name ) ; return true ; } FileUtility . DeleteFile ( destLocation , isThrow : false ) ; if ( File . Exists ( destLocation ) ) { request . WriteError ( ErrorCategory . ResourceUnavailable , destLocation , Constants . Messages . UnableToOverwriteExistingFile , destLocation ) ; return false ; } } bool downloadSuccessful = false ; try { if ( pkgItem . PackageSource . Repository == null ) { return false ; } if ( pkgItem . PackageSource . Repository . IsFile ) { using ( var input = File . OpenRead ( pkgItem . Package . FullFilePath ) ) { using ( var output = new FileStream ( destLocation , FileMode . Create , FileAccess . Write , FileShare . Read ) ) { input . CopyTo ( output ) ; } } } else { string append = String . Format ( CultureInfo . InvariantCulture , " " , pkgItem . Id , pkgItem . Version ) ; string httpquery = PathUtility . UriCombine ( pkgItem . PackageSource . Repository . Source , append ) ; downloadSuccessful = NuGetClient . DownloadPackage ( pkgItem . Id , pkgItem . Version , destLocation , string . IsNullOrWhiteSpace ( pkgItem . Package . ContentSrcUrl ) ? httpquery : pkgItem . Package . ContentSrcUrl , request , pkgItem . PackageSource , progressTracker ) ; } } catch ( Exception ex ) { ex . Dump ( request ) ; return false ; } if ( downloadSuccessful ) { request . Verbose ( Resources . Messages . SuccessfullyDownloaded , pkgItem . Id ) ; request . YieldPackage ( pkgItem , pkgItem . PackageSource . Name , Path . GetDirectoryName ( destLocation ) ) ; return true ; } return false ; }
Download a single package to destination without checking for dependencies
Download a single package to destination without checking for dependencies
// this is if the user says -force // combine the path and the file name // if the file already exists // if no force, just return // here we know it is forced, so delete // if after we try delete, it is still there, tells the user we can't perform the action // if no repository, we can't do anything //V2 download package protocol: //sample url: http://www.nuget.org/api/v2/package/jQuery/2.1.3 // provide the directory we save to to yieldpackage
None
pkgItem
PackageItem
None
request
NuGetRequest
None
destLocation
string
None
progressTracker
ProgressTracker
None