Esempio n. 1
0
        public Task RestoreMissingPackages()
        {
            TaskScheduler uiScheduler;

            try
            {
                uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
            }
            catch (InvalidOperationException)
            {
                // this exception occurs during unit tests
                uiScheduler = TaskScheduler.Default;
            }

            Task task = Task.Factory.StartNew(() =>
            {
                IVsPackageManager packageManager   = _packageManagerFactory.CreatePackageManagerWithAllPackageSources();
                IPackageRepository localRepository = packageManager.LocalRepository;
                var projectReferences = GetAllPackageReferences(packageManager);
                foreach (var reference in projectReferences)
                {
                    if (!localRepository.Exists(reference.Id, reference.Version))
                    {
                        packageManager.InstallPackage(reference.Id, reference.Version, ignoreDependencies: true, allowPrereleaseVersions: true);
                    }
                }
            });

            task.ContinueWith(originalTask =>
            {
                if (originalTask.IsFaulted)
                {
                    ExceptionHelper.WriteToActivityLog(originalTask.Exception);
                }
                else
                {
                    // we don't allow canceling
                    Debug.Assert(!originalTask.IsCanceled);

                    // after we're done with restoring packages, do the check again
                    CheckForMissingPackages();
                }
            }, uiScheduler);

            return(task);
        }
        /// <summary>
        /// Restores the given package into the packages folder represented by 'fileSystem'.
        /// </summary>
        /// <param name="package">The package to be restored.</param>
        private void RestorePackage(PackageReference package)
        {
            WriteLine(VerbosityLevel.Normal, Resources.RestoringPackage, package);

            // during package restore, use local cache as the primary source, other sources
            // as secondary source.
            IVsPackageManagerFactory packageManagerFactory = ServiceLocator.GetInstance <IVsPackageManagerFactory>();
            var allSources     = packageManagerFactory.CreatePackageManagerWithAllPackageSources().SourceRepository;
            var repository     = new AggregateRepository(new[] { MachineCache.Default, allSources });
            var packageManager = packageManagerFactory.CreatePackageManager(repository, useFallbackForDependencies: false);

            using (packageManager.SourceRepository.StartOperation(RepositoryOperationNames.Restore, package.Id, package.Version.ToString()))
            {
                var resolvedPackage = PackageHelper.ResolvePackage(
                    packageManager.SourceRepository, package.Id, package.Version);
                NuGet.Common.PackageExtractor.InstallPackage(packageManager, resolvedPackage);
                WriteLine(VerbosityLevel.Normal, Resources.PackageRestored, resolvedPackage);
            }
        }
        public Task RestoreMissingPackages()
        {
            TaskScheduler uiScheduler;

            try
            {
                uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
            }
            catch (InvalidOperationException)
            {
                // this exception occurs during unit tests
                uiScheduler = TaskScheduler.Default;
            }

            Task task = Task.Factory.StartNew(() =>
            {
                IVsPackageManager packageManager   = _packageManagerFactory.CreatePackageManagerWithAllPackageSources();
                IPackageRepository localRepository = packageManager.LocalRepository;
                var projectReferences = GetAllPackageReferences(packageManager);
                foreach (var reference in projectReferences)
                {
                    if (!localRepository.Exists(reference.Id, reference.Version))
                    {
                        // Resolve the package to install
                        IPackage package = PackageRepositoryHelper.ResolvePackage(
                            packageManager.SourceRepository,
                            packageManager.LocalRepository,
                            reference.Id,
                            reference.Version,
                            allowPrereleaseVersions: true);

                        // Resolve actions
                        var resolver = new ActionResolver()
                        {
                            Logger                  = packageManager.Logger,
                            DependencyVersion       = packageManager.DependencyVersion,
                            IgnoreDependencies      = true,
                            AllowPrereleaseVersions = true
                        };
                        resolver.AddOperation(PackageAction.Install, package, new NullProjectManager(packageManager));
                        var actions = resolver.ResolveActions();

                        // Execute actions
                        var actionExecutor = new ActionExecutor()
                        {
                            Logger = packageManager.Logger
                        };
                        actionExecutor.Execute(actions);
                    }
                }
            });

            task.ContinueWith(originalTask =>
            {
                if (originalTask.IsFaulted)
                {
                    ExceptionHelper.WriteToActivityLog(originalTask.Exception);
                }
                else
                {
                    // we don't allow canceling
                    Debug.Assert(!originalTask.IsCanceled);

                    // after we're done with restoring packages, do the check again
                    CheckForMissingPackages();
                }
            }, uiScheduler);

            return(task);
        }