Inheritance: ICloneable
        public Solution Read(string solutionFile)
        {
            var solution = new Solution();

            using (var reader = new StreamReader(solutionFile))
            {
                var lines = new List<string>();
                string text;
                while ((text = reader.ReadLine()) != null)
                {
                    lines.Add(text);
                }

                int lineIndex = 0;
                ParseHeader(solution, lines, ref lineIndex);
                ParseProjects(solution, lines, ref lineIndex);
                ParseGlobal(solution, lines, ref lineIndex);
                ParseNestedProjects(solution);

                var section = solution.FindGlobalSection(Solution.NestedProjectsSectionName);
                if (section != null)
                {
                    solution.Global.Sections.Remove(section);
                }
            }

            return solution;
        }
        private void ParseProjects(Solution solution, List<string> lines, ref int lineIndex)
        {
            bool more = true;
            while (more && lineIndex < lines.Count)
            {
                string line = lines[lineIndex++];
                if (!line.StartsWith("Project("))
                {
                    more = false;
                }
                else
                {
                    var project = new SolutionProject();
                    int index = line.IndexOf('{') + 1;
                    project.ProjectTypeId = new Guid(line.Substring(index, line.IndexOf('}', index) - index));
                    index = line.LastIndexOf('{') + 1;
                    project.ProjectId = new Guid(line.Substring(index, line.IndexOf('}', index) - index));
                    index = line.IndexOf("= \"") + 3;
                    project.Name = line.Substring(index, line.IndexOf('\"', index) - index);
                    index = line.IndexOf(", \"", index) + 3;
                    project.Path = line.Substring(index, line.IndexOf('\"', index) - index);

                    ParseProjectSections(project, lines, ref lineIndex);

                    solution.Projects.Add(project);
                }

                if (!more)
                {
                    lineIndex--;
                }
            }
        }
        public void Write(string solutionFile, Solution solution)
        {
            this._solutionFile = solutionFile;
            this._solution = solution;

            using (StreamWriter writer = File.CreateText(this._solutionFile))
            {
                WriteHeader(writer);
                WriteProjects(writer);
                WriteGlobal(writer);
            }
        }
        private void ParseHeader(Solution solution, List<string> lines, ref int lineIndex)
        {
            bool more = true;
            while (more && lineIndex < lines.Count)
            {
                string line = lines[lineIndex++];
                if (line.StartsWith("Project("))
                {
                    more = false;
                }
                else if (line.Contains("Format Version"))
                {
                    string fileVersion = line.Split(' ').Last();
                    switch (fileVersion)
                    {
                        case "10.00":
                            solution.FileVersion = SolutionFileVersion.VisualStudio2008;
                            break;

                        case "11.00":
                            solution.FileVersion = SolutionFileVersion.VisualStudio2010;
                            break;

                        case "12.00":
                            solution.FileVersion = SolutionFileVersion.VisualStudio2012;
                            break;

                        default:
                            throw new InvalidOperationException("Unsupported Visual Studio solution file");
                    }
                }
                else if (line.Contains("Visual Studio"))
                {
                    ;
                }

                if (!more)
                {
                    lineIndex--;
                }
            }
        }
