Skip to content

Commit

Permalink
Merge pull request #61 from asmorger/exclude-projects
Browse files Browse the repository at this point in the history
Adds ability to exclude projects from solution-wide analysis

+semver: minor
  • Loading branch information
eNeRGy164 authored Aug 17, 2023
2 parents 99cf327 + d9175a3 commit cb30357
Show file tree
Hide file tree
Showing 15 changed files with 321 additions and 33 deletions.
51 changes: 51 additions & 0 deletions src/LivingDocumentation.Analyzer/AnalyzerSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Buildalyzer;
using Buildalyzer.Workspaces;

namespace LivingDocumentation;

public sealed class AnalyzerSetup : IDisposable
{
public IEnumerable<Project> Projects;
public readonly Workspace Workspace;

private AnalyzerSetup(AnalyzerManager Manager)
{
this.Workspace = Manager.GetWorkspace();
this.Projects = this.Workspace.CurrentSolution.Projects;
}

public void Dispose()
{
this.Workspace.Dispose();
}

public static AnalyzerSetup BuildSolutionAnalyzer(string solutionFile, IEnumerable<string> excludedProjects = default!)
{
var excludedSet = excludedProjects is not null ? new HashSet<string>(excludedProjects, StringComparer.OrdinalIgnoreCase) : new(0);

var manager = new AnalyzerManager(solutionFile);
var analysis = new AnalyzerSetup(manager);

var assembliesInSolution = analysis.Workspace.CurrentSolution.Projects.Select(p => p.AssemblyName).ToList();

// Every project in the solution, except unit test projects
analysis.Projects = analysis.Projects
.Where(p => !ProjectContainsTestPackageReference(manager, p))
.Where(p => string.IsNullOrEmpty(p.FilePath) || !excludedSet.Contains(p.FilePath));

return analysis;
}

public static AnalyzerSetup BuildProjectAnalyzer(string projectFile)
{
var manager = new AnalyzerManager();
manager.GetProject(projectFile);

return new AnalyzerSetup(manager);
}

private static bool ProjectContainsTestPackageReference(AnalyzerManager manager, Project p)
{
return manager.Projects.First(mp => p.Id.Id == mp.Value.ProjectGuid).Value.ProjectFile.PackageReferences.Any(pr => pr.Name.Contains("Test", StringComparison.Ordinal));
}
}
3 changes: 3 additions & 0 deletions src/LivingDocumentation.Analyzer/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class Options
[Option("project", Required = true, SetName = "project", HelpText = "The project to analyze.")]
public string? ProjectPath { get; set; }

[Option("exclude", Required = false, SetName = "solution", Separator = ',', HelpText = "Any projects to exclude from analysis.")]
public IEnumerable<string> ExcludedProjectPaths { get; set; } = Enumerable.Empty<string>();

[Option("output", Required = true, HelpText = "The location of the output.")]
public string? OutputPath { get; set; }

Expand Down
41 changes: 8 additions & 33 deletions src/LivingDocumentation.Analyzer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Diagnostics;
using Buildalyzer;
using Buildalyzer.Workspaces;
using Newtonsoft.Json;

namespace LivingDocumentation;
Expand Down Expand Up @@ -28,14 +26,14 @@ private static async Task RunApplicationAsync(Options options)
var types = new List<TypeDescription>();

var stopwatch = Stopwatch.StartNew();
if (options.SolutionPath is not null)
{
await AnalyzeSolutionFileAsync(types, options.SolutionPath).ConfigureAwait(false);
}
else

using (var analyzer = options.SolutionPath is not null
? AnalyzerSetup.BuildSolutionAnalyzer(options.SolutionPath, options.ExcludedProjectPaths)
: AnalyzerSetup.BuildProjectAnalyzer(options.ProjectPath!))
{
await AnalyzeProjectFileAsync(types, options.ProjectPath!).ConfigureAwait(false);
await AnalyzeWorkspace(types, analyzer).ConfigureAwait(false);
}

stopwatch.Stop();

// Write analysis
Expand All @@ -52,35 +50,12 @@ private static async Task RunApplicationAsync(Options options)
}
}

private static async Task AnalyzeSolutionFileAsync(List<TypeDescription> types, string solutionFile)
private static async Task AnalyzeWorkspace(List<TypeDescription> types, AnalyzerSetup analysis)
{
var manager = new AnalyzerManager(solutionFile);
var workspace = manager.GetWorkspace();
var assembliesInSolution = workspace.CurrentSolution.Projects.Select(p => p.AssemblyName).ToList();

// Every project in the solution, except unit test projects
var projects = workspace.CurrentSolution.Projects
.Where(p => !manager.Projects.First(mp => p.Id.Id == mp.Value.ProjectGuid).Value.ProjectFile.PackageReferences.Any(pr => pr.Name.Contains("Test", StringComparison.Ordinal)));

foreach (var project in projects)
foreach (var project in analysis.Projects)
{
await AnalyzeProjectAsyc(types, project).ConfigureAwait(false);
}

workspace.Dispose();
}

private static async Task AnalyzeProjectFileAsync(List<TypeDescription> types, string projectFile)
{
var manager = new AnalyzerManager();
manager.GetProject(projectFile);
var workspace = manager.GetWorkspace();

var project = workspace.CurrentSolution.Projects.First();

await AnalyzeProjectAsyc(types, project).ConfigureAwait(false);

workspace.Dispose();
}

private static async Task AnalyzeProjectAsyc(List<TypeDescription> types, Project project)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
7 changes: 7 additions & 0 deletions tests/AnalyzerSetupVerification/AnotherProject/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace AnotherProject
{
public class Class1
{

}
}
7 changes: 7 additions & 0 deletions tests/AnalyzerSetupVerification/OtherProject/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OtherProject
{
public class Class1
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
7 changes: 7 additions & 0 deletions tests/AnalyzerSetupVerification/Project/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Project
{
public class Class1
{

}
}
9 changes: 9 additions & 0 deletions tests/AnalyzerSetupVerification/Project/Project.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
43 changes: 43 additions & 0 deletions tests/AnalyzerSetupVerification/SolutionWithTests.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33213.308
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Project", "Project\Project.csproj", "{3394B9C3-A5D3-4145-B70D-3D54B8852596}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OtherProject", "OtherProject\OtherProject.csproj", "{586E3262-BB21-4F37-BA2D-3B56706D4746}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestProject", "TestProject\TestProject.csproj", "{E9E8CDAD-2B87-4C53-A1E0-7BC625375ADA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnotherProject", "AnotherProject\AnotherProject.csproj", "{91EA6563-C625-43F4-9C37-23B7DB54FD73}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3394B9C3-A5D3-4145-B70D-3D54B8852596}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3394B9C3-A5D3-4145-B70D-3D54B8852596}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3394B9C3-A5D3-4145-B70D-3D54B8852596}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3394B9C3-A5D3-4145-B70D-3D54B8852596}.Release|Any CPU.Build.0 = Release|Any CPU
{586E3262-BB21-4F37-BA2D-3B56706D4746}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{586E3262-BB21-4F37-BA2D-3B56706D4746}.Debug|Any CPU.Build.0 = Debug|Any CPU
{586E3262-BB21-4F37-BA2D-3B56706D4746}.Release|Any CPU.ActiveCfg = Release|Any CPU
{586E3262-BB21-4F37-BA2D-3B56706D4746}.Release|Any CPU.Build.0 = Release|Any CPU
{E9E8CDAD-2B87-4C53-A1E0-7BC625375ADA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E9E8CDAD-2B87-4C53-A1E0-7BC625375ADA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E9E8CDAD-2B87-4C53-A1E0-7BC625375ADA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E9E8CDAD-2B87-4C53-A1E0-7BC625375ADA}.Release|Any CPU.Build.0 = Release|Any CPU
{91EA6563-C625-43F4-9C37-23B7DB54FD73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{91EA6563-C625-43F4-9C37-23B7DB54FD73}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91EA6563-C625-43F4-9C37-23B7DB54FD73}.Release|Any CPU.ActiveCfg = Release|Any CPU
{91EA6563-C625-43F4-9C37-23B7DB54FD73}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BD82645A-5556-4DFF-8F8A-AEF5EAEE04A9}
EndGlobalSection
EndGlobal
37 changes: 37 additions & 0 deletions tests/AnalyzerSetupVerification/SolutionWithoutTests.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33213.308
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Project", "Project\Project.csproj", "{5CA13AC2-756B-45BB-9EC8-9F71C31A8F35}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OtherProject", "OtherProject\OtherProject.csproj", "{50FB5E30-AB8D-4D9F-BCDE-98F0BACC084F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AnotherProject", "AnotherProject\AnotherProject.csproj", "{6412962A-D13A-4B91-B2A0-6F592BCB5CAF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5CA13AC2-756B-45BB-9EC8-9F71C31A8F35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5CA13AC2-756B-45BB-9EC8-9F71C31A8F35}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5CA13AC2-756B-45BB-9EC8-9F71C31A8F35}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5CA13AC2-756B-45BB-9EC8-9F71C31A8F35}.Release|Any CPU.Build.0 = Release|Any CPU
{50FB5E30-AB8D-4D9F-BCDE-98F0BACC084F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{50FB5E30-AB8D-4D9F-BCDE-98F0BACC084F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50FB5E30-AB8D-4D9F-BCDE-98F0BACC084F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50FB5E30-AB8D-4D9F-BCDE-98F0BACC084F}.Release|Any CPU.Build.0 = Release|Any CPU
{6412962A-D13A-4B91-B2A0-6F592BCB5CAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6412962A-D13A-4B91-B2A0-6F592BCB5CAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6412962A-D13A-4B91-B2A0-6F592BCB5CAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6412962A-D13A-4B91-B2A0-6F592BCB5CAF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FF21EC90-3752-4496-B630-3FD0CEB652E1}
EndGlobalSection
EndGlobal
22 changes: 22 additions & 0 deletions tests/AnalyzerSetupVerification/TestProject/TestProject.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Project\Project.csproj" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions tests/AnalyzerSetupVerification/TestProject/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace TestProject
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
}
}
}
1 change: 1 addition & 0 deletions tests/AnalyzerSetupVerification/TestProject/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
Loading

0 comments on commit cb30357

Please sign in to comment.