Last active
August 29, 2015 14:12
-
-
Save npretto/def591a5d24e96ad0569 to your computer and use it in GitHub Desktop.
MacroTools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package; | |
| import haxe.macro.Context; | |
| import haxe.macro.Expr; | |
| import sys.FileSystem; | |
| using sys.io.File; | |
| using StringTools; | |
| class MacroTools { | |
| macro static public function folderToPaths(folder:String):Array<Field> { | |
| var fields = Context.getBuildFields(); | |
| for (file in FileSystem.readDirectory(folder)) | |
| { | |
| if (!FileSystem.isDirectory(folder + "/" + file)) | |
| { | |
| var newField = { | |
| name: fieldName(file), | |
| doc: folder+"/"+file, | |
| meta: [], | |
| access: [AStatic, APublic, AInline], | |
| kind: FVar(macro : String, | |
| macro $v{folder+"/"+file}), | |
| pos: Context.currentPos() | |
| }; | |
| fields.push(newField); | |
| } | |
| } | |
| return fields; | |
| } | |
| macro static public function folderToProperties(folder:String):Array<Field> { | |
| var fields = Context.getBuildFields(); | |
| for (file in FileSystem.readDirectory(folder)) | |
| { | |
| if (!FileSystem.isDirectory(folder + "/" + file)) | |
| { | |
| //*/ | |
| var prop_name = fieldName(file); | |
| var field_name = "get_"+prop_name; | |
| //var f = macro : { | |
| //public static inline function $field_name():String | |
| //{ | |
| // | |
| //return Main.loadFile($v{folder}+"/"+$v{file}); | |
| //} | |
| //}; | |
| //switch (f) { | |
| //case TAnonymous(ff): | |
| //fields = fields.concat(ff); | |
| //default: | |
| //throw 'unreachable'; | |
| //} | |
| var prop_name = fieldName(file); | |
| var field_name = "get_"+prop_name; | |
| var test = macro : { | |
| public static var $prop_name(get, null):String; | |
| static inline function $field_name():String | |
| { | |
| return Main.loadFile('${$v{folder}}/${$v{file}}'); | |
| } | |
| }; | |
| switch (test) { | |
| case TAnonymous(pp): | |
| fields = fields.concat(pp); | |
| default: | |
| throw 'unreachable'; | |
| } | |
| //var newField = { | |
| //name: fieldName(file), | |
| //doc: folder+"/"+file, | |
| //meta: [], | |
| //access: [AStatic, APublic], | |
| //kind: FProp("get","null",macro :String), | |
| //pos: Context.currentPos() | |
| //}; | |
| //fields.push(newField); | |
| /*/ | |
| var expList = []; | |
| expList.push( macro return "a+"+Main.loadFile($v { folder } +"/" + $v { file } )); | |
| var newField = { | |
| name: fieldName(file), | |
| doc: folder+"/"+file, | |
| meta: [], | |
| access: [AStatic, APublic,AInline], | |
| kind: FFun( { | |
| args:[], | |
| ret:null, | |
| params:[], | |
| expr:{expr:EBlock(expList), pos:Context.currentPos()} | |
| }), | |
| pos: Context.currentPos() | |
| }; | |
| fields.push(newField); | |
| //*/ | |
| } | |
| } | |
| return fields; | |
| } | |
| public static function fieldName(fileName:String) | |
| { | |
| fileName = fileName.replace(".", "_"); | |
| return fileName; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment