Skip to content

Commit

Permalink
Added support for DefineConstants and LangVersion to Buildalyzer.Work…
Browse files Browse the repository at this point in the history
…spaces (#86)
  • Loading branch information
daveaglick committed Nov 1, 2018
1 parent 7f545d9 commit df80c42
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 4 deletions.
47 changes: 47 additions & 0 deletions src/Buildalyzer.Workspaces/AnalyzerResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,56 @@ private static ProjectInfo GetProjectInfo(AnalyzerResult analyzerResult, Workspa
documents: GetDocuments(analyzerResult, projectId),
projectReferences: GetExistingProjectReferences(analyzerResult, workspace),
metadataReferences: GetMetadataReferences(analyzerResult),
parseOptions: CreateParseOptions(analyzerResult, languageName),
compilationOptions: CreateCompilationOptions(analyzerResult, languageName));
return projectInfo;
}

private static ParseOptions CreateParseOptions(AnalyzerResult analyzerResult, string languageName)
{
if (languageName == LanguageNames.CSharp)
{
CSharpParseOptions parseOptions = new CSharpParseOptions();

// Add any constants
string constants = analyzerResult.GetProperty("DefineConstants");
if(!string.IsNullOrWhiteSpace(constants))
{
parseOptions = parseOptions
.WithPreprocessorSymbols(constants.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()));
}

// Get language version
string langVersion = analyzerResult.GetProperty("LangVersion");
Microsoft.CodeAnalysis.CSharp.LanguageVersion languageVersion;
if(!string.IsNullOrWhiteSpace(langVersion)
&& Microsoft.CodeAnalysis.CSharp.LanguageVersionFacts.TryParse(langVersion, out languageVersion))
{
parseOptions = parseOptions.WithLanguageVersion(languageVersion);
}

return parseOptions;
}

if (languageName == LanguageNames.VisualBasic)
{
VisualBasicParseOptions parseOptions = new VisualBasicParseOptions();

// Get language version
string langVersion = analyzerResult.GetProperty("LangVersion");
Microsoft.CodeAnalysis.VisualBasic.LanguageVersion languageVersion = Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.Default;
if (!string.IsNullOrWhiteSpace(langVersion)
&& Microsoft.CodeAnalysis.VisualBasic.LanguageVersionFacts.TryParse(langVersion, ref languageVersion))
{
parseOptions = parseOptions.WithLanguageVersion(languageVersion);
}

return parseOptions;
}

return null;
}

