Skip to content

Instantly share code, notes, and snippets.

@jasononeil
Last active December 18, 2015 05:39
Show Gist options
  • Save jasononeil/5734248 to your computer and use it in GitHub Desktop.
Save jasononeil/5734248 to your computer and use it in GitHub Desktop.
Macro example for EzeQL_ - create ".hx" files at macro time, and then go ahead and use them in your code.
-neko Main.hx.n
-main Main
--macro BuildMacro.build()
import haxe.macro.Expr;
import haxe.macro.Context;
import sys.FileSystem;
import sys.io.File;
import haxe.io.Path;
class BuildMacro
{
static function build()
{
var className = "DoesNotExist.hx";
var classDef = "class DoesNotExist { public function new() { trace('Now it does exist'); } }";
addFileToRepo( className, classDef );
return null;
}
static function addFileToRepo( filename:String, definition:String )
{
// Figure out the path of "Main.hx", and then put our file in the same spot
var mainFilePath = FileSystem.fullPath( Context.resolvePath( "Main.hx" ) );
var classFilePath = Path.directory(mainFilePath) + "/" + filename;
if ( !FileSystem.exists(classFilePath)) {
File.saveContent( classFilePath, definition );
}
}
}
class Main
{
static function main()
{
trace ("Haxe!");
new DoesNotExist();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment