Ejemplo n.º 1
0
        public static SolutionInfo ConsolidateSolutions(string newName, string baseDir, out string warnings, params SolutionInfo[] solutions)
        {
            var allProjects = solutions.SelectMany(s => s.Projects).Distinct(BaseProject.ProjectGuidLocationComparer).ToList();

            warnings = SolutionDiagnostics.DiagnoseDupeGuids(solutions);

            var scattered   = from solution in solutions where solution.BaseDir != baseDir select solution;
            var cleanedList = (from solution in solutions where solution.BaseDir == baseDir select solution).ToList();

            //base directory is output directory
            //copy everything from solutions which aren't in the base directory into the base directory
            foreach (var slnInfo in scattered)
            {
                var source = slnInfo.BaseDir;
                var dest   = baseDir;

                FileUtil.RecursiveFileCopy(source, dest);

                //add new items to cleaned list
                SolutionInfo copiedSln = SolutionInfo.Parse(dest + "\\" + slnInfo.Name + ".sln");
                cleanedList.Add(copiedSln);

                //remove the old sln file which was copied
                File.Delete(baseDir + "\\" + slnInfo.Name + ".sln");
            }

            return(MergeSolutions(newName, baseDir, out warnings, cleanedList.ToArray()));
        }
Ejemplo n.º 2
0
        public static SolutionInfo MergeSolutions(string newName, string baseDir, out string warnings, params SolutionInfo[] solutions)
        {
            var allProjects = solutions.SelectMany(s => s.Projects).Distinct(BaseProject.ProjectGuidLocationComparer).ToList();

            warnings = SolutionDiagnostics.DiagnoseDupeGuids(solutions);

            var mergedSln = new SolutionInfo(newName, baseDir, solutions[0].PropsSection, new NestedProjectsInfo())
            {
                Projects = allProjects
            };

            mergedSln.CreateNestedDirs()
            .Projects.ForEach(pr => pr.ProjectInfo.SolutionInfo = mergedSln);

            return(mergedSln);
        }
Ejemplo n.º 3
0
        public static SolutionInfo MergeSolutions(string newName, string baseDir, out string warnings, params SolutionInfo[] solutions)
        {
            var duplicates = solutions
                             .SelectMany(s => s.Projects)
                             .GroupBy(p => p.Guid)
                             .Where(g => g.Count() > 1)
                             .Select(y => y.Key)
                             .ToList();

            var relocation  = new Dictionary <string, Dictionary <string, string> >();
            var allProjects = new List <BaseProject>();

            foreach (var solution in solutions)
            {
                foreach (var project in solution.Projects)
                {
                    var shouldRelocate = duplicates.Contains(project.Guid);
                    if (shouldRelocate)
                    {
                        // Whenever we see this project GUID in the nested project section,
                        // we are going to replace it with this new GUID.
                        var newGuid = $"{{{Guid.NewGuid().ToString()}}}";

                        if (relocation.ContainsKey(solution.Name))
                        {
                            relocation[solution.Name].Add(project.Guid, newGuid);
                        }
                        else
                        {
                            relocation.Add(solution.Name, new Dictionary <string, string> {
                                { project.Guid, newGuid }
                            });
                        }

                        // We have a copy of this GUID - update it now.
                        project.Guid = newGuid;
                    }

                    allProjects.Add(project);
                }
            }

            warnings = SolutionDiagnostics.DiagnoseDupeGuids(solutions);

            var mergedSln = new SolutionInfo(newName, baseDir, solutions[0].PropsSection, new NestedProjectsInfo())
            {
                Projects = allProjects
            };

            // Parse the following section in each .sln file:
            //
            //     GlobalSection(NestedProjects) = preSolution
            //         {GUID A} = {GUID B}
            //         {GUID C} = {GUID D}
            //         ...
            //     EndGlobalSection
            //
            // and determine its original location associations (e.g. A belongs to B, C belongs to D).
            char[] charsToTrim = { ' ', '\t' };
            var    nested      =
                new Regex(@"GlobalSection\(NestedProjects\)\s=\spreSolution(?<Section>[\s\S]*?)EndGlobalSection",
                          RegexOptions.Multiline | RegexOptions.Compiled);
            var allNestedProjects = new Dictionary <string, Dictionary <string, string> >();

            foreach (var solution in solutions)
            {
                var found = nested.Match(solution.Text).Groups["Section"].Value;
                if (string.IsNullOrEmpty(found))
                {
                    continue;
                }

                var value = found
                            .Trim(charsToTrim)
                            .Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
                            .Select(part => part.Split('='))
                            .ToDictionary(
                    split =>     // GUID of this project.
                {
                    var newGuid = string.Empty;
                    var key     = split[0].Trim(charsToTrim);
                    if (relocation.ContainsKey(solution.Name) &&
                        relocation[solution.Name].TryGetValue(key, out newGuid))
                    {
                        key = newGuid;
                    }

                    return(key);
                },
                    split =>     // GUID of location where this project belongs.
                {
                    var newGuid = string.Empty;
                    var key     = split[1].Trim(charsToTrim);
                    if (relocation.ContainsKey(solution.Name) &&
                        relocation[solution.Name].TryGetValue(key, out newGuid))
                    {
                        key = newGuid;
                    }

                    return(key);
                });

                allNestedProjects.Add(solution.Name, value);
            }

            mergedSln.CreateNestedDirs(allNestedProjects)
            .Projects.ForEach(pr =>
            {
                pr.ProjectInfo.SolutionInfo = mergedSln;
            });

            return(mergedSln);
        }