private static CompilationOptions CreateCompilationOptions(AnalyzerResult analyzerResult, string languageName)
{
string outputType = analyzerResult.GetProperty("OutputType");
Expand All @@ -148,6 +194,7 @@ private static CompilationOptions CreateCompilationOptions(AnalyzerResult analyz
{
return new CSharpCompilationOptions(kind.Value);
}

if (languageName == LanguageNames.VisualBasic)
{
return new VisualBasicCompilationOptions(kind.Value);
Expand Down
3 changes: 2 additions & 1 deletion tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ public class SimpleProjectsFixture
@"SdkNetCoreProject\SdkNetCoreProject.csproj",
@"SdkNetCoreProjectImport\SdkNetCoreProjectImport.csproj",
@"SdkNetCoreProjectWithReference\SdkNetCoreProjectWithReference.csproj",
@"SdkNetCoreProjectWithImportedProps\SdkNetCoreProjectWithImportedProps.csproj",
@"SdkNetStandardProject\SdkNetStandardProject.csproj",
@"SdkNetStandardProjectImport\SdkNetStandardProjectImport.csproj",
@"SdkNetStandardProjectWithPackageReference\SdkNetStandardProjectWithPackageReference.csproj",
@"SdkProjectWithImportedProps\SdkProjectWithImportedProps.csproj"
@"SdkNetStandardProjectWithConstants\SdkNetStandardProjectWithConstants.csproj"
};

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ public void AddsProjectReferences(bool addProjectReferences, int totalProjects)
workspace.CurrentSolution.Projects.Count().ShouldBe(totalProjects, log.ToString());
}

[Test]
public void SupportsConstants()
{
// Given
StringWriter log = new StringWriter();
ProjectAnalyzer analyzer = GetProjectAnalyzer(@"projects\SdkNetStandardProjectWithConstants\SdkNetStandardProjectWithConstants.csproj", log);

// When
Workspace workspace = analyzer.GetWorkspace();
Compilation compilation = workspace.CurrentSolution.Projects.First().GetCompilationAsync().Result;

// Then
compilation.GetSymbolsWithName(x => x == "Class1").ShouldBeEmpty(log.ToString());
compilation.GetSymbolsWithName(x => x == "Class2").ShouldNotBeEmpty(log.ToString());
}

private ProjectAnalyzer GetProjectAnalyzer(string projectFile, StringWriter log, AnalyzerManager manager = null)
{
// The path will get normalized inside the .GetProject() call below
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace SdkProjectWithImportedProps
namespace SdkNetCoreProjectWithImportedProps
{
public class Class1
{
Expand Down
24 changes: 24 additions & 0 deletions tests/projects/SdkNetStandardProjectWithConstants/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace SdkNetStandardProject
{
#if DEF1
public class Class1
{
public void Foo()
{
Console.WriteLine("Bar");
}
}
#endif

#if DEF2
public class Class2
{
public void Foo()
{
Console.WriteLine("Bar");
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<DefineConstants>DEF2</DefineConstants>
</PropertyGroup>

</Project>
10 changes: 8 additions & 2 deletions tests/projects/TestProjects.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SdkFrameworkProject", "SdkF
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LegacyFrameworkProjectWithPackageReference", "LegacyFrameworkProjectWithPackageReference\LegacyFrameworkProjectWithPackageReference.csproj", "{CD3445E2-A34A-4D87-9E8A-5081CBFA2F86}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SdkProjectWithImportedProps", "SdkProjectWithImportedProps\SdkProjectWithImportedProps.csproj", "{A50B19F8-7FAA-4BE9-B292-666B60714F79}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SdkNetCoreProjectWithImportedProps", "SdkNetCoreProjectWithImportedProps\SdkNetCoreProjectWithImportedProps.csproj", "{A50B19F8-7FAA-4BE9-B292-666B60714F79}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SdkMultiTargetingProject", "SdkMultiTargetingProject\SdkMultiTargetingProject.csproj", "{A4B9EAF8-861F-4194-9748-30AB6080ED90}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SdkNetStandardProjectWithPackageReference", "SdkNetStandardProjectWithPackageReference\SdkNetStandardProjectWithPackageReference.csproj", "{340391DE-8611-4E04-8EF5-CD5CF546D647}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SdkNetCoreProjectWithReference", "SdkNetCoreProjectWithReference\SdkNetCoreProjectWithReference.csproj", "{7B920FD1-84C7-4B87-AA03-F9C558EAC332}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SdkNetCoreProjectWithReference", "SdkNetCoreProjectWithReference\SdkNetCoreProjectWithReference.csproj", "{7B920FD1-84C7-4B87-AA03-F9C558EAC332}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SdkNetStandardProjectWithConstants", "SdkNetStandardProjectWithConstants\SdkNetStandardProjectWithConstants.csproj", "{9146E23A-6939-4BDF-9BB1-E0F5E7B97AAE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -82,6 +84,10 @@ Global
{7B920FD1-84C7-4B87-AA03-F9C558EAC332}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7B920FD1-84C7-4B87-AA03-F9C558EAC332}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7B920FD1-84C7-4B87-AA03-F9C558EAC332}.Release|Any CPU.Build.0 = Release|Any CPU
{9146E23A-6939-4BDF-9BB1-E0F5E7B97AAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9146E23A-6939-4BDF-9BB1-E0F5E7B97AAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9146E23A-6939-4BDF-9BB1-E0F5E7B97AAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9146E23A-6939-4BDF-9BB1-E0F5E7B97AAE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit df80c42

Please sign in to comment.