[macro] add @:style macro

This commit is contained in:
2018-08-09 12:14:58 +03:00
parent 7d26241584
commit 7f28abd635
4 changed files with 57 additions and 2 deletions

View File

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

View File

@@ -0,0 +1,45 @@
package haxework.macro;
import haxe.macro.Expr.MetadataEntry;
import haxe.macro.Context;
import haxe.macro.Type.ClassType;
class StyleMacro {
private static inline var metaName:String = ':style';
public static function has(classType:ClassType):Bool {
for (md in classType.meta.get()) if (md.name == metaName) {
return true;
}
return false;
}
public static var style:Dynamic;
private var classType:ClassType;
private var bindings:Map<String, String>;
private var meta(get, never):MetadataEntry;
private var styleFile:String;
private function get_meta():MetadataEntry {
for (md in classType.meta.get()) if (md.name == metaName) {
return md;
}
return null;
}
public function new(classType:ClassType) {
this.classType = classType;
var params = Util.getMetaParams(meta);
var filePath = params[0];
if (filePath == null) {
filePath = classType.pack.join("/") + "/" + classType.name + ".yaml";
}
styleFile = Context.resolvePath(filePath);
style = FileUtil.loadFile(styleFile);
}
}

View File

@@ -39,11 +39,17 @@ class TemplateMacro {
this.classType = classType;
this.fields = fields;
var params = Util.getMetaParams(meta);
templateFile = Context.resolvePath(params[0]);
var filePath = params[0];
if (filePath == null) {
filePath = classType.pack.join("/") + "/" + classType.name + ".yaml";
}
templateFile = Context.resolvePath(filePath);
template = FileUtil.loadFile(templateFile);
if (params.length > 1) {
var styleFile = params.length > 1 ? Context.resolvePath(params[1]) : null;
style = FileUtil.loadFile(styleFile);
} else {
style = StyleMacro.style;
}
bindings = findViewsBindings(fields);
}

View File

@@ -1,5 +1,6 @@
package haxework.parser;
import haxework.macro.StyleMacro;
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type.ClassType;
@@ -38,6 +39,9 @@ class Parser {
fields = result;
}
// process class meta
if (StyleMacro.has(ct)) {
new StyleMacro(ct);
}
if (TemplateMacro.has(ct)) {
modify = true;
var template = new TemplateMacro(ct, fields);