Example #1
0
 private void CleanUp()
 {
     if (_content != null)
     {
         _content.CleanUp();
         _content = null;
     }
 }
        private IVsWindowFrame CreateDocWindow(
            Project project,
            string documentName,
            IVsHierarchy hier,
            uint itemId)
        {
            uint windowFlags =
                (uint)_VSRDTFLAGS.RDT_DontAddToMRU |
                (uint)_VSRDTFLAGS.RDT_DontSaveAs;

            var solutionManager = ServiceLocator.GetInstance <ISolutionManager>();
            var nugetProject    = solutionManager.GetNuGetProject(project.Name);

            var uiContextFactory = ServiceLocator.GetInstance <INuGetUIContextFactory>();
            var uiContext        = uiContextFactory.Create(this, new [] { nugetProject });

            var uiFactory    = ServiceLocator.GetInstance <INuGetUIFactory>();
            var uiController = uiFactory.Create(uiContext, _uiProjectContext);

            var model = new PackageManagerModel(uiController, uiContext);
            var vsWindowSearchHostfactory = ServiceLocator.GetGlobalService <SVsWindowSearchHostFactory, IVsWindowSearchHostFactory>();
            var control        = new PackageManagerControl(model, Settings, vsWindowSearchHostfactory);
            var windowPane     = new PackageManagerWindowPane(control);
            var ppunkDocView   = Marshal.GetIUnknownForObject(windowPane);
            var ppunkDocData   = Marshal.GetIUnknownForObject(model);
            var guidEditorType = Guid.Empty;
            var guidCommandUI  = Guid.Empty;
            var caption        = String.Format(
                CultureInfo.CurrentCulture,
                Resx.Label_NuGetWindowCaption,
                project.Name // **** myDoc.Target.Name);
                );

            IVsWindowFrame windowFrame;
            IVsUIShell     uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
            int            hr      = uiShell.CreateDocumentWindow(
                windowFlags,
                documentName,
                (IVsUIHierarchy)hier,
                itemId,
                ppunkDocView,
                ppunkDocData,
                ref guidEditorType,
                null,
                ref guidCommandUI,
                null,
                caption,
                string.Empty,
                null,
                out windowFrame);

            ErrorHandler.ThrowOnFailure(hr);
            return(windowFrame);
        }
Example #3
0
        private void CreatePackageManagerControl()
        {
            _container = Initialize();

            // This method is called from MainWindow's constructor. Current thread is the main thread
            var mainThread             = Thread.CurrentThread;
            var synchronizationContext = SynchronizationContext.Current;

            NuGetUIThreadHelper.SetCustomJoinableTaskFactory(mainThread, synchronizationContext);

            Title  = "NuGet Standalone UI";
            Height = 800;
            Width  = 1000;

            var repositoryProvider = new SourceRepositoryProvider(_settings, Repository.Provider.GetVisualStudio());
            var settings           = new DefaultSettings();

            var testSolutionManager = new TestSolutionManager(@"c:\temp\test");

            var projectA = testSolutionManager.AddNewMSBuildProject("projectA");
            //var projectB = testSolutionManager.AddNewMSBuildProject("projectB");
            //var projectC = testSolutionManager.AddProjectKProject("projectK");
            var projectBuildIntegrated = testSolutionManager.AddBuildIntegratedProject("BuildIntProj", NuGetFramework.Parse("net46"));

            var projects = new[] { projectBuildIntegrated };

            var packageRestoreManager = new PackageRestoreManager(
                repositoryProvider,
                settings,
                testSolutionManager);

            var contextFactory = new StandaloneUIContextFactory(
                repositoryProvider,
                testSolutionManager,
                settings,
                packageRestoreManager: packageRestoreManager,
                optionsPage: null);
            var context      = contextFactory.Create(@"c:\temp\test\settings.txt", projects);
            var uiController = _uiServiceFactory.Create(
                context,
                new NuGetUIProjectContext(new StandaloneUILogger(_textBox, _scrollViewer), _sourceControlManagerProvider, _commonOperations));

            var model = new PackageManagerModel(uiController, context, isSolution: false, editorFactoryGuid: Guid.Empty);

            model.SolutionName     = "test solution";
            _packageManagerControl =
                new PackageManagerControl(model, _settings, new SimpleSearchBoxFactory(), vsShell: null);
            layoutGrid.Children.Add(_packageManagerControl);
        }
