Skip to content

Commit

Permalink
Consider removing Sourceberg
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuo41 committed Sep 12, 2020
1 parent 3c0434c commit 47c9945
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 56 deletions.
8 changes: 8 additions & 0 deletions Decuplr.Serialization.Binary.sln
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Decuplr.Serialization.Binar
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Decuplr.Sourceberg", "src\Decuplr.Sourceberg\Decuplr.Sourceberg.csproj", "{380F69E3-6029-4A6D-AEA1-E075C6FDFCC2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Decuplr.Sourceberg.Generator", "src\Decuplr.Sourceberg.Generator\Decuplr.Sourceberg.Generator.csproj", "{D9C06096-C823-4BB4-BEBB-9FCFC7F6F39B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -116,6 +118,12 @@ Global
{380F69E3-6029-4A6D-AEA1-E075C6FDFCC2}.NoAutogen|Any CPU.Build.0 = Debug|Any CPU
{380F69E3-6029-4A6D-AEA1-E075C6FDFCC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{380F69E3-6029-4A6D-AEA1-E075C6FDFCC2}.Release|Any CPU.Build.0 = Release|Any CPU
{D9C06096-C823-4BB4-BEBB-9FCFC7F6F39B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D9C06096-C823-4BB4-BEBB-9FCFC7F6F39B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9C06096-C823-4BB4-BEBB-9FCFC7F6F39B}.NoAutogen|Any CPU.ActiveCfg = Debug|Any CPU
{D9C06096-C823-4BB4-BEBB-9FCFC7F6F39B}.NoAutogen|Any CPU.Build.0 = Debug|Any CPU
{D9C06096-C823-4BB4-BEBB-9FCFC7F6F39B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D9C06096-C823-4BB4-BEBB-9FCFC7F6F39B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 6 additions & 0 deletions src/Decuplr.Sourceberg.Generator/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System;

namespace Decuplr.Sourceberg.Generator {
public class Class1 {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

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

</Project>
49 changes: 49 additions & 0 deletions src/Decuplr.Sourceberg/Internal/SourcebergBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Decuplr.Sourceberg.Generation;
using Decuplr.Sourceberg.Services;
using Decuplr.Sourceberg.Services.Implementation;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.DependencyInjection;

namespace Decuplr.Sourceberg.Internal {
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract class SourcebergBase {

private readonly ServiceCollection _services = new ServiceCollection();

private protected IReadOnlyList<AnalyzerGroupInfo<ISymbol, SymbolAnalysisContextSource, SymbolKind>> SymbolActionGroups { get; }
private protected IReadOnlyList<AnalyzerGroupInfo<SyntaxNode, SyntaxNodeAnalysisContextSource, SyntaxKind>> SyntaxActionGroups { get; }

private protected SourcebergBase(GeneratorStartup startup) {
var syntaxActionSetup = new SyntaxAnalyzerSetup();
var symbolActionSetup = new SymbolAnalyzerGroupSetup();
startup.ConfigureAnalyzer(new AnalyzerSetupContext(syntaxActionSetup, symbolActionSetup));
startup.ConfigureServices(_services);
SyntaxActionGroups = syntaxActionSetup.AnalyzerGroups;
SymbolActionGroups = symbolActionSetup.AnalyzerGroups;

foreach (var (actionGroup, _) in SyntaxActionGroups) {
actionGroup.RegisterServices(_services);
}
foreach (var (actionGroup, _) in SymbolActionGroups) {
actionGroup.RegisterServices(_services);
}

// Add default services
_services.AddScopedGroup<SourceContextAccessor, IAnalysisLifetime>();
_services.AddScopedGroup<TypeSymbolProvider, ITypeSymbolProvider, ISourceAddition>();

_services.AddScopedGroup<AttributeLayoutProvider, IAttributeLayoutProvider>();
_services.AddScopedGroup<ContextCollectionProvider, IContextCollectionProvider>();
_services.AddScoped<TypeSymbolLocatorCache>();
}

protected IServiceProvider CreateServiceProvider() => _services.BuildServiceProvider();


}

}
66 changes: 10 additions & 56 deletions src/Decuplr.Sourceberg/Internal/SourcebergGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,70 +18,24 @@

namespace Decuplr.Sourceberg.Internal {

public class SourcebergBase {

private readonly ServiceCollection _services = new ServiceCollection();

private protected IReadOnlyList<AnalyzerGroupInfo<ISymbol, SymbolAnalysisContextSource, SymbolKind>> SymbolActionGroups { get; }
private protected IReadOnlyList<AnalyzerGroupInfo<SyntaxNode, SyntaxNodeAnalysisContextSource, SyntaxKind>> SyntaxActionGroups { get; }

private protected SourcebergBase(GeneratorStartup startup) {
var syntaxActionSetup = new SyntaxAnalyzerSetup();
var symbolActionSetup = new SymbolAnalyzerGroupSetup();
startup.ConfigureAnalyzer(new AnalyzerSetupContext(syntaxActionSetup, symbolActionSetup));
startup.ConfigureServices(_services);
SyntaxActionGroups = syntaxActionSetup.AnalyzerGroups;
SymbolActionGroups = symbolActionSetup.AnalyzerGroups;

foreach (var (actionGroup, _) in SyntaxActionGroups) {
actionGroup.RegisterServices(_services);
}
foreach (var (actionGroup, _) in SymbolActionGroups) {
actionGroup.RegisterServices(_services);
}

// Add default services
_services.AddScopedGroup<SourceContextAccessor, IAnalysisLifetime>();
_services.AddScopedGroup<TypeSymbolProvider, ITypeSymbolProvider, ISourceAddition>();

_services.AddScopedGroup<AttributeLayoutProvider, IAttributeLayoutProvider>();
_services.AddScopedGroup<ContextCollectionProvider, IContextCollectionProvider>();
_services.AddScoped<TypeSymbolLocatorCache>();
}

protected IServiceProvider CreateServiceProvider() => _services.BuildServiceProvider();


}

[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class SyntaxCapture : ISyntaxReceiver {
private readonly List<SyntaxNode> _nodes = new List<SyntaxNode>();

internal IReadOnlyList<SyntaxNode> CapturedNodes => _nodes;
internal GeneratorStartup Startup { get; }

internal SyntaxCapture(GeneratorStartup startup) => Startup = startup;

public void OnVisitSyntaxNode(SyntaxNode syntaxNode) {
if (Startup.ShouldCapture(syntaxNode))
_nodes.Add(syntaxNode);
}
}

[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class SourcebergGenerator : SourcebergBase {

private static readonly DiagnosticDescriptor UnexpectedExceptionDescriptor
= new DiagnosticDescriptor("SCBRG-XXX", "An unexpected source generator exception has occured.", "An unexpected exception {0} has occured : {1}", "InternalError", DiagnosticSeverity.Error, true);

private readonly SourceGeneratorContext _generatorContext;
private readonly SyntaxCapture _capture;
private readonly SourcebergSyntaxReceiver _capture;
private readonly Dictionary<SyntaxTree, SemanticModel> _semanticModelCache = new Dictionary<SyntaxTree, SemanticModel>();

private SourcebergGenerator(SourceGeneratorContext generatorContext, SyntaxCapture capture)
private SourcebergGenerator(SourceGeneratorContext generatorContext, SourcebergSyntaxReceiver capture)
: base(capture.Startup) {
_generatorContext = generatorContext;
_capture = capture;
}

private Diagnostic GetExceptionDiagnostic(Exception e) => Diagnostic.Create(UnexpectedExceptionDescriptor, Location.None, e.GetType(), e);

private SemanticModel GetSemanticModel(SyntaxNode node) {
return _semanticModelCache.GetOrAdd(node.SyntaxTree, tree => _generatorContext.Compilation.GetSemanticModel(tree));
}
Expand Down Expand Up @@ -180,13 +134,13 @@ public void RunGeneration() {
}
}
catch(Exception e) {
// Catch exception and report diagnostic
"Report Diagnostic"
GetExceptionDiagnostic(e);
// should we rethrow?
}
}

internal static SourcebergGenerator? CreateGenerator(SourceGeneratorContext context) {
if (context.SyntaxReceiver is SyntaxCapture syntaxCapture)
if (context.SyntaxReceiver is SourcebergSyntaxReceiver syntaxCapture)
return new SourcebergGenerator(context, syntaxCapture);
return null;
}
Expand Down
22 changes: 22 additions & 0 deletions src/Decuplr.Sourceberg/Internal/SourcebergSyntaxReceiver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.ComponentModel;
using Decuplr.Sourceberg.Generation;
using Microsoft.CodeAnalysis;

namespace Decuplr.Sourceberg.Internal {
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class SourcebergSyntaxReceiver : ISyntaxReceiver {
private readonly List<SyntaxNode> _nodes = new List<SyntaxNode>();

internal IReadOnlyList<SyntaxNode> CapturedNodes => _nodes;
internal GeneratorStartup Startup { get; }

internal SourcebergSyntaxReceiver(GeneratorStartup startup) => Startup = startup;

public void OnVisitSyntaxNode(SyntaxNode syntaxNode) {
if (Startup.ShouldCapture(syntaxNode))
_nodes.Add(syntaxNode);
}
}

}

0 comments on commit 47c9945

Please sign in to comment.