A game about forced loneliness, made by TACStudios
at master 80 lines 3.1 kB view raw
1/*--------------------------------------------------------------------------------------------- 2 * Copyright (c) Microsoft Corporation. All rights reserved. 3 * Licensed under the MIT License. See License.txt in the project root for license information. 4 *--------------------------------------------------------------------------------------------*/ 5using System.Collections.Generic; 6using System.Text.RegularExpressions; 7 8namespace Microsoft.Unity.VisualStudio.Editor 9{ 10 internal static class SolutionParser 11 { 12 // Compared to the bridge implementation, we are not returning "{" "}" from Guids 13 private static readonly Regex ProjectDeclaration = new Regex(@"Project\(\""{(?<projectFactoryGuid>.*?)}\""\)\s+=\s+\""(?<name>.*?)\"",\s+\""(?<fileName>.*?)\"",\s+\""{(?<projectGuid>.*?)}\""(?<metadata>.*?)\bEndProject\b", RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.Compiled); 14 private static readonly Regex PropertiesDeclaration = new Regex(@"GlobalSection\((?<name>([\w]+Properties|NestedProjects))\)\s+=\s+(?<type>(?:post|pre)Solution)(?<entries>.*?)EndGlobalSection", RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.Compiled); 15 private static readonly Regex PropertiesEntryDeclaration = new Regex(@"^\s*(?<key>.*?)=(?<value>.*?)$", RegexOptions.Multiline | RegexOptions.ExplicitCapture | RegexOptions.Compiled); 16 17 public static Solution ParseSolutionFile(string filename, IFileIO fileIO) 18 { 19 return ParseSolutionContent(fileIO.ReadAllText(filename)); 20 } 21 22 public static Solution ParseSolutionContent(string content) 23 { 24 return new Solution 25 { 26 Projects = ParseSolutionProjects(content), 27 Properties = ParseSolutionProperties(content) 28 }; 29 } 30 31 private static SolutionProjectEntry[] ParseSolutionProjects(string content) 32 { 33 var projects = new List<SolutionProjectEntry>(); 34 var mc = ProjectDeclaration.Matches(content); 35 36 foreach (Match match in mc) 37 { 38 projects.Add(new SolutionProjectEntry 39 { 40 ProjectFactoryGuid = match.Groups["projectFactoryGuid"].Value, 41 Name = match.Groups["name"].Value, 42 FileName = match.Groups["fileName"].Value, 43 ProjectGuid = match.Groups["projectGuid"].Value, 44 Metadata = match.Groups["metadata"].Value 45 }); 46 } 47 48 return projects.ToArray(); 49 } 50 51 private static SolutionProperties[] ParseSolutionProperties(string content) 52 { 53 var properties = new List<SolutionProperties>(); 54 var mc = PropertiesDeclaration.Matches(content); 55 56 foreach (Match match in mc) 57 { 58 var sp = new SolutionProperties 59 { 60 Entries = new List<KeyValuePair<string, string>>(), 61 Name = match.Groups["name"].Value, 62 Type = match.Groups["type"].Value 63 }; 64 65 var entries = match.Groups["entries"].Value; 66 var mec = PropertiesEntryDeclaration.Matches(entries); 67 foreach (Match entry in mec) 68 { 69 var key = entry.Groups["key"].Value.Trim(); 70 var value = entry.Groups["value"].Value.Trim(); 71 sp.Entries.Add(new KeyValuePair<string, string>(key, value)); 72 } 73 74 properties.Add(sp); 75 } 76 77 return properties.ToArray(); 78 } 79 } 80}