protected override void ProcessRecordCore()
        {
            Preprocess();

            if (All.IsPresent)
            {
                var projects = EnvDTESolutionUtility.GetAllEnvDTEProjects(DTE);
                WriteObject(projects, enumerateCollection: true);
            }
            else
            {
                // No name specified; return default project (if not null)
                if (Name == null)
                {
                    Project defaultProject = GetDefaultProject();
                    if (defaultProject != null)
                    {
                        WriteObject(defaultProject);
                    }
                }
                else
                {
                    // get all projects matching name(s) - handles wildcards
                    WriteObject(GetProjectsByName(Name), enumerateCollection: true);
                }
            }
        }
Esempio n. 2
0
        private async Task <IEnumerable <Project> > GetProjectsInSolutionAsync(DTE dte)
        {
            IEnumerable <Project> allProjects = await EnvDTESolutionUtility.GetAllEnvDTEProjectsAsync(dte);

            IEnumerable <Project> supportedProjects = allProjects.Where(EnvDTEProjectUtility.IsSupported);

            return(supportedProjects);
        }
        private async Task <IEnumerable <Project> > GetProjectsInSolutionAsync(DTE dte)
        {
            IEnumerable <Project> allProjects = await EnvDTESolutionUtility.GetAllEnvDTEProjectsAsync(dte);

            var supportedProjects = new List <Project>();

            foreach (Project project in allProjects)
            {
                if (await EnvDTEProjectUtility.IsSupportedAsync(project))
                {
                    supportedProjects.Add(project);
                }
            }

            return(supportedProjects);
        }
        /// <summary>
        /// Return all projects in the solution matching the provided names. Wildcards are supported.
        /// This method will automatically generate error records for non-wildcarded project names that
        /// are not found.
        /// </summary>
        /// <param name="projectNames">An array of project names that may or may not include wildcards.</param>
        /// <returns>Projects matching the project name(s) provided.</returns>
        protected IEnumerable <Project> GetProjectsByName(string[] projectNames)
        {
            var allValidProjectNames = GetAllValidProjectNames().ToList();
            var allDteProjects       = EnvDTESolutionUtility.GetAllEnvDTEProjects(DTE);

            foreach (string projectName in projectNames)
            {
                // if ctrl+c hit, leave immediately
                if (Stopping)
                {
                    break;
                }

                // Treat every name as a wildcard; results in simpler code
                var pattern = new WildcardPattern(projectName, WildcardOptions.IgnoreCase);

                var matches = from s in allValidProjectNames
                              where pattern.IsMatch(s)
                              select VsSolutionManager.GetNuGetProject(s);

                int count = 0;
                foreach (var project in matches)
                {
                    if (project != null)
                    {
                        count++;
                        string  name       = project.GetMetadata <string>(NuGetProjectMetadataKeys.UniqueName);
                        Project dteProject = allDteProjects
                                             .Where(p => StringComparer.OrdinalIgnoreCase.Equals(EnvDTEProjectUtility.GetCustomUniqueName(p), name))
                                             .FirstOrDefault();
                        yield return(dteProject);
                    }
                }

                // We only emit non-terminating error record if a non-wildcarded name was not found.
                // This is consistent with built-in cmdlets that support wildcarded search.
                // A search with a wildcard that returns nothing should not be considered an error.
                if ((count == 0) &&
                    !WildcardPattern.ContainsWildcardCharacters(projectName))
                {
                    ErrorHandler.WriteProjectNotFoundError(projectName, terminating: false);
                }
            }
        }
        public bool TryCreateContext(string projectUniqueName, out IVsPathContext outputPathContext)
        {
            if (projectUniqueName == null)
            {
                throw new ArgumentNullException(nameof(projectUniqueName));
            }

            // invoke async operation from within synchronous method
            outputPathContext = NuGetUIThreadHelper.JoinableTaskFactory.Run(
                async() =>
            {
                await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                var dte = await _dte.GetValueAsync();

                EnvDTE.Project dteProject = null;
                var lookup = await EnvDTESolutionUtility.GetPathToDTEProjectLookupAsync(dte);

                if (lookup.ContainsKey(projectUniqueName))
                {
                    dteProject = lookup[projectUniqueName];
                }

                if (dteProject == null)
                {
                    return(null);
                }

                var nuGetProject = await _solutionManager.Value.GetOrCreateProjectAsync(dteProject, _projectContext.Value);

                // It's possible the project isn't a NuGet-compatible project at all.
                if (nuGetProject == null)
                {
                    return(null);
                }

                await TaskScheduler.Default;

                return(await CreatePathContextAsync(nuGetProject, CancellationToken.None));
            });

            return(outputPathContext != null);
        }
Esempio n. 6
0
        private async Task <NuGetProject> CreateNuGetProjectAsync(string projectUniqueName)
        {
            await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            DTE dte = await _asyncServiceprovider.GetDTEAsync();

            IEnumerable <Project> supportedProjects = await EnvDTESolutionUtility.GetAllEnvDTEProjectsAsync(dte);

            foreach (Project solutionProject in supportedProjects)
            {
                string solutionProjectPath = solutionProject.GetFullProjectPath();

                if (!string.IsNullOrEmpty(solutionProjectPath) &&
                    PathUtility.GetStringComparerBasedOnOS().Equals(solutionProjectPath, projectUniqueName))
                {
                    return(await _solutionManager.Value.GetOrCreateProjectAsync(solutionProject, _projectContext.Value));
                }
            }

            return(null);
        }
        /// <summary>
        /// Get default project in the type of EnvDTE.Project, to keep PowerShell scripts backward-compatbility.
        /// </summary>
        /// <returns></returns>
        protected Project GetDefaultProject()
        {
            string  customUniqueName  = string.Empty;
            Project defaultDTEProject = null;

            NuGetProject defaultNuGetProject = VsSolutionManager.DefaultNuGetProject;

            // Solution may be open without a project in it. Then defaultNuGetProject is null.
            if (defaultNuGetProject != null)
            {
                customUniqueName = defaultNuGetProject.GetMetadata <string>(NuGetProjectMetadataKeys.UniqueName);
            }

            // Get all DTE projects in the solution and compare by CustomUnique names, especially for projects under solution folders.
            IEnumerable <Project> allDTEProjects = EnvDTESolutionUtility.GetAllEnvDTEProjects(DTE);

            if (allDTEProjects != null)
            {
                defaultDTEProject = allDTEProjects.Where(p => StringComparer.OrdinalIgnoreCase.Equals(EnvDTEProjectUtility.GetCustomUniqueName(p), customUniqueName)).FirstOrDefault();
            }

            return(defaultDTEProject);
        }