protected override void ProcessRecordCore() { Preprocess(); if (All.IsPresent) { VsSolutionManager.EnsureSolutionIsLoaded(); var projects = VsSolutionManager.GetAllVsProjectAdapters().Select(p => p.Project); WriteObject(projects, enumerateCollection: true); } else { // No name specified; return default project (if not null) if (Name == null) { var defaultProject = GetDefaultProject(); if (defaultProject != null) { WriteObject(defaultProject.Project); } } else { // get all projects matching name(s) - handles wildcards WriteObject(GetProjectsByName(Name).Select(p => p.Project), enumerateCollection: true); } } }
/// <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 <IVsProjectAdapter> GetProjectsByName(string[] projectNames) { var allValidProjectNames = GetAllValidProjectNames().ToList(); var allVsProjectAdapters = VsSolutionManager.GetAllVsProjectAdapters(); foreach (var 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); var count = 0; foreach (var project in matches) { if (project != null) { count++; var name = project.GetMetadata <string>(NuGetProjectMetadataKeys.UniqueName); var vsProjectAdapter = allVsProjectAdapters .FirstOrDefault(p => StringComparer.OrdinalIgnoreCase.Equals(p.CustomUniqueName, name)); yield return(vsProjectAdapter); } } // 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); } } }