protected override bool Generate(JsonSchemaItem item, JsonSchemaItemMetadata metadata)
        {
            try
            {
                var outputFilePath = item.GetOutputFilePath(TargetLanguage);
                var schema         = JsonSchema4.FromFileAsync(item.SchemaFile).Result;
                var settings       = new CSharpGeneratorSettings
                {
                    Namespace = metadata.Namespace,
                };
                var code = new CSharpGenerator(schema, settings).GenerateFile();
                File.WriteAllText(outputFilePath, code);

                return(true);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, item.SchemaFile);
                return(false);
            }
        }
Example #2
0
        public override bool Execute()
        {
            // Setup Logger
            Utils.Logger.Init(Log);

            foreach (var taskItem in JsonSchemaFiles)
            {
                var jsonSchemaItem = new JsonSchemaItem(taskItem);
                foreach (var generator in Generators)
                {
                    // Generate file
                    var outputFilePath = jsonSchemaItem.GetOutputFilePath(generator.TargetLanguage);
                    Utils.Logger.LogMessage($"Generating {jsonSchemaItem.SchemaFile} to {outputFilePath}.");
                    if (!generator.Generate(jsonSchemaItem))
                    {
                        Utils.Logger.LogError($"Failed to generate {jsonSchemaItem.SchemaFile} to {outputFilePath}.");
                        return(false);
                    }
                }
            }
            return(true);
        }
        public bool Generate(JsonSchemaItem item)
        {
            if (!item.MetadataMap.TryGetValue(TargetLanguage, out var metadata))
            {
                Logger.LogError($"Failed to get metadata for target language {TargetLanguage}");
                return(false);
            }

            if (!metadata.GenerateFile)
            {
                return(true);
            }

            var outputFilePath  = item.GetOutputFilePath(TargetLanguage);
            var outputDirectory = Path.GetDirectoryName(outputFilePath);

            if (!Directory.Exists(outputDirectory))
            {
                Directory.CreateDirectory(outputDirectory);
            }
            Logger.LogMessage($"Generating {item.SchemaFile} to {outputFilePath}.");
            return(Generate(item, metadata));
        }