Beispiel #5
0
 public object Clone()
 {
     var solution = new Solution();
     solution.FileVersion = this.FileVersion;
     solution.Header = this.Header.Clone() as SolutionHeader;
     foreach (var project in this.Projects)
     {
         solution.Projects.Add(project.Clone() as SolutionProject);
     }
     solution.Global = this.Global.Clone() as SolutionGlobal;
     return solution;
 }
        private void ParseNestedProjects(Solution solution)
        {
            SolutionGlobal.Section nestedProjectsSection = null;
            foreach (var section in solution.Global.Sections)
            {
                if (section.SectionName == Solution.NestedProjectsSectionName)
                {
                    foreach (string item in section.SectionItems)
                    {
                        int index = item.IndexOf('{') + 1;
                        Guid projectId = new Guid(item.Substring(index, item.IndexOf('}', index) - index));
                        index = item.LastIndexOf('{') + 1;
                        Guid parentProjectId = new Guid(item.Substring(index, item.IndexOf('}', index) - index));

                        var project = solution.FindProject(projectId);
                        var parentProject = solution.FindProject(parentProjectId);
                        if (project != null && parentProject != null)
                        {
                            project.ParentProject = parentProject;
                        }
                    }
                }
            }
            if (nestedProjectsSection != null)
            {
                solution.Global.Sections.Remove(nestedProjectsSection);
            }
        }
 private void ParseGlobal(Solution solution, List<string> lines, ref int lineIndex)
 {
     bool more = true;
     while (more && lineIndex < lines.Count)
     {
         string line = lines[lineIndex++];
         if (line.StartsWith("Global"))
         {
             ;
         }
         else if (line.StartsWith("EndGlobal"))
         {
             more = false;
         }
         else if (line.StartsWith("\tGlobalSection"))
         {
             --lineIndex;
             ParseGlobalSection(solution.Global, lines, ref lineIndex);
         }
     }
 }
        private void MergeSolutionFolders(Solution solution)
        {
            var solutionFolders = GenerateSolutionFolders(solution);
            foreach (var project in solution.Projects)
            {
                if (project.IsFolder)
                {
                    string solutionFolderPath = solution.GetSolutionFolderPath(project);
                    if (solutionFolders.Contains(solutionFolderPath))
                    {
                        solutionFolders.Remove(solutionFolderPath);
                    }
                }
            }

            // Add new solution folders that are not present in current solution
            var solutionFolderMap = new Dictionary<string, Guid>();
            foreach (var solutionFolder in solutionFolders)
            {
                SolutionProject parentProject = null;
                int index = solutionFolder.LastIndexOf('.');
                if (index > 0)
                {
                    parentProject = solution.FindFolderWithPath(solutionFolder.Substring(0, index));
                }

                var project = new SolutionProject()
                {
                    ProjectTypeId = ProjectType.SolutionFolder.ProjectGuid,
                    ProjectId = Guid.NewGuid(),
                    ParentProject = parentProject,
                    Name = solution.GetSolutionFolderName(solutionFolder),
                    Path = solution.GetSolutionFolderName(solutionFolder),
                };

                solution.Projects.Add(project);
                solutionFolderMap.Add(solutionFolder, project.ProjectId);
            }

            // Now attach parents to projects
            foreach (var project in solution.Projects)
            {
                string folderPath = EvaluateProjectFolderPath(solution, project, _options.SolutionFolderLevels);
                if (folderPath != null)
                {
                    SolutionProject parentProject = solution.FindFolderWithPath(folderPath);
                    if (parentProject != null)
                    {
                        project.ParentProject = parentProject;
                    }
                }
            }
        }
        public Solution Build(Solution oldSolution, IList<SolutionProject> projects)
        {
            this.NumberOfProjectsAdded = 0;
            this.NumberOfProjectsRemoved = 0;

            var addProject = new Action<Solution, SolutionProject>((s, p) =>
                {
                    s.Projects.Add(p);
                    ++this.NumberOfProjectsAdded;
                    _logger.Write(string.Format("Added project {0}", p.Name));
                });

            var removeProject = new Action<Solution, SolutionProject>((s, p) =>
            {
                s.Projects.Remove(p);
                _logger.Write(string.Format("Removed project {0}", p.Name));
                ++this.NumberOfProjectsRemoved;
            });

            Solution newSolution;
            if (oldSolution == null || _options.UpdateMode == SolutionUpdateMode.Replace)
            {
                newSolution = new Solution();
                newSolution.FileVersion = _options.SolutionFileVersion;

                foreach (var project in projects)
                {
                    addProject(newSolution, project);
                }
            }
            else
            {
                newSolution = oldSolution.Clone() as Solution;

                foreach (var project in projects)
                {
                    if (!newSolution.Projects.Contains(project))
                    {
                        addProject(newSolution, project);
                    }
                }

                if (_options.UpdateMode == SolutionUpdateMode.AddRemove)
                {
                    var projectsToRemove = new List<SolutionProject>();
                    foreach (var project in newSolution.Projects)
                    {
                        if (ProjectTypes.Find(project.ProjectTypeId) != null && !projects.Contains(project))
                        {
                            projectsToRemove.Add(project);
                        }
                    }

                    foreach (var project in projectsToRemove)
                    {
                        removeProject(newSolution, project);
                    }
                }
            }

            if (_options.SolutionFolderLevels != 0)
            {
                MergeSolutionFolders(newSolution);
            }

            return newSolution;
        }
        internal string EvaluateProjectFolderPath(Solution solution, SolutionProject project, int folderLevels = -1)
        {
            if (folderLevels < 0)
                folderLevels = _options.SolutionFolderLevels;

            if (project.IsFolder)
            {
                string solutionFolderPath = solution.GetSolutionFolderPath(project);
                return solutionFolderPath.Contains(".") ? solutionFolderPath.Substring(0, solutionFolderPath.LastIndexOf('.')) : null;
            }
            else
            {
                string folderBaseName = SkipCommonLevels(EvaluateFolderBaseName(project));
                int subnameCount = 0;
                int index = 0;
                while (subnameCount < folderLevels && index >= 0)
                {
                    subnameCount++;
                    index = folderBaseName.IndexOf('.', index + 1);
                }
                if (index < 0)
                {
                    index = folderBaseName.Length;
                }
                return folderBaseName.Substring(0, index);
            }
        }
 internal List<string> GenerateSolutionFolders(Solution solution)
 {
     var solutionFolders = new List<string>();
     int startLevel = 1;
     int endLevel = _options.SolutionFolderLevels;
     for (int level = startLevel; level <= endLevel; level++)
     {
         var folders = (from p in solution.Projects
                        where !p.IsFolder
                        select EvaluateProjectFolderPath(solution, p, level)).Distinct();
         solutionFolders.AddRange(folders);
     }
     var sortedFolders = from f in solutionFolders orderby f select f;
     return sortedFolders.Distinct().ToList();
 }