Example #1
0
        /// <summary>
        /// Loads the specified project file name.
        /// </summary>
        /// <param name="projectFileName">Name of the project file.</param>
        /// <returns>VSProject class containing project information.</returns>
        public static VSProject Load(string projectFileName)
        {
            using (Stream stream = File.OpenRead(projectFileName))
            {
                VSProject data = new VSProject(projectFileName)
                {
                    _propertiesDictionary = true
                };

                XmlReaderSettings xmlReaderSettings = new XmlReaderSettings
                {
                    IgnoreComments = true,
                    IgnoreProcessingInstructions = true,
                    IgnoreWhitespace             = true,
                };

                using (XmlReader xmlReader = XmlReader.Create(stream, xmlReaderSettings))
                {
                    xmlReader.Read();
                    while (!xmlReader.EOF)
                    {
                        switch (xmlReader.NodeType)
                        {
                        case XmlNodeType.XmlDeclaration:
                            xmlReader.Read();
                            break;

                        case XmlNodeType.Element:
                            if (xmlReader.Name == "Project")
                            {
                                data.ReadProject(xmlReader);
                            }

                            xmlReader.Read();
                            break;

                        default:
                            xmlReader.Read();
                            continue;
                        }
                    }
                }

                return(data);
            }
        }
Example #2
0
        /// <summary>
        /// Loads the specified VisualStudio solution file and returns a <see cref="VSSolution"/> representing the solution.
        /// </summary>
        /// <param name="fileName">The name of the solution file.</param>
        /// <returns>A <see cref="VSSolution"/> representing the solution.</returns>
        public static VSSolution Load(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return(null);
            }

            fileName = fileName.Replace(@"\", "/");
            VSSolution solution = new VSSolution(fileName);

            using (Stream stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    VSSolutionFileParser parser = new VSSolutionFileParser(reader);

                    string line = parser.NextLine();

                    Match solutionMatch = RegexSolutionVersion.Match(line);

                    if (solutionMatch.Success == false)
                    {
                        parser.ThrowParserException("Not a solution file.");
                    }

                    solution.SolutionVersion = decimal.Parse(
                        solutionMatch.Groups["version"].Value,
                        CultureInfo.InvariantCulture);

                    while (true)
                    {
                        line = parser.NextLine();

                        if (line == null)
                        {
                            break;
                        }

                        // exit the loop when 'Global' section appears
                        if (RegexGlobal.IsMatch(line))
                        {
                            break;
                        }

                        Match projectMatch = RegexProject.Match(line);

                        if (!projectMatch.Success)
                        {
                            // skip any solution properties
                            if (RegexSolutionProperty.IsMatch(line))
                            {
                                continue;
                            }

                            // if nothing matches, we have a problem
                            parser.ThrowParserException(
                                string.Format(
                                    CultureInfo.InvariantCulture,
                                    "Could not parse solution file (line {0}): '{1}'.",
                                    parser.LineCount,
                                    line));
                        }

                        Guid   projectGuid     = new Guid(projectMatch.Groups["projectGuid"].Value);
                        string projectName     = projectMatch.Groups["name"].Value;
                        string projectFileName = projectMatch.Groups["path"].Value;
                        Guid   projectTypeGuid = new Guid(projectMatch.Groups["projectTypeGuid"].Value);

                        if (projectTypeGuid == VSProjectType.SolutionFolderProjectType.ProjectTypeGuid)
                        {
                            var project = new VSSolutionFilesInfo(
                                solution,
                                projectGuid,
                                projectName,
                                projectTypeGuid);

                            solution._solutionFolders.Add(project);
                            project.Parse(parser);
                        }
                        else
                        {
                            var project = new VSProject(
                                solution,
                                projectGuid,
                                projectName,
                                new LocalPath(projectFileName),
                                projectTypeGuid);
                            solution._projects.Add(project);
                            project.Parse(parser);
                        }
                    }
                }
            }

            return(solution);
        }