Created
September 27, 2016 13:12
-
-
Save josuigoa/de9fad3623ba50a4c298fde87b77fcd1 to your computer and use it in GitHub Desktop.
This macro reads the given directory and creates a class with the paths of the resources in it recursively. The class name and resources extensions are customizable
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
(function (console) { "use strict"; | |
var Main = function() { | |
console.log("img/0/irudia.png"); | |
console.log("snd/abestia.mp3"); | |
}; | |
Main.main = function() { | |
new Main(); | |
}; | |
var q = window.jQuery; | |
var js = js || {} | |
js.JQuery = q; | |
Main.main(); | |
})(typeof console != "undefined" ? console : {log:function(){}}); |
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; | |
@:build(macros.ResourceBuilder.build('../img/', ['jpg', 'png', 'bmp'], 'Img', 'img/')) | |
@:build(macros.ResourceBuilder.build('../snd/', ['mp3', 'ogg'], 'Snd', 'snd/')) | |
class Main { | |
public function new() { | |
trace( Img.irudia__png ); | |
trace( Snd.abestia__mp3 ); | |
} | |
static function main() { | |
new Main(); | |
} | |
} |
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 macros; | |
import haxe.macro.Expr; | |
using StringTools; | |
class ResourceBuilder { | |
static var extensions:Array<String>; | |
static public function build(dir:String, _extensions:Array<String> = null, class_name:String = 'Res', base_dir:String = null) { | |
var res = []; | |
extensions = _extensions; | |
try { | |
recursive_dir_read(haxe.macro.Context.resolvePath(dir), (base_dir == null)?dir:base_dir, res); | |
} catch(e:Dynamic) { | |
trace(e); | |
} | |
var fields = []; | |
for (i in res) { | |
fields.push({ | |
name : i.name, | |
access : [AStatic, APublic, AInline], | |
kind : FVar(macro:String, macro $v{i.path}), | |
pos : haxe.macro.Context.currentPos() | |
}); | |
} | |
var _class = macro class Tmp {}; | |
_class.name = class_name; | |
_class.fields = fields; | |
haxe.macro.Context.defineType(_class); | |
return haxe.macro.Context.getBuildFields(); | |
} | |
static function recursive_dir_read(resolved_dir:String, dir:String, res:Array<FileReference>) { | |
if (sys.FileSystem.isDirectory(resolved_dir)) { | |
for (f in sys.FileSystem.readDirectory(resolved_dir)) { | |
if (sys.FileSystem.isDirectory(resolved_dir+f)) { | |
recursive_dir_read('${resolved_dir}${f}/', '${dir}${f}/', res); | |
} else { | |
if (extensions == null || extensions.indexOf(haxe.io.Path.extension(f)) != -1) | |
res.push(new FileReference(f, dir+f)); | |
} | |
} | |
} | |
} | |
} | |
class FileReference { | |
public var name:String; | |
public var path:String; | |
public function new(_n:String, _p:String) { | |
name = _n.replace('-', '_').replace('.', '__').replace(' ', '_'); | |
path = _p; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment