Skip to content

Commit

Permalink
Also generate OpenApi Schemas; Prettier Schemas after Generation (#464)
Browse files Browse the repository at this point in the history
Generate OpenAPI Schemas, too


Also generate OpenApi Schemas; Prettier Schemas after Generation
  • Loading branch information
hf-kklein authored Aug 1, 2024
1 parent 0637132 commit 37d171a
Show file tree
Hide file tree
Showing 105 changed files with 35,108 additions and 23,409 deletions.
79 changes: 64 additions & 15 deletions SchemaGenerator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
using System.Text;
using Newtonsoft.Json.Schema;
using BO4E.BO;
using NJsonSchema;
using NJsonSchema.Generation;
using JsonSchema = NJsonSchema.JsonSchema;

Console.WriteLine("Starting schema generation process...");

int offset;
string outputDirectory;
string jsonOutputDirectory;
string openApiOutputDirectory;

// Validate arguments
if (args.Length < 2)
if (args.Length < 3)
{
Console.Error.WriteLine("Error: You must provide both an offset and an output directory.");
Console.Error.WriteLine("Usage: dotnet run -- <offset> <outputDirectory>");
Console.Error.WriteLine("Error: You must provide an offset, a JSON output directory, and an OpenAPI output directory.");
Console.Error.WriteLine("Usage: dotnet run -- <offset> <jsonOutputDirectory> <openApiOutputDirectory>");
Environment.Exit(1); // Exit with a specific error code for missing arguments
}

Expand All @@ -21,16 +25,19 @@
Environment.Exit(2); // Exit with a specific error code for invalid offset
}

// Get the output directory from the arguments
outputDirectory = args[1];
// Get the output directories from the arguments
jsonOutputDirectory = args[1];
openApiOutputDirectory = args[2];

Console.WriteLine($"Offset provided: {offset}");
Console.WriteLine($"Output directory: {outputDirectory}");
Console.WriteLine($"JSON Output directory: {jsonOutputDirectory}");
Console.WriteLine($"OpenAPI Output directory: {openApiOutputDirectory}");

// Generate schemas
try
{
JsonSchemaGenerator.GenerateSchemas(offset, outputDirectory);
JsonSchemaGenerator.GenerateSchemas(offset, jsonOutputDirectory);
JsonSchemaGenerator.GenerateOpenApiSchemas(offset, openApiOutputDirectory);
Console.WriteLine("Schema generation completed successfully.");
}
catch (Exception ex)
Expand All @@ -42,14 +49,14 @@
Environment.Exit(0); // Success