Example #4
0
        public ApexTestUIProject GetApexTestUIProject(string project, TimeSpan timeout, TimeSpan interval)
        {
            PackageManagerControl packageManagerControl = null;

            var timer = Stopwatch.StartNew();

            while (packageManagerControl == null && timer.Elapsed < timeout)
            {
                packageManagerControl = GetProjectPackageManagerControl(project);

                if (packageManagerControl == null)
                {
                    System.Threading.Thread.Sleep(interval);
                }
            }

            if (packageManagerControl == null)
            {
                throw new TimeoutException($"The package manager control did not load within {timeout}");
            }

            return(new ApexTestUIProject(packageManagerControl));
        }
        private void CreatePackageManagerControl()
        {
            _container = Initialize();

            this.Title = "NuGet Standalone UI";
            Height     = 800;
            Width      = 1000;

            var repositoryProvider = new SourceRepositoryProvider(_settings, Repository.Provider.GetVisualStudio());
            var settings           = new DefaultSettings();

            var testSolutionManager = new TestSolutionManager(@"c:\temp\test");

            var projectA = testSolutionManager.AddNewMSBuildProject("projectA");
            var projectB = testSolutionManager.AddNewMSBuildProject("projectB");
            //var projectC = testSolutionManager.AddProjectKProject("projectK");

            var projects = new NuGetProject[] { projectA, projectB };

            var packageRestoreManager = new PackageRestoreManager(repositoryProvider, settings, testSolutionManager);
            var contextFactory        = new StandaloneUIContextFactory(
                repositoryProvider,
                testSolutionManager,
                settings,
                packageRestoreManager: packageRestoreManager,
                optionsPage: null);
            var context      = contextFactory.Create(@"c:\temp\test\settings.txt", projects);
            var uiController = _uiServiceFactory.Create(
                context,
                new NuGetUIProjectContext(new StandaloneUILogger(_textBox, _scrollViewer), _sourceControlManagerProvider, _commonOperations));

            PackageManagerModel model = new PackageManagerModel(uiController, context);

            model.SolutionName     = "test solution";
            _packageManagerControl = new PackageManagerControl(model, _settings);
            layoutGrid.Children.Add(_packageManagerControl);
        }
Example #6
0
        private async Task <IVsWindowFrame> CreateDocWindowForSolutionAsync()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            IVsWindowFrame windowFrame = null;
            var            solution    = await this.GetServiceAsync <IVsSolution>();

            var uiShell = await this.GetServiceAsync <SVsUIShell, IVsUIShell>();

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

            // when VSSolutionManager is already initialized, then use the existing APIs to check pre-conditions.
            if (!await SolutionManager.Value.IsSolutionAvailableAsync())
            {
                throw new InvalidOperationException(Resources.SolutionIsNotSaved);
            }

            var projects = (await SolutionManager.Value.GetNuGetProjectsAsync()).ToArray();

            if (projects.Length == 0)
            {
                MessageHelper.ShowWarningMessage(Resources.NoSupportedProjectsInSolution, Resources.ErrorDialogBoxTitle);
                return(null);
            }

            // pass empty array of NuGetProject
            var uiController = UIFactory.Value.Create(projects);

            var solutionName = (string)_dte.Solution.Properties.Item("Name").Value;

            var model = new PackageManagerModel(
                uiController,
                isSolution: true,
                editorFactoryGuid: GuidList.guidNuGetEditorType)
            {
                SolutionName = solutionName
            };

            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.Value);
            var windowPane     = new PackageManagerWindowPane(control);
            var guidEditorType = GuidList.guidNuGetEditorType;
            var guidCommandUI  = Guid.Empty;
            var caption        = Resx.Label_SolutionNuGetWindowCaption;
            var documentName   = await SolutionManager.Value.GetSolutionFilePathAsync();

            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)solution,
                    (uint)VSConstants.VSITEMID.Root,
                    ppunkDocView,
                    ppunkDocData,
                    ref guidEditorType,
                    null,
                    ref guidCommandUI,
                    null,
                    caption,
                    string.Empty,
                    null,
                    out windowFrame);

                if (windowFrame != null)
                {
                    WindowFrameHelper.AddF1HelpKeyword(windowFrame, keywordValue: F1KeywordValuePmUI);
                }
            }
            finally
            {
                if (ppunkDocView != IntPtr.Zero)
                {
                    Marshal.Release(ppunkDocData);
                }

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

            ErrorHandler.ThrowOnFailure(hr);
            return(windowFrame);
        }
