[gui] support yaml in builder style

This commit is contained in:
2018-02-15 23:56:46 +03:00
parent 5b4e8dbb5e
commit 97b7686694
3 changed files with 223 additions and 210 deletions

View File

@@ -4,7 +4,7 @@
"license": "BSD",
"tags": ["flash", "openfl"],
"description": "Framework.",
"version": "0.7.0",
"version": "0.7.1",
"releasenote": "Update.",
"contributors": ["shmyga"],
"classPath": "src/main",

View File

@@ -1,6 +1,7 @@
package haxework.gui.build;
#if macro
import haxework.gui.build.BuilderUtil;
import haxe.macro.Context;
import haxework.gui.build.PositionJsonParser;
import haxe.macro.Expr;
@@ -25,17 +26,12 @@ class Builder {
templateFile = Context.resolvePath(templatePath[0]);
templateKey = templatePath[1];
var ext = templateFile.split('.').pop();
template = switch(ext) {
case 'json': BuilderUtil.loadJsonFile(templateFile);
case 'yml' | 'yaml': BuilderUtil.loadYamlFile(templateFile);
case x: throw 'Unsupported template format: "${x}"';
}
template = BuilderUtil.loadFile(templateFile);
if (templateKey != null) template = Reflect.field(template, templateKey);
if (templateMeta[1] != null) {
styleFile = Context.resolvePath(templateMeta[1]);
style = BuilderUtil.loadJsonFile(styleFile);
style = BuilderUtil.loadFile(styleFile);
}
fields = Context.getBuildFields();
@@ -43,6 +39,16 @@ class Builder {
i = 0;
}
private static function getSpecField(object:Dynamic, field:String):Dynamic {
if (Reflect.hasField(object, "@" + field)) {
return Reflect.field(object, "@" + field);
} else if (Reflect.hasField(object, "$" + field)) {
return Reflect.field(object, "$" + field);
} else {
return null;
}
}
private function getPosition(?position:JsonKeyPosition):Position {
var min = position == null ? 1 : position.min + 32; // :-(
var max = position == null ? 1 : position.max + 32;
@@ -63,7 +69,7 @@ class Builder {
var res = "haxework.provider.Provider.get(haxework.resources.IResources)." + a[1];
var bindExpr = res + ".bind(\"" + a[2] + "\", " + name + ", \"" + key + "\")";
exprs.push(Context.parse(bindExpr, getPosition(position)));
//res + ".get(\"" + a[2] + "\")";
//res + ".get(\"" + a[2] + "\")";
null;
case "locale":
"new haxework.locale.LString(\"" + a[1] + "\")";
@@ -79,15 +85,12 @@ class Builder {
}
}
private function getType(value:Dynamic, position:Position):String {
if (Reflect.hasField(value, "@type")) {
return Reflect.field(value, "@type");
} else if (Reflect.hasField(value, "$type")) {
return Reflect.field(value, "$type");
} else {
private static function getType(value:Dynamic, position:Position):String {
var type:String = getSpecField(value, "type");
if (type == null) {
Context.error("Need @type field", position);
return null;
}
return type;
}
private function getValue(name:String, key:String, value:Dynamic, position:JsonKeyPosition):Dynamic {
@@ -96,7 +99,7 @@ class Builder {
return getValue(null, null, v, position);
});
} else if (Std.is(value, String)) {
if (value.charAt(0) == "@") {
if (value.charAt(0) == "@" || value.charAt(0) == "$") {
specialValue(name, key, value.substring(1, value.length).split(":"), position);
} else if (~/(0x|#)[A-Fa-f\d]{6}/.match(value)) {
Std.parseInt(StringTools.replace(Std.string(value), "#", "0x"));
@@ -126,8 +129,9 @@ class Builder {
}
private function createElement(template:Dynamic, name:String):String {
if (Reflect.hasField(template, "@style")) {
var s = Reflect.field(style, Reflect.field(template, "@style"));
var s = getSpecField(template, "style");
if (s != null) {
var s = Reflect.field(style, s);
for (key in Reflect.fields(s)) {
if (key.charAt(0) != "$" && !Reflect.hasField(template, key)) {
Reflect.setField(template, key, Reflect.field(s, key));

View File

@@ -9,33 +9,42 @@ import haxe.macro.Expr.ExprDef;
class BuilderUtil {
public static function loadJsonFile(path:String) {
public static function loadJsonFile(path:String):Dynamic {
Context.registerModuleDependency(Context.getLocalModule(), path);
var content = sys.io.File.getContent(path);
var json = null;
try {
json = PositionJsonParser.parse(content, path);
} catch(error:Dynamic) {
} catch (error:Dynamic) {
Context.error(error, Context.makePosition({min:0, max:0, file:path}));
}
Context.parse(content, Context.makePosition({min:0, max:0, file:path}));
return json;
}
public static function loadYamlFile(path:String) {
public static function loadYamlFile(path:String):Dynamic {
Context.registerModuleDependency(Context.getLocalModule(), path);
var content = sys.io.File.getContent(path);
var result = null;
try {
// ToDo: extract poisiton info
result = Yaml.parse(content, Parser.options().useObjects());
} catch(error:Dynamic) {
} catch (error:Dynamic) {
Context.error(error, Context.makePosition({min:0, max:0, file:path}));
}
//Context.parse(content, Context.makePosition({min:0, max:0, file:path}));
return result;
}
public static function loadFile(path:String):Dynamic {
var ext = path.split('.').pop();
return switch(ext) {
case 'json': BuilderUtil.loadJsonFile(path);
case 'yml' | 'yaml': BuilderUtil.loadYamlFile(path);
case x: throw 'Unsupported template format: "${x}"';
}
}
public static function getMeta(key:String):Array<String> {
var c = Context.getLocalClass().get();
for (meta in c.meta.get()) {