2
0
mirror of https://github.com/esiur/esiur-dart.git synced 2025-06-27 14:53:11 +00:00
This commit is contained in:
2021-07-21 12:18:01 +03:00
parent 737397da11
commit 6be04c39ed
27 changed files with 659 additions and 513 deletions

View File

@ -10,19 +10,19 @@ import '../Resource/Template/TemplateDataType.dart';
import '../Resource/Template/TypeTemplate.dart';
class TemplateGenerator {
// static RegExp urlRegex = new RegExp("^(?:([\S]*)://([^/]*)/?)");
// static RegExp urlRegex = RegExp("^(?:([\S]*)://([^/]*)/?)");
static final _urlRegex = RegExp(r'^(?:([^\s|:]*):\/\/([^\/]*)\/?(.*))');
static String generateRecord(
TypeTemplate template, List<TypeTemplate> templates) {
var className = template.className.split('.').last;
var rt = new StringBuffer();
var rt = StringBuffer();
rt.writeln("class ${className} extends IRecord {");
template.properties.forEach((p) {
var ptTypeName = getTypeName(template, p.valueType, templates);
rt.writeln("${ptTypeName} ${p.name};");
rt.writeln("${ptTypeName}? ${p.name};");
rt.writeln();
});
@ -48,40 +48,78 @@ class TemplateGenerator {
rt.writeln("return rt;");
rt.writeln("}");
// add template
var descProps = template.properties.map((p) {
var isArray = p.valueType.type & 0x80 == 0x80;
var ptType = p.valueType.type & 0x7F;
var ptTypeName = getTypeName(
template, TemplateDataType(ptType, p.valueType.typeGuid), templates);
// return "Prop(\"${p.name}\", ${ptTypeName}, ${isArray})";
return "Prop('${p.name}', ${ptTypeName}, ${isArray}, ${_escape(p.readExpansion)}, ${_escape(p.writeExpansion)})";
}).join(', ');
rt.writeln("""@override
TemplateDescriber get template => TemplateDescriber('${template.className}', properties: [${descProps}]);""");
rt.writeln("\r\n}");
return rt.toString();
}
static String _translateClassName(String className) {
var cls = className.split('.');
var nameSpace = cls.take(cls.length - 1).join('_').toLowerCase();
return "$nameSpace.${cls.last}";
}
static String getTypeName(TypeTemplate forTemplate,
TemplateDataType templateDataType, List<TypeTemplate> templates) {
if (templateDataType.type == DataType.Resource) {
if (templateDataType.typeGuid == forTemplate.classId)
return forTemplate.className.split('.').last;
else {
var tmp =
templates.firstWhere((x) => x.classId == templateDataType.typeGuid);
var tmp = templates.firstWhere((x) =>
x.classId == templateDataType.typeGuid &&
(x.type == TemplateType.Resource ||
x.type == TemplateType.Wrapper));
if (tmp == null) return "dynamic"; // something went wrong
var cls = tmp.className.split('.');
var nameSpace = cls.take(cls.length - 1).join('_');
return "$nameSpace.${cls.last}";
return _translateClassName(tmp.className);
}
} else if (templateDataType.type == DataType.ResourceArray) {
if (templateDataType.typeGuid == forTemplate.classId)
return "List<${forTemplate.className.split('.').last}>";
else {
var tmp =
templates.firstWhere((x) => x.classId == templateDataType.typeGuid);
var tmp = templates.firstWhere((x) =>
x.classId == templateDataType.typeGuid &&
(x.type == TemplateType.Resource ||
x.type == TemplateType.Wrapper));
if (tmp == null) return "dynamic"; // something went wrong
var cls = tmp.className.split('.');
var nameSpace = cls.take(cls.length - 1).join('_');
return "List<$nameSpace.${cls.last}>";
return "List<${_translateClassName(tmp.className)}>";
}
} else if (templateDataType.type == DataType.Record) {
if (templateDataType.typeGuid == forTemplate.classId)
return forTemplate.className.split('.').last;
else {
var tmp = templates.firstWhere((x) =>
x.classId == templateDataType.typeGuid &&
x.type == TemplateType.Record);
if (tmp == null) return "dynamic"; // something went wrong
return _translateClassName(tmp.className);
}
} else if (templateDataType.type == DataType.RecordArray) {
if (templateDataType.typeGuid == forTemplate.classId)
return "List<${forTemplate.className.split('.').last}>";
else {
var tmp = templates.firstWhere((x) =>
x.classId == templateDataType.typeGuid &&
x.type == TemplateType.Record);
if (tmp == null) return "dynamic"; // something went wrong
return "List<${_translateClassName(tmp.className)}>";
}
}
@ -104,7 +142,7 @@ class TemplateGenerator {
case DataType.DecimalArray:
return "List<double>";
case DataType.Float32:
return "List<double>";
return "double";
case DataType.Float32Array:
return "List<double>";
case DataType.Float64:
@ -172,7 +210,7 @@ class TemplateGenerator {
String username = null,
String password = null]) async {
try {
if (!_urlRegex.hasMatch(url)) throw new Exception("Invalid IIP URL");
if (!_urlRegex.hasMatch(url)) throw Exception("Invalid IIP URL");
var path = _urlRegex.allMatches(url).first;
var con = await Warehouse.get<DistributedConnection>(
@ -181,12 +219,15 @@ class TemplateGenerator {
? {username: username, password: password}
: null);
if (con == null) throw new Exception("Can't connect to server");
if (con == null) throw Exception("Can't connect to server");
if (isNullOrEmpty(dir)) dir = path[2].replaceAll(":", "_");
var templates = await con.getLinkTemplates(path[3]);
// no longer needed
Warehouse.remove(con);
var dstDir = Directory("lib/$dir");
if (!dstDir.existsSync()) dstDir.createSync();
@ -201,9 +242,8 @@ class TemplateGenerator {
templates.forEach((tmp) {
if (tmp != skipTemplate) {
var cls = tmp.className.split('.');
var nameSpace = cls.take(cls.length - 1).join('_');
imports.writeln(
"import '${tmp.className}.Generated.dart' as $nameSpace;");
var nameSpace = cls.take(cls.length - 1).join('_').toLowerCase();
imports.writeln("import '${tmp.className}.g.dart' as $nameSpace;");
}
});
@ -213,51 +253,61 @@ class TemplateGenerator {
// make sources
templates.forEach((tmp) {
print("Generating `${tmp.className}`.");
if (tmp.type == TemplateType.Resource) {
var source = makeImports(tmp) + generateClass(tmp, templates);
var f = File("${dstDir.path}/${tmp.className}.Generated.dart");
var f = File("${dstDir.path}/${tmp.className}.g.dart");
f.writeAsStringSync(source);
} else if (tmp.type == TemplateType.Record) {
var source = makeImports(tmp) + generateRecord(tmp, templates);
var f = File("${dstDir.path}/${tmp.className}.Generated.dart");
var f = File("${dstDir.path}/${tmp.className}.g.dart");
f.writeAsStringSync(source);
}
});
// generate info class
var typesFile =
"using System; \r\n namespace Esiur { public static class Generated { public static Type[] Resources {get;} = new Type[] { " +
templates
.where((x) => x.type == TemplateType.Resource)
.map((x) => "typeof(${x.className})")
.join(',') +
" }; \r\n public static Type[] Records { get; } = new Type[] { " +
templates
.where((x) => x.type == TemplateType.Record)
.map((x) => "typeof(${x.className})")
.join(',') +
" }; " +
"\r\n } \r\n}";
var f = File("${dstDir.path}/Esiur.Generated.cs");
var defineCreators = templates.map((tmp) {
// creator
var className = _translateClassName(tmp.className);
return "Warehouse.defineCreator(${className}, () => ${className}(), () => <${className}>[]);";
}).join("\r\n");
var putTemplates = templates.map((tmp) {
var className = _translateClassName(tmp.className);
return "Warehouse.putTemplate(TypeTemplate.fromType(${className}));";
}).join("\r\n");
var typesFile = makeImports(null) +
"\r\n void init_${dir}(){ ${defineCreators} \r\n ${putTemplates}}";
var f = File("${dstDir.path}/init.g.dart");
f.writeAsStringSync(typesFile);
return dstDir.path;
} catch (ex) {
//File.WriteAllText("C:\\gen\\gettemplate.err", ex.ToString());
throw ex;
}
}
static String _escape(String str) {
if (str == null)
return "null";
else
return "r'$str'";
}
static String generateClass(
TypeTemplate template, List<TypeTemplate> templates) {
var className = template.className.split('.').last;
var rt = new StringBuffer();
var rt = StringBuffer();
rt.writeln("class $className extends DistributedResource {");
rt.writeln(
"$className(DistributedConnection connection, int instanceId, int age, String link) : super(connection, instanceId, age, link) {");
// "$className(DistributedConnection connection, int instanceId, int age, String link) : super(connection, instanceId, age, link) {");
"$className() {");
template.events.forEach((e) {
rt.writeln("on('${e.name}', (x) => _${e.name}Controller.add(x));");
@ -273,9 +323,9 @@ class TemplateGenerator {
.join(","));
rt.writeln(") {");
rt.writeln("var rt = new AsyncReply<$rtTypeName>();");
rt.writeln("var rt = AsyncReply<$rtTypeName>();");
rt.writeln(
"invokeByArrayArguments(${f.index}, [${f.arguments.map((x) => x.name).join(',')}])");
"internal_invokeByArrayArguments(${f.index}, [${f.arguments.map((x) => x.name).join(',')}])");
rt.writeln(".then<dynamic>((x) => rt.trigger(x))");
rt.writeln(".error((x) => rt.triggerError(x))");
rt.writeln(".chunk((x) => rt.triggerChunk(x));");
@ -299,6 +349,43 @@ class TemplateGenerator {
rt.writeln("}");
});
// add template
var descProps = template.properties.map((p) {
var isArray = p.valueType.type & 0x80 == 0x80;
var ptType = p.valueType.type & 0x7F;
var ptTypeName = getTypeName(
template, TemplateDataType(ptType, p.valueType.typeGuid), templates);
return "Prop('${p.name}', ${ptTypeName}, ${isArray}, ${_escape(p.readExpansion)}, ${_escape(p.writeExpansion)})";
}).join(', ');
var descFuncs = template.functions.map((f) {
var isArray = f.returnType.type & 0x80 == 0x80;
var ftType = f.returnType.type & 0x7F;
var ftTypeName = getTypeName(
template, TemplateDataType(ftType, f.returnType.typeGuid), templates);
var args = f.arguments.map((a) {
var isArray = a.type.type & 0x80 == 0x80;
var atType = a.type.type & 0x7F;
var atTypeName = getTypeName(
template, TemplateDataType(atType, a.type.typeGuid), templates);
return "Arg('${a.name}', ${atTypeName}, ${isArray})";
}).join(', ');
return "Func('${f.name}', ${ftTypeName}, ${isArray}, [${args}], ${_escape(f.expansion)})";
}).join(', ');
var descEvents = template.events.map((e) {
var isArray = e.argumentType.type & 0x80 == 0x80;
var etType = e.argumentType.type & 0x7F;
var etTypeName = getTypeName(template,
TemplateDataType(etType, e.argumentType.typeGuid), templates);
return "Evt('${e.name}', ${etTypeName}, ${isArray}, ${e.listenable}, ${_escape(e.expansion)})";
}).join(', ');
rt.writeln(
"TemplateDescriber get template => TemplateDescriber('${template.className}', properties: [${descProps}], functions: [${descFuncs}], events: [$descEvents]);");
rt.writeln("\r\n}");
return rt.toString();