示例#1
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);
        }
示例#2
0
        public void Execute(GeneratorExecutionContext context)
        {
            if (context.AreGodotSourceGeneratorsDisabled())
            {
                return;
            }

            INamedTypeSymbol[] godotClasses = context
                                              .Compilation.SyntaxTrees
                                              .SelectMany(tree =>
                                                          tree.GetRoot().DescendantNodes()
                                                          .OfType <ClassDeclarationSyntax>()
                                                          .SelectGodotScriptClasses(context.Compilation)
                                                          // Report and skip non-partial classes
                                                          .Where(x =>
            {
                if (x.cds.IsPartial())
                {
                    if (x.cds.IsNested() && !x.cds.AreAllOuterTypesPartial(out var typeMissingPartial))
                    {
                        Common.ReportNonPartialGodotScriptOuterClass(context, typeMissingPartial !);
                        return(false);
                    }

                    return(true);
                }

                Common.ReportNonPartialGodotScriptClass(context, x.cds, x.symbol);
                return(false);
            })
                                                          .Select(x => x.symbol)
                                                          )
                                              .Distinct <INamedTypeSymbol>(SymbolEqualityComparer.Default)
                                              .ToArray();

            if (godotClasses.Length > 0)
            {
                var typeCache = new MarshalUtils.TypeCache(context.Compilation);

                foreach (var godotClass in godotClasses)
                {
                    VisitGodotScriptClass(context, typeCache, godotClass);
                }
            }
        }