From 3a65cab96b2b8026169b5babe314c320e60b73ca Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 10 Dec 2023 23:45:48 +0000 Subject: [PATCH 1/3] Unity separate generator project The main differences are: - Doesn't generate the attributes class in assemblies (and because of that doesn't require RegisterForPostInitialization) - Uses CodeAnalysis 3.8.0 - Doesn't throw exception when attribute types are missing - Skips Unity builtin assemblies For some reason more pragma warning disable are required when in a separate directory Fix missing type exception not ignored --- .../Generator}/ContainerGenerator.Roslyn3.cs | 4 ++- src/Jab.Roslyn3/Jab.Roslyn3.csproj | 4 +++ .../Generator/ContainerGenerator.Unity.cs | 27 +++++++++++++++++++ src/Jab.Unity/Jab.Unity.csproj | 27 +++++++++++++++++++ src/Jab/ContainerGenerator.cs | 6 +++++ .../ContainerGenerator.Roslyn4.cs | 2 ++ src/Jab/Jab.csproj | 4 +++ src/Jab/ServiceProviderBuilder.cs | 15 +++++++++-- src/jab.sln | 14 ++++++++++ 9 files changed, 100 insertions(+), 3 deletions(-) rename src/{Jab => Jab.Roslyn3/Generator}/ContainerGenerator.Roslyn3.cs (65%) create mode 100644 src/Jab.Unity/Generator/ContainerGenerator.Unity.cs create mode 100644 src/Jab.Unity/Jab.Unity.csproj rename src/Jab/{ => Generator}/ContainerGenerator.Roslyn4.cs (86%) diff --git a/src/Jab/ContainerGenerator.Roslyn3.cs b/src/Jab.Roslyn3/Generator/ContainerGenerator.Roslyn3.cs similarity index 65% rename from src/Jab/ContainerGenerator.Roslyn3.cs rename to src/Jab.Roslyn3/Generator/ContainerGenerator.Roslyn3.cs index b1a08b9..313b9ab 100644 --- a/src/Jab/ContainerGenerator.Roslyn3.cs +++ b/src/Jab.Roslyn3/Generator/ContainerGenerator.Roslyn3.cs @@ -1,7 +1,9 @@ #if !ROSLYN4_0_OR_GREATER namespace Jab { - public partial class ContainerGenerator: ISourceGenerator +#pragma warning disable RS1001 // We don't want this to be discovered as analyzer but it simplifies testing + public partial class ContainerGenerator : ISourceGenerator +#pragma warning restore RS1001 // We don't want this to be discovered as analyzer but it simplifies testing { public void Initialize(GeneratorInitializationContext context) { diff --git a/src/Jab.Roslyn3/Jab.Roslyn3.csproj b/src/Jab.Roslyn3/Jab.Roslyn3.csproj index c30c9b1..e05aba1 100644 --- a/src/Jab.Roslyn3/Jab.Roslyn3.csproj +++ b/src/Jab.Roslyn3/Jab.Roslyn3.csproj @@ -10,6 +10,10 @@ + + + + diff --git a/src/Jab.Unity/Generator/ContainerGenerator.Unity.cs b/src/Jab.Unity/Generator/ContainerGenerator.Unity.cs new file mode 100644 index 0000000..70f4299 --- /dev/null +++ b/src/Jab.Unity/Generator/ContainerGenerator.Unity.cs @@ -0,0 +1,27 @@ +namespace Jab +{ + /// + /// In order to use inside Unity follow: + /// https://docs.unity3d.com/Manual/roslyn-analyzers.html + /// Also requires the Jab.Attributes.dll + /// +#pragma warning disable RS1001 // We don't want this to be discovered as analyzer but it simplifies testing + public partial class ContainerGenerator: ISourceGenerator +#pragma warning restore RS1001 // We don't want this to be discovered as analyzer but it simplifies testing + { + public void Initialize(GeneratorInitializationContext context) + { + context.RegisterForSyntaxNotifications(() => new SyntaxCollector()); + } + + public void Execute(GeneratorExecutionContext context) + { + var moduleName = context.Compilation.SourceModule.Name; + if (moduleName.StartsWith("UnityEngine.")) return; + if (moduleName.StartsWith("UnityEditor.")) return; + if (moduleName.StartsWith("Unity.")) return; + + Execute(new GeneratorContext(context)); + } + } +} \ No newline at end of file diff --git a/src/Jab.Unity/Jab.Unity.csproj b/src/Jab.Unity/Jab.Unity.csproj new file mode 100644 index 0000000..236064e --- /dev/null +++ b/src/Jab.Unity/Jab.Unity.csproj @@ -0,0 +1,27 @@ + + + + netstandard2.0 + false + $(NoWarn);RS2008 + + + + TRACE;IGNORE_MISSING_TYPE + + + + TRACE;IGNORE_MISSING_TYPE + + + + + + + + + + + + + diff --git a/src/Jab/ContainerGenerator.cs b/src/Jab/ContainerGenerator.cs index 7aa4353..543ac39 100644 --- a/src/Jab/ContainerGenerator.cs +++ b/src/Jab/ContainerGenerator.cs @@ -344,6 +344,12 @@ private void Execute(GeneratorContext context) context.AddSource($"{root.Type.Name}.Generated.cs", codeWriter.ToString()); } } +#if IGNORE_MISSING_TYPE + catch (MissingTypeException) + { + //Ignore + } +#endif catch (Exception e) { context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.UnexpectedErrorDescriptor, Location.None, e.ToString().Replace(Environment.NewLine, " "))); diff --git a/src/Jab/ContainerGenerator.Roslyn4.cs b/src/Jab/Generator/ContainerGenerator.Roslyn4.cs similarity index 86% rename from src/Jab/ContainerGenerator.Roslyn4.cs rename to src/Jab/Generator/ContainerGenerator.Roslyn4.cs index bd2d04c..75d319d 100644 --- a/src/Jab/ContainerGenerator.Roslyn4.cs +++ b/src/Jab/Generator/ContainerGenerator.Roslyn4.cs @@ -1,7 +1,9 @@ #if ROSLYN4_0_OR_GREATER namespace Jab { +#pragma warning disable RS1001 // We don't want this to be discovered as analyzer but it simplifies testing public partial class ContainerGenerator : IIncrementalGenerator +#pragma warning restore RS1001 // We don't want this to be discovered as analyzer but it simplifies testing { public void Initialize(IncrementalGeneratorInitializationContext context) { diff --git a/src/Jab/Jab.csproj b/src/Jab/Jab.csproj index fc9b84f..0de46b8 100644 --- a/src/Jab/Jab.csproj +++ b/src/Jab/Jab.csproj @@ -38,6 +38,10 @@ + + + + diff --git a/src/Jab/ServiceProviderBuilder.cs b/src/Jab/ServiceProviderBuilder.cs index 1d04f53..9f2acf0 100644 --- a/src/Jab/ServiceProviderBuilder.cs +++ b/src/Jab/ServiceProviderBuilder.cs @@ -80,12 +80,12 @@ public KnownTypes(Compilation compilation, IAssemblySymbol assemblySymbol) static INamedTypeSymbol GetTypeByMetadataNameOrThrow(IAssemblySymbol assemblySymbol, string fullyQualifiedMetadataName) => assemblySymbol.GetTypeByMetadataName(fullyQualifiedMetadataName) - ?? throw new InvalidOperationException($"Type with metadata '{fullyQualifiedMetadataName}' not found"); + ?? throw new MissingTypeException(fullyQualifiedMetadataName); static INamedTypeSymbol GetTypeFromCompilationByMetadataNameOrThrow(Compilation compilation, string fullyQualifiedMetadataName) => compilation.GetTypeByMetadataName(fullyQualifiedMetadataName) - ?? throw new InvalidOperationException($"Type with metadata '{fullyQualifiedMetadataName}' not found"); + ?? throw new MissingTypeException(fullyQualifiedMetadataName); IEnumerableType = GetTypeFromCompilationByMetadataNameOrThrow(compilation, IEnumerableMetadataName); IServiceProviderType = GetTypeFromCompilationByMetadataNameOrThrow(compilation, IServiceProviderMetadataName); @@ -116,6 +116,17 @@ static INamedTypeSymbol GetTypeFromCompilationByMetadataNameOrThrow(Compilation } } +public class MissingTypeException : Exception +{ + public MissingTypeException(string fullyQualifiedMetadataName) + : base($"Type with metadata '{fullyQualifiedMetadataName}' not found") + { + FullyQualifiedMetadataName = fullyQualifiedMetadataName; + } + + public string FullyQualifiedMetadataName { get; } +} + internal class ServiceProviderBuilder { private readonly GeneratorContext _context; diff --git a/src/jab.sln b/src/jab.sln index f8eeded..ae436bd 100644 --- a/src/jab.sln +++ b/src/jab.sln @@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jab.Attributes", "Jab.Attri EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jab.FunctionalTests.Attributes", "Jab.FunctionalTests.Attributes\Jab.FunctionalTests.Attributes.csproj", "{471D59B3-518B-4821-9643-6010AACF1433}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jab.Unity", "Jab.Unity\Jab.Unity.csproj", "{6BD07051-FFDD-4801-9440-BBC73E46F80C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -170,5 +172,17 @@ Global {471D59B3-518B-4821-9643-6010AACF1433}.Release|x64.Build.0 = Release|Any CPU {471D59B3-518B-4821-9643-6010AACF1433}.Release|x86.ActiveCfg = Release|Any CPU {471D59B3-518B-4821-9643-6010AACF1433}.Release|x86.Build.0 = Release|Any CPU + {6BD07051-FFDD-4801-9440-BBC73E46F80C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6BD07051-FFDD-4801-9440-BBC73E46F80C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6BD07051-FFDD-4801-9440-BBC73E46F80C}.Debug|x64.ActiveCfg = Debug|Any CPU + {6BD07051-FFDD-4801-9440-BBC73E46F80C}.Debug|x64.Build.0 = Debug|Any CPU + {6BD07051-FFDD-4801-9440-BBC73E46F80C}.Debug|x86.ActiveCfg = Debug|Any CPU + {6BD07051-FFDD-4801-9440-BBC73E46F80C}.Debug|x86.Build.0 = Debug|Any CPU + {6BD07051-FFDD-4801-9440-BBC73E46F80C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6BD07051-FFDD-4801-9440-BBC73E46F80C}.Release|Any CPU.Build.0 = Release|Any CPU + {6BD07051-FFDD-4801-9440-BBC73E46F80C}.Release|x64.ActiveCfg = Release|Any CPU + {6BD07051-FFDD-4801-9440-BBC73E46F80C}.Release|x64.Build.0 = Release|Any CPU + {6BD07051-FFDD-4801-9440-BBC73E46F80C}.Release|x86.ActiveCfg = Release|Any CPU + {6BD07051-FFDD-4801-9440-BBC73E46F80C}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection EndGlobal From 9df57bb433e82b121b6a12c64c91ee4c9310d924 Mon Sep 17 00:00:00 2001 From: Alon Talmi <130899578+AlonTalmi@users.noreply.github.com> Date: Wed, 13 Dec 2023 18:59:23 +0100 Subject: [PATCH 2/3] Switch from try-catch to HasKnownTypes check --- .../Generator/ContainerGenerator.Unity.cs | 6 ++---- src/Jab.Unity/Jab.Unity.csproj | 4 ++-- src/Jab/ContainerGenerator.cs | 6 ------ src/Jab/ServiceProviderBuilder.cs | 14 ++++---------- 4 files changed, 8 insertions(+), 22 deletions(-) diff --git a/src/Jab.Unity/Generator/ContainerGenerator.Unity.cs b/src/Jab.Unity/Generator/ContainerGenerator.Unity.cs index 70f4299..f9c4b6c 100644 --- a/src/Jab.Unity/Generator/ContainerGenerator.Unity.cs +++ b/src/Jab.Unity/Generator/ContainerGenerator.Unity.cs @@ -16,10 +16,8 @@ public void Initialize(GeneratorInitializationContext context) public void Execute(GeneratorExecutionContext context) { - var moduleName = context.Compilation.SourceModule.Name; - if (moduleName.StartsWith("UnityEngine.")) return; - if (moduleName.StartsWith("UnityEditor.")) return; - if (moduleName.StartsWith("Unity.")) return; + if(!KnownTypes.HasKnownTypes(context.Compilation.SourceModule)) + return; Execute(new GeneratorContext(context)); } diff --git a/src/Jab.Unity/Jab.Unity.csproj b/src/Jab.Unity/Jab.Unity.csproj index 236064e..595b6e3 100644 --- a/src/Jab.Unity/Jab.Unity.csproj +++ b/src/Jab.Unity/Jab.Unity.csproj @@ -7,11 +7,11 @@ - TRACE;IGNORE_MISSING_TYPE + TRACE; - TRACE;IGNORE_MISSING_TYPE + TRACE; diff --git a/src/Jab/ContainerGenerator.cs b/src/Jab/ContainerGenerator.cs index 543ac39..7aa4353 100644 --- a/src/Jab/ContainerGenerator.cs +++ b/src/Jab/ContainerGenerator.cs @@ -344,12 +344,6 @@ private void Execute(GeneratorContext context) context.AddSource($"{root.Type.Name}.Generated.cs", codeWriter.ToString()); } } -#if IGNORE_MISSING_TYPE - catch (MissingTypeException) - { - //Ignore - } -#endif catch (Exception e) { context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.UnexpectedErrorDescriptor, Location.None, e.ToString().Replace(Environment.NewLine, " "))); diff --git a/src/Jab/ServiceProviderBuilder.cs b/src/Jab/ServiceProviderBuilder.cs index 9f2acf0..0eaeebb 100644 --- a/src/Jab/ServiceProviderBuilder.cs +++ b/src/Jab/ServiceProviderBuilder.cs @@ -80,12 +80,12 @@ public KnownTypes(Compilation compilation, IAssemblySymbol assemblySymbol) static INamedTypeSymbol GetTypeByMetadataNameOrThrow(IAssemblySymbol assemblySymbol, string fullyQualifiedMetadataName) => assemblySymbol.GetTypeByMetadataName(fullyQualifiedMetadataName) - ?? throw new MissingTypeException(fullyQualifiedMetadataName); + ?? throw new InvalidOperationException($"Type with metadata '{fullyQualifiedMetadataName}' not found"); static INamedTypeSymbol GetTypeFromCompilationByMetadataNameOrThrow(Compilation compilation, string fullyQualifiedMetadataName) => compilation.GetTypeByMetadataName(fullyQualifiedMetadataName) - ?? throw new MissingTypeException(fullyQualifiedMetadataName); + ?? throw new InvalidOperationException($"Type with metadata '{fullyQualifiedMetadataName}' not found"); IEnumerableType = GetTypeFromCompilationByMetadataNameOrThrow(compilation, IEnumerableMetadataName); IServiceProviderType = GetTypeFromCompilationByMetadataNameOrThrow(compilation, IServiceProviderMetadataName); @@ -114,17 +114,11 @@ static INamedTypeSymbol GetTypeFromCompilationByMetadataNameOrThrow(Compilation ModuleAttribute = GetTypeByMetadataNameOrThrow(assemblySymbol, ServiceProviderModuleAttributeMetadataName); } -} -public class MissingTypeException : Exception -{ - public MissingTypeException(string fullyQualifiedMetadataName) - : base($"Type with metadata '{fullyQualifiedMetadataName}' not found") + public static bool HasKnownTypes(IModuleSymbol sourceModule) { - FullyQualifiedMetadataName = fullyQualifiedMetadataName; + return sourceModule.ReferencedAssemblySymbols.Any(s => s.Name == JabAttributesAssemblyName); } - - public string FullyQualifiedMetadataName { get; } } internal class ServiceProviderBuilder From bf944ad74191ecf3b7bb38879d1c982cfab52c77 Mon Sep 17 00:00:00 2001 From: Alon Talmi <130899578+AlonTalmi@users.noreply.github.com> Date: Wed, 13 Dec 2023 20:25:02 +0100 Subject: [PATCH 3/3] Move ContainerGenerators outside the Generator directory --- .../{Generator => }/ContainerGenerator.Roslyn3.cs | 0 src/Jab.Roslyn3/Jab.Roslyn3.csproj | 2 +- .../{Generator => }/ContainerGenerator.Unity.cs | 0 src/Jab.Unity/Jab.Unity.csproj | 10 +--------- src/Jab/{Generator => }/ContainerGenerator.Roslyn4.cs | 0 src/Jab/Jab.csproj | 4 ---- 6 files changed, 2 insertions(+), 14 deletions(-) rename src/Jab.Roslyn3/{Generator => }/ContainerGenerator.Roslyn3.cs (100%) rename src/Jab.Unity/{Generator => }/ContainerGenerator.Unity.cs (100%) rename src/Jab/{Generator => }/ContainerGenerator.Roslyn4.cs (100%) diff --git a/src/Jab.Roslyn3/Generator/ContainerGenerator.Roslyn3.cs b/src/Jab.Roslyn3/ContainerGenerator.Roslyn3.cs similarity index 100% rename from src/Jab.Roslyn3/Generator/ContainerGenerator.Roslyn3.cs rename to src/Jab.Roslyn3/ContainerGenerator.Roslyn3.cs diff --git a/src/Jab.Roslyn3/Jab.Roslyn3.csproj b/src/Jab.Roslyn3/Jab.Roslyn3.csproj index e05aba1..fccf948 100644 --- a/src/Jab.Roslyn3/Jab.Roslyn3.csproj +++ b/src/Jab.Roslyn3/Jab.Roslyn3.csproj @@ -11,7 +11,7 @@ - + diff --git a/src/Jab.Unity/Generator/ContainerGenerator.Unity.cs b/src/Jab.Unity/ContainerGenerator.Unity.cs similarity index 100% rename from src/Jab.Unity/Generator/ContainerGenerator.Unity.cs rename to src/Jab.Unity/ContainerGenerator.Unity.cs diff --git a/src/Jab.Unity/Jab.Unity.csproj b/src/Jab.Unity/Jab.Unity.csproj index 595b6e3..805ea50 100644 --- a/src/Jab.Unity/Jab.Unity.csproj +++ b/src/Jab.Unity/Jab.Unity.csproj @@ -6,20 +6,12 @@ $(NoWarn);RS2008 - - TRACE; - - - - TRACE; - - - + diff --git a/src/Jab/Generator/ContainerGenerator.Roslyn4.cs b/src/Jab/ContainerGenerator.Roslyn4.cs similarity index 100% rename from src/Jab/Generator/ContainerGenerator.Roslyn4.cs rename to src/Jab/ContainerGenerator.Roslyn4.cs diff --git a/src/Jab/Jab.csproj b/src/Jab/Jab.csproj index 0de46b8..fc9b84f 100644 --- a/src/Jab/Jab.csproj +++ b/src/Jab/Jab.csproj @@ -38,10 +38,6 @@ - - - -