Example #1
0
        internal bool OnApply()
        {
            try
            {
                var packageRestoreConsent = new PackageManagement.PackageRestoreConsent(_settings);
                packageRestoreConsent.IsGrantedInSettings = packageRestoreConsentCheckBox.Checked;
                packageRestoreConsent.IsAutomatic         = packageRestoreAutomaticCheckBox.Checked;

                var bindingRedirects = new BindingRedirectBehavior(_settings);
                bindingRedirects.IsSkipped = skipBindingRedirects.Checked;
#if !VS14
                // package management format selection
                var packageManagement = new PackageManagementFormat(_settings);
                packageManagement.SelectedPackageManagementFormat = defaultPackageManagementFormatItems.SelectedIndex;
                packageManagement.Enabled = showPackageManagementChooser.Checked;
                packageManagement.ApplyChanges();
#endif
            }
            catch (InvalidOperationException)
            {
                MessageHelper.ShowErrorMessage(Resources.ShowError_ConfigInvalidOperation, Resources.ErrorDialogBoxTitle);
                return(false);
            }
            catch (UnauthorizedAccessException)
            {
                MessageHelper.ShowErrorMessage(Resources.ShowError_ConfigUnauthorizedAccess, Resources.ErrorDialogBoxTitle);
                return(false);
            }

            return(true);
        }
        protected async Task CheckPackageManagementFormat()
        {
            bool packagesConfigAndSupportsPackageReferences = false;

            if (Project.ProjectStyle == ProjectModel.ProjectStyle.PackagesConfig)
            {
                packagesConfigAndSupportsPackageReferences = Project.ProjectServices.Capabilities.SupportsPackageReferences;
            }

            // The Project is compatible with, but is currently not a PackageReference-style project, and no packages are currently installed.
            if (packagesConfigAndSupportsPackageReferences && !(await Project.GetInstalledPackagesAsync(Token)).Any())
            {
                var packageManagementFormat = new PackageManagementFormat(ConfigSettings);

                // The "Default Package Management Format" setting is PackageReference, so we can migrate this NuGet Project.
                if (packageManagementFormat.SelectedPackageManagementFormat == 1)
                {
                    var newProject = await VsSolutionManager.UpgradeProjectToPackageReferenceAsync(Project);

                    if (newProject != null)
                    {
                        Project = newProject;
                    }
                }
            }
        }
Example #3
0
        internal void OnActivated()
        {
            if (!_initialized)
            {
                try
                {
                    var packageRestoreConsent = new PackageManagement.PackageRestoreConsent(_settings);

                    packageRestoreConsentCheckBox.Checked   = packageRestoreConsent.IsGrantedInSettings;
                    packageRestoreAutomaticCheckBox.Checked = packageRestoreConsent.IsAutomatic;
                    packageRestoreAutomaticCheckBox.Enabled = packageRestoreConsentCheckBox.Checked;

                    var bindingRedirects = new BindingRedirectBehavior(_settings);
                    skipBindingRedirects.Checked = bindingRedirects.IsSkipped;
#if !VS14
                    // package management format selection
                    var packageManagement = new PackageManagementFormat(_settings);
                    defaultPackageManagementFormatItems.SelectedIndex = packageManagement.SelectedPackageManagementFormat;
                    showPackageManagementChooser.Checked = packageManagement.Enabled;
#endif
                }
                catch (InvalidOperationException)
                {
                    MessageHelper.ShowErrorMessage(Resources.ShowError_ConfigInvalidOperation, Resources.ErrorDialogBoxTitle);
                }
                catch (UnauthorizedAccessException)
                {
                    MessageHelper.ShowErrorMessage(Resources.ShowError_ConfigUnauthorizedAccess, Resources.ErrorDialogBoxTitle);
                }
            }

            _initialized = true;
        }
        internal bool OnApply()
        {
            try
            {
                var packageRestoreConsent = new PackageManagement.PackageRestoreConsent(_settings);
                packageRestoreConsent.IsGrantedInSettings = packageRestoreConsentCheckBox.Checked;
                packageRestoreConsent.IsAutomatic         = packageRestoreAutomaticCheckBox.Checked;

                var bindingRedirects = new BindingRedirectBehavior(_settings);
                bindingRedirects.IsSkipped = skipBindingRedirects.Checked;
#if !VS14
                // package management format selection
                var packageManagement = new PackageManagementFormat(_settings);
                packageManagement.SelectedPackageManagementFormat = defaultPackageManagementFormatItems.SelectedIndex;
                packageManagement.Enabled = showPackageManagementChooser.Checked;
                packageManagement.ApplyChanges();
#endif
            }
            // Thrown during creating or saving NuGet.Config.
            catch (NuGetConfigurationException ex)
            {
                MessageHelper.ShowErrorMessage(ex.Message, Resources.ErrorDialogBoxTitle);
                return(false);
            }
            // Thrown if no nuget.config found.
            catch (InvalidOperationException ex)
            {
                MessageHelper.ShowErrorMessage(ex.Message, Resources.ErrorDialogBoxTitle);
                return(false);
            }
            catch (UnauthorizedAccessException)
            {
                MessageHelper.ShowErrorMessage(Resources.ShowError_ConfigUnauthorizedAccess, Resources.ErrorDialogBoxTitle);
                return(false);
            }
            // Unknown exception.
            catch (Exception ex)
            {
                MessageHelper.ShowErrorMessage(Resources.ShowError_ApplySettingFailed, Resources.ErrorDialogBoxTitle);
                ActivityLog.LogError(NuGetUI.LogEntrySource, ex.ToString());
                return(false);
            }

            return(true);
        }
        internal void OnActivated()
        {
            if (!_initialized)
            {
                try
                {
                    var packageRestoreConsent = new PackageManagement.PackageRestoreConsent(_settings);

                    packageRestoreConsentCheckBox.Checked   = packageRestoreConsent.IsGrantedInSettings;
                    packageRestoreAutomaticCheckBox.Checked = packageRestoreConsent.IsAutomatic;
                    packageRestoreAutomaticCheckBox.Enabled = packageRestoreConsentCheckBox.Checked;

                    var bindingRedirects = new BindingRedirectBehavior(_settings);
                    skipBindingRedirects.Checked = bindingRedirects.IsSkipped;
#if !VS14
                    // package management format selection
                    var packageManagement = new PackageManagementFormat(_settings);
                    defaultPackageManagementFormatItems.SelectedIndex = packageManagement.SelectedPackageManagementFormat;
                    showPackageManagementChooser.Checked = packageManagement.Enabled;
#endif
                }
                // Thrown during creating or saving NuGet.Config.
                catch (NuGetConfigurationException ex)
                {
                    MessageHelper.ShowErrorMessage(ex.Message, Resources.ErrorDialogBoxTitle);
                }
                // Thrown if no nuget.config found.
                catch (InvalidOperationException ex)
                {
                    MessageHelper.ShowErrorMessage(ex.Message, Resources.ErrorDialogBoxTitle);
                }
                catch (UnauthorizedAccessException)
                {
                    MessageHelper.ShowErrorMessage(Resources.ShowError_ConfigUnauthorizedAccess, Resources.ErrorDialogBoxTitle);
                }
                // Unknown exception.
                catch (Exception ex)
                {
                    MessageHelper.ShowErrorMessage(Resources.ShowError_SettingActivatedFailed, Resources.ErrorDialogBoxTitle);
                    ActivityLog.LogError(NuGetUI.LogEntrySource, ex.ToString());
                }
            }

            _initialized = true;
        }
Example #6
0
        protected async Task CheckPackageManagementFormat()
        {
            // check if Project has any packages installed
            if (!(await Project.GetInstalledPackagesAsync(Token)).Any())
            {
                var packageManagementFormat = new PackageManagementFormat(ConfigSettings);

                // if default format is PackageReference then update NuGet Project
                if (packageManagementFormat.SelectedPackageManagementFormat == 1)
                {
                    var newProject = await VsSolutionManager.UpgradeProjectToPackageReferenceAsync(Project);

                    if (newProject != null)
                    {
                        Project = newProject;
                    }
                }
            }
        }
        /// <summary>
        /// Internal install method. All installs from the VS API and template wizard end up here.
        /// </summary>
        internal async Task InstallInternalAsync(
            Project project,
            List <PackageIdentity> packages,
            ISourceRepositoryProvider repoProvider,
            VSAPIProjectContext projectContext,
            bool includePrerelease,
            bool ignoreDependencies,
            CancellationToken token)
        {
            // Go off the UI thread. This may be called from the UI thread. Only switch to the UI thread where necessary
            // This method installs multiple packages and can likely take more than a few secs
            // So, go off the UI thread explicitly to improve responsiveness
            await TaskScheduler.Default;

            var gatherCache = new GatherCache();
            var sources     = repoProvider.GetRepositories().ToList();

            // store expanded node state
            var expandedNodes = await VsHierarchyUtility.GetAllExpandedNodesAsync();

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

                var packageManager = CreatePackageManager(repoProvider);

                // find the project
                var nuGetProject = await _solutionManager.GetOrCreateProjectAsync(project, projectContext);

                var packageManagementFormat = new PackageManagementFormat(_settings);
                // 1 means PackageReference
                var preferPackageReference = packageManagementFormat.SelectedPackageManagementFormat == 1;

                // Check if default package format is set to `PackageReference` and project has no
                // package installed yet then upgrade it to `PackageReference` based project.
                if (preferPackageReference &&
                    (nuGetProject is MSBuildNuGetProject) &&
                    !(await nuGetProject.GetInstalledPackagesAsync(token)).Any() &&
                    await NuGetProjectUpgradeUtility.IsNuGetProjectUpgradeableAsync(nuGetProject, project, needsAPackagesConfig: false))
                {
                    nuGetProject = await _solutionManager.UpgradeProjectToPackageReferenceAsync(nuGetProject);
                }

                // install the package
                foreach (var package in packages)
                {
                    var installedPackageReferences = await nuGetProject.GetInstalledPackagesAsync(token);

                    // Check if the package is already installed
                    if (package.Version != null &&
                        PackageServiceUtilities.IsPackageInList(installedPackageReferences, package.Id, package.Version))
                    {
                        continue;
                    }

                    // Perform the install
                    await InstallInternalCoreAsync(
                        packageManager,
                        gatherCache,
                        nuGetProject,
                        package,
                        sources,
                        projectContext,
                        includePrerelease,
                        ignoreDependencies,
                        token);
                }
            }
            finally
            {
                // collapse nodes
                await VsHierarchyUtility.CollapseAllNodesAsync(expandedNodes);
            }
        }