ViewBuilder fileName

This commit is contained in:
2015-07-03 12:32:58 +03:00
parent 37846c4a18
commit 7fb65137fa

View File

@@ -7,22 +7,16 @@ import haxe.macro.Context;
@:remove @:autoBuild(haxework.gui.ViewBuilderImpl.build()) @:remove @:autoBuild(haxework.gui.ViewBuilderImpl.build())
extern interface ViewBuilder {} extern interface ViewBuilder {}
typedef BuildData = {
var fields:Array<Field>;
var exprs:Array<Expr>;
var style:Dynamic;
}
class ViewBuilderImpl {
#if macro #if macro
static function loadFile(path:String, json:Bool = true) { private class BuilderUtil {
var p = Context.resolvePath(path);
Context.registerModuleDependency(Context.getLocalModule(), p); public static function loadFile(path:String, json:Bool = true) {
var content = sys.io.File.getContent(p); Context.registerModuleDependency(Context.getLocalModule(), path);
var content = sys.io.File.getContent(path);
return json ? Json.parse(content) : content; return json ? Json.parse(content) : content;
} }
private 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()) {
if (meta.name == key) { if (meta.name == key) {
@@ -34,10 +28,45 @@ class ViewBuilderImpl {
} }
return []; return [];
} }
}
private static var i = 0; private class Builder {
private static function specialValue(data:BuildData, name:String, key:String, a:Array<String>):Dynamic { private var templateFile:String;
private var templateKey:String;
private var styleFile:String;
private var template:Dynamic;
private var fields:Array<Field>;
private var exprs:Array<Expr>;
private var style:Dynamic;
private var i:Int;
public function new() {
var templateMeta = BuilderUtil.getMeta(":template");
var templatePath = templateMeta[0].split("@");
templateFile = Context.resolvePath(templatePath[0]);
templateKey = templatePath[1];
template = BuilderUtil.loadFile(templateFile);
if (templateKey != null) template = Reflect.field(template, templateKey);
if (templateMeta[1] != null) {
styleFile = Context.resolvePath(templateMeta[1]);
style = BuilderUtil.loadFile(styleFile);
}
fields = Context.getBuildFields();
exprs = [];
i = 0;
}
private function getPosition():Position {
return Context.makePosition({min:0, max:0, file:templateFile + (templateKey != null ? "@" + templateKey : "")});
}
private function specialValue(name:String, key:String, a:Array<String>):Dynamic {
return switch (a[0]) { return switch (a[0]) {
case "asset": case "asset":
switch (a[1]) { switch (a[1]) {
@@ -49,7 +78,7 @@ class ViewBuilderImpl {
case "res": case "res":
var res = "haxework.provider.Provider.get(haxework.resources.IResources)." + a[1]; var res = "haxework.provider.Provider.get(haxework.resources.IResources)." + a[1];
var bindExpr = res + ".bind(\"" + a[2] + "\", " + name + ", \"" + key + "\")"; var bindExpr = res + ".bind(\"" + a[2] + "\", " + name + ", \"" + key + "\")";
data.exprs.push(Context.parse(bindExpr, Context.currentPos())); exprs.push(Context.parse(bindExpr, getPosition()));
//res + ".get(\"" + a[2] + "\")"; //res + ".get(\"" + a[2] + "\")";
null; null;
case "locale": case "locale":
@@ -57,8 +86,8 @@ class ViewBuilderImpl {
case "class": case "class":
a[1]; a[1];
case "layout": case "layout":
var template = loadFile(a[1]); var template = BuilderUtil.loadFile(a[1]);
return getValue(data, name, key, template); return getValue(name, key, template);
case "link": case "link":
"(links == null) ? untyped this : Reflect.field(links, \"" + a[1] + "\")"; "(links == null) ? untyped this : Reflect.field(links, \"" + a[1] + "\")";
case _: case _:
@@ -66,12 +95,12 @@ class ViewBuilderImpl {
} }
} }
private static function getValue(data:BuildData, name:String, key:String, value:Dynamic):Dynamic { private function getValue(name:String, key:String, value:Dynamic):Dynamic {
return if (Std.is(value, Array)) { return if (Std.is(value, Array)) {
value.map(function(v) { return getValue(data, null, null, v); }); value.map(function(v) { return getValue(null, null, v); });
} else if (Std.is(value, String)) { } else if (Std.is(value, String)) {
if (value.charAt(0) == "@") { if (value.charAt(0) == "@") {
specialValue(data, name, key, value.substring(1, value.length).split(":")); specialValue(name, key, value.substring(1, value.length).split(":"));
} 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"));
} else { } else {
@@ -83,8 +112,8 @@ class ViewBuilderImpl {
if (Reflect.hasField(value, "type")) { if (Reflect.hasField(value, "type")) {
var n = "a" + i++; var n = "a" + i++;
var type = Reflect.field(value, "type"); var type = Reflect.field(value, "type");
data.exprs.push(Context.parse("var " + n + " = new " + type + "()", Context.currentPos())); exprs.push(Context.parse("var " + n + " = new " + type + "()", getPosition()));
createElement(data, value, n); createElement(value, n);
n; n;
} else { } else {
value; value;
@@ -94,9 +123,9 @@ class ViewBuilderImpl {
} }
} }
private static function createElement(data:BuildData, template:Dynamic, name:String):String { private function createElement(template:Dynamic, name:String):String {
if (Reflect.hasField(template, "style")) { if (Reflect.hasField(template, "style")) {
var s = Reflect.field(data.style, Reflect.field(template, "style")); var s = Reflect.field(style, Reflect.field(template, "style"));
for (key in Reflect.fields(s)) if (!Reflect.hasField(template, key)) { for (key in Reflect.fields(s)) if (!Reflect.hasField(template, key)) {
Reflect.setField(template, key, Reflect.field(s, key)); Reflect.setField(template, key, Reflect.field(s, key));
} }
@@ -105,47 +134,33 @@ class ViewBuilderImpl {
if (Reflect.hasField(template, "id")) { if (Reflect.hasField(template, "id")) {
var id = Reflect.field(template, "id"); var id = Reflect.field(template, "id");
var type = Reflect.field(template, "type"); var type = Reflect.field(template, "type");
var expr = Context.parse("var a:" + type, Context.currentPos()); var expr = Context.parse("var a:" + type, getPosition());
var type = switch (expr.expr) { var type = switch (expr.expr) {
case EVars(vars): vars[0].type; case EVars(vars): vars[0].type;
case _: null; case _: null;
} }
data.fields.push({ fields.push({
name: id, name: id,
access: [APublic], access: [APublic],
pos: Context.currentPos(), pos: getPosition(),
kind: FProp("default", "null", type) kind: FProp("default", "null", type)
}); });
data.exprs.push(Context.parse("this." + id + " = " + name, Context.currentPos())); exprs.push(Context.parse("this." + id + " = " + name, getPosition()));
} }
for (key in Reflect.fields(template)) { for (key in Reflect.fields(template)) {
if (["type", "style"].indexOf(key) > -1) continue; if (["type", "style"].indexOf(key) > -1) continue;
var value = getValue(data, name, key, Reflect.field(template, key)); var value = getValue(name, key, Reflect.field(template, key));
if (value != null) { if (value != null) {
data.exprs.push(Context.parse(name + "." + key + " = cast " + value, Context.currentPos())); exprs.push(Context.parse(name + "." + key + " = cast " + value, getPosition()));
} }
} }
return name; return name;
} }
public static function build() {
//trace("Build: " + Context.getLocalClass().get().module);
var templateMeta = getMeta(":template");
var templatePath = templateMeta[0].split("@");
var template = loadFile(templatePath[0]);
if (templatePath[1] != null) template = Reflect.field(template, templatePath[1]);
var style = templateMeta[1] == null ? {} : loadFile(templateMeta[1]);
var fields = Context.getBuildFields(); public function build():Array<Field> {
createElement(template, "this");
var data = {
fields: fields,
exprs: [],
style: style
};
i = 0;
createElement(data, template, "this");
var init = false; var init = false;
for (f in fields) if (f.name == "init") { for (f in fields) if (f.name == "init") {
@@ -156,33 +171,40 @@ class ViewBuilderImpl {
fields.push({ fields.push({
name: "build", name: "build",
access: [APublic], access: [APublic],
pos: Context.currentPos(), pos: getPosition(),
kind: FFun({ kind: FFun({
args: [{name:"links", type:TPath({name:"Dynamic", pack:[], params:[]}), opt:true, value:null}], args: [{name:"links", type:TPath({name:"Dynamic", pack:[], params:[]}), opt:true, value:null}],
expr: macro $b{data.exprs}, expr: macro $b{exprs},
params: [], params: [],
ret: null ret: null
}) })
}); });
var contrExprs = []; var contstrExprs = [];
contrExprs.push(macro super()); contstrExprs.push(macro super());
contrExprs.push(macro build(links)); contstrExprs.push(macro build(links));
if (init) contrExprs.push(macro init()); if (init) contstrExprs.push(macro init());
fields.push({ fields.push({
name: "new", name: "new",
access: [APublic], access: [APublic],
pos: Context.currentPos(), pos: getPosition(),
kind: FFun({ kind: FFun({
args: [{name:"links", type:TPath({name:"Dynamic", pack:[], params:[]}), opt:true, value:null}], args: [{name:"links", type:TPath({name:"Dynamic", pack:[], params:[]}), opt:true, value:null}],
expr: macro $b{contrExprs}, expr: macro $b{contstrExprs},
params: [], params: [],
ret: null ret: null
}) })
}); });
return fields; return fields;
} }
}
#end
class ViewBuilderImpl {
#if macro
public static function build() {
return new Builder().build();
}
#end #end
} }