SchemaProcessing.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SharpGLTF.CodeGen;
  6. using SharpGLTF.SchemaReflection;
  7. using System.Threading.Tasks;
  8. using NJsonSchema.References;
  9. using JSONSCHEMA = NJsonSchema.JsonSchema;
  10. namespace SharpGLTF
  11. {
  12. public static class SchemaProcessing
  13. {
  14. #region schema loader
  15. public static SchemaType.Context LoadExtensionSchemaContext(string srcSchema)
  16. {
  17. var context = LoadSchemaContext(srcSchema);
  18. context.IgnoredByCodeEmittierMainSchema();
  19. return context;
  20. }
  21. public static SchemaType.Context LoadSchemaContext(string srcSchema)
  22. {
  23. var schema = LoadSchema(srcSchema);
  24. var settings = new NJsonSchema.CodeGeneration.CSharp.CSharpGeneratorSettings
  25. {
  26. Namespace = "glTf.POCO",
  27. ClassStyle = NJsonSchema.CodeGeneration.CSharp.CSharpClassStyle.Poco
  28. };
  29. var ctypes = new NJsonSchema.CodeGeneration.CSharp.CSharpTypeResolver(settings);
  30. ctypes.Resolve(schema, false, null);
  31. return SchemaTypesReader.Generate(ctypes);
  32. }
  33. private static JSONSCHEMA LoadSchema(string filePath)
  34. {
  35. // https://blogs.msdn.microsoft.com/benjaminperkins/2017/03/08/how-to-call-an-async-method-from-a-console-app-main-method/
  36. if (!System.IO.File.Exists(filePath)) throw new System.IO.FileNotFoundException(nameof(filePath), filePath);
  37. return JSONSCHEMA
  38. .FromFileAsync(filePath, s => _Resolver(s, filePath))
  39. .ConfigureAwait(false)
  40. .GetAwaiter()
  41. .GetResult();
  42. }
  43. static NJsonSchema.JsonReferenceResolver _Resolver(JSONSCHEMA schema, string basePath)
  44. {
  45. var generator = new NJsonSchema.NewtonsoftJson.Generation.NewtonsoftJsonSchemaGeneratorSettings();
  46. var solver = new NJsonSchema.JsonSchemaAppender(schema, generator.TypeNameGenerator);
  47. return new MyReferenceResolver(solver);
  48. }
  49. class MyReferenceResolver : NJsonSchema.JsonReferenceResolver
  50. {
  51. public MyReferenceResolver(NJsonSchema.JsonSchemaAppender resolver) : base(resolver) { }
  52. public override Task<IJsonReference> ResolveFileReferenceAsync(string filePath, System.Threading.CancellationToken cancellationToken)
  53. {
  54. if (System.IO.File.Exists(filePath)) return base.ResolveFileReferenceAsync(filePath, cancellationToken);
  55. filePath = System.IO.Path.GetFileName(filePath);
  56. filePath = System.IO.Path.Combine(Constants.MainSchemaDir, filePath);
  57. if (System.IO.File.Exists(filePath)) return base.ResolveFileReferenceAsync(filePath, cancellationToken);
  58. throw new System.IO.FileNotFoundException(filePath);
  59. }
  60. }
  61. #endregion
  62. public static void EmitCodeFromSchema(string projectPath, string dstFile, SchemaType.Context ctx, IReadOnlyList<SchemaProcessor> extensions)
  63. {
  64. var newEmitter = new CSharpEmitter();
  65. newEmitter.DeclareContext(ctx);
  66. foreach(var ext in extensions)
  67. {
  68. ext.PrepareTypes(newEmitter, ctx);
  69. }
  70. var textOut = newEmitter.EmitContext(ctx);
  71. var dstDir = _FindTargetDirectory(projectPath);
  72. var dstPath = System.IO.Path.Combine(dstDir, $"{dstFile}.cs");
  73. System.IO.File.WriteAllText(dstPath, textOut);
  74. }
  75. private static string _FindTargetDirectory(string dstDir)
  76. {
  77. var dir = Constants.LocalRepoDirectory;
  78. while (dir.Length > 3)
  79. {
  80. var xdir = System.IO.Path.Combine(dir, dstDir);
  81. if (System.IO.Directory.Exists(xdir)) return xdir;
  82. dir = System.IO.Path.GetDirectoryName(dir); // move up
  83. }
  84. return null;
  85. }
  86. }
  87. }