-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #918 from DustinCampbell/new-solution-parser
Fix solution parsing (again!)
- Loading branch information
Showing
10 changed files
with
724 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/OmniSharp.MSBuild/SolutionParsing/GlobalSectionBlock.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Collections.Immutable; | ||
|
||
namespace OmniSharp.MSBuild.SolutionParsing | ||
{ | ||
internal sealed class GlobalSectionBlock : SectionBlock | ||
{ | ||
private GlobalSectionBlock(string name, ImmutableArray<Property> properties) | ||
: base(name, properties) | ||
{ | ||
} | ||
|
||
public static GlobalSectionBlock Parse(string headerLine, Scanner scanner) | ||
{ | ||
var (name, properties) = ParseNameAndProperties( | ||
"GlobalSection", "EndGlobalSection", headerLine, scanner); | ||
|
||
return new GlobalSectionBlock(name, properties); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/OmniSharp.MSBuild/SolutionParsing/InvalidSolutionFileException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
|
||
namespace OmniSharp.MSBuild.SolutionParsing | ||
{ | ||
internal class InvalidSolutionFileException : Exception | ||
{ | ||
public InvalidSolutionFileException() | ||
{ | ||
} | ||
|
||
public InvalidSolutionFileException(string message) : base(message) | ||
{ | ||
} | ||
|
||
public InvalidSolutionFileException(string message, Exception innerException) : base(message, innerException) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.IO; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace OmniSharp.MSBuild.SolutionParsing | ||
{ | ||
internal class ProjectBlock | ||
{ | ||
private const string SolutionFolderGuid = "{2150E333-8FDC-42A3-9474-1A3956D46DE8}"; | ||
|
||
// An example of a project line looks like this: | ||
// Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClassLibrary1", "ClassLibrary1\ClassLibrary1.csproj", "{DEBCE986-61B9-435E-8018-44B9EF751655}" | ||
private static readonly Lazy<Regex> s_lazyProjectHeader = new Lazy<Regex>( | ||
() => new Regex | ||
( | ||
"^" // Beginning of line | ||
+ "Project\\(\"(?<PROJECTTYPEGUID>.*)\"\\)" | ||
+ "\\s*=\\s*" // Any amount of whitespace plus "=" plus any amount of whitespace | ||
+ "\"(?<PROJECTNAME>.*)\"" | ||
+ "\\s*,\\s*" // Any amount of whitespace plus "," plus any amount of whitespace | ||
+ "\"(?<RELATIVEPATH>.*)\"" | ||
+ "\\s*,\\s*" // Any amount of whitespace plus "," plus any amount of whitespace | ||
+ "\"(?<PROJECTGUID>.*)\"" | ||
+ "$", // End-of-line | ||
RegexOptions.Compiled) | ||
); | ||
|
||
public string ProjectTypeGuid { get; } | ||
public string ProjectName { get; } | ||
public string RelativePath { get; } | ||
public string ProjectGuid { get; } | ||
public ImmutableArray<SectionBlock> Sections { get; } | ||
|
||
public bool IsSolutionFolder => ProjectTypeGuid.Equals(SolutionFolderGuid, StringComparison.OrdinalIgnoreCase); | ||
|
||
private ProjectBlock(string projectTypeGuid, string projectName, string relativePath, string projectGuid, ImmutableArray<SectionBlock> sections) | ||
{ | ||
ProjectTypeGuid = projectTypeGuid; | ||
ProjectName = projectName; | ||
RelativePath = relativePath; | ||
ProjectGuid = projectGuid; | ||
Sections = sections; | ||
} | ||
|
||
public static ProjectBlock Parse(string headerLine, Scanner scanner) | ||
{ | ||
var match = s_lazyProjectHeader.Value.Match(headerLine); | ||
if (!match.Success) | ||
{ | ||
return null; | ||
} | ||
|
||
var projectTypeGuid = match.Groups["PROJECTTYPEGUID"].Value.Trim(); | ||
var projectName = match.Groups["PROJECTNAME"].Value.Trim(); | ||
var relativePath = match.Groups["RELATIVEPATH"].Value.Trim(); | ||
var projectGuid = match.Groups["PROJECTGUID"].Value.Trim(); | ||
|
||
// If the project name is empty, set it to a generated generic value. | ||
if (string.IsNullOrEmpty(projectName)) | ||
{ | ||
projectName = "EmptyProjectName." + Guid.NewGuid(); | ||
} | ||
|
||
if (relativePath.IndexOfAny(Path.GetInvalidPathChars()) >= 0) | ||
{ | ||
throw new InvalidSolutionFileException("A project path contains an invalid character."); | ||
} | ||
|
||
var sections = ImmutableArray.CreateBuilder<SectionBlock>(); | ||
|
||
// Search for project dependencies. Keep reading until we either... | ||
// 1. reach the end of the file, | ||
// 2. see "ProjectSection( at the beginning of the line, or | ||
// 3. see "EndProject at the beginning of the line. | ||
|
||
string line; | ||
while ((line = scanner.NextLine()) != null) | ||
{ | ||
if (line == "EndProject") | ||
{ | ||
break; | ||
} | ||
|
||
if (line.StartsWith("ProjectSection(")) | ||
{ | ||
var section = ProjectSectionBlock.Parse(line, scanner); | ||
if (section != null) | ||
{ | ||
sections.Add(section); | ||
} | ||
} | ||
} | ||
|
||
return new ProjectBlock(projectTypeGuid, projectName, relativePath, projectGuid, sections.ToImmutable()); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/OmniSharp.MSBuild/SolutionParsing/ProjectSectionBlock.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Collections.Immutable; | ||
|
||
namespace OmniSharp.MSBuild.SolutionParsing | ||
{ | ||
internal sealed class ProjectSectionBlock : SectionBlock | ||
{ | ||
private ProjectSectionBlock(string name, ImmutableArray<Property> properties) | ||
: base(name, properties) | ||
{ | ||
} | ||
|
||
public static ProjectSectionBlock Parse(string headerLine, Scanner scanner) | ||
{ | ||
var (name, properties) = ParseNameAndProperties( | ||
"ProjectSection", "EndProjectSection", headerLine, scanner); | ||
|
||
return new ProjectSectionBlock(name, properties); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace OmniSharp.MSBuild.SolutionParsing | ||
{ | ||
internal sealed class Property | ||
{ | ||
// An example of a property line looks like this: | ||
// AspNetCompiler.VirtualPath = "/webprecompile" | ||
// Because website projects now include the target framework moniker as | ||
// one of their properties, <PROPERTYVALUE> may have an '=' in it. | ||
private static readonly Lazy<Regex> s_lazyPropertyLine = new Lazy<Regex>( | ||
() => new Regex | ||
( | ||
"^" // Beginning of line | ||
+ "(?<PROPERTYNAME>[^=]*)" | ||
+ "\\s*=\\s*" // Any amount of whitespace plus "=" plus any amount of whitespace | ||
+ "(?<PROPERTYVALUE>.*)" | ||
+ "$", // End-of-line | ||
RegexOptions.Compiled) | ||
); | ||
|
||
public string Name { get; } | ||
public string Value { get; } | ||
|
||
private Property(string name, string value) | ||
{ | ||
Name = name; | ||
Value = value; | ||
} | ||
|
||
public static Property Parse(string propertyLine) | ||
{ | ||
var match = s_lazyPropertyLine.Value.Match(propertyLine); | ||
|
||
if (!match.Success) | ||
{ | ||
return null; | ||
} | ||
|
||
var name = match.Groups["PROPERTYNAME"].Value.Trim(); | ||
var value = match.Groups["PROPERTYVALUE"].Value.Trim(); | ||
|
||
return new Property(name, value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
namespace OmniSharp.MSBuild.SolutionParsing | ||
{ | ||
internal class Scanner : IDisposable | ||
{ | ||
private readonly StringReader _reader; | ||
private int _currentLineNumber; | ||
|
||
public Scanner(string text) | ||
{ | ||
_reader = new StringReader(text); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_reader.Dispose(); | ||
} | ||
|
||
public string NextLine() | ||
{ | ||
var line = _reader.ReadLine(); | ||
|
||
_currentLineNumber++; | ||
|
||
if (line != null) | ||
{ | ||
line = line.Trim(); | ||
} | ||
|
||
return line; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Immutable; | ||
|
||
namespace OmniSharp.MSBuild.SolutionParsing | ||
{ | ||
internal abstract class SectionBlock | ||
{ | ||
public string Name { get; } | ||
public ImmutableArray<Property> Properties { get; } | ||
|
||
protected SectionBlock(string name, ImmutableArray<Property> properties) | ||
{ | ||
Name = name; | ||
Properties = properties; | ||
} | ||
|
||
protected static (string name, ImmutableArray<Property> properties) ParseNameAndProperties( | ||
string startSection, string endSection, string headerLine, Scanner scanner) | ||
{ | ||
var startIndex = startSection.Length; | ||
if (!startSection.EndsWith("(")) | ||
{ | ||
startIndex++; | ||
} | ||
|
||
var endIndex = headerLine.IndexOf(')', startIndex); | ||
var name = endIndex >= startIndex | ||
? headerLine.Substring(startIndex, endIndex - startIndex) | ||
: headerLine.Substring(startIndex); | ||
|
||
var properties = ImmutableArray.CreateBuilder<Property>(); | ||
|
||
string line; | ||
while ((line = scanner.NextLine()) != null) | ||
{ | ||
if (line.StartsWith(endSection, StringComparison.Ordinal)) | ||
{ | ||
break; | ||
} | ||
|
||
var property = Property.Parse(line); | ||
if (property != null) | ||
{ | ||
properties.Add(property); | ||
} | ||
} | ||
|
||
return (name, properties.ToImmutable()); | ||
} | ||
} | ||
} |
Oops, something went wrong.