Example #7
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 (!await SolutionManager.Value.IsSolutionAvailableAsync())
            {
                throw new InvalidOperationException(Resources.SolutionIsNotSaved);
            }

            var uniqueName   = EnvDTEProjectInfoUtility.GetUniqueName(project);
            var nugetProject = await SolutionManager.Value.GetNuGetProjectAsync(uniqueName);

            // 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.Value.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.Value);

            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);
                if (windowFrame != null)
                {
                    WindowFrameHelper.AddF1HelpKeyword(windowFrame, keywordValue: F1KeywordValuePmUI);
                }
            }
            finally
            {
                if (ppunkDocView != IntPtr.Zero)
                {
                    Marshal.Release(ppunkDocData);
                }

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

            ErrorHandler.ThrowOnFailure(hr);
            return(windowFrame);
        }
Example #8
0
 internal ApexTestUIProject(PackageManagerControl pmc)
 {
     _packageManagerControl = pmc ?? throw new ArgumentNullException(nameof(pmc));
 }
Example #9
0
 internal ApexTestUIProject(PackageManagerControl project)
 {
     _packageManagerControl = project;
 }
        private IVsWindowFrame CreateDocWindowForSolution()
        {
            // TODO: Need to wait until solution is loaded
            IVsWindowFrame windowFrame = null;
            IVsSolution    solution    = ServiceLocator.GetInstance <IVsSolution>();
            IVsUIShell     uiShell     = (IVsUIShell)GetService(typeof(SVsUIShell));
            uint           windowFlags =
                (uint)_VSRDTFLAGS.RDT_DontAddToMRU |
                (uint)_VSRDTFLAGS.RDT_DontSaveAs;

            var solutionManager = ServiceLocator.GetInstance <ISolutionManager>();
            var projects        = solutionManager.GetNuGetProjects();

            if (!projects.Any())
            {
                // there are no supported projects.
                // TODO: MessageHelper.ShowWarningMessage(
                //    Resx.NoSupportedProjectsInSolution, Resources.ErrorDialogBoxTitle);
                return(null);
            }

            var uiContextFactory = ServiceLocator.GetInstance <INuGetUIContextFactory>();
            var uiContext        = uiContextFactory.Create(this, projects);

            var uiFactory    = ServiceLocator.GetInstance <INuGetUIFactory>();
            var uiController = uiFactory.Create(uiContext, _uiProjectContext);

            var solutionName = (string)_dte.Solution.Properties.Item("Name").Value;
            var model        = new PackageManagerModel(uiController, uiContext);

            model.SolutionName = solutionName;
            var vsWindowSearchHostfactory = ServiceLocator.GetGlobalService <SVsWindowSearchHostFactory, IVsWindowSearchHostFactory>();
            var control        = new PackageManagerControl(model, Settings, vsWindowSearchHostfactory);
            var windowPane     = new PackageManagerWindowPane(control);
            var ppunkDocView   = Marshal.GetIUnknownForObject(windowPane);
            var ppunkDocData   = Marshal.GetIUnknownForObject(model);
            var guidEditorType = Guid.Empty;
            var guidCommandUI  = Guid.Empty;
            var caption        = String.Format(
                CultureInfo.CurrentCulture,
                Resx.Label_NuGetWindowCaption,
                solutionName);
            var documentName = _dte.Solution.FullName;
            int hr           = uiShell.CreateDocumentWindow(
                windowFlags,
                documentName,
                (IVsUIHierarchy)solution,
                (uint)VSConstants.VSITEMID.Root,
                ppunkDocView,
                ppunkDocData,
                ref guidEditorType,
                null,
                ref guidCommandUI,
                null,
                caption,
                string.Empty,
                null,
                out windowFrame);

            ErrorHandler.ThrowOnFailure(hr);
            return(windowFrame);
        }
