Add pubspec.yaml

This commit is contained in:
Fabian Stamm 2022-07-21 09:09:10 +02:00
parent 68af1ff442
commit 276fd12894
6 changed files with 87 additions and 34 deletions

3
.gitignore vendored
View File

@ -12,3 +12,6 @@ examples/Dart/out
templates/CSharp/bin templates/CSharp/bin
templates/CSharp/obj templates/CSharp/obj
lib/ lib/
templates/Dart/.dart_tool
templates/Dart/.packages
templates/Dart/pubspec.lock

View File

@ -1,14 +1,12 @@
import "./out/Test.dart"; import "./out/lib/example.dart";
import "./out/TestAtom.dart";
import "./out/TestEnum.dart";
import "dart:convert"; import "dart:convert";
int main() { int main() {
var t = new TestAtom(10, false, "hello"); var t = TestAtom(val_boolean: false, val_number: 1, val_string: "hi");
print(jsonEncode(t)); print(jsonEncode(t));
var t2 = TestEnum.VAL2; var t2 = TestEnum.VAL2;
print(jsonEncode(t2)); print(jsonEncode(t2));
return 0; return 0;
} }

View File

@ -2,6 +2,7 @@ import "./import";
define csharp_namespace Example; define csharp_namespace Example;
define rust_crate example; define rust_crate example;
define dart_library_name example;
enum TestEnum { enum TestEnum {
VAL1, VAL1,

View File

@ -17,13 +17,17 @@ function toDartType(type: string): string {
return (conversion as any)[type] || type; return (conversion as any)[type] || type;
} }
export class DartTarget extends CompileTarget<{}> { export class DartTarget extends CompileTarget<{ dart_library_name: string }> {
name: string = "dart"; name: string = "dart";
start(): void { start(): void {
if (this.options.allow_bytes == true) { if (this.options.allow_bytes == true) {
throw new Error("Dart has no support for 'bytes' yet!"); 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) { getImport(name: string) {
@ -55,13 +59,13 @@ export class DartTarget extends CompileTarget<{}> {
} }
a(0, ``); 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(0, ``);
a(1, `${definition.name}.fromJson(Map<String, dynamic> json) {`); a(1, `${definition.name}.fromJson(Map<String, dynamic> json) {`);
for (const field of definition.fields) { for (const field of definition.fields) {
a(2, `if(json.containsKey("${field.name}")) {`); a(2, `if(json.containsKey("${field.name}")) {`);
const parseField = (value: string)=>{ const parseField = (value: string) => {
if (conversion[field.type]) { if (conversion[field.type]) {
return value; return value;
} else { } else {
@ -69,12 +73,12 @@ export class DartTarget extends CompileTarget<{}> {
} }
} }
if(field.array) { if (field.array) {
a(3, `this.${field.name} = [];`); a(3, `this.${field.name} = [];`);
a(3, `(json["${field.name}"] as List<dynamic>).forEach((e) => {`) a(3, `(json["${field.name}"] as List<dynamic>).forEach((e) => {`)
a(4, `this.${field.name}!.add(${parseField("e")})`); a(4, `this.${field.name}!.add(${parseField("e")})`);
a(3, `});`) a(3, `});`)
} else if(field.map) { } else if (field.map) {
a(3, `this.${field.name} = {};`); a(3, `this.${field.name} = {};`);
a(3, `(json["${field.name}"] as Map<${toDartType(field.map)},dynamic>).forEach((key, value) => {`) a(3, `(json["${field.name}"] as Map<${toDartType(field.map)},dynamic>).forEach((key, value) => {`)
a(4, `this.${field.name}![key] = ${parseField("value")}`); a(4, `this.${field.name}![key] = ${parseField("value")}`);
@ -91,7 +95,7 @@ export class DartTarget extends CompileTarget<{}> {
a(1, `}`); a(1, `}`);
a(0, `}`); a(0, `}`);
this.writeFile(`${definition.name}.dart`, getResult()); this.writeFile(`lib/src/${definition.name}.dart`, getResult());
} }
generateEnum(definition: EnumDefinition): void { generateEnum(definition: EnumDefinition): void {
@ -114,6 +118,7 @@ export class DartTarget extends CompileTarget<{}> {
} }
a(3, `default:`); a(3, `default:`);
a(4, `return null;`); a(4, `return null;`);
a(2, `}`);
a(1, `}`); a(1, `}`);
a(0, `}`); a(0, `}`);
@ -132,7 +137,7 @@ export class DartTarget extends CompileTarget<{}> {
// a(1, `}`); // a(1, `}`);
// a(0, `}`); // a(0, `}`);
this.writeFile(`${definition.name}.dart`, getResult()); this.writeFile(`lib/src/${definition.name}.dart`, getResult());
} }
generateService(definition: ServiceDefinition): void { generateService(definition: ServiceDefinition): void {
@ -145,24 +150,30 @@ export class DartTarget extends CompileTarget<{}> {
finalize(steps: Step[]): void { finalize(steps: Step[]): void {
const { a, getResult } = LineAppender(); const { a, getResult } = LineAppender();
// steps.forEach(([type, def]) => { a(0, `library ${this.options.dart_library_name};`)
// switch (type) { a(0, ``);
// 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":
// }
// });
// 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));
} }
} }

View File

@ -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

View File

@ -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