示例#1
0
        protected override void Initialize()
        {
            IVsSolution?solution = _solution.Value;

            Assumes.Present(solution);

            Verify.HResult(solution.AdviseSolutionEvents(this, out _cookie));
        }
示例#2
0
        public async Task InitializeAsync(IAsyncServiceProvider asyncServiceProvider)
        {
            Assumes.Null(_solution);
            Assumes.True(_context.IsOnMainThread, "Must be on UI thread");

            _solution = await asyncServiceProvider.GetServiceAsync <IVsSolution, IVsSolution>();

            Verify.HResult(_solution.AdviseSolutionEvents(this, out _cookie));
        }
        protected override async Task InitializeCoreAsync(CancellationToken cancellationToken)
        {
            await _threadingService.SwitchToUIThread(cancellationToken);

            IVsSolution?solution = _solution.Value;

            Assumes.Present(solution);

            Verify.HResult(solution.AdviseSolutionEvents(this, out _cookie));
        }
        private IVsHierarchy?TryGetIVsHierarchy(Project project)
        {
            IVsSolution?solution = _solution.Value;

            if (solution !.GetProjectOfUniqueName(project.UniqueName, out IVsHierarchy projectHierarchy) == HResult.OK)
            {
                return(projectHierarchy);
            }

            return(null);
        }
示例#5
0
            private async Task <IVsHierarchy?> TryGetIVsHierarchyAsync(Project project)
            {
                IVsSolution?solutionService = await _solutionService.GetValueAsync();

                if (solutionService !.GetProjectOfUniqueName(project.UniqueName, out IVsHierarchy projectHierarchy) == HResult.OK)
                {
                    return(projectHierarchy);
                }

                return(null);
            }
        public ActiveDocumentMonitor(string launchProfilesPath, string userFile,
                                     string[] startupFiles, IServiceProvider services)
        {
            this.launchProfilesPath = launchProfilesPath;
            this.userFile           = userFile;
            this.startupFiles       = startupFiles.ToDictionary(x => x, StringComparer.OrdinalIgnoreCase);

            solution  = (IVsSolution)services.GetService(typeof(SVsSolution));
            rdt       = (IVsRunningDocumentTable)services.GetService(typeof(SVsRunningDocumentTable));
            selection = (IVsMonitorSelection)services.GetService(typeof(SVsShellMonitorSelection));

            EnsureMonitoring();
        }
示例#7
0
        public void Dispose()
        {
            _threadHandling.Value.VerifyOnUIThread();

            if (_solutionCookie != VSConstants.VSCOOKIE_NIL)
            {
                if (_vsSolution != null)
                {
                    Verify.HResult(_vsSolution.UnadviseSolutionEvents(_solutionCookie));
                    _solutionCookie = VSConstants.VSCOOKIE_NIL;
                    _vsSolution = null;
                }
            }
        }
        public override void Initialize(ProjectSnapshotManagerBase projectManager)
        {
            _projectSnapshotManager = projectManager;

            _ = _joinableTaskContext.Factory.RunAsync(async() =>
            {
                await _joinableTaskContext.Factory.SwitchToMainThreadAsync();

                if (_serviceProvider.GetService(typeof(SVsSolution)) is IVsSolution solution)
                {
                    _solution = solution;
                    _solution.AdviseSolutionEvents(this, out _cookie);
                }
            });
        }
        protected override async Task InitializeCoreAsync(CancellationToken cancellationToken)
        {
            await _threadingService.SwitchToUIThread(cancellationToken);

            IVsSolution?solution = _solution.Value;

            Assumes.Present(solution);

            HResult result = solution.AdviseSolutionEvents(this, out _cookie);

            if (result.Failed)
            {
                throw result.Exception;
            }
        }