Example #11
0
        private async Task <IVsWindowFrame> CreateDocWindowForSolutionAsync()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            IVsWindowFrame windowFrame = null;
            var            solution    = await this.GetServiceAsync <IVsSolution>();

            var uiShell = await this.GetServiceAsync <SVsUIShell, IVsUIShell>();

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

            if (await SolutionManager.SolutionHasDeferredProjectsAsync() && !SolutionManager.IsInitialized)
            {
                // when VSSolutionManager is not yet initialized, then do a quick pre-condition checks without initializing it fully
                // which might take some time so we'll initialize it after showing the manager ui window.
                var preCheckResult = await SolutionManager.CheckSolutionUIPreConditionsAsync();

                // key represent if solution is available or not.
                if (!preCheckResult.Key)
                {
                    throw new InvalidOperationException(Resources.SolutionIsNotSaved);
                }

                // value represent if there is any project in the solution which NuGet supports.
                if (!preCheckResult.Value)
                {
                    // NOTE: The menu 'Manage NuGet Packages For Solution' will be disabled in this case.
                    // But, it is possible, that, before NuGetPackage is loaded in VS, the menu is enabled and used.
                    // For once, this message will be shown. Once the package is loaded, the menu will get disabled as appropriate
                    MessageHelper.ShowWarningMessage(Resources.NoSupportedProjectsInSolution, Resources.ErrorDialogBoxTitle);
                    return(null);
                }
            }
            else
            {
                // when VSSolutionManager is already initialized, then use the existing APIs to check pre-conditions.
                if (!await SolutionManager.IsSolutionAvailableAsync())
                {
                    throw new InvalidOperationException(Resources.SolutionIsNotSaved);
                }

                var projects = await SolutionManager.GetNuGetProjectsAsync();

                if (!projects.Any())
                {
                    MessageHelper.ShowWarningMessage(Resources.NoSupportedProjectsInSolution, Resources.ErrorDialogBoxTitle);
                    return(null);
                }
            }

            // pass empty array of NuGetProject
            var uiController = UIFactory.Create(new NuGetProject[0]);

            var solutionName = (string)_dte.Solution.Properties.Item("Name").Value;

            var model = new PackageManagerModel(
                uiController,
                isSolution: true,
                editorFactoryGuid: GuidList.guidNuGetEditorType)
            {
                SolutionName = solutionName
            };

            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        = Resx.Label_SolutionNuGetWindowCaption;
            var documentName   = _dte.Solution.FullName;

            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)solution,
                    (uint)VSConstants.VSITEMID.Root,
                    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);
        }
Example #12
0
 /// <summary>
 /// Initializes a new instance of the EditorPane class.
 /// </summary>
 public PackageManagerWindowPane(PackageManagerControl control)
     : base(null)
 {
     _content = control;
 }
Example #13
0
 public void SetupSearch(IVsWindowSearch pWindowSearch)
 {
     _control = pWindowSearch as PackageManagerControl;
 }
