Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: create generator project #1

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vs/
[Bb]in/
[Oo]bj/
17 changes: 17 additions & 0 deletions src/Geis.ScratchApp/Geis.ScratchApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<ProjectReference
Include="..\Geis.ValueParserGenerators\Geis.ValueParserGenerators.csproj"
OutputItemType="Analyzer"/>
<ProjectReference Include="..\Geis\Geis.csproj" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions src/Geis.ScratchApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Geis;
Copy link
Member

@Insomniak47 Insomniak47 Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to be the old man "No, the kids are wrong" but I'd prefer not using top level statements


var collection = new ValueParserCollection();
collection.AddDefaultParser<int>();
collection.AddDefaultParser<bool>();
49 changes: 49 additions & 0 deletions src/Geis.ValueParserGenerators/DefaultValueParserGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Immutable;

namespace Geis.ValueParserGenerators;

[Generator]
public class DefaultValueParserGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
//System.Diagnostics.Debugger.Launch();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to put this behind a flag and conditional compile it in


var provider = context
.SyntaxProvider
.CreateSyntaxProvider(
predicate: (c, _) => c is GenericNameSyntax n,
transform: (n, _) => (GenericNameSyntax)n.Node)
.Where(x => x is not null);

var compilation = context.CompilationProvider.Combine(provider.Collect());

context.RegisterSourceOutput(
compilation,
(spc, source) => Execute(spc, source.Left, source.Right));
}

private void Execute(
SourceProductionContext context,
Compilation compilation,
ImmutableArray<GenericNameSyntax> genericNames)
{
foreach (var name in genericNames)
{
// todo: add explicitly constructor value parsers that reference the TryParse or new(string) member of T.

context.ReportDiagnostic(
Diagnostic.Create(
new DiagnosticDescriptor(
id: "GVPG0001",
title: "Possible Reference To AddDefaultParser",
messageFormat: "Found a possible reference to the AddDefaultParser method.",
category: "Accomplishment",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true),
name.GetLocation()));
}
}
}
17 changes: 17 additions & 0 deletions src/Geis.ValueParserGenerators/Geis.ValueParserGenerators.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" />
</ItemGroup>

</Project>
37 changes: 37 additions & 0 deletions src/Geis.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.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Geis.ValueParserGenerators", "Geis.ValueParserGenerators\Geis.ValueParserGenerators.csproj", "{388C1697-D0ED-4340-AE58-22CF614FD50D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Geis", "Geis\Geis.csproj", "{8B1B4AA8-17E8-4A0C-946B-FD975A2598EA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Geis.ScratchApp", "Geis.ScratchApp\Geis.ScratchApp.csproj", "{435D5879-9BB7-4106-BB3A-2B22750C08EF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{388C1697-D0ED-4340-AE58-22CF614FD50D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{388C1697-D0ED-4340-AE58-22CF614FD50D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{388C1697-D0ED-4340-AE58-22CF614FD50D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{388C1697-D0ED-4340-AE58-22CF614FD50D}.Release|Any CPU.Build.0 = Release|Any CPU
{8B1B4AA8-17E8-4A0C-946B-FD975A2598EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8B1B4AA8-17E8-4A0C-946B-FD975A2598EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8B1B4AA8-17E8-4A0C-946B-FD975A2598EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8B1B4AA8-17E8-4A0C-946B-FD975A2598EA}.Release|Any CPU.Build.0 = Release|Any CPU
{435D5879-9BB7-4106-BB3A-2B22750C08EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{435D5879-9BB7-4106-BB3A-2B22750C08EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{435D5879-9BB7-4106-BB3A-2B22750C08EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{435D5879-9BB7-4106-BB3A-2B22750C08EF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3043616A-5042-4512-816E-3345E1FA7AB3}
EndGlobalSection
EndGlobal
9 changes: 9 additions & 0 deletions src/Geis/Geis.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

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

</Project>
17 changes: 17 additions & 0 deletions src/Geis/ValueParserCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Geis;

public class ValueParserCollection
{
private readonly Dictionary<Type, Func<string, object?>> _parsers = new();

public ValueParserCollection SetParser<T>(Func<string, T?> parser) where T : notnull
{
_parsers[typeof(T)] = s => parser(s);
return this;
}
}

public static class ValueParserCollectionExtensions
{
public static ValueParserCollection AddDefaultParser<T>(this ValueParserCollection collection) => collection;
}