Skip to content

Commit

Permalink
Merge branch 'feature/analyzers'
Browse files Browse the repository at this point in the history
  • Loading branch information
isadorasophia committed Sep 27, 2023
2 parents 0326064 + 24c27b6 commit f9c70d9
Show file tree
Hide file tree
Showing 31 changed files with 1,299 additions and 1,248 deletions.
6 changes: 6 additions & 0 deletions Bang.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bang.Analyzers", "src\Bang.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bang.Analyzers.Tests", "src\Bang.Analyzers.Tests\Bang.Analyzers.Tests.csproj", "{0EED28D5-6A33-47F9-9D9E-C587D99DFD93}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bang.Generator", "src\Bang.Generator\Bang.Generator.csproj", "{945C48DA-7A5B-4EB3-93AF-270396334BA0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{0EED28D5-6A33-47F9-9D9E-C587D99DFD93}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0EED28D5-6A33-47F9-9D9E-C587D99DFD93}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0EED28D5-6A33-47F9-9D9E-C587D99DFD93}.Release|Any CPU.Build.0 = Release|Any CPU
{945C48DA-7A5B-4EB3-93AF-270396334BA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{945C48DA-7A5B-4EB3-93AF-270396334BA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{945C48DA-7A5B-4EB3-93AF-270396334BA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{945C48DA-7A5B-4EB3-93AF-270396334BA0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ We are on nuget! So you can either use this repository as a submodule or simply
<!-- Main Package. -->
<PackageReference Include="Murder.Bang" Version="0.0.2-alpha" />

<!-- Generator package. Mandatory, include it so Bang can generate lookup classes and extensions for your Components. -->
<PackageReference Include="Murder.Bang.Generator" Version="0.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

<!-- Optional Package with analyzers and code fixes. -->
<PackageReference Include="Murder.Bang.Analyzers" Version="0.0.3">
<PrivateAssets>all</PrivateAssets>
Expand Down
2 changes: 1 addition & 1 deletion src/Bang.Analyzers/Bang.Analyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<Description>Source code analyzers for Bang</Description>

<PackageId>Murder.Bang.Analyzers</PackageId>
<PackageVersion>0.0.4</PackageVersion>
<PackageVersion>0.0.5</PackageVersion>
<Authors>Murder Authors</Authors>
<Company>Murder Engine</Company>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
Expand Down
29 changes: 14 additions & 15 deletions src/Bang.Generator/Bang.Generator.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>Generator</AssemblyName>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<AssemblyName>Bang.Generator</AssemblyName>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>11</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<PackageId>Murder.Bang.Generator</PackageId>
<Version>0.0.1-alpha</Version>
<Authors>Isadora</Authors>
<Version>0.0.5</Version>
<Authors>Murder Authors</Authors>
<Company>Murder Engine</Company>

<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageDescription>A helper generator for the Bang ECS framework.</PackageDescription>
<PackageDescription>Source code generator for the Bang ECS framework.</PackageDescription>

<!-- Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand All @@ -27,18 +27,17 @@
</PropertyGroup>

<ItemGroup>
<None Include="template.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<None Include="..\..\LICENSE" Pack="true" PackagePath="" Visible="false" />

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<!-- Include .dll in the correct folder. -->
<None Include="$(OutputPath)/$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Bang\Bang.csproj" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.4.0" PrivateAssets="all" />
<PackageReference Include="PolySharp" Version="1.12.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
104 changes: 104 additions & 0 deletions src/Bang.Generator/BangExtensionsGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using Bang.Generator.Extensions;
using Bang.Generator.Metadata;
using System.Collections.Immutable;
using Bang.Generator.Templating;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.VisualBasic;

namespace Bang.Generator;

[Generator]
public sealed class BangExtensionsGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var potentialComponents = context.PotentialComponents().Collect();
var stateMachines = context.PotentialStateMachines().Collect();
var compilation = potentialComponents
.Combine(stateMachines)
.Combine(context.CompilationProvider);

context.RegisterSourceOutput(
compilation,
(c, t) => Execute(c, t.Right, t.Left.Left, t.Left.Right)
);
}

public void Execute(
SourceProductionContext context,
Compilation compilation,
ImmutableArray<TypeDeclarationSyntax> potentialComponents,
ImmutableArray<ClassDeclarationSyntax> potentialStateMachines)
{
#if DEBUG
// Uncomment this if you need to use a debugger.
// if (!System.Diagnostics.Debugger.IsAttached)
// {
// System.Diagnostics.Debugger.Launch();
// }
#endif

// Bail if any important type symbol is not resolvable.
var bangTypeSymbols = BangTypeSymbols.FromCompilation(compilation);
if (bangTypeSymbols is null)
return;

var referencedAssemblyTypeFetcher = new ReferencedAssemblyTypeFetcher(compilation);

// Gets the best possible name for the parent lookup class assemblies.
var parentLookupClass = referencedAssemblyTypeFetcher
.GetAllCompiledClassesWithSubtypes()
.Where(t => t.IsSubtypeOf(bangTypeSymbols.ComponentsLookupClass))
.OrderBy(NumberOfParentClasses)
.LastOrDefault() ?? bangTypeSymbols.ComponentsLookupClass;

var projectName = compilation.AssemblyName?.Replace(".", "") ?? "My";

var metadataFetcher = new MetadataFetcher(compilation);

// All files that will be created by this generator.
var templates = ImmutableArray.Create(
Templates.ComponentTypes(projectName),
Templates.MessageTypes(projectName),
Templates.EntityExtensions(projectName),
Templates.LookupImplementation(projectName)
);

var projectMetadata = new TypeMetadata.Project(
projectName,
parentLookupClass.Name.Replace("ComponentsLookup", ""),
parentLookupClass.FullyQualifiedName()
);
foreach (var template in templates)
{
template.Process(projectMetadata);
}

// Fetch all relevant metadata.
var allTypeMetadata =
metadataFetcher.FetchMetadata(
bangTypeSymbols,
potentialComponents,
potentialStateMachines
);

// Process metadata.
foreach (var metadata in allTypeMetadata)
{
foreach (var template in templates)
{
template.Process(metadata);
}
}

// Generate sources.
foreach (var template in templates)
{
context.AddSource(template.FileName, template.GetDocumentWithReplacements());
}
}

private static int NumberOfParentClasses(INamedTypeSymbol type)
=> type.BaseType is null ? 0 : 1 + NumberOfParentClasses(type.BaseType);
}
198 changes: 0 additions & 198 deletions src/Bang.Generator/Entrypoint.cs

This file was deleted.

Loading

0 comments on commit f9c70d9

Please sign in to comment.