/// <summary>
/// Generates plain JSON schemas from business object model classes.
/// Generates plain JSON and OpenAPI schemas from business object model classes.
/// </summary>
public class JsonSchemaGenerator
{
private const int LastDataRowOffset = 50;
private const int MaxSchemasPerHour = 10;

public static void GenerateSchemas(int offset, string outputDirectory)
public static void GenerateSchemas(int offset, string jsonOutputDirectory)
{
var relevantBusinessObjectTypes = typeof(BusinessObject).Assembly
.GetTypes()
Expand All @@ -63,17 +70,17 @@ public static void GenerateSchemas(int offset, string outputDirectory)
try
{
// Ensure the output directory exists
if (!Directory.Exists(outputDirectory))
if (!Directory.Exists(jsonOutputDirectory))
{
Directory.CreateDirectory(outputDirectory);
Directory.CreateDirectory(jsonOutputDirectory);
}

foreach (var type in relevantBusinessObjectTypes.Skip(offset).Take(MaxSchemasPerHour))
{
var schema = BusinessObject.GetJsonSchema(type);
var path = Path.Combine(outputDirectory, $"{type.Name}.json");
var path = Path.Combine(jsonOutputDirectory, $"{type.Name}.json");

Console.WriteLine($"Generating schema for {type.Name} at {path}.");
Console.WriteLine($"Generating JSON schema for {type.Name} at {path}.");

if (!File.Exists(path))
{
Expand All @@ -88,7 +95,49 @@ public static void GenerateSchemas(int offset, string outputDirectory)
}
catch (JSchemaException jse)
{
Console.Error.WriteLine($"Schema generation failed with error: {jse.Message}");
Console.Error.WriteLine($"JSON schema generation failed with error: {jse.Message}");
Environment.Exit(3); // Exit with a specific error code for schema generation failure
}
}

public static void GenerateOpenApiSchemas(int offset, string openApiOutputDirectory)
{
var relevantBusinessObjectTypes = typeof(BusinessObject).Assembly
.GetTypes()
.Where(t => t.IsSubclassOf(typeof(BusinessObject)));

try
{
// Ensure the output directory exists
if (!Directory.Exists(openApiOutputDirectory))
{
Directory.CreateDirectory(openApiOutputDirectory);
}

foreach (var type in relevantBusinessObjectTypes.Skip(offset))
{
var schema = JsonSchema.FromType(type, new SystemTextJsonSchemaGeneratorSettings()
{
SchemaType = SchemaType.OpenApi3
});
var path = Path.Combine(openApiOutputDirectory, $"{type.Name}.json");

Console.WriteLine($"Generating OpenAPI schema for {type.Name} at {path}.");

if (!File.Exists(path))
{
using (File.Create(path))
{
}
}

var utf8WithoutByteOrderMark = new UTF8Encoding(false);
File.WriteAllText(path, schema.ToJson(), utf8WithoutByteOrderMark);
}
}
catch (Exception ex)
{
Console.Error.WriteLine($"OpenAPI schema generation failed with error: {ex.Message}");
Environment.Exit(3); // Exit with a specific error code for schema generation failure
}
}
Expand Down
4 changes: 4 additions & 0 deletions SchemaGenerator/SchemaGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
<ProjectReference Include="..\BO4E\BO4E.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NJsonSchema" Version="11.0.2" />
</ItemGroup>

</Project>
90 changes: 78 additions & 12 deletions SchemaGenerator/generate-json-schemas.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,89 @@
# Cross-platform script to run the .NET console application with different offsets and an optional output directory
# Invoke it like this: powershell.exe C:/github/BO4E-dotnet2/SchemaGenerator/generate-json-schemas.sh
# ----------------------------------------------------------------------------
# generate-schemas.sh
# Cross-platform script to run the .NET console application for schema generation.
# Generates both JSON and OpenAPI schemas for business objects with specified offsets.
#
# Usage:
# powershell.exe C:/github/BO4E-dotnet2/SchemaGenerator/generate-json-schemas.sh [jsonOutputDirectory] [openApiOutputDirectory]
#
# Arguments:
# jsonOutputDirectory (optional) - Directory to output JSON schema files. Defaults to ../json-schema-files.
# openApiOutputDirectory (optional) - Directory to output OpenAPI schema files. Defaults to ../open-api-schemas.
#
# Example:
# powershell.exe C:/github/BO4E-dotnet2/SchemaGenerator/generate-json-schemas.sh ../json-schema ../openapi-schema
#
# ----------------------------------------------------------------------------

# Define the offsets
offsets=(10 20 30 40 50)

# Define the default output directory
default_output_directory="../json-schema-files" # json-schema-files in repo root
# Define the default output directories
default_json_output_directory="../json-schema-files" # json-schema-files in repo root
default_openapi_output_directory="../open-api-schemas" # open-api-schemas in repo root

# Check if an output directory argument is provided
if [ $# -eq 1 ]; then
output_directory=$1
else
output_directory=$default_output_directory
# Display usage information if no arguments are provided or if there's an error in the arguments
usage() {
echo "Usage: $0 [jsonOutputDirectory] [openApiOutputDirectory]"
echo " jsonOutputDirectory Directory to output JSON schema files. Default: $default_json_output_directory"
echo " openApiOutputDirectory Directory to output OpenAPI schema files. Default: $default_openapi_output_directory"
echo ""
echo "Example:"
echo " $0 ../json-schema ../openapi-schema"
}

# Check if output directory arguments are provided and set directories
if [ $# -gt 2 ]; then
echo "Error: Too many arguments."
usage
exit 1
fi

json_output_directory=${1:-$default_json_output_directory}
openapi_output_directory=${2:-$default_openapi_output_directory}

# Ensure that output directories are valid and accessible
if [ ! -d "$json_output_directory" ]; then
echo "Error: JSON output directory '$json_output_directory' does not exist or is not accessible."
usage
exit 1
fi

if [ ! -d "$openapi_output_directory" ]; then
echo "Error: OpenAPI output directory '$openapi_output_directory' does not exist or is not accessible."
usage
exit 1
fi

# Define the project path
project_path="C:/github/BO4E-dotnet2/SchemaGenerator/SchemaGenerator.csproj"

# Check if the project path exists
if [ ! -f "$project_path" ]; then
echo "Error: Project file not found at '$project_path'. Ensure the project path is correct."
exit 1
fi

# Loop through the offsets and call the .NET application
for offset in "${offsets[@]}"
do
echo "Running schema generation with offset: $offset in directory: $output_directory"
# Run the .NET application
dotnet run -- $offset $output_directory
echo "Running schema generation with offset: $offset"
echo "JSON output directory: $json_output_directory"
echo "OpenAPI output directory: $openapi_output_directory"
# Run the .NET application with the project path
dotnet run --project "$project_path" -- $offset $json_output_directory $openapi_output_directory
done

# Check if Prettier is installed and format the output files
if command -v prettier &> /dev/null
then
echo "Prettier is installed. Formatting output files..."
prettier -w "$json_output_directory/*.json"
prettier -w "$openapi_output_directory/*.json"
echo "Formatting complete."
else
echo "Prettier is not installed. Skipping formatting."
fi

# Keep the window open
read -p "Press any key to exit..."
Loading

0 comments on commit 37d171a

Please sign in to comment.