[macro] add @:style macro
This commit is contained in:
45
src/main/haxework/macro/StyleMacro.hx
Normal file
45
src/main/haxework/macro/StyleMacro.hx
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user