[gui] support yaml in builder style
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
@@ -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));
|
||||
|
||||
@@ -9,7 +9,7 @@ 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;
|
||||
@@ -22,7 +22,7 @@ class BuilderUtil {
|
||||
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;
|
||||
@@ -36,6 +36,15 @@ class BuilderUtil {
|
||||
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()) {
|
||||
|
||||
Reference in New Issue
Block a user