Exemplo n.º 1
0
        /// <summary>
        /// Core install method. All installs from the VS API and template wizard end up here.
        /// </summary>
        internal async Task InstallInternal(Project project, List <PackageIdentity> packages, ISourceRepositoryProvider repoProvider, VSAPIProjectContext projectContext, bool ignoreDependencies, CancellationToken token)
        {
            // store expanded node state
            IDictionary <string, ISet <VsHierarchyItem> > expandedNodes = VsHierarchyHelper.GetAllExpandedNodes(_solutionManager);

            try
            {
                DependencyBehavior depBehavior = ignoreDependencies ? DependencyBehavior.Ignore : DependencyBehavior.Lowest;

                bool includePrerelease = false;

                ResolutionContext resolution = new ResolutionContext(depBehavior, includePrerelease, false);

                NuGetPackageManager packageManager = new NuGetPackageManager(repoProvider, _settings, _solutionManager);

                // find the project
                NuGetProject nuGetProject = PackageManagementHelpers.GetProject(_solutionManager, project, projectContext);

                // install the package
                foreach (PackageIdentity package in packages)
                {
                    if (package.Version == null)
                    {
                        if (!_packageServices.IsPackageInstalled(project, package.Id))
                        {
                            await packageManager.InstallPackageAsync(nuGetProject, package.Id, resolution, projectContext, repoProvider.GetRepositories(), Enumerable.Empty <SourceRepository>(), token);
                        }
                    }
                    else
                    {
                        if (!_packageServices.IsPackageInstalledEx(project, package.Id, package.Version.ToString()))
                        {
                            await packageManager.InstallPackageAsync(nuGetProject, package, resolution, projectContext, repoProvider.GetRepositories(), Enumerable.Empty <SourceRepository>(), token);
                        }
                    }
                }
            }
            finally
            {
                // collapse nodes
                VsHierarchyHelper.CollapseAllNodes(_solutionManager, expandedNodes);
            }
        }
Exemplo n.º 2
0
        private IEnumerable <PackageReference> GetInstalledPackageReferences(Project project)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }

            List <PackageReference> packages = new List <PackageReference>();

            if (_solutionManager != null && !String.IsNullOrEmpty(_solutionManager.SolutionDirectory))
            {
                InitializePackageManagerAndPackageFolderPath();

                var nuGetProject = PackageManagementHelpers.GetProject(_solutionManager, project, new VSAPIProjectContext());
                var task         = System.Threading.Tasks.Task.Run(async() => await nuGetProject.GetInstalledPackagesAsync(CancellationToken.None));
                task.Wait();

                packages.AddRange(task.Result);
            }

            return(packages);
        }
Exemplo n.º 3
0
        public IEnumerable <IVsPackageMetadata> GetInstalledPackages(Project project)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }

            List <IVsPackageMetadata> packages = new List <IVsPackageMetadata>();

            // Debug.Assert(_solutionManager.SolutionDirectory != null, "SolutionDir is null");

            if (_solutionManager != null && !String.IsNullOrEmpty(_solutionManager.SolutionDirectory))
            {
                InitializePackageManagerAndPackageFolderPath();

                var nuGetProject = PackageManagementHelpers.GetProject(_solutionManager, project, new VSAPIProjectContext());
                var task         = System.Threading.Tasks.Task.Run(async() => await nuGetProject.GetInstalledPackagesAsync(CancellationToken.None));
                task.Wait();

                foreach (var package in task.Result)
                {
                    // Get the install path for package
                    string installPath = _packageManager.PackagesFolderNuGetProject.GetInstalledPath(package.PackageIdentity);

                    if (!String.IsNullOrEmpty(installPath))
                    {
                        // normalize the path and take the dir if the nupkg path was given
                        var dir = new DirectoryInfo(installPath);
                        installPath = dir.FullName;
                    }

                    var metadata = new VsPackageMetadata(package.PackageIdentity, installPath);
                    packages.Add(metadata);
                }
            }

            return(packages);
        }
Exemplo n.º 4
0
        public void UninstallPackage(Project project, string packageId, bool removeDependencies)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }

            if (String.IsNullOrEmpty(packageId))
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, CommonResources.Argument_Cannot_Be_Null_Or_Empty, "packageId"));
            }

            NuGetPackageManager packageManager = new NuGetPackageManager(_sourceRepositoryProvider, _settings, _solutionManager);


            UninstallationContext uninstallContext = new UninstallationContext(removeDependencies, false);
            VSAPIProjectContext   projectContext   = new VSAPIProjectContext();

            // find the project
            NuGetProject nuGetProject = PackageManagementHelpers.GetProject(_solutionManager, project, projectContext);

            // uninstall the package
            PackageManagementHelpers.RunSync(async() => await packageManager.UninstallPackageAsync(nuGetProject, packageId, uninstallContext, projectContext, CancellationToken.None));
        }