Skip to content

Instantly share code, notes, and snippets.

@sonygod
Forked from back2dos/Module.hx
Created November 19, 2013 13:31
Show Gist options
  • Save sonygod/7545381 to your computer and use it in GitHub Desktop.
Save sonygod/7545381 to your computer and use it in GitHub Desktop.
package ;
import haxe.macro.Type;
import haxe.macro.Expr;
import haxe.macro.Context;
class Module {
static function build(?module:String) {
function isIncluded(meta:MetaAccess)
return
(module == null && !meta.has(':module'))
||
Lambda.exists(meta.get(), function (m) return switch m {
case {
name: ':module',
params: []
} if (module == null): true;
case {
name: ':module',
params: [{ expr: EConst(CString(m))}]
} if (module == m): true;
default: false;
});
function checkType(b:BaseType) {
if (!isIncluded(b.meta)) {
Context.warning('excluding ' + b.name, b.pos);
b.exclude();
}
}
Context.onGenerate(function (types:Array<Type>) {
for (t in types) switch t {
case TInst(b, _): checkType(b.get());
case TEnum(b, _): checkType(b.get());
default:
}
});
}
}
@sonygod
Copy link
Author

sonygod commented Nov 19, 2013

One way to tackle this is do decorate all classes with different metadata.

Something like this:

@:module('a')
class A {
}

@:module('b')
class B {
}

class X {
}

And then you will need something like this:
https://gist.github.com/back2dos/6465183
And then you would compile with --macro Module.build(); to get
anything that is not part of some module (that would include the std
lib and X) and then --macro Module.build('a'); and --macro Module.build('b');

And you must be sure to load them in the right order of course. Cyclic
dependencies will probably break your neck. And you'll need to
@:expose stuff in the gist, along the lines of Caue's snippet.

Regards,
Juraj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment