Beispiel #1
0
        private async void ExecuteUpgradeNuGetProjectCommandAsync(object sender, EventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            if (ShouldMEFBeInitialized())
            {
                await InitializeMEFAsync();
            }

            var project = EnvDTEProjectInfoUtility.GetActiveProject(VsMonitorSelection);

            if (!await NuGetProjectUpgradeUtility.IsNuGetProjectUpgradeableAsync(null, project))
            {
                MessageHelper.ShowWarningMessage(Resources.ProjectMigrateErrorMessage, Resources.ErrorDialogBoxTitle);
                return;
            }

            var uniqueName = await EnvDTEProjectInfoUtility.GetCustomUniqueNameAsync(project);

            // Close NuGet Package Manager if it is open for this project
            var windowFrame = FindExistingWindowFrame(project);

            windowFrame?.CloseFrame((uint)__FRAMECLOSE.FRAMECLOSE_SaveIfDirty);

            var nuGetProject = await SolutionManager.Value.GetNuGetProjectAsync(uniqueName);

            var uiController = ServiceLocator.GetInstance <INuGetUIFactory>().Create(nuGetProject);
            var settings     = uiController.UIContext.UserSettingsManager.GetSettings(GetProjectSettingsKey(nuGetProject));

            await uiController.UIContext.UIActionEngine.UpgradeNuGetProjectAsync(uiController, nuGetProject);

            uiController.UIContext.UserSettingsManager.PersistSettings();
        }
        public static async Task <bool> IsNuGetProjectUpgradeableAsync(NuGetProject nuGetProject, Project envDTEProject = null)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            if (nuGetProject == null && envDTEProject == null)
            {
                return(false);
            }

            if (nuGetProject == null)
            {
                var solutionManager = ServiceLocator.GetInstance <IVsSolutionManager>();

                var projectSafeName = await EnvDTEProjectInfoUtility.GetCustomUniqueNameAsync(envDTEProject);

                nuGetProject = await solutionManager.GetNuGetProjectAsync(projectSafeName);

                if (nuGetProject == null)
                {
                    return(false);
                }
            }

            if (!nuGetProject.ProjectServices.Capabilities.SupportsPackageReferences)
            {
                return(false);
            }

            var msBuildNuGetProject = nuGetProject as MSBuildNuGetProject;

            if (msBuildNuGetProject == null || !msBuildNuGetProject.PackagesConfigNuGetProject.PackagesConfigExists())
            {
                return(false);
            }

            if (envDTEProject == null)
            {
                var vsmsBuildNuGetProjectSystem =
                    msBuildNuGetProject.ProjectSystem as VsMSBuildProjectSystem;
                if (vsmsBuildNuGetProjectSystem == null)
                {
                    return(false);
                }
                envDTEProject = vsmsBuildNuGetProjectSystem.VsProjectAdapter.Project;
            }

            if (!EnvDTEProjectUtility.IsSupported(envDTEProject))
            {
                return(false);
            }
            var projectGuids = VsHierarchyUtility.GetProjectTypeGuids(envDTEProject);

            if (projectGuids.Any(t => UnupgradeableProjectTypes.Contains(t)))
            {
                return(false);
            }

            // Project is supported language, and not an unsupported type
            return(UpgradeableProjectTypes.Contains(envDTEProject.Kind) &&
                   projectGuids.All(projectTypeGuid => !SupportedProjectTypes.IsUnsupported(projectTypeGuid)));
        }
        private static async Task <NuGetProject> GetNuGetProject(Project envDTEProject)
        {
            var solutionManager = ServiceLocator.GetInstance <IVsSolutionManager>();

            var projectSafeName = await EnvDTEProjectInfoUtility.GetCustomUniqueNameAsync(envDTEProject);

            var nuGetProject = await solutionManager.GetNuGetProjectAsync(projectSafeName);

            return(nuGetProject);
        }
Beispiel #4
0
        public static string GetCustomUniqueName(PSObject psObject)
        {
            return(NuGetUIThreadHelper.JoinableTaskFactory.Run(async delegate
            {
                await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                return await EnvDTEProjectInfoUtility.GetCustomUniqueNameAsync(
                    (EnvDTE.Project)psObject.BaseObject);
            }));
        }
Beispiel #5
0
        public async Task <NuGetProject> GetOrCreateProjectAsync(EnvDTE.Project project, INuGetProjectContext projectContext)
        {
            await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            var projectSafeName = await EnvDTEProjectInfoUtility.GetCustomUniqueNameAsync(project);

            var nuGetProject = GetNuGetProject(projectSafeName);

            // if the project does not exist in the solution (this is true for new templates)
            // create it manually
            if (nuGetProject == null)
            {
                nuGetProject = CreateNuGetProject(project, projectContext);
            }

            return(nuGetProject);
        }
Beispiel #6
0
        private async Task ExecuteUpgradeNuGetProjectCommandImplAsync()
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            var project    = EnvDTEProjectInfoUtility.GetActiveProject(VsMonitorSelection);
            var uniqueName = await EnvDTEProjectInfoUtility.GetCustomUniqueNameAsync(project);

            // Close NuGet Package Manager if it is open for this project
            var windowFrame = FindExistingWindowFrame(project);

            windowFrame?.CloseFrame((uint)__FRAMECLOSE.FRAMECLOSE_SaveIfDirty);

            var nuGetProject = await SolutionManager.Value.GetNuGetProjectAsync(uniqueName);

            var uiController = ServiceLocator.GetInstance <INuGetUIFactory>().Create(nuGetProject);
            var settings     = uiController.UIContext.UserSettingsManager.GetSettings(GetProjectSettingsKey(nuGetProject));

            await uiController.UIContext.UIActionEngine.UpgradeNuGetProjectAsync(uiController, nuGetProject);

            uiController.UIContext.UserSettingsManager.PersistSettings();
        }
Beispiel #7
0
        public static async Task <bool> IsNuGetProjectUpgradeableAsync(NuGetProject nuGetProject, Project envDTEProject = null)
        {
            await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            if (nuGetProject == null && envDTEProject == null)
            {
                return(false);
            }

            if (nuGetProject == null)
            {
                var solutionManager = ServiceLocator.GetInstance <IVsSolutionManager>();

                var projectSafeName = await EnvDTEProjectInfoUtility.GetCustomUniqueNameAsync(envDTEProject);

                nuGetProject = await solutionManager.GetNuGetProjectAsync(projectSafeName);

                if (nuGetProject == null)
                {
                    return(false);
                }
            }

            // check if current project is packages.config based or not
            var msBuildNuGetProject = nuGetProject as MSBuildNuGetProject;

            if (msBuildNuGetProject == null || !msBuildNuGetProject.PackagesConfigNuGetProject.PackagesConfigExists())
            {
                return(false);
            }

            // this further check if current project system supports VSProject4 or not which is essential to skip
            // projects like c++ which currently doesn't support VSProject4 implementation for PackageReference
            if (!msBuildNuGetProject.ProjectServices.Capabilities.SupportsPackageReferences)
            {
                return(false);
            }

            if (envDTEProject == null)
            {
                var vsmsBuildNuGetProjectSystem =
                    msBuildNuGetProject.ProjectSystem as VsMSBuildProjectSystem;
                if (vsmsBuildNuGetProjectSystem == null)
                {
                    return(false);
                }
                envDTEProject = vsmsBuildNuGetProjectSystem.VsProjectAdapter.Project;
            }

            if (!EnvDTEProjectUtility.IsSupported(envDTEProject))
            {
                return(false);
            }
            var projectGuids = VsHierarchyUtility.GetProjectTypeGuids(envDTEProject);

            if (projectGuids.Any(t => UnupgradeableProjectTypes.Contains(t)))
            {
                return(false);
            }

            // Project is supported language, and not an unsupported type
            return(UpgradeableProjectTypes.Contains(envDTEProject.Kind) &&
                   projectGuids.All(projectTypeGuid => !SupportedProjectTypes.IsUnsupported(projectTypeGuid)));
        }
Beispiel #8
0
        private async Task<IVsWindowFrame> CreateDocWindowAsync(
            Project project,
            string documentName,
            IVsHierarchy hier,
            uint itemId)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            var windowFlags =
                (uint)_VSRDTFLAGS.RDT_DontAddToMRU |
                (uint)_VSRDTFLAGS.RDT_DontSaveAs;

            if (!SolutionManager.IsSolutionAvailable)
            {
                throw new InvalidOperationException(Resources.SolutionIsNotSaved);
            }

            var customUniqueName = await EnvDTEProjectInfoUtility.GetCustomUniqueNameAsync(project);
            var nugetProject = SolutionManager.GetNuGetProject(customUniqueName);

            // If we failed to generate a cache entry in the solution manager something went wrong.
            if (nugetProject == null)
            {
                throw new InvalidOperationException(
                    string.Format(Resources.ProjectHasAnInvalidNuGetConfiguration, project.Name));
            }

            // load packages.config. This makes sure that an exception will get thrown if there
            // are problems with packages.config, such as duplicate packages. When an exception
            // is thrown, an error dialog will pop up and this doc window will not be created.
            var installedPackages = await nugetProject.GetInstalledPackagesAsync(CancellationToken.None);

            var uiController = UIFactory.Create(nugetProject);

            var model = new PackageManagerModel(
                uiController,
                isSolution: false,
                editorFactoryGuid: GuidList.guidNuGetEditorType);

            var vsWindowSearchHostfactory = await GetServiceAsync(typeof(SVsWindowSearchHostFactory)) as IVsWindowSearchHostFactory;
            var vsShell = await GetServiceAsync(typeof(SVsShell)) as IVsShell4;
            var control = new PackageManagerControl(model, Settings.Value, vsWindowSearchHostfactory, vsShell, OutputConsoleLogger);
            var windowPane = new PackageManagerWindowPane(control);
            var guidEditorType = GuidList.guidNuGetEditorType;
            var guidCommandUI = Guid.Empty;
            var caption = string.Format(
                CultureInfo.CurrentCulture,
                Resx.Label_NuGetWindowCaption,
                project.Name);

            IVsWindowFrame windowFrame;
            var uiShell = await GetServiceAsync(typeof(SVsUIShell)) as IVsUIShell;

            var ppunkDocView = IntPtr.Zero;
            var ppunkDocData = IntPtr.Zero;
            var hr = 0;

            try
            {
                ppunkDocView = Marshal.GetIUnknownForObject(windowPane);
                ppunkDocData = Marshal.GetIUnknownForObject(model);
                hr = uiShell.CreateDocumentWindow(
                    windowFlags,
                    documentName,
                    (IVsUIHierarchy)hier,
                    itemId,
                    ppunkDocView,
                    ppunkDocData,
                    ref guidEditorType,
                    null,
                    ref guidCommandUI,
                    null,
                    caption,
                    string.Empty,
                    null,
                    out windowFrame);
            }
            finally
            {
                if (ppunkDocView != IntPtr.Zero)
                {
                    Marshal.Release(ppunkDocData);
                }

                if (ppunkDocData != IntPtr.Zero)
                {
                    Marshal.Release(ppunkDocView);
                }
            }

            ErrorHandler.ThrowOnFailure(hr);
            return windowFrame;
        }