src/Microsoft.PackageManagement.NuGetProvider/NugetLightClient.cs
/// <summary>
/// Install a single package. Also install any of its dependency if they are available (the dependency will be installed first).
/// For dependencies, we will only get those that are not installed.
/// Operation is either install or download
/// installOrDownloadFunction is a function that takes in a packageitem and performs either install or download on it
/// </summary>
/// <param name="pkgItem"></param>
/// <param name="request"></param>
/// <param name="operation"></param>
/// <param name="installOrDownloadFunction"></param>
/// <returns></returns>
Install a single package. Also install any of its dependency if they are available (the dependency will be installed first).
For dependencies, we will only get those that are not installed.
Operation is either install or download
installOrDownloadFunction is a function that takes in a packageitem and performs either install or download on it
Install a single package . Also install any of its dependency if they are available ( the dependency will be installed first ) . For dependencies we will only get those that are not installed . Operation is either install or download installOrDownloadFunction is a function that takes in a packageitem and performs either install or download on it
internal static bool InstallOrDownloadPackageHelper(PackageItem pkgItem, NuGetRequest request, string operation,
            Func<PackageItem, ProgressTracker, bool> installOrDownloadFunction)
        {
            request.OriginalSources = pkgItem.Sources;
            bool hasDependencyLoop = false;
            var dependencies = NuGetClient.GetPackageDependenciesToInstall(request, pkgItem, ref hasDependencyLoop).ToArray();
            if (hasDependencyLoop)
            {
                request.WriteError(ErrorCategory.DeadlockDetected, pkgItem.Id, Constants.Messages.DependencyLoopDetected, pkgItem.Id);
                return false;
            }
            if (request.IsCanceled)
            {
                return false;
            }
            int n = 0;
            int numberOfDependencies = dependencies.Count();
            ProgressTracker progressTracker = ProgressTracker.StartProgress(null, string.Format(CultureInfo.InvariantCulture, Messages.InstallingOrDownloadingPackage, operation, pkgItem.Id), request);
            try
            {
                if (numberOfDependencies > 0)
                {
                    foreach (var dep in dependencies)
                    {
                        request.Progress(progressTracker.ProgressID, (n * 100 / (numberOfDependencies + 1)), string.Format(CultureInfo.InvariantCulture, Messages.InstallingOrDownloadingDependencyPackage, operation, dep.Id));
                        ProgressTracker subProgressTracker = ProgressTracker.StartProgress(progressTracker, string.Format(CultureInfo.InvariantCulture, Messages.InstallingOrDownloadingPackage, operation, dep.Id), request);
                        try
                        {
                            if (!installOrDownloadFunction(dep, subProgressTracker))
                            {
                                request.WriteError(ErrorCategory.InvalidResult, dep.Id, Constants.Messages.DependentPackageFailedInstallOrDownload, dep.Id, CultureInfo.CurrentCulture.TextInfo.ToLower(operation));
                                return false;
                            }
                        }
                        finally
                        {
                            request.CompleteProgress(subProgressTracker.ProgressID, true);
                        }
                        n++;
                        request.Progress(progressTracker.ProgressID, (n * 100 / (numberOfDependencies + 1)), string.Format(CultureInfo.InvariantCulture, Messages.InstalledOrDownloadedDependencyPackage, operation, dep.Id));
                    }
                }
                if (installOrDownloadFunction(pkgItem, new ProgressTracker(progressTracker.ProgressID, (n * 100 / (numberOfDependencies + 1)), 100)))
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                ex.Dump(request);
            }
            finally
            {
                request.CompleteProgress(progressTracker.ProgressID, true);
            }
            request.WriteError(ErrorCategory.InvalidResult, pkgItem.Id, Constants.Messages.PackageFailedInstallOrDownload, pkgItem.Id, CultureInfo.CurrentCulture.TextInfo.ToLower(operation));
            return false;
        }
internal static bool InstallOrDownloadPackageHelper ( PackageItem pkgItem , NuGetRequest request , string operation , Func < PackageItem , ProgressTracker , bool > installOrDownloadFunction ) { request . OriginalSources = pkgItem . Sources ; bool hasDependencyLoop = false ; var dependencies = NuGetClient . GetPackageDependenciesToInstall ( request , pkgItem , ref hasDependencyLoop ) . ToArray ( ) ; if ( hasDependencyLoop ) { request . WriteError ( ErrorCategory . DeadlockDetected , pkgItem . Id , Constants . Messages . DependencyLoopDetected , pkgItem . Id ) ; return false ; } if ( request . IsCanceled ) { return false ; } int n = 0 ; int numberOfDependencies = dependencies . Count ( ) ; ProgressTracker progressTracker = ProgressTracker . StartProgress ( null , string . Format ( CultureInfo . InvariantCulture , Messages . InstallingOrDownloadingPackage , operation , pkgItem . Id ) , request ) ; try { if ( numberOfDependencies > 0 ) { foreach ( var dep in dependencies ) { request . Progress ( progressTracker . ProgressID , ( n * 100 / ( numberOfDependencies + 1 ) ) , string . Format ( CultureInfo . InvariantCulture , Messages . InstallingOrDownloadingDependencyPackage , operation , dep . Id ) ) ; ProgressTracker subProgressTracker = ProgressTracker . StartProgress ( progressTracker , string . Format ( CultureInfo . InvariantCulture , Messages . InstallingOrDownloadingPackage , operation , dep . Id ) , request ) ; try { if ( ! installOrDownloadFunction ( dep , subProgressTracker ) ) { request . WriteError ( ErrorCategory . InvalidResult , dep . Id , Constants . Messages . DependentPackageFailedInstallOrDownload , dep . Id , CultureInfo . CurrentCulture . TextInfo . ToLower ( operation ) ) ; return false ; } } finally { request . CompleteProgress ( subProgressTracker . ProgressID , true ) ; } n ++ ; request . Progress ( progressTracker . ProgressID , ( n * 100 / ( numberOfDependencies + 1 ) ) , string . Format ( CultureInfo . InvariantCulture , Messages . InstalledOrDownloadedDependencyPackage , operation , dep . Id ) ) ; } } if ( installOrDownloadFunction ( pkgItem , new ProgressTracker ( progressTracker . ProgressID , ( n * 100 / ( numberOfDependencies + 1 ) ) , 100 ) ) ) { return true ; } } catch ( Exception ex ) { ex . Dump ( request ) ; } finally { request . CompleteProgress ( progressTracker . ProgressID , true ) ; } request . WriteError ( ErrorCategory . InvalidResult , pkgItem . Id , Constants . Messages . PackageFailedInstallOrDownload , pkgItem . Id , CultureInfo . CurrentCulture . TextInfo . ToLower ( operation ) ) ; return false ; }
Install a single package.
Install a single package .
// pkgItem.Sources is the source that the user input. The request will try this source. // Get the dependencies that are not already installed // If there is a dependency loop. Warn the user and don't install the package // package itself didn't install. Report error // request may get canceled if there is a package dependencies missing // Start progress // check that this package has dependency and the user didn't want to skip dependencies // let's install dependencies // start a subprogress bar for the dependent package // Check that we successfully installed the dependency // Now let's install the main package // the start progress should be where we finished installing the dependencies // Report that we have completed installing the package and its dependency this does not mean there are no errors. // Just that it's completed. // package itself didn't install. Report error
None
pkgItem
PackageItem
None
request
NuGetRequest
None
operation
string
None
installOrDownloadFunction
Func<PackageItem, ProgressTracker, bool>
None