private void ExecuteInternal(GeneratorExecutionContext context)
        {
            try
            {
                var codeGenerator = new Neo4JCodeGenerator();

                foreach (GraphQLConfig config in context.GetConfigurations())
                {
                    if (config.Extensions.Neo4J is { } settings&&
                        context.GetSchemaDocuments(config) is { Count: > 0 } schemaDocuments)
                    {
                        var codeGeneratorContext = new CodeGeneratorContext(
                            settings.Name,
                            settings.DatabaseName,
                            settings.Namespace ?? throw new Exception("Namespace is required"), // TODO: Review in PR!!
                            schemaDocuments);

                        CodeGenerationResult?result = codeGenerator.Generate(codeGeneratorContext);
                        foreach (SourceFile?sourceFile in result.SourceFiles)
                        {
                            context.AddSource(sourceFile.Name, sourceFile.Source);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                context.ReportError(ex);
            }
        }
Exemple #2
0
        public static IReadOnlyList <DocumentNode> GetSchemaDocuments(
            this GeneratorExecutionContext context,
            GraphQLConfig config)
        {
            var list = new List <DocumentNode>();

            var rootDirectory = GetDirectoryName(config.Location) + DirectorySeparatorChar;
            var glob          = Glob.Parse(config.Documents);

            foreach (string file in context.AdditionalFiles
                     .Select(t => t.Path)
                     .Where(t => GetExtension(t).Equals(
                                GraphQLExtension,
                                StringComparison.OrdinalIgnoreCase))
                     .Where(t => t.StartsWith(rootDirectory) && glob.IsMatch(t)))
            {
                try
                {
                    DocumentNode document = Utf8GraphQLParser.Parse(File.ReadAllBytes(file));

                    if (!document.Definitions.OfType <IExecutableDefinitionNode>().Any())
                    {
                        list.Add(document);
                    }
                }
                catch (SyntaxException ex)
                {
                    context.ReportError(ex);
                }
            }

            return(list);
        }
Exemple #3
0
        public static IReadOnlyList <GraphQLConfig> GetConfigurations(
            this GeneratorExecutionContext context)
        {
            var list = new List <GraphQLConfig>();

            foreach (var configLocation in GetConfigurationFiles(context))
            {
                try
                {
                    string        json   = File.ReadAllText(configLocation);
                    GraphQLConfig config = GraphQLConfig.FromJson(json);
                    config.Location = configLocation;
                    list.Add(config);
                }
                catch (Exception ex)
                {
                    context.ReportError(
                        ErrorBuilder.New()
                        .SetMessage(ex.Message)
                        .SetException(ex)
                        .SetExtension(ErrorHelper.File, configLocation)
                        .AddLocation(new Location(1, 1))
                        .Build());
                }
            }

            return(list);
        }
 public static void ReportInternalError(
     this GeneratorExecutionContext context,
     LocalizableString message,
     DiagnosticSeverity severity = DiagnosticSeverity.Error,
     Location?location           = null
     )
 {
     context.ReportError(
         Constants.InternalErrorId,
         message,
         severity,
         location
         );
 }
        public void Execute(GeneratorExecutionContext context)
        {
            // Add the attribute declaration
            context.AddSource("constructFromAttr", ConstructFromGenerator.constructFromAttrSource);

            // Get the syntax receiver
            if (context.SyntaxReceiver is not ConstructFromSyntaxReceiver syntaxReceiver)
            {
                context.ReportInternalError("Unexpected error: could not retrieve the syntax.");
                return;
            }

            // Add the conversions
            var messages = new List <string>(syntaxReceiver.Messages);

            foreach (var candidate in syntaxReceiver.Candidates)
            {
                messages.Add(
                    $"Candidate type found: {candidate.TypeDeclaration.Identifier.ValueText} from {candidate.FromType}"
                    );

                // Get the semantic model of the type declaration
                var semanticModel =
                    context.Compilation.GetSemanticModel(candidate.TypeDeclaration.SyntaxTree);
                var typeDeclarationSymbol = semanticModel.GetDeclaredSymbol(
                    candidate.TypeDeclaration,
                    context.CancellationToken
                    );
                if (typeDeclarationSymbol is null)
                {
                    context.ReportInternalError(
                        "Unexpected error: could not get type information for fromType.",
                        location: candidate.FromType.GetLocation()
                        );
                    continue;
                }

                // TODO: reject generic types - not yet implemented
                if (typeDeclarationSymbol.IsGenericType)
                {
                    context.ReportInternalError(
                        "Generic types are currently not supported.",
                        location: candidate.FromType.GetLocation()
                        );
                    continue;
                }

                // Get the target type's symbol
                var fromTypeSymbolInfo = semanticModel.GetSymbolInfo(
                    candidate.FromType,
                    context.CancellationToken
                    );
                if (fromTypeSymbolInfo.Symbol is not INamedTypeSymbol fromTypeSymbol)
                {
                    context.ReportInternalError(
                        "Unexpected error: could not get type information for fromType.",
                        location: candidate.FromType.GetLocation()
                        );
                    continue;
                }

                // Reject unbound generic types
                if (fromTypeSymbol.IsUnboundGenericType)
                {
                    context.ReportError(
                        Constants.UnboundGenericErrorId,
                        "Unbound generic types are not supported.",
                        location: candidate.FromType.GetLocation()
                        );
                    continue;
                }

                // Type kind name
                var typeKindName = typeDeclarationSymbol switch
                {
                    { IsRecord : true } => "record",