-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move JSON Schema Generation to separate
.csproj
; Add shell script (#…
…463) * Move JSON Schema Generation to separate `.csproj`; Add shell script previously the JSON schemas in `json-schema-files` were generated by a unittest. That's not as intuitive as it could be. disclaimer: i generated most of the code with chatti: https://chatgpt.com/share/36c015f9-468a-43b5-b614-690e0b672d57 * remove bom * remove more boms
- Loading branch information
Showing
39 changed files
with
31,062 additions
and
562 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System.Text; | ||
using Newtonsoft.Json.Schema; | ||
using BO4E.BO; | ||
|
||
Console.WriteLine("Starting schema generation process..."); | ||
|
||
int offset; | ||
string outputDirectory; | ||
|
||
// Validate arguments | ||
if (args.Length < 2) | ||
{ | ||
Console.Error.WriteLine("Error: You must provide both an offset and an output directory."); | ||
Console.Error.WriteLine("Usage: dotnet run -- <offset> <outputDirectory>"); | ||
Environment.Exit(1); // Exit with a specific error code for missing arguments | ||
} | ||
|
||
if (!int.TryParse(args[0], out offset)) | ||
{ | ||
Console.Error.WriteLine($"Error: Invalid argument '{args[0]}'. Please provide a valid integer offset."); | ||
Environment.Exit(2); // Exit with a specific error code for invalid offset | ||
} | ||
|
||
// Get the output directory from the arguments | ||
outputDirectory = args[1]; | ||
|
||
Console.WriteLine($"Offset provided: {offset}"); | ||
Console.WriteLine($"Output directory: {outputDirectory}"); | ||
|
||
// Generate schemas | ||
try | ||
{ | ||
JsonSchemaGenerator.GenerateSchemas(offset, outputDirectory); | ||
Console.WriteLine("Schema generation completed successfully."); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.Error.WriteLine($"An unexpected error occurred: {ex.Message}"); | ||
Environment.Exit(4); // Exit with a specific error code for unexpected errors | ||
} | ||
|
||
Environment.Exit(0); // Success | ||
|
||
/// <summary> | ||
/// Generates plain JSON 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) | ||
{ | ||
var relevantBusinessObjectTypes = typeof(BusinessObject).Assembly | ||
.GetTypes() | ||
.Where(t => t.IsSubclassOf(typeof(BusinessObject))); | ||
|
||
if (relevantBusinessObjectTypes.Count() > LastDataRowOffset + MaxSchemasPerHour) | ||
{ | ||
throw new InvalidOperationException("Too many BusinessObject types. Increase the LastDataRowOffset or adjust the MaxSchemasPerHour."); | ||
} | ||
|
||
try | ||
{ | ||
// Ensure the output directory exists | ||
if (!Directory.Exists(outputDirectory)) | ||
{ | ||
Directory.CreateDirectory(outputDirectory); | ||
} | ||
|
||
foreach (var type in relevantBusinessObjectTypes.Skip(offset).Take(MaxSchemasPerHour)) | ||
{ | ||
var schema = BusinessObject.GetJsonSchema(type); | ||
var path = Path.Combine(outputDirectory, $"{type.Name}.json"); | ||
|
||
Console.WriteLine($"Generating schema for {type.Name} at {path}."); | ||
|
||
if (!File.Exists(path)) | ||
{ | ||
using (File.Create(path)) | ||
{ | ||
} | ||
} | ||
|
||
var utf8WithoutByteOrderMark = new UTF8Encoding(false); | ||
File.WriteAllText(path, schema.ToString(SchemaVersion.Draft7), utf8WithoutByteOrderMark); | ||
} | ||
} | ||
catch (JSchemaException jse) | ||
{ | ||
Console.Error.WriteLine($"Schema generation failed with error: {jse.Message}"); | ||
Environment.Exit(3); // Exit with a specific error code for schema generation failure | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\BO4E\BO4E.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# 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 | ||
|
||
# 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 | ||
|
||
# Check if an output directory argument is provided | ||
if [ $# -eq 1 ]; then | ||
output_directory=$1 | ||
else | ||
output_directory=$default_output_directory | ||
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 | ||
done |
Oops, something went wrong.