diff options
author | Ignacio Etcheverry <ignalfonsore@gmail.com> | 2021-03-06 00:12:42 +0100 |
---|---|---|
committer | Ignacio Etcheverry <ignalfonsore@gmail.com> | 2021-03-06 21:50:32 +0100 |
commit | e2afe700f62aaa503daed0519ff88b0aa1a559c1 (patch) | |
tree | 8bedb525e7a671531138d5f15ad2b27c97069a58 /modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators | |
parent | d4191e48c5f4af920e87a70fe3c5ae9219a3332b (diff) |
Add C# source generator for a new ScriptPath attribute
This source generator adds a newly introduced attribute,
`ScriptPath` to all classes that:
- Are top-level classes (not inner/nested).
- Have the `partial` modifier.
- Inherit `Godot.Object`.
- The class name matches the file name.
A build error is thrown if the generator finds a class that meets these
conditions but is not declared `partial`, unless the class is annotated
with the `DisableGodotGenerators` attribute.
We also generate an `AssemblyHasScripts` assembly attribute which Godot
uses to get all the script classes in the assembly, eliminating the need
for Godot to search them. We can also avoid searching in assemblies that
don't have this attribute. This will be good for performance in the
future once we support multiple assemblies with Godot script classes.
This is an example of what the generated code looks like:
```
using Godot;
namespace Foo {
[ScriptPathAttribute("res://Player.cs")]
// Multiple partial declarations are allowed
[ScriptPathAttribute("res://Foo/Player.cs")]
partial class Player {}
}
[assembly:AssemblyHasScripts(new System.Type[] { typeof(Foo.Player) })]
```
The new attributes replace script metadata which we were generating by
determining the namespace of script classes with a very simple parser.
This fixes several issues with the old approach related to parser
errors and conditional compilation.
It also makes the task part of the MSBuild project build, rather than
a separate step executed by the Godot editor.
Diffstat (limited to 'modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators')
6 files changed, 331 insertions, 0 deletions
diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs new file mode 100644 index 0000000000..4867c986e6 --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs @@ -0,0 +1,33 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace Godot.SourceGenerators +{ + public static class Common + { + public static void ReportNonPartialGodotScriptClass( + GeneratorExecutionContext context, + ClassDeclarationSyntax cds, INamedTypeSymbol symbol + ) + { + string message = + "Missing partial modifier on declaration of type '" + + $"{symbol.FullQualifiedName()}' which is a subclass of '{GodotClasses.Object}'"; + + string description = $"{message}. Subclasses of '{GodotClasses.Object}' must be " + + "declared with the partial modifier or annotated with the " + + $"attribute '{GodotClasses.DisableGodotGeneratorsAttr}'."; + + context.ReportDiagnostic(Diagnostic.Create( + new DiagnosticDescriptor(id: "GODOT-G0001", + title: message, + messageFormat: message, + category: "Usage", + DiagnosticSeverity.Error, + isEnabledByDefault: true, + description), + cds.GetLocation(), + cds.SyntaxTree.FilePath)); + } + } +} diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs new file mode 100644 index 0000000000..c3e74822d5 --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs @@ -0,0 +1,86 @@ +using System.Collections.Generic; +using System.Linq; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace Godot.SourceGenerators +{ + static class ExtensionMethods + { + public static bool TryGetGlobalAnalyzerProperty( + this GeneratorExecutionContext context, string property, out string? value + ) => context.AnalyzerConfigOptions.GlobalOptions + .TryGetValue("build_property." + property, out value); + + private static bool InheritsFrom(this INamedTypeSymbol? symbol, string baseName) + { + if (symbol == null) + return false; + + while (true) + { + if (symbol.ToString() == baseName) + { + return true; + } + + if (symbol.BaseType != null) + { + symbol = symbol.BaseType; + continue; + } + + break; + } + + return false; + } + + private static bool IsGodotScriptClass( + this ClassDeclarationSyntax cds, Compilation compilation, + out INamedTypeSymbol? symbol + ) + { + var sm = compilation.GetSemanticModel(cds.SyntaxTree); + + var classTypeSymbol = sm.GetDeclaredSymbol(cds); + + if (classTypeSymbol?.BaseType == null + || !classTypeSymbol.BaseType.InheritsFrom(GodotClasses.Object)) + { + symbol = null; + return false; + } + + symbol = classTypeSymbol; + return true; + } + + public static IEnumerable<(ClassDeclarationSyntax cds, INamedTypeSymbol symbol)> SelectGodotScriptClasses( + this IEnumerable<ClassDeclarationSyntax> source, + Compilation compilation + ) + { + foreach (var cds in source) + { + if (cds.IsGodotScriptClass(compilation, out var symbol)) + yield return (cds, symbol!); + } + } + + public static bool IsPartial(this ClassDeclarationSyntax cds) + => cds.Modifiers.Any(SyntaxKind.PartialKeyword); + + public static bool HasDisableGeneratorsAttribute(this INamedTypeSymbol symbol) + => symbol.GetAttributes().Any(attr => + attr.AttributeClass?.ToString() == GodotClasses.DisableGodotGeneratorsAttr); + + private static SymbolDisplayFormat FullyQualifiedFormatOmitGlobal { get; } = + SymbolDisplayFormat.FullyQualifiedFormat + .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted); + + public static string FullQualifiedName(this INamedTypeSymbol symbol) + => symbol.ToDisplayString(NullableFlowState.NotNull, FullyQualifiedFormatOmitGlobal); + } +} diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Godot.SourceGenerators.csproj b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Godot.SourceGenerators.csproj new file mode 100644 index 0000000000..224d7e5b5a --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Godot.SourceGenerators.csproj @@ -0,0 +1,40 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <PropertyGroup> + <TargetFramework>netstandard2.0</TargetFramework> + <LangVersion>8.0</LangVersion> + <Nullable>enable</Nullable> + </PropertyGroup> + <PropertyGroup> + <Description>Core C# source generator for Godot projects.</Description> + <Authors>Godot Engine contributors</Authors> + + <PackageId>Godot.SourceGenerators</PackageId> + <Version>4.0.0</Version> + <PackageVersion>$(PackageVersion_Godot_SourceGenerators)</PackageVersion> + <RepositoryUrl>https://github.com/godotengine/godot/tree/master/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators</RepositoryUrl> + <PackageProjectUrl>$(RepositoryUrl)</PackageProjectUrl> + <PackageLicenseExpression>MIT</PackageLicenseExpression> + + <GeneratePackageOnBuild>true</GeneratePackageOnBuild> <!-- Generates a package at build --> + <IncludeBuildOutput>false</IncludeBuildOutput> <!-- Do not include the generator as a lib dependency --> + </PropertyGroup> + <ItemGroup> + <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" PrivateAssets="all" /> + <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.1" PrivateAssets="all" /> + </ItemGroup> + <ItemGroup> + <!-- Package the generator in the analyzer directory of the nuget package --> + <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" /> + + <!-- Package the props file --> + <None Include="Godot.SourceGenerators.props" Pack="true" PackagePath="build" Visible="false" /> + </ItemGroup> + + <Target Name="CopyNupkgToSConsOutputDir" AfterTargets="Pack"> + <PropertyGroup> + <GodotSourceRootPath>$(SolutionDir)\..\..\..\..\</GodotSourceRootPath> + <GodotOutputDataDir>$(GodotSourceRootPath)\bin\GodotSharp\</GodotOutputDataDir> + </PropertyGroup> + <Copy SourceFiles="$(PackageOutputPath)$(PackageId).$(PackageVersion).nupkg" DestinationFolder="$(GodotOutputDataDir)Tools\nupkgs\" /> + </Target> +</Project> diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Godot.SourceGenerators.props b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Godot.SourceGenerators.props new file mode 100644 index 0000000000..f9b47ad5b1 --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Godot.SourceGenerators.props @@ -0,0 +1,7 @@ +<Project> + <ItemGroup> + <!-- $(GodotProjectDir) is defined by Godot.NET.Sdk --> + <CompilerVisibleProperty Include="GodotProjectDir" /> + <CompilerVisibleProperty Include="GodotScriptPathAttributeGenerator" /> + </ItemGroup> +</Project> diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotClasses.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotClasses.cs new file mode 100644 index 0000000000..29e41d155a --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotClasses.cs @@ -0,0 +1,9 @@ +namespace Godot.SourceGenerators +{ + public static class GodotClasses + { + public const string Object = "Godot.Object"; + public const string DisableGodotGeneratorsAttr = "Godot.DisableGodotGeneratorsAttribute"; + public const string AssemblyHasScriptsAttr = "Godot.AssemblyHasScriptsAttribute"; + } +} diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPathAttributeGenerator.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPathAttributeGenerator.cs new file mode 100644 index 0000000000..150e59e414 --- /dev/null +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ScriptPathAttributeGenerator.cs @@ -0,0 +1,156 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Text; + +namespace Godot.SourceGenerators +{ + [Generator] + public class ScriptPathAttributeGenerator : ISourceGenerator + { + public void Execute(GeneratorExecutionContext context) + { + if (context.TryGetGlobalAnalyzerProperty("GodotScriptPathAttributeGenerator", out string? toggle) + && toggle == "disabled") + { + return; + } + + // NOTE: IsNullOrEmpty doesn't work well with nullable checks + // ReSharper disable once ReplaceWithStringIsNullOrEmpty + if (!context.TryGetGlobalAnalyzerProperty("GodotProjectDir", out string? godotProjectDir) + || godotProjectDir!.Length == 0) + { + throw new InvalidOperationException("Property 'GodotProjectDir' is null or empty."); + } + + var godotClasses = context.Compilation.SyntaxTrees + .SelectMany(tree => + tree.GetRoot().DescendantNodes() + .OfType<ClassDeclarationSyntax>() + // Ignore inner classes + .Where(cds => !(cds.Parent is ClassDeclarationSyntax)) + .SelectGodotScriptClasses(context.Compilation) + // Report and skip non-partial classes + .Where(x => + { + if (x.cds.IsPartial() || x.symbol.HasDisableGeneratorsAttribute()) + return true; + Common.ReportNonPartialGodotScriptClass(context, x.cds, x.symbol); + return false; + }) + ) + // Ignore classes whose name is not the same as the file name + .Where(x => Path.GetFileNameWithoutExtension(x.cds.SyntaxTree.FilePath) == x.symbol.Name) + .GroupBy(x => x.symbol) + .ToDictionary(g => g.Key, g => g.Select(x => x.cds)); + + foreach (var godotClass in godotClasses) + { + VisitGodotScriptClass(context, godotProjectDir, + symbol: godotClass.Key, + classDeclarations: godotClass.Value); + } + + if (godotClasses.Count <= 0) + return; + + AddScriptTypesAssemblyAttr(context, godotClasses); + } + + private static void VisitGodotScriptClass( + GeneratorExecutionContext context, + string godotProjectDir, + INamedTypeSymbol symbol, + IEnumerable<ClassDeclarationSyntax> classDeclarations + ) + { + var attributesBuilder = new StringBuilder(); + + // Remember syntax trees for which we already added an attribute, to prevent unnecessary duplicates. + var attributedTrees = new List<SyntaxTree>(); + + foreach (var cds in classDeclarations) + { + if (attributedTrees.Contains(cds.SyntaxTree)) + continue; + + attributedTrees.Add(cds.SyntaxTree); + + if (attributesBuilder.Length != 0) + attributesBuilder.Append("\n "); + + attributesBuilder.Append(@"[ScriptPathAttribute(""res://"); + attributesBuilder.Append(RelativeToDir(cds.SyntaxTree.FilePath, godotProjectDir)); + attributesBuilder.Append(@""")]"); + } + + string classNs = symbol.ContainingNamespace.Name; + string className = symbol.Name; + + var source = $@"using Godot; +namespace {classNs} +{{ + {attributesBuilder} + partial class {className} + {{ + }} +}} +"; + context.AddSource(classNs + "." + className + "_ScriptPath_Generated", + SourceText.From(source, Encoding.UTF8)); + } + + private static void AddScriptTypesAssemblyAttr(GeneratorExecutionContext context, + Dictionary<INamedTypeSymbol, IEnumerable<ClassDeclarationSyntax>> godotClasses) + { + var sourceBuilder = new StringBuilder(); + + sourceBuilder.Append("[assembly:"); + sourceBuilder.Append(GodotClasses.AssemblyHasScriptsAttr); + sourceBuilder.Append("(new System.Type[] {"); + + bool first = true; + + foreach (var godotClass in godotClasses) + { + var qualifiedName = godotClass.Key.ToDisplayString( + NullableFlowState.NotNull, SymbolDisplayFormat.FullyQualifiedFormat); + if (!first) + sourceBuilder.Append(", "); + first = false; + sourceBuilder.Append("typeof("); + sourceBuilder.Append(qualifiedName); + sourceBuilder.Append(")"); + } + + sourceBuilder.Append("})]\n"); + + context.AddSource("AssemblyScriptTypes_Generated", + SourceText.From(sourceBuilder.ToString(), Encoding.UTF8)); + } + + public void Initialize(GeneratorInitializationContext context) + { + } + + private static string RelativeToDir(string path, string dir) + { + // Make sure the directory ends with a path separator + dir = Path.Combine(dir, " ").TrimEnd(); + + if (Path.DirectorySeparatorChar == '\\') + dir = dir.Replace("/", "\\") + "\\"; + + var fullPath = new Uri(Path.GetFullPath(path), UriKind.Absolute); + var relRoot = new Uri(Path.GetFullPath(dir), UriKind.Absolute); + + // MakeRelativeUri converts spaces to %20, hence why we need UnescapeDataString + return Uri.UnescapeDataString(relRoot.MakeRelativeUri(fullPath).ToString()); + } + } +} |