public void InitializeSolution(Solution solution)
        {

            ReadNextLine();
            solution.Version = GetSolutionVersion(_currentLine);

            Match match;
            SolutionRootNodeType nodeType = SolutionRootNodeType.Unknown;

            ReadNextLine();

            while ((nodeType = GetCurrentRootNodeType(out match)) != SolutionRootNodeType.Unknown)
            {
                switch (nodeType)
                {
                    case SolutionRootNodeType.Project:
                        solution.Nodes.Add(ReadProjectEntry(solution, match));
                        break;
                    case SolutionRootNodeType.Property:
                        break;
                    case SolutionRootNodeType.Global:
                        ReadGlobalSection(solution);
                        break;
                }
                ReadNextLine();
            }

            FixNestedProjects(solution);
        }
        public void WriteSolution(Solution solution)
        {
            WriteSolutionHeader(solution.Version);

            foreach (var node in solution.Nodes)
            {
                WriteNode(node);
            }

            _writer.WriteLine("Global");

            foreach (var section in solution.GlobalSections)
            {
                WriteSection(section);
            }

            _writer.WriteLine("EndGlobal");

        }
        private void FixNestedProjects(Solution solution)
        {
            var nestedProjectsSection = solution.GlobalSections.FirstOrDefault(x => x.Name == "NestedProjects");
            if (nestedProjectsSection != null)
            {
                foreach (var item in nestedProjectsSection)
                {
                    Guid projectGuid;
                    Guid folderGuid;

                    if (Guid.TryParse(item.Key, out projectGuid) && Guid.TryParse(item.Value, out folderGuid))
                    {   
                        var project = solution.GetSolutionNode(x => x.ObjectGuid == projectGuid);
                        var folder = (SolutionFolder)solution.GetSolutionNode(x => x.ObjectGuid == folderGuid);
                        if (project != null && folder != null)
                        {
                            project.Parent.Nodes.Remove(project);
                            folder.Nodes.Add(project);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Opens an existing solution file and parses out the sub projects.
        /// </summary>
        /// <param name = "solutionFile">The file path to the solution file.</param>
        public static Solution OpenSolution(string solutionFile)
        {
            using (var reader = new StreamReader(solutionFile))
            {
                using (var solutionReader = new SolutionReader(reader))
                {
                    var solution = new Solution();
                    solution.FilePath = new FilePath(solutionFile);
                    solution.Name = solution.FilePath.FileName;
                    solutionReader.InitializeSolution(solution);

                    var settingsPath = solution.FilePath.ChangeExtension(".litesettings").FullPath;
                    if (File.Exists(settingsPath))
                    {
                        try
                        {
                            var settings = SolutionSettings.ReadSettings(settingsPath);
                            solution.Settings = settings;
                        }
                        catch
                        {
                            // TODO: notify user
                        }
                    }
                    else
                    {
                        solution.Settings = new SolutionSettings();
                    }

                    solution.InitializeEventHandlers();
                    solution.Settings.HasChanged = false;

                    return solution;
                }
            }
        }
 public SolutionNode(Solution solution, IconProvider iconProvider)
     : base(solution, iconProvider)
 {
     Solution = solution;
 }
        private void ReadGlobalSection(Solution parent)
        {
            Match match;

            ReadNextLine();

            while (IsAtSection(out match))
            {
                var section = ReadSection(match);
                if (section.SectionType != "Global")
                    ThrowSyntaxError("Expected \"GlobalSection\" keyword.");

                parent.GlobalSections.Add(section);

                ReadNextLine();
            }

            if (_currentLine != "EndGlobal")
                ThrowSyntaxError("Expected \"EndGlobal\" keyword.");
        }
        private SolutionFolder ReadProjectEntry(Solution parent, Match match)
        {
            string path = match.Groups["HintPath"].Value;
            SolutionFolder entry;

            Guid typeID = Guid.Parse(match.Groups["TypeGuid"].Value);

            if (typeID == SolutionFolder.SolutionFolderGuid)
            {
                entry = new SolutionFolder();
            }
            else
            {
                entry = new ProjectEntry();
            }

            entry.TypeGuid = typeID;
            entry.Name = match.Groups["Name"].Value;
            entry.FilePath = new FilePath(parent.FilePath.ParentDirectory.FullPath, match.Groups["HintPath"].Value);
            entry.ObjectGuid = Guid.Parse(match.Groups["ProjectGuid"].Value);

            ReadNextLine();

            Match sectionMatch;
            while (IsAtSection(out sectionMatch))
            {
                entry.Sections.Add(ReadSection(sectionMatch));
                ReadNextLine();
            }
            
            if (_currentLine != "EndProject")
                ThrowSyntaxError("Expected \"EndProject\" keyword.");

            return entry;
        }