Пример #1
0
        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);
        }
Пример #2
0
        public void Execute(GeneratorExecutionContext context)
        {
            if (context.AreGodotSourceGeneratorsDisabled())
            {
                return;
            }

            if (context.IsGodotToolsProject())
            {
                return;
            }

            // NOTE: NotNullWhen diagnostics don't work on projects targeting .NET Standard 2.0
            // ReSharper disable once ReplaceWithStringIsNullOrEmpty
            if (!context.TryGetGlobalAnalyzerProperty("GodotProjectDir", out string?godotProjectDir) ||
                godotProjectDir !.Length == 0)
            {
                throw new InvalidOperationException("Property 'GodotProjectDir' is null or empty.");
            }

            Dictionary <INamedTypeSymbol, IEnumerable <ClassDeclarationSyntax> > godotClasses = context
                                                                                                .Compilation.SyntaxTrees
                                                                                                .SelectMany(tree =>
                                                                                                            tree.GetRoot().DescendantNodes()
                                                                                                            .OfType <ClassDeclarationSyntax>()
                                                                                                            // Ignore inner classes
                                                                                                            .Where(cds => !cds.IsNested())
                                                                                                            .SelectGodotScriptClasses(context.Compilation)
                                                                                                            // Report and skip non-partial classes
                                                                                                            .Where(x =>
            {
                if (x.cds.IsPartial())
                {
                    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);
        }
Пример #3
0
 public static bool IsGodotToolsProject(this GeneratorExecutionContext context)
 => context.TryGetGlobalAnalyzerProperty("IsGodotToolsProject", out string?toggle) &&
 toggle != null &&
 toggle.Equals("true", StringComparison.OrdinalIgnoreCase);
Пример #4
0
 public static bool AreGodotSourceGeneratorsDisabled(this GeneratorExecutionContext context)
 => context.TryGetGlobalAnalyzerProperty("GodotSourceGenerators", out string?toggle) &&
 toggle != null &&
 toggle.Equals("disabled", StringComparison.OrdinalIgnoreCase);