Example #14
0
        private async Task <IVsWindowFrame> CreateDocWindowForSolutionAsync()
        {
            await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            IVsWindowFrame windowFrame = null;
            var            solution    = await this.GetServiceAsync <IVsSolution>();

            var uiShell = await this.GetServiceAsync <SVsUIShell, IVsUIShell>();

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

            // when VSSolutionManager is already initialized, then use the existing APIs to check pre-conditions.
            if (!await SolutionManager.Value.IsSolutionAvailableAsync())
            {
                throw new InvalidOperationException(Resources.SolutionIsNotSaved);
            }

            IServiceBroker serviceBroker = await ServiceBrokerProvider.Value.GetAsync();

            IReadOnlyCollection <IProjectContextInfo> projectContexts;

            using (INuGetProjectManagerService projectManagerService = await serviceBroker.GetProxyAsync <INuGetProjectManagerService>(
                       NuGetServices.ProjectManagerService))
            {
                Assumes.NotNull(projectManagerService);
                projectContexts = await projectManagerService.GetProjectsAsync(CancellationToken.None);

                if (projectContexts.Count == 0)
                {
                    MessageHelper.ShowWarningMessage(Resources.NoSupportedProjectsInSolution, Resources.ErrorDialogBoxTitle);
                    return(null);
                }
            }

            INuGetUI uiController = await UIFactory.Value.CreateAsync(serviceBroker, projectContexts.ToArray());

            var solutionName = (string)_dte.Solution.Properties.Item("Name").Value;

            // This model takes ownership of --- and Dispose() responsibility for --- the INuGetUI instance.
            var model = new PackageManagerModel(
                uiController,
                isSolution: true,
                editorFactoryGuid: GuidList.guidNuGetEditorType)
            {
                SolutionName = solutionName
            };

            PackageManagerControl control = await PackageManagerControl.CreateAsync(model, OutputConsoleLogger.Value);

            var windowPane     = new PackageManagerWindowPane(control);
            var guidEditorType = GuidList.guidNuGetEditorType;
            var guidCommandUI  = Guid.Empty;
            var caption        = Resx.Label_SolutionNuGetWindowCaption;
            var documentName   = await SolutionManager.Value.GetSolutionFilePathAsync();

            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)solution,
                    (uint)VSConstants.VSITEMID.Root,
                    ppunkDocView,
                    ppunkDocData,
                    ref guidEditorType,
                    null,
                    ref guidCommandUI,
                    null,
                    caption,
                    string.Empty,
                    null,
                    out windowFrame);

                if (windowFrame != null)
                {
                    WindowFrameHelper.AddF1HelpKeyword(windowFrame, keywordValue: F1KeywordValuePmUI);
                    WindowFrameHelper.DisableWindowAutoReopen(windowFrame);
                }
            }
            finally
            {
                if (ppunkDocView != IntPtr.Zero)
                {
                    Marshal.Release(ppunkDocData);
                }

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

            ErrorHandler.ThrowOnFailure(hr);
            return(windowFrame);
        }
        private async Task<IVsWindowFrame> CreateDocWindowAsync(
            Project project,
            string documentName,
            IVsHierarchy hier,
            uint itemId)
        {
            uint windowFlags =
                (uint)_VSRDTFLAGS.RDT_DontAddToMRU |
                (uint)_VSRDTFLAGS.RDT_DontSaveAs;

            var solutionManager = ServiceLocator.GetInstance<ISolutionManager>();

            if (!solutionManager.IsSolutionAvailable)
            {
                throw new InvalidOperationException(Strings.SolutionIsNotSaved);
            }

            var nugetProject = solutionManager.GetNuGetProject(EnvDTEProjectUtility.GetCustomUniqueName(project));

            // 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 uiContextFactory = ServiceLocator.GetInstance<INuGetUIContextFactory>();
            var uiContext = uiContextFactory.Create(this, new[] { nugetProject });

            var uiFactory = ServiceLocator.GetInstance<INuGetUIFactory>();
            var uiController = uiFactory.Create(uiContext, _uiProjectContext);

            var model = new PackageManagerModel(uiController, uiContext, isSolution: false, editorFactoryGuid: GuidList.guidNuGetEditorType);
            var vsWindowSearchHostfactory = ServiceLocator.GetGlobalService<SVsWindowSearchHostFactory, IVsWindowSearchHostFactory>();
            var vsShell = ServiceLocator.GetGlobalService<SVsShell, IVsShell4>();
            var control = new PackageManagerControl(model, Settings, 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;
            IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));

            IntPtr ppunkDocView = IntPtr.Zero;
            IntPtr ppunkDocData = IntPtr.Zero;
            int 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;
        }
        private async Task<IVsWindowFrame> CreateDocWindowForSolutionAsync()
        {
            IVsWindowFrame windowFrame = null;
            IVsSolution solution = ServiceLocator.GetInstance<IVsSolution>();
            IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
            uint windowFlags =
                (uint)_VSRDTFLAGS.RDT_DontAddToMRU |
                (uint)_VSRDTFLAGS.RDT_DontSaveAs;

            var solutionManager = ServiceLocator.GetInstance<ISolutionManager>();

            if (!solutionManager.IsSolutionAvailable)
            {
                throw new InvalidOperationException(Strings.SolutionIsNotSaved);
            }

            var projects = solutionManager.GetNuGetProjects();
            if (!projects.Any())
            {
                // NOTE: The menu 'Manage NuGet Packages For Solution' will be disabled in this case.
                // But, it is possible, that, before NuGetPackage is loaded in VS, the menu is enabled and used.
                // For once, this message will be shown. Once the package is loaded, the menu will get disabled as appropriate
                MessageHelper.ShowWarningMessage(Resources.NoSupportedProjectsInSolution, Resources.ErrorDialogBoxTitle);
                return null;
            }

            // 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.
            foreach (var project in projects)
            {
                await project.GetInstalledPackagesAsync(CancellationToken.None);
            }

            var uiContextFactory = ServiceLocator.GetInstance<INuGetUIContextFactory>();
            var uiContext = uiContextFactory.Create(this, projects);

            var uiFactory = ServiceLocator.GetInstance<INuGetUIFactory>();
            var uiController = uiFactory.Create(uiContext, _uiProjectContext);

            var solutionName = (string)_dte.Solution.Properties.Item("Name").Value;
            var model = new PackageManagerModel(uiController, uiContext, isSolution: true, editorFactoryGuid: GuidList.guidNuGetEditorType);
            model.SolutionName = solutionName;
            var vsWindowSearchHostfactory = ServiceLocator.GetGlobalService<SVsWindowSearchHostFactory, IVsWindowSearchHostFactory>();
            var vsShell = ServiceLocator.GetGlobalService<SVsShell, IVsShell4>();
            var control = new PackageManagerControl(model, Settings, vsWindowSearchHostfactory, vsShell, _outputConsoleLogger);
            var windowPane = new PackageManagerWindowPane(control);
            var guidEditorType = GuidList.guidNuGetEditorType;
            var guidCommandUI = Guid.Empty;
            var caption = Resx.Label_SolutionNuGetWindowCaption;
            var documentName = _dte.Solution.FullName;

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

            try
            {
                ppunkDocView = Marshal.GetIUnknownForObject(windowPane);
                ppunkDocData = Marshal.GetIUnknownForObject(model);
                hr = uiShell.CreateDocumentWindow(
                    windowFlags,
                    documentName,
                    (IVsUIHierarchy)solution,
                    (uint)VSConstants.VSITEMID.Root,
                    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;
        }
        private async ValueTask <IVsWindowFrame> CreateToolWindowAsync(WorkspaceVisualNodeBase workspaceVisualNodeBase, IVsHierarchy hier, uint itemId)
        {
            await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            if (!Guid.TryParse(IProjectContextInfoUtility.GetProjectGuidStringFromVslsQueryString(workspaceVisualNodeBase.VSSelectionMoniker), out Guid projectGuid))
            {
                throw new InvalidOperationException();
            }

            IVsWindowFrame windowFrame = null;

            var uiShell = await _asyncServiceProvider.GetServiceAsync <SVsUIShell, IVsUIShell>();

            Assumes.Present(uiShell);

            uint       toolWindowId;
            bool       foundToolWindowId = _projectGuidToToolWindowId.TryGetValue(projectGuid.ToString(), out toolWindowId);
            const uint FTW_none          = 0;

            if (foundToolWindowId)
            {
                ErrorHandler.ThrowOnFailure(
                    uiShell.FindToolWindowEx(
                        FTW_none,                                  //grfFTW - badly-documented enum value.
                        typeof(PackageManagerToolWindowPane).GUID, // rguidPersistenceSlot
                        toolWindowId,                              // dwToolWindowId
                        out windowFrame));

                if (windowFrame != null)
                {
                    ((IVsWindowFrame2)windowFrame).ActivateOwnerDockedWindow();
                }
                else
                {
                    _projectGuidToToolWindowId.Remove(projectGuid.ToString());
                }
            }

            if (windowFrame == null)
            {
                IServiceBroker serviceBroker = await ServiceBrokerProvider.Value.GetAsync();

                IProjectContextInfo projectContextInfo = await IProjectContextInfoUtility.CreateAsync(serviceBroker, projectGuid.ToString(), CancellationToken.None);

                INuGetUI uiController = await UIFactory.Value.CreateAsync(serviceBroker, projectContextInfo);

                // This model takes ownership of --- and Dispose() responsibility for --- the INuGetUI instance.
                var model   = new PackageManagerModel(uiController, isSolution: false, editorFactoryGuid: GuidList.NuGetEditorType);
                var control = await PackageManagerControl.CreateAsync(model, OutputConsoleLogger.Value);

                var caption = string.Format(CultureInfo.CurrentCulture, Resx.Label_NuGetWindowCaption, Path.GetFileNameWithoutExtension(workspaceVisualNodeBase.NodeMoniker));

                int[] pfDefaultPosition = null;

                var windowPane = new PackageManagerToolWindowPane(control, projectGuid.ToString());
                ErrorHandler.ThrowOnFailure(
                    uiShell.CreateToolWindow(
                        (uint)__VSCREATETOOLWIN.CTW_fInitNew,
                        ++_maxToolWindowId,                        // dwToolWindowId
                        windowPane,                                // ToolWindowPane
                        Guid.Empty,                                // rclsidTool = GUID_NULL
                        typeof(PackageManagerToolWindowPane).GUID, // rguidPersistenceSlot
                        Guid.Empty,                                // reserved - do not use - GUID_NULL
                        null,                                      // IServiceProvider
                        caption,
                        pfDefaultPosition,
                        out windowFrame));
                _projectGuidToToolWindowId.Add(projectGuid.ToString(), _maxToolWindowId);
                windowPane.Closed += WindowPane_Closed;

                if (windowFrame != null)
                {
                    WindowFrameHelper.AddF1HelpKeyword(windowFrame, keywordValue: F1KeywordValuePmUI);
                    WindowFrameHelper.DisableWindowAutoReopen(windowFrame);
                    WindowFrameHelper.DockToolWindow(windowFrame);
                }
            }

            return(windowFrame);
        }