示例#10
0
        protected override void Dispose(bool disposing)
        {
            if (!disposing)
            {
                return;
            }

            if (_cookie != VSConstants.VSCOOKIE_NIL)
            {
                IVsSolution?solution = _solution.Value;
                Assumes.Present(solution);

                Verify.HResult(solution.UnadviseSolutionEvents(_cookie));
                _cookie = VSConstants.VSCOOKIE_NIL;
            }
        }
        protected override async Task DisposeCoreAsync(bool initialized)
        {
            if (initialized)
            {
                if (_cookie != VSConstants.VSCOOKIE_NIL)
                {
                    await _threadingService.SwitchToUIThread();

                    IVsSolution?solution = _solution.Value;
                    Assumes.Present(solution);

                    Verify.HResult(solution.UnadviseSolutionEvents(_cookie));

                    _cookie = VSConstants.VSCOOKIE_NIL;
                }
            }
        }
        /// <summary>
        /// Returns the <see cref="IVsHierarchy"/> for the project.
        /// </summary>
        public static async Task <IVsHierarchy?> ToHierarchyAsync(this Project project)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            IVsSolution?vsSolution = await VS.Solution.GetSolutionAsync();

            if (vsSolution.GetProjectOfUniqueName(project.UniqueName, out IVsHierarchy? hierarchy) == VSConstants.S_OK)
            {
                return(hierarchy);
            }

            return(null);
        }
        /// <summary>Adds one or more files to the project.</summary>
        public static async Task AddFilesToProjectAsync(this Project project, params string[] files)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            if (project == null || project.IsKind(ProjectTypes.ASPNET_CORE) || project.IsKind(ProjectTypes.DOTNET_CORE) || project.IsKind(ProjectTypes.SSDT))
            {
                return;
            }

            DTE2?dte = await VS.GetDTEAsync();

            if (project.IsKind(ProjectTypes.WEBSITE))
            {
                Command command = dte.Commands.Item("SolutionExplorer.Refresh");

                if (command.IsAvailable)
                {
                    dte.ExecuteCommand(command.Name);
                }

                return;
            }

            IVsSolution?solutionService = await VS.GetRequiredServiceAsync <SVsSolution, IVsSolution>();

            solutionService.GetProjectOfUniqueName(project.UniqueName, out IVsHierarchy? hierarchy);

            if (hierarchy == null)
            {
                return;
            }

            var ip     = (IVsProject)hierarchy;
            var result = new VSADDRESULT[files.Count()];

            ip.AddItem(VSConstants.VSITEMID_ROOT,
                       VSADDITEMOPERATION.VSADDITEMOP_LINKTOFILE,
                       string.Empty,
                       (uint)files.Count(),
                       files.ToArray(),
                       IntPtr.Zero,
                       result);
        }
        public async Task InitializeAsync()
        {
            await _threadHandling.Value.SwitchToUIThread();

            // Initialize our cache file
            string?appDataFolder = await _shellUtilitiesHelper.Value.GetLocalAppDataFolderAsync(_vsShellService);

            if (appDataFolder != null)
            {
                _versionDataCacheFile = new RemoteCacheFile(Path.Combine(appDataFolder, VersionDataFilename), VersionCompatibilityDownloadFwlink,
                                                            TimeSpan.FromHours(CacheFileValidHours), _fileSystem.Value, _httpClient);
            }

            VisualStudioVersion = await _shellUtilitiesHelper.Value.GetVSVersionAsync(_vsAppIdService);

            _vsSolution = await _vsSolutionService.GetValueAsync();

            Verify.HResult(_vsSolution.AdviseSolutionEvents(this, out _solutionCookie));

            // Check to see if a solution is already open. If so we set _solutionOpened to true so that subsequent projects added to
            // this solution are processed.
            if (ErrorHandler.Succeeded(_vsSolution.GetProperty((int)__VSPROPID4.VSPROPID_IsSolutionFullyLoaded, out object isFullyLoaded)) &&
                isFullyLoaded is bool isFullyLoadedBool &&
                isFullyLoadedBool)
            {
                SolutionOpen = true;
                // do not block package initialization on this
                _threadHandling.Value.RunAndForget(async() =>
                {
                    // Perform file IO on the thread pool
                    await TaskScheduler.Default;

                    // First make sure that the cache file exists
                    if (_versionDataCacheFile != null && !_versionDataCacheFile.CacheFileExists())
                    {
                        await _versionDataCacheFile.TryToUpdateCacheFileAsync();
                    }

                    // check if the project is compatible
                    await CheckCompatibilityAsync();
                }, unconfiguredProject: null);
            }
        }
