protected async Task CheckPackageManagementFormat() { bool packagesConfigAndSupportsPackageReferences = false; if (Project.ProjectStyle == ProjectModel.ProjectStyle.PackagesConfig) { packagesConfigAndSupportsPackageReferences = Project.ProjectServices.Capabilities.SupportsPackageReferences; } // The Project is compatible with, but is currently not a PackageReference-style project, and no packages are currently installed. if (packagesConfigAndSupportsPackageReferences && !(await Project.GetInstalledPackagesAsync(Token)).Any()) { var packageManagementFormat = new PackageManagementFormat(ConfigSettings); // The "Default Package Management Format" setting is PackageReference, so we can migrate this NuGet Project. if (packageManagementFormat.SelectedPackageManagementFormat == 1) { var newProject = await VsSolutionManager.UpgradeProjectToPackageReferenceAsync(Project); if (newProject != null) { Project = newProject; } } } }
protected override void ProcessRecordCore() { Preprocess(); if (All.IsPresent) { VsSolutionManager.EnsureSolutionIsLoaded(); var projects = NuGetUIThreadHelper.JoinableTaskFactory.Run( async() => (await VsSolutionManager.GetAllVsProjectAdaptersAsync()).Select(p => p.Project)); WriteObject(projects, enumerateCollection: true); } else { // No name specified; return default project (if not null) if (Name == null) { var defaultProject = NuGetUIThreadHelper.JoinableTaskFactory.Run( async() => await GetDefaultProjectAsync()); if (defaultProject != null) { WriteObject(defaultProject.Project); } } else { // get all projects matching name(s) - handles wildcards NuGetUIThreadHelper.JoinableTaskFactory.Run( async() => WriteObject((await GetProjectsByNameAsync(Name)).Select(p => p.Project), enumerateCollection: true)); } } }
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 possibly valid project names in the current solution. This includes all /// unique names and safe names. /// </summary> /// <returns></returns> private IEnumerable <string> GetAllValidProjectNames() { var nugetProjects = VsSolutionManager.GetNuGetProjects(); var safeNames = nugetProjects?.Select(p => VsSolutionManager.GetNuGetProjectSafeName(p)); var uniqueNames = nugetProjects?.Select(p => NuGetProject.GetUniqueNameOrName(p)); return(uniqueNames.Concat(safeNames).Distinct()); }
/// <summary> /// Return all possibly valid project names in the current solution. This includes all /// unique names and safe names. /// </summary> /// <returns></returns> private async Task <IEnumerable <string> > GetAllValidProjectNamesAsync() { var nugetProjects = await VsSolutionManager.GetNuGetProjectsAsync(); var safeNames = await Task.WhenAll(nugetProjects?.Select(p => VsSolutionManager.GetNuGetProjectSafeNameAsync(p))); var uniqueNames = nugetProjects?.Select(p => NuGetProject.GetUniqueNameOrName(p)); return(uniqueNames.Concat(safeNames).Distinct()); }
/// <summary> /// Get default project in the type of <see cref="IVsProjectAdapter"/>, to keep PowerShell scripts backward-compatbility. /// </summary> /// <returns></returns> protected IVsProjectAdapter GetDefaultProject() { var defaultNuGetProject = VsSolutionManager.DefaultNuGetProject; // Solution may be open without a project in it. Then defaultNuGetProject is null. if (defaultNuGetProject != null) { return(VsSolutionManager.GetVsProjectAdapter(defaultNuGetProject)); } return(null); }
/// <summary> /// Get default project in the type of <see cref="IVsProjectAdapter"/>, to keep PowerShell scripts backward-compatbility. /// </summary> /// <returns></returns> protected async Task <IVsProjectAdapter> GetDefaultProjectAsync() { var defaultNuGetProject = await VsSolutionManager.GetDefaultNuGetProjectAsync(); // Solution may be open without a project in it. Then defaultNuGetProject is null. if (defaultNuGetProject != null) { return(await VsSolutionManager.GetVsProjectAdapterAsync(defaultNuGetProject)); } return(null); }
protected override void Preprocess() { base.Preprocess(); if (string.IsNullOrEmpty(ProjectName)) { ProjectName = VsSolutionManager.DefaultNuGetProjectName; } // Get the projects in the solution that's not the current default or specified project to sync the package identity to. Projects = VsSolutionManager.GetNuGetProjects() .Where(p => !StringComparer.OrdinalIgnoreCase.Equals(p.GetMetadata <string>(NuGetProjectMetadataKeys.Name), ProjectName)) .ToList(); }
protected IEnumerable <NuGetProject> GetNuGetProjectsByName(string[] projectNames) { List <NuGetProject> nuGetProjects = new List <NuGetProject>(); foreach (Project project in GetProjectsByName(projectNames)) { NuGetProject nuGetProject = VsSolutionManager.GetNuGetProject(project.Name); if (nuGetProject != null) { nuGetProjects.Add(nuGetProject); } } return(nuGetProjects); }
protected override void Preprocess() { base.Preprocess(); if (string.IsNullOrEmpty(ProjectName)) { ProjectName = VsSolutionManager.DefaultNuGetProjectName; } NuGetUIThreadHelper.JoinableTaskFactory.Run(async() => { // Get the projects in the solution that's not the current default or specified project to sync the package identity to. _projects = (await VsSolutionManager.GetNuGetProjectsAsync()) .Where(p => !StringComparer.OrdinalIgnoreCase.Equals(p.GetMetadata <string>(NuGetProjectMetadataKeys.Name), ProjectName)) .ToList(); }); }
protected override void Preprocess() { base.Preprocess(); ParseUserInputForVersion(); if (!_projectSpecified) { Projects = VsSolutionManager.GetNuGetProjects().ToList(); } else { Projects = new List <NuGetProject>() { Project }; } }
protected async Task CheckMissingPackagesAsync() { var solutionDirectory = await VsSolutionManager.GetSolutionDirectoryAsync(); var packages = await PackageRestoreManager.GetPackagesInSolutionAsync(solutionDirectory, CancellationToken.None); if (packages.Any(p => p.IsMissing)) { var packageRestoreConsent = new PackageRestoreConsent(ConfigSettings); if (packageRestoreConsent.IsGranted) { await TaskScheduler.Default; using (var cacheContext = new SourceCacheContext()) { var logger = new LoggerAdapter(this); var downloadContext = new PackageDownloadContext(cacheContext) { ParentId = OperationId, ClientPolicyContext = ClientPolicyContext.GetClientPolicy(ConfigSettings, logger) }; var result = await PackageRestoreManager.RestoreMissingPackagesAsync( solutionDirectory, packages, this, downloadContext, logger, Token); if (result.Restored) { await PackageRestoreManager.RaisePackagesMissingEventForSolutionAsync(solutionDirectory, CancellationToken.None); return; } } } ErrorHandler.HandleException( new InvalidOperationException(Resources.Cmdlet_MissingPackages), terminating: true, errorId: NuGetErrorId.MissingPackages, category: ErrorCategory.InvalidOperation); } }
/// <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 async Task <IEnumerable <IVsProjectAdapter> > GetProjectsByNameAsync(string[] projectNames) { var result = new List <IVsProjectAdapter>(); var allValidProjectNames = await GetAllValidProjectNamesAsync(); 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 = allValidProjectNames .Where(s => pattern.IsMatch(s)) .ToArray(); // 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 ((matches.Length == 0) && !WildcardPattern.ContainsWildcardCharacters(projectName)) { ErrorHandler.WriteProjectNotFoundError(projectName, terminating: false); } foreach (var match in matches) { var matchedProject = await VsSolutionManager.GetNuGetProjectAsync(match); if (matchedProject != null) { var projectAdapter = await VsSolutionManager.GetVsProjectAdapterAsync(matchedProject); result.Add(projectAdapter); } } } return(result); }
/// <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); } } }
/// <summary> /// Check if solution is open. If not, throw terminating error /// </summary> protected void CheckSolutionState() { if (!VsSolutionManager.IsSolutionOpen) { ErrorHandler.ThrowSolutionNotOpenTerminatingError(); } NuGetUIThreadHelper.JoinableTaskFactory.Run(async() => { if (!(await VsSolutionManager.IsSolutionAvailableAsync())) { ErrorHandler.HandleException( new InvalidOperationException(VisualStudio.Strings.SolutionIsNotSaved), terminating: true, errorId: NuGetErrorId.UnsavedSolution, category: ErrorCategory.InvalidOperation); } }); }
protected async Task CheckPackageManagementFormat() { // check if Project has any packages installed if (!(await Project.GetInstalledPackagesAsync(Token)).Any()) { var packageManagementFormat = new PackageManagementFormat(ConfigSettings); // if default format is PackageReference then update NuGet Project if (packageManagementFormat.SelectedPackageManagementFormat == 1) { var newProject = await VsSolutionManager.UpgradeProjectToPackageReferenceAsync(Project); if (newProject != null) { Project = newProject; } } } }
private void Preprocess() { UseRemoteSourceOnly = ListAvailable.IsPresent || (!String.IsNullOrEmpty(Source) && !Updates.IsPresent); UseRemoteSource = ListAvailable.IsPresent || Updates.IsPresent || !String.IsNullOrEmpty(Source); CollapseVersions = !AllVersions.IsPresent; UpdateActiveSourceRepository(Source); GetNuGetProject(ProjectName); // When ProjectName is not specified, get all of the projects in the solution if (string.IsNullOrEmpty(ProjectName)) { Projects = VsSolutionManager.GetNuGetProjects().ToList(); } else { Projects = new List <NuGetProject> { Project }; } }
/// <summary> /// Get the default NuGet Project /// </summary> /// <param name="projectName"></param> protected void GetNuGetProject(string projectName = null) { if (string.IsNullOrEmpty(projectName)) { Project = VsSolutionManager.DefaultNuGetProject; if (VsSolutionManager.IsSolutionAvailable && Project == null) { ErrorHandler.WriteProjectNotFoundError("Default", terminating: true); } } else { Project = VsSolutionManager.GetNuGetProject(projectName); if (VsSolutionManager.IsSolutionAvailable && Project == null) { ErrorHandler.WriteProjectNotFoundError(projectName, terminating: true); } } }
/// <summary> /// Get the default NuGet Project /// </summary> /// <param name="projectName"></param> protected async Task GetNuGetProjectAsync(string projectName = null) { if (string.IsNullOrEmpty(projectName)) { Project = await VsSolutionManager.GetDefaultNuGetProjectAsync(); if ((await VsSolutionManager.IsSolutionAvailableAsync()) && Project == null) { ErrorHandler.WriteProjectNotFoundError("Default", terminating: true); } } else { Project = await VsSolutionManager.GetNuGetProjectAsync(projectName); if ((await VsSolutionManager.IsSolutionAvailableAsync()) && Project == null) { ErrorHandler.WriteProjectNotFoundError(projectName, terminating: true); } } }
private void Preprocess() { UseRemoteSourceOnly = ListAvailable.IsPresent || (!String.IsNullOrEmpty(Source) && !Updates.IsPresent); UseRemoteSource = ListAvailable.IsPresent || Updates.IsPresent || !String.IsNullOrEmpty(Source); CollapseVersions = !AllVersions.IsPresent; UpdateActiveSourceRepository(Source); NuGetUIThreadHelper.JoinableTaskFactory.Run(async() => { await GetNuGetProjectAsync(ProjectName); // When ProjectName is not specified, get all of the projects in the solution if (string.IsNullOrEmpty(ProjectName)) { Projects = (await VsSolutionManager.GetNuGetProjectsAsync()).ToList(); } else { Projects = new List <NuGetProject> { Project }; } }); }
protected override void Preprocess() { base.Preprocess(); ParseUserInputForVersion(); if (!_projectSpecified) { Projects = VsSolutionManager.GetNuGetProjects().ToList(); } else { Projects = new List <NuGetProject> { Project }; } if (Reinstall) { ActionType = NuGetActionType.Reinstall; } else { ActionType = NuGetActionType.Update; } }
/// <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(); 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 = allValidProjectNames .Where(s => pattern.IsMatch(s)) .Select(s => VsSolutionManager.GetNuGetProject(s)) .Where(p => p != null) .ToList(); foreach (var project in matches) { yield return(VsSolutionManager.GetVsProjectAdapter(project)); } // 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 ((matches.Count == 0) && !WildcardPattern.ContainsWildcardCharacters(projectName)) { ErrorHandler.WriteProjectNotFoundError(projectName, terminating: false); } } }
protected override void Preprocess() { base.Preprocess(); ParseUserInputForVersion(); if (!_projectSpecified) { Projects = NuGetUIThreadHelper.JoinableTaskFactory.Run(async() => await VsSolutionManager.GetNuGetProjectsAsync()).ToList(); } else { Projects = new List <NuGetProject> { Project }; } if (Reinstall) { ActionType = NuGetActionType.Reinstall; } else { ActionType = NuGetActionType.Update; } }
protected void RefreshUI(IEnumerable <NuGetProjectAction> actions) { var resolvedActions = actions.Select(action => new ResolvedAction(action.Project, action)); VsSolutionManager.OnActionsExecuted(resolvedActions); }