Browse Source

GeneratorKind: add CLIOptions property + refactor hardcoded options

pull/1794/head
Deadlocklogic 2 years ago
parent
commit
765ebf6563
  1. 30
      src/CLI/CLI.cs
  2. 36
      src/Generator/GeneratorKind.cs

30
src/CLI/CLI.cs

@ -206,33 +206,13 @@ namespace CppSharp @@ -206,33 +206,13 @@ namespace CppSharp
static void GetGeneratorKind(string generator, List<string> errorMessages)
{
switch (generator.ToLower())
foreach (GeneratorKind generatorKind in GeneratorKind.Registered)
{
case "csharp":
options.Kind = GeneratorKind.CSharp;
return;
case "cli":
options.Kind = GeneratorKind.CLI;
return;
case "c":
options.Kind = GeneratorKind.C;
return;
case "cpp":
options.Kind = GeneratorKind.CPlusPlus;
return;
case "napi":
options.Kind = GeneratorKind.NAPI;
return;
case "qjs":
options.Kind = GeneratorKind.QuickJS;
return;
case "ts":
case "typescript":
options.Kind = GeneratorKind.TypeScript;
return;
case "emscripten":
options.Kind = GeneratorKind.Emscripten;
if (generatorKind.IsCLIOptionMatch(generator))
{
options.Kind = generatorKind;
return;
}
}
errorMessages.Add($"Unknown generator kind: {generator}.");

36
src/Generator/GeneratorKind.cs

@ -7,6 +7,7 @@ using CppSharp.Generators.Emscripten; @@ -7,6 +7,7 @@ using CppSharp.Generators.Emscripten;
using CppSharp.Generators.TS;
using System;
using System.Collections.Generic;
using System.Linq;
namespace CppSharp.Generators
{
@ -15,22 +16,24 @@ namespace CppSharp.Generators @@ -15,22 +16,24 @@ namespace CppSharp.Generators
/// </summary>
public class GeneratorKind : IEquatable<GeneratorKind>
{
private static readonly HashSet<string> s_registeredIDSet = new();
public static readonly HashSet<GeneratorKind> Registered = new();
public string ID { get; }
public string Name { get; }
public System.Type Type { get; }
public string[] CLIOptions { get; }
public GeneratorKind(string id, string name, System.Type type)
public GeneratorKind(string id, string name, System.Type type, string[] cLIOptions = null)
{
if (s_registeredIDSet.Contains(id))
if (Registered.Any(kind => kind.ID == id))
{
throw new Exception($"GeneratorKind has an already registered ID: {ID}");
}
s_registeredIDSet.Add(id);
ID = id;
Name = name;
Type = type;
CLIOptions = cLIOptions;
Registered.Add(this);
}
public Generator CreateGenerator(BindingContext context)
@ -38,6 +41,15 @@ namespace CppSharp.Generators @@ -38,6 +41,15 @@ namespace CppSharp.Generators
return (Generator)Activator.CreateInstance(Type, context);
}
public bool IsCLIOptionMatch(string cliOption)
{
if (CLIOptions == null)
{
return false;
}
return CLIOptions.Any(cliOption.Contains);
}
public static bool operator ==(GeneratorKind obj1, GeneratorKind obj2)
{
if (ReferenceEquals(obj1, obj2))
@ -81,19 +93,19 @@ namespace CppSharp.Generators @@ -81,19 +93,19 @@ namespace CppSharp.Generators
}
public const string CLI_ID = "CLI";
public static readonly GeneratorKind CLI = new(CLI_ID, "C++/CLI", typeof(CLIGenerator));
public static readonly GeneratorKind CLI = new(CLI_ID, "C++/CLI", typeof(CLIGenerator), new[] { "cli" });
public const string CSharp_ID = "CSharp";
public static readonly GeneratorKind CSharp = new(CSharp_ID, "C#", typeof(CSharpGenerator));
public static readonly GeneratorKind CSharp = new(CSharp_ID, "C#", typeof(CSharpGenerator), new[] { "csharp" });
public const string C_ID = "C";
public static readonly GeneratorKind C = new(C_ID, "C", typeof(CGenerator));
public static readonly GeneratorKind C = new(C_ID, "C", typeof(CGenerator), new[] { "c" });
public const string CPlusPlus_ID = "CPlusPlus";
public static readonly GeneratorKind CPlusPlus = new(CPlusPlus_ID, "CPlusPlus", typeof(CppGenerator));
public static readonly GeneratorKind CPlusPlus = new(CPlusPlus_ID, "CPlusPlus", typeof(CppGenerator), new[] { "cpp" });
public const string Emscripten_ID = "Emscripten";
public static readonly GeneratorKind Emscripten = new(Emscripten_ID, "Emscripten", typeof(EmscriptenGenerator));
public static readonly GeneratorKind Emscripten = new(Emscripten_ID, "Emscripten", typeof(EmscriptenGenerator), new[] { "emscripten" });
public const string ObjectiveC_ID = "ObjectiveC";
public static readonly GeneratorKind ObjectiveC = new(ObjectiveC_ID, "ObjectiveC", typeof(NotImplementedGenerator));
@ -105,13 +117,13 @@ namespace CppSharp.Generators @@ -105,13 +117,13 @@ namespace CppSharp.Generators
public static readonly GeneratorKind Swift = new(Swift_ID, "Swift", typeof(NotImplementedGenerator));
public const string QuickJS_ID = "QuickJS";
public static readonly GeneratorKind QuickJS = new(QuickJS_ID, "QuickJS", typeof(QuickJSGenerator));
public static readonly GeneratorKind QuickJS = new(QuickJS_ID, "QuickJS", typeof(QuickJSGenerator), new[] { "qjs" });
public const string NAPI_ID = "NAPI";
public static readonly GeneratorKind NAPI = new(NAPI_ID, "N-API", typeof(NAPIGenerator));
public static readonly GeneratorKind NAPI = new(NAPI_ID, "N-API", typeof(NAPIGenerator), new[] { "napi" });
public const string TypeScript_ID = "TypeScript";
public static readonly GeneratorKind TypeScript = new(TypeScript_ID, "TypeScript", typeof(TSGenerator));
public static readonly GeneratorKind TypeScript = new(TypeScript_ID, "TypeScript", typeof(TSGenerator), new[] { "ts", "typescript" });
}
public class NotImplementedGenerator : Generator

Loading…
Cancel
Save