示例#15
0
        /// <summary>
        /// Returns the export T of the startup projects if those projects support the specified capabilities
        /// </summary>
        public ImmutableArray <T> GetExportFromDotNetStartupProjects <T>(string capabilityMatch) where T : class
        {
            DTE?dte = _dte.Value;

            Assumes.Present(dte);

            if (dte.Solution.SolutionBuild.StartupProjects is Array startupProjects && startupProjects.Length > 0)
            {
                IVsSolution?solution = _solution.Value;
                Assumes.Present(solution);

                var results = PooledArray <T> .GetInstance();

                foreach (string projectName in startupProjects)
                {
                    solution.GetProjectOfUniqueName(projectName, out IVsHierarchy hier);

                    if (hier != null && hier.IsCapabilityMatch(capabilityMatch))
                    {
                        string?projectPath = hier.GetProjectFilePath();

                        if (projectPath != null)
                        {
                            T?export = _projectExportProvider.GetExport <T>(projectPath);

                            if (export != null)
                            {
                                results.Add(export);
                            }
                        }
                    }
                }

                return(results.ToImmutableAndFree());
            }

            return(ImmutableArray <T> .Empty);
        }
示例#16
0
        /// <inheritdoc/>
        public IEnumerator <Reference> GetEnumerator()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            // Not all projects can have references (for example Shared Projects),
            // so when enumerating over the references in the project, we won't throw
            // an error if the manager or provider context cannot be retrieved.
            if (TryGetManager(out IVsReferenceManagerUser manager))
            {
                IVsSharedProjectReferenceProviderContext?sharedProjectContext = null;

                foreach (IVsReferenceProviderContext context in manager.GetProviderContexts().OfType <IVsReferenceProviderContext>())
                {
                    // Remember the shared project context, because it may not actually provide the
                    // references to shared projects, meaning we may have to create them ourselves.
                    if (context is IVsSharedProjectReferenceProviderContext shared)
                    {
                        sharedProjectContext = shared;
                    }

                    foreach (IVsReference reference in context.References.OfType <IVsReference>())
                    {
                        if (reference is IVsAssemblyReference assemblyReference)
                        {
                            yield return(new AssemblyReference(assemblyReference));
                        }
                        else if (reference is IVsProjectReference projectReference)
                        {
                            yield return(new ProjectReference(projectReference));
                        }
                        else
                        {
                            yield return(new Reference(reference));
                        }
                    }
                }

                // Shared projects don't seem to be listed in the provider contexts, so if there is a context
                // for shared projects but it's empty, then we'll define the shared project references ourselves.
                if (sharedProjectContext is not null && sharedProjectContext.References.Length == 0)
                {
                    IVsSolution?solution = null;

                    _project.GetItemInfo(out IVsHierarchy hierarchy, out _, out _);

                    foreach (IVsHierarchy sharedHierarchy in hierarchy.EnumOwningProjectsOfSharedAssets())
                    {
                        // A shared project seems to list itself as an owning project, so ignore
                        // this hierarchy if it's the same one that we got from our project.
                        if (sharedHierarchy == hierarchy)
                        {
                            continue;
                        }

                        IVsSharedAssetsProject?sharedProject = sharedHierarchy.GetSharedAssetsProject();
                        if (sharedProject is not null)
                        {
                            Project?project = SolutionItem.FromHierarchy(sharedHierarchy, VSConstants.VSITEMID_ROOT) as Project;
                            if (project is not null)
                            {
                                if (solution is null)
                                {
                                    solution = VS.GetRequiredService <SVsSolution, IVsSolution>();
                                }

                                IVsSharedProjectReference reference = (IVsSharedProjectReference)sharedProjectContext.CreateReference();
                                PopulateSharedProjectReference(reference, solution, project, sharedProject);
                                yield return(new ProjectReference(reference));
                            }
                        }
                    }
                }
            }
        }