Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 3
0
        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 => configuration.GetActualPath(p.RelativePath) == 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);
            }
        }
Ejemplo n.º 4
0
        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)) // check that it's not already in the solution
                {
                    projects.Add("\"" + configuration.GetActualPath(mapping.Value) + "\"");
                }
            }

            if (projects.Any())
            {
                await ExecuteCommandAsync(
                    "dotnet sln \"" + configuration.ActualSolution + "\" add " + string.Join(" ", projects), host);
            }
        }
Ejemplo n.º 5
0
        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");
                    }
                }
            }
        }
Ejemplo n.º 6
0
        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");
                    }
                }
            }
        }