From 276fd12894ccce8a4c192a39b45f5184910d2afa Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Thu, 21 Jul 2022 09:09:10 +0200 Subject: [PATCH] Add pubspec.yaml --- .gitignore | 3 ++ examples/Dart/Test.dart | 14 +++---- examples/example.jrpc | 1 + src/targets/dart.ts | 63 ++++++++++++++++------------ templates/Dart/analysis_options.yaml | 30 +++++++++++++ templates/Dart/pubspec.yaml | 10 +++++ 6 files changed, 87 insertions(+), 34 deletions(-) create mode 100644 templates/Dart/analysis_options.yaml create mode 100644 templates/Dart/pubspec.yaml diff --git a/.gitignore b/.gitignore index e08447c..9161167 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,6 @@ examples/Dart/out templates/CSharp/bin templates/CSharp/obj lib/ +templates/Dart/.dart_tool +templates/Dart/.packages +templates/Dart/pubspec.lock \ No newline at end of file diff --git a/examples/Dart/Test.dart b/examples/Dart/Test.dart index 8029caf..df12525 100644 --- a/examples/Dart/Test.dart +++ b/examples/Dart/Test.dart @@ -1,14 +1,12 @@ -import "./out/Test.dart"; -import "./out/TestAtom.dart"; -import "./out/TestEnum.dart"; +import "./out/lib/example.dart"; import "dart:convert"; int main() { - var t = new TestAtom(10, false, "hello"); - print(jsonEncode(t)); + var t = TestAtom(val_boolean: false, val_number: 1, val_string: "hi"); + print(jsonEncode(t)); - var t2 = TestEnum.VAL2; - print(jsonEncode(t2)); + var t2 = TestEnum.VAL2; + print(jsonEncode(t2)); - return 0; + return 0; } diff --git a/examples/example.jrpc b/examples/example.jrpc index f555267..8b259a0 100644 --- a/examples/example.jrpc +++ b/examples/example.jrpc @@ -2,6 +2,7 @@ import "./import"; define csharp_namespace Example; define rust_crate example; +define dart_library_name example; enum TestEnum { VAL1, diff --git a/src/targets/dart.ts b/src/targets/dart.ts index 5b85263..7af7b8f 100644 --- a/src/targets/dart.ts +++ b/src/targets/dart.ts @@ -17,13 +17,17 @@ function toDartType(type: string): string { return (conversion as any)[type] || type; } -export class DartTarget extends CompileTarget<{}> { +export class DartTarget extends CompileTarget<{ dart_library_name: string }> { name: string = "dart"; start(): void { if (this.options.allow_bytes == true) { throw new Error("Dart has no support for 'bytes' yet!"); } + + if (!this.options.dart_library_name) { + throw new Error("Setting dart_library_name is required for DART target!"); + } } getImport(name: string) { @@ -55,13 +59,13 @@ export class DartTarget extends CompileTarget<{}> { } a(0, ``); - a(1, `${definition.name}(${definition.fields.map(e => `this.${e.name}`).join(",")});`); + a(1, `${definition.name}({${definition.fields.map(e => `this.${e.name}`).join(",")}});`); a(0, ``); a(1, `${definition.name}.fromJson(Map json) {`); for (const field of definition.fields) { a(2, `if(json.containsKey("${field.name}")) {`); - - const parseField = (value: string)=>{ + + const parseField = (value: string) => { if (conversion[field.type]) { return value; } else { @@ -69,12 +73,12 @@ export class DartTarget extends CompileTarget<{}> { } } - if(field.array) { + if (field.array) { a(3, `this.${field.name} = [];`); a(3, `(json["${field.name}"] as List).forEach((e) => {`) a(4, `this.${field.name}!.add(${parseField("e")})`); a(3, `});`) - } else if(field.map) { + } else if (field.map) { a(3, `this.${field.name} = {};`); a(3, `(json["${field.name}"] as Map<${toDartType(field.map)},dynamic>).forEach((key, value) => {`) a(4, `this.${field.name}![key] = ${parseField("value")}`); @@ -91,7 +95,7 @@ export class DartTarget extends CompileTarget<{}> { a(1, `}`); a(0, `}`); - this.writeFile(`${definition.name}.dart`, getResult()); + this.writeFile(`lib/src/${definition.name}.dart`, getResult()); } generateEnum(definition: EnumDefinition): void { @@ -114,6 +118,7 @@ export class DartTarget extends CompileTarget<{}> { } a(3, `default:`); a(4, `return null;`); + a(2, `}`); a(1, `}`); a(0, `}`); @@ -132,7 +137,7 @@ export class DartTarget extends CompileTarget<{}> { // a(1, `}`); // a(0, `}`); - this.writeFile(`${definition.name}.dart`, getResult()); + this.writeFile(`lib/src/${definition.name}.dart`, getResult()); } generateService(definition: ServiceDefinition): void { @@ -145,24 +150,30 @@ export class DartTarget extends CompileTarget<{}> { finalize(steps: Step[]): void { const { a, getResult } = LineAppender(); - // steps.forEach(([type, def]) => { - // switch (type) { - // case "type": - // a(0, `pub ${this.getImport(def.name)}`); - // break; - // case "enum": - // a(0, `pub ${this.getImport(def.name)}`); - // break; - // default: - // console.warn( - // chalk.yellow("[DART] WARNING:"), - // "unimplemented step found:", - // type - // ); - // // case "service": - // } - // }); + a(0, `library ${this.options.dart_library_name};`) + a(0, ``); - // this.writeFile(`mod.dart`, getResult()); + + steps.forEach(([type, def]) => { + switch (type) { + case "type": + a(0, `export 'src/${def.name}.dart';`); + break; + case "enum": + a(0, `export 'src/${def.name}.dart';`); + break; + default: + console.warn( + chalk.yellow("[DART] WARNING:"), + "unimplemented step found:", + type + ); + // case "service": + } + }); + + this.writeFile(`lib/${this.options.dart_library_name}.dart`, getResult()); + + this.writeFile(`pubspec.yaml`, this.getTemplate("Dart/pubspec.yaml").replace("__NAME__", this.options.dart_library_name)); } } diff --git a/templates/Dart/analysis_options.yaml b/templates/Dart/analysis_options.yaml new file mode 100644 index 0000000..55c7822 --- /dev/null +++ b/templates/Dart/analysis_options.yaml @@ -0,0 +1,30 @@ +# This file configures the static analysis results for your project (errors, +# warnings, and lints). +# +# This enables the 'recommended' set of lints from `package:lints`. +# This set helps identify many issues that may lead to problems when running +# or consuming Dart code, and enforces writing Dart using a single, idiomatic +# style and format. +# +# If you want a smaller set of lints you can change this to specify +# 'package:lints/core.yaml'. These are just the most critical lints +# (the recommended set includes the core lints). +# The core lints are also what is used by pub.dev for scoring packages. + +include: package:lints/recommended.yaml + +# Uncomment the following section to specify additional rules. + +# linter: +# rules: +# - camel_case_types + +# analyzer: +# exclude: +# - path/to/excluded/files/** + +# For more information about the core and recommended set of lints, see +# https://dart.dev/go/core-lints + +# For additional information about configuring this file, see +# https://dart.dev/guides/language/analysis-options \ No newline at end of file diff --git a/templates/Dart/pubspec.yaml b/templates/Dart/pubspec.yaml new file mode 100644 index 0000000..9ecbfee --- /dev/null +++ b/templates/Dart/pubspec.yaml @@ -0,0 +1,10 @@ +name: __NAME__ +description: JRPC +version: 1.0.0 + +environment: + sdk: '>=2.17.6 <3.0.0' + +dependencies: +dev_dependencies: + lints: ^2.0.0