[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", "license": "BSD",
"tags": ["flash", "openfl"], "tags": ["flash", "openfl"],
"description": "Framework.", "description": "Framework.",
"version": "0.7.0", "version": "0.7.1",
"releasenote": "Update.", "releasenote": "Update.",
"contributors": ["shmyga"], "contributors": ["shmyga"],
"classPath": "src/main", "classPath": "src/main",

View File

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

View File

@@ -9,7 +9,7 @@ import haxe.macro.Expr.ExprDef;
class BuilderUtil { class BuilderUtil {
public static function loadJsonFile(path:String) { public static function loadJsonFile(path:String):Dynamic {
Context.registerModuleDependency(Context.getLocalModule(), path); Context.registerModuleDependency(Context.getLocalModule(), path);
var content = sys.io.File.getContent(path); var content = sys.io.File.getContent(path);
var json = null; var json = null;
@@ -22,7 +22,7 @@ class BuilderUtil {
return json; return json;
} }
public static function loadYamlFile(path:String) { public static function loadYamlFile(path:String):Dynamic {
Context.registerModuleDependency(Context.getLocalModule(), path); Context.registerModuleDependency(Context.getLocalModule(), path);
var content = sys.io.File.getContent(path); var content = sys.io.File.getContent(path);
var result = null; var result = null;
@@ -36,6 +36,15 @@ class BuilderUtil {
return result; 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> { public static function getMeta(key:String):Array<String> {
var c = Context.getLocalClass().get(); var c = Context.getLocalClass().get();
for (meta in c.meta.get()) { for (meta in c.meta.get()) {