mirror of
https://github.com/esiur/esiur-dart.git
synced 2025-05-06 20:02:59 +00:00
Support getx in template generator
This commit is contained in:
parent
eed16df29a
commit
48f635f047
@ -23,7 +23,8 @@ void main(List<String> arguments) async {
|
|||||||
final parser = ArgParser()
|
final parser = ArgParser()
|
||||||
..addOption('username', abbr: 'u')
|
..addOption('username', abbr: 'u')
|
||||||
..addOption('password', abbr: 'p')
|
..addOption('password', abbr: 'p')
|
||||||
..addOption('dir', abbr: 'd');
|
..addOption('dir', abbr: 'd')
|
||||||
|
..addFlag("getx", abbr: 'x');
|
||||||
|
|
||||||
var results = parser.parse(arguments.skip(2));
|
var results = parser.parse(arguments.skip(2));
|
||||||
|
|
||||||
@ -34,8 +35,11 @@ void main(List<String> arguments) async {
|
|||||||
//print("Username ${username} password ${password} dir ${dir}");
|
//print("Username ${username} password ${password} dir ${dir}");
|
||||||
|
|
||||||
// make template
|
// make template
|
||||||
var destDir =
|
var destDir = await TemplateGenerator.getTemplate(link,
|
||||||
await TemplateGenerator.getTemplate(link, dir, username, password);
|
dir: dir,
|
||||||
|
username: username,
|
||||||
|
password: password,
|
||||||
|
getx: results['getx']);
|
||||||
|
|
||||||
print("Generated directory `${destDir}`");
|
print("Generated directory `${destDir}`");
|
||||||
|
|
||||||
|
@ -209,9 +209,10 @@ class TemplateGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Future<String> getTemplate(String url,
|
static Future<String> getTemplate(String url,
|
||||||
[String? dir = null,
|
{String? dir,
|
||||||
String? username = null,
|
String? username,
|
||||||
String? password = null]) async {
|
String? password,
|
||||||
|
bool getx = false}) async {
|
||||||
try {
|
try {
|
||||||
if (!_urlRegex.hasMatch(url)) throw Exception("Invalid IIP URL");
|
if (!_urlRegex.hasMatch(url)) throw Exception("Invalid IIP URL");
|
||||||
|
|
||||||
@ -233,7 +234,7 @@ class TemplateGenerator {
|
|||||||
|
|
||||||
var dstDir = Directory("lib/$dir");
|
var dstDir = Directory("lib/$dir");
|
||||||
|
|
||||||
if (!dstDir.existsSync()) dstDir.createSync();
|
if (!dstDir.existsSync()) dstDir.createSync(recursive: true);
|
||||||
|
|
||||||
//Map<String, String> namesMap = Map<String, String>();
|
//Map<String, String> namesMap = Map<String, String>();
|
||||||
|
|
||||||
@ -241,6 +242,9 @@ class TemplateGenerator {
|
|||||||
var imports = StringBuffer();
|
var imports = StringBuffer();
|
||||||
imports.writeln("import 'dart:async';");
|
imports.writeln("import 'dart:async';");
|
||||||
imports.writeln("import 'package:esiur/esiur.dart';");
|
imports.writeln("import 'package:esiur/esiur.dart';");
|
||||||
|
if (getx) {
|
||||||
|
imports.writeln("import 'package:get/get.dart';");
|
||||||
|
}
|
||||||
// make import names
|
// make import names
|
||||||
templates.forEach((tmp) {
|
templates.forEach((tmp) {
|
||||||
if (tmp != skipTemplate) {
|
if (tmp != skipTemplate) {
|
||||||
@ -257,16 +261,16 @@ class TemplateGenerator {
|
|||||||
// make sources
|
// make sources
|
||||||
templates.forEach((tmp) {
|
templates.forEach((tmp) {
|
||||||
print("Generating `${tmp.className}`.");
|
print("Generating `${tmp.className}`.");
|
||||||
|
final filePath = "${dstDir.path}/${tmp.className}.g.dart";
|
||||||
|
final f = File(filePath);
|
||||||
|
|
||||||
|
var source = "";
|
||||||
if (tmp.type == TemplateType.Resource) {
|
if (tmp.type == TemplateType.Resource) {
|
||||||
var source = makeImports(tmp) + generateClass(tmp, templates);
|
source = makeImports(tmp) + generateClass(tmp, templates, getx: getx);
|
||||||
var f = File("${dstDir.path}/${tmp.className}.g.dart");
|
|
||||||
f.writeAsStringSync(source);
|
|
||||||
} else if (tmp.type == TemplateType.Record) {
|
} else if (tmp.type == TemplateType.Record) {
|
||||||
var source = makeImports(tmp) + generateRecord(tmp, templates);
|
source = makeImports(tmp) + generateRecord(tmp, templates);
|
||||||
var f = File("${dstDir.path}/${tmp.className}.g.dart");
|
|
||||||
f.writeAsStringSync(source);
|
|
||||||
}
|
}
|
||||||
|
f.writeAsStringSync(source);
|
||||||
});
|
});
|
||||||
|
|
||||||
// generate info class
|
// generate info class
|
||||||
@ -288,6 +292,8 @@ class TemplateGenerator {
|
|||||||
var f = File("${dstDir.path}/init.g.dart");
|
var f = File("${dstDir.path}/init.g.dart");
|
||||||
f.writeAsStringSync(typesFile);
|
f.writeAsStringSync(typesFile);
|
||||||
|
|
||||||
|
Process.run("dart", ["format", dstDir.path]);
|
||||||
|
|
||||||
return dstDir.path;
|
return dstDir.path;
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
throw ex;
|
throw ex;
|
||||||
@ -302,7 +308,8 @@ class TemplateGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static String generateClass(
|
static String generateClass(
|
||||||
TypeTemplate template, List<TypeTemplate> templates) {
|
TypeTemplate template, List<TypeTemplate> templates,
|
||||||
|
{bool getx = false}) {
|
||||||
var className = template.className.split('.').last;
|
var className = template.className.split('.').last;
|
||||||
|
|
||||||
var rt = StringBuffer();
|
var rt = StringBuffer();
|
||||||
@ -316,8 +323,25 @@ class TemplateGenerator {
|
|||||||
rt.writeln("on('${e.name}', (x) => _${e.name}Controller.add(x));");
|
rt.writeln("on('${e.name}', (x) => _${e.name}Controller.add(x));");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (getx) {
|
||||||
|
rt.writeln("ob = obs;");
|
||||||
|
rt.writeln("_sub = properyModified.listen((_) => ob.trigger(this));");
|
||||||
|
}
|
||||||
|
|
||||||
rt.writeln("}");
|
rt.writeln("}");
|
||||||
|
|
||||||
|
if (getx) {
|
||||||
|
rt.writeln("\nlate final Rx<$className> ob;");
|
||||||
|
rt.writeln("late final StreamSubscription? _sub;\n");
|
||||||
|
|
||||||
|
rt.writeln("""@override
|
||||||
|
void destroy() {
|
||||||
|
_sub?.cancel();
|
||||||
|
|
||||||
|
super.destroy();
|
||||||
|
}""");
|
||||||
|
}
|
||||||
|
|
||||||
template.functions.forEach((f) {
|
template.functions.forEach((f) {
|
||||||
var rtTypeName = getTypeName(template, f.returnType, templates, true);
|
var rtTypeName = getTypeName(template, f.returnType, templates, true);
|
||||||
rt.write("AsyncReply<$rtTypeName> ${f.name}(");
|
rt.write("AsyncReply<$rtTypeName> ${f.name}(");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user