private static void AddPackage(ReferenceSwitcherConfiguration configuration, ProjectInSolution solutionProject, Project project, string packageName, string packageVersion) { var projectName = Path.GetFileNameWithoutExtension(solutionProject.AbsolutePath); var switchedProject = ( from r in configuration.Restore where string.Equals(r.Name, projectName, StringComparison.OrdinalIgnoreCase) select r).FirstOrDefault(); if (switchedProject != null) { var reference = switchedProject.GetSwitchedPackage(packageName); if (!string.IsNullOrEmpty(reference.Include)) { project.AddItem("Reference", reference.Include, reference.Metadata); } else { if (!project.Items.Any(i => i.ItemType == "PackageReference" && i.EvaluatedInclude == packageName)) // check that the reference is not already present { var items = project.AddItem("PackageReference", packageName, packageVersion == null ? Enumerable.Empty <KeyValuePair <string, string> >() : // this is the case if CentralPackageVersions is in use new[] { new KeyValuePair <string, string>("Version", packageVersion) }); items.ToList().ForEach(item => { item.Metadata?.ToList().ForEach(metadata => metadata.Xml.ExpressedAsAttribute = true); }); } } } }
private static void SwitchToProjects(ReferenceSwitcherConfiguration configuration, IConsoleHost host) { var solution = SolutionFile.Parse(configuration.ActualSolution); foreach (var solutionProject in solution.ProjectsInOrder) { if (solutionProject.ProjectType != SolutionProjectType.SolutionFolder) { try { using (var projectInformation = ProjectExtensions.LoadProject(solutionProject.AbsolutePath)) { foreach (var mapping in configuration.Mappings) { var packageName = mapping.Key; var projectPaths = mapping.Value.Select(p => configuration.GetActualPath(p)).ToList(); var switchedProjects = SwitchToProject(configuration, solutionProject, projectInformation, packageName, projectPaths, host); foreach (var s in switchedProjects) { host.WriteMessage("Project " + Path.GetFileName(s.Key) + " packages:\n"); host.WriteMessage(" " + packageName + " v" + s.Value + "\n replaced by:\n"); projectPaths.ForEach(p => host.WriteMessage(" " + Path.GetFileName(p) + "\n")); } } } } catch (Exception e) { host.WriteError("The project '" + solutionProject.AbsolutePath + "' could not be loaded: " + e.Message + "\n"); } } } }
private async Task AddProjectsToSolutionAsync(ReferenceSwitcherConfiguration configuration, IConsoleHost host) { var solution = SolutionFile.Parse(configuration.ActualSolution); var projects = new List <string>(); var solutionFolderArg = ""; foreach (var mapping in configuration.Mappings) { foreach (var path in mapping.Value) { if (solution.ProjectsInOrder.All(p => p.ProjectName != mapping.Key)) // check that it's not already in the solution { projects.Add("\"" + configuration.GetActualPath(path) + "\""); } } } if (!string.IsNullOrWhiteSpace(configuration.SolutionFolder)) { solutionFolderArg = $" --solution-folder {configuration.SolutionFolder}"; } if (projects.Any()) { await ExecuteCommandAsync( "dotnet", "sln \"" + configuration.ActualSolution + "\" add " + string.Join(" ", projects) + solutionFolderArg, host); } }
private static void AddPackage(ReferenceSwitcherConfiguration configuration, ProjectInSolution solutionProject, Project project, string packageName, string packageVersion) { var projectName = Path.GetFileNameWithoutExtension(solutionProject.AbsolutePath); var switchedProject = ( from r in configuration.Restore where string.Equals(r.Name, projectName, StringComparison.OrdinalIgnoreCase) select r).FirstOrDefault(); if (switchedProject != null) { var reference = switchedProject.GetSwitchedPackage(packageName); if (!string.IsNullOrEmpty(reference.Include)) { project.AddItem("Reference", reference.Include, reference.Metadata); } else { var items = project.AddItem("PackageReference", packageName, new[] { new KeyValuePair <string, string>("Version", packageVersion) }); items.ToList().ForEach(item => { item.Metadata?.ToList().ForEach(metadata => metadata.Xml.ExpressedAsAttribute = true); }); } } }
private static IReadOnlyDictionary <string, string> SwitchToProject(ReferenceSwitcherConfiguration configuration, ProjectInSolution solutionProject, ProjectInformation projectInformation, string packageName, string projectPath, IConsoleHost host) { var switchedProjects = new Dictionary <string, string>(); var project = projectInformation.Project; var projectDirectory = Path.GetFullPath(Path.GetDirectoryName(solutionProject.AbsolutePath)); foreach (var item in project.Items .Where(i => i.ItemType == "PackageReference" || i.ItemType == "Reference").ToList()) { var packageReference = item.EvaluatedInclude.Split(',').First().Trim(); if (packageReference == packageName) { var isPackageReference = (item.ItemType == "PackageReference"); var packageVersion = item.Metadata.SingleOrDefault(m => m.Name == "Version")?.EvaluatedValue ?? null; project.RemoveItem(item); project.AddItem("ProjectReference", PathUtilities.ToRelativePath(projectPath, projectDirectory)); SetRestoreProjectInformation(configuration, item, project.FullPath, isPackageReference, packageName, packageVersion); switchedProjects[solutionProject.AbsolutePath] = packageVersion; } } project.Save(); return(switchedProjects); }
private static void SetRestoreProjectInformation(ReferenceSwitcherConfiguration configuration, ProjectItem item, string projectFullPath, bool isPackageReference, string packageName, string packageVersion) { var projectName = Path.GetFileNameWithoutExtension(projectFullPath); var restoreProjectInformation = (from r in configuration.Restore where string.Equals(r.Name, projectName, StringComparison.OrdinalIgnoreCase) select r).FirstOrDefault(); if (restoreProjectInformation is null) { restoreProjectInformation = new RestoreProjectInformation() { Name = projectName }; configuration.Restore.Add(restoreProjectInformation); } SwitchedPackage switchedPackage = null; if (restoreProjectInformation.Packages != null) { switchedPackage = ( from p in restoreProjectInformation.Packages where string.Equals(p.PackageName, packageName, StringComparison.OrdinalIgnoreCase) select p).FirstOrDefault(); } else { restoreProjectInformation.Packages = new List <SwitchedPackage>(); } if (switchedPackage is null) { switchedPackage = new SwitchedPackage(); restoreProjectInformation.Packages.Add(switchedPackage); } if (!isPackageReference) { switchedPackage.Include = item.EvaluatedInclude; if (item.Metadata.Any()) { switchedPackage.Metadata = new List <KeyValuePair <string, string> >(); foreach (var metadata in item.Metadata) { switchedPackage.Metadata.Add( new KeyValuePair <string, string>(metadata.Name, metadata.EvaluatedValue)); } } } switchedPackage.PackageName = packageName; switchedPackage.PackageVersion = packageVersion; }
public override async Task <object> RunAsync(CommandLineProcessor processor, IConsoleHost host) { var configuration = ReferenceSwitcherConfiguration.Load(Configuration); SwitchToPackages(host, configuration); await RemoveProjectsFromSolutionAsync(configuration, host); configuration.Save(); return(null); }
private static IReadOnlyList <(string ProjectPath, string PackageVersion)> SwitchToPackage( ReferenceSwitcherConfiguration configuration, ProjectInSolution solutionProject, ProjectInformation projectInformation, List <string> switchedProjectPaths, string switchedPackageName, List <string> mappedProjectFilePaths, IConsoleHost host) { var switchedProjects = new List <(string ProjectPath, string PackageVersion)>(); var absoluteProjectPaths = switchedProjectPaths.Select(p => PathUtilities.ToAbsolutePath(p, Directory.GetCurrentDirectory())).ToList(); var project = projectInformation.Project; var projectName = Path.GetFileNameWithoutExtension(solutionProject.AbsolutePath); var projectFileName = Path.GetFileName(solutionProject.AbsolutePath); var projectDirectory = Path.GetDirectoryName(solutionProject.AbsolutePath); // do not modify mapped projects unless we are always keeping them in the solution if (!mappedProjectFilePaths.Contains(projectFileName) || !configuration.RemoveProjects) { var restoreProjectInformation = ( from r in configuration.Restore where string.Equals(r.Name, projectName, StringComparison.OrdinalIgnoreCase) select r).FirstOrDefault(); if (restoreProjectInformation != null) { var count = 0; var matchingProjectReferences = project.Items.Where ( i => i.ItemType == "ProjectReference" && absoluteProjectPaths.Contains(PathUtilities.ToAbsolutePath(i.EvaluatedInclude, projectDirectory)) ).ToList(); foreach (var item in matchingProjectReferences) { project.RemoveItem(item); var packageVersion = GetPackageVersion(restoreProjectInformation, switchedPackageName); AddPackage(configuration, solutionProject, project, switchedPackageName, packageVersion); switchedProjects.Add((solutionProject.AbsolutePath, packageVersion)); count++; } if (count > 0) { ProjectExtensions.SaveWithLineEndings(projectInformation); } } } return(switchedProjects); }
public override async Task <object> RunAsync(CommandLineProcessor processor, IConsoleHost host) { var configuration = ReferenceSwitcherConfiguration.Load(Configuration, host); if (configuration == null) { return(null); } await AddProjectsToSolutionAsync(configuration, host); SwitchToProjects(configuration, host); configuration.Save(); return(null); }
public override async Task <object> RunAsync(CommandLineProcessor processor, IConsoleHost host) { var configuration = ReferenceSwitcherConfiguration.Load(Configuration, host); if (configuration == null) { return(null); } SwitchToPackages(host, configuration); await RemoveProjectsFromSolutionAsync(configuration, host); configuration.Restore = null; // restore information no longer needed configuration.Save(); return(null); }
private void SwitchToPackages(IConsoleHost host, ReferenceSwitcherConfiguration configuration) { var solution = SolutionFile.Parse(configuration.ActualSolution); foreach (var mapping in configuration.Mappings) { var projectPath = mapping.Value.ActualPath; var packageName = mapping.Key; var packageVersion = mapping.Value.Version; var switchedProjects = SwitchToPackage(solution, projectPath, packageName, packageVersion, host); foreach (var s in switchedProjects) { host.WriteMessage(Path.GetFileName(s) + ": \n"); host.WriteMessage(" " + Path.GetFileName(projectPath) + " => " + packageName + " v" + packageVersion + "\n"); } } }
private async Task AddProjectsToSolutionAsync(ReferenceSwitcherConfiguration configuration, IConsoleHost host) { var solution = SolutionFile.Parse(configuration.ActualSolution); var projects = new List <string>(); foreach (var mapping in configuration.Mappings) { if (solution.ProjectsInOrder.All(p => p.ProjectName != mapping.Key)) { projects.Add("\"" + mapping.Value.ActualPath + "\""); } } if (projects.Any()) { await ExecuteCommandAsync("dotnet sln \"" + configuration.ActualSolution + "\" add " + string.Join(" ", projects), host); } }
private async Task RemoveProjectsFromSolutionAsync(ReferenceSwitcherConfiguration configuration, IConsoleHost host) { var solution = SolutionFile.Parse(configuration.ActualSolution); var projects = new List <string>(); foreach (var mapping in configuration.Mappings) { var project = solution.ProjectsInOrder.FirstOrDefault(p => p.ProjectName == mapping.Key); if (project != null) { projects.Add("\"" + mapping.Value.ActualPath + "\""); } } if (projects.Any()) { await ExecuteCommandAsync("dotnet sln \"" + configuration.ActualSolution + "\" remove " + string.Join(" ", projects), host); } }
private static void SwitchToPackages(IConsoleHost host, ReferenceSwitcherConfiguration configuration) { var solution = SolutionFile.Parse(configuration.ActualSolution); var globalProperties = ProjectExtensions.GetGlobalProperties(Path.GetFullPath(configuration.ActualSolution)); var mappedProjectFilePaths = configuration.Mappings.Values .SelectMany(x => x) .Select(p => Path.GetFileName(p)) .ToList(); foreach (var solutionProject in solution.ProjectsInOrder) { if (solutionProject.ProjectType != SolutionProjectType.SolutionFolder && ProjectExtensions.IsSupportedProject(solutionProject.AbsolutePath)) { try { using (var projectInformation = ProjectExtensions.LoadProject(solutionProject.AbsolutePath, globalProperties)) { foreach (var mapping in configuration.Mappings) { var projectPaths = mapping.Value.Select(p => configuration.GetActualPath(p)).ToList(); var packageName = mapping.Key; var switchedProjects = SwitchToPackage( configuration, solutionProject, projectInformation, projectPaths, packageName, mappedProjectFilePaths, host); if (switchedProjects.Count > 0) { host.WriteMessage("Project " + solutionProject.ProjectName + " with project references:\n"); projectPaths.ForEach(p => host.WriteMessage(" " + Path.GetFileName(p) + "\n")); host.WriteMessage(" replaced by package: " + packageName + " v" + switchedProjects.First().PackageVersion + "\n"); } } } } catch (Exception e) { host.WriteError($"The project '{solutionProject.AbsolutePath}' could not be loaded: {e}\n"); } } } }
private void SwitchToProjects(ReferenceSwitcherConfiguration configuration, IConsoleHost host) { var solution = SolutionFile.Parse(configuration.ActualSolution); foreach (var mapping in configuration.Mappings) { var packageName = mapping.Key; var projectPath = mapping.Value.ActualPath; var switchedProjects = SwitchToProject(solution, packageName, projectPath, host); foreach (var s in switchedProjects) { host.WriteMessage(Path.GetFileName(s.Key) + ": \n"); host.WriteMessage(" " + packageName + " v" + s.Value + " => " + Path.GetFileName(projectPath) + "\n"); } if (switchedProjects.Any()) { mapping.Value.Version = switchedProjects.First().Value; } } }
private static void SwitchToPackages(IConsoleHost host, ReferenceSwitcherConfiguration configuration) { var solution = SolutionFile.Parse(configuration.ActualSolution); var mappedProjectFilePaths = configuration.Mappings.Select(m => Path.GetFileName(m.Value)).ToList(); foreach (var solutionProject in solution.ProjectsInOrder) { if (solutionProject.ProjectType != SolutionProjectType.SolutionFolder && ProjectExtensions.IsSupportedProject(solutionProject.AbsolutePath)) { try { using (var projectInformation = ProjectExtensions.LoadProject(solutionProject.AbsolutePath)) { foreach (var mapping in configuration.Mappings) { var projectPath = configuration.GetActualPath(mapping.Value); var packageName = mapping.Key; var switchedProjects = SwitchToPackage( configuration, solutionProject, projectInformation, projectPath, packageName, mappedProjectFilePaths, host); foreach (var s in switchedProjects) { host.WriteMessage(Path.GetFileName(s.ProjectPath) + ": \n"); host.WriteMessage(" " + Path.GetFileName(projectPath) + " => " + packageName + " v" + s.PackageVersion + "\n"); } } } } catch (Exception e) { host.WriteError("The project '" + solutionProject.AbsolutePath + "' could not be loaded: " + e.Message + "\n"); } } } }
private static IReadOnlyDictionary <string, string> SwitchToProject(ReferenceSwitcherConfiguration configuration, ProjectInSolution solutionProject, ProjectInformation projectInformation, string packageName, List <string> projectPaths, IConsoleHost host) { var switchedProjects = new Dictionary <string, string>(); var project = projectInformation.Project; var projectDirectory = Path.GetFullPath(Path.GetDirectoryName(solutionProject.AbsolutePath)); var centralVersioning = project.GetProperty("CentralPackagesFile") != null || // https://github.com/microsoft/MSBuildSdks/tree/master/src/CentralPackageVersions project.GetPropertyValue("ManagePackageVersionsCentrally") == "true"; // https://github.com/NuGet/Home/wiki/Centrally-managing-NuGet-package-versions foreach (var item in project.Items .Where(i => i.ItemType == "PackageReference" || i.ItemType == "Reference").ToList()) { var packageReference = item.EvaluatedInclude.Split(',').First().Trim(); if (packageReference == packageName) { var isPackageReference = (item.ItemType == "PackageReference"); var packageVersion = centralVersioning ? null : item.Metadata.SingleOrDefault(m => m.Name == "Version")?.EvaluatedValue ?? null; project.RemoveItem(item); foreach (var projectPath in projectPaths) { project.AddItem("ProjectReference", PathUtilities.ToRelativePath(projectPath, projectDirectory)); SetRestoreProjectInformation(configuration, item, project.FullPath, isPackageReference, packageName, packageVersion); } switchedProjects[solutionProject.AbsolutePath] = packageVersion; } } ProjectExtensions.SaveWithLineEndings(projectInformation); return(switchedProjects); }
private async Task RemoveProjectsFromSolutionAsync(ReferenceSwitcherConfiguration configuration, IConsoleHost host) { var solution = SolutionFile.Parse(configuration.ActualSolution); var projects = new List <string>(); foreach (var mapping in configuration.Mappings) { foreach (var path in mapping.Value) { var project = solution.ProjectsInOrder.FirstOrDefault (p => PathUtilities.ToAbsolutePath(p.RelativePath, Path.GetDirectoryName(configuration.ActualSolution)) == configuration.GetActualPath(path)); if (project != null) { projects.Add("\"" + configuration.GetActualPath(path) + "\""); } } } if (projects.Any()) { await ExecuteCommandAsync("dotnet", "sln \"" + configuration.ActualSolution + "\" remove " + string.Join(" ", projects), host); } }