XY Notice
I know about macros, __before_compile__
and other attributes, compiler tracers and such stuff. The question is exactly about implementing wrapper for Elixir compiler which be compatible with Mix.Task.Compiler
, nothing else.
The Question
How do I write the Mix.Task.Compiler
which will do some global changes to the AST before calling Elixir’s compiler to generate .beam
files.
Current solution
Right now my compiler
- Searches for
.ex
files in the path specified byMix.Project.config()[:elixirc_paths]
- Calls
Code.string_to_quoted()
on each file - Changes the AST
- Calls
Code.compile_quoted
withFile.write!()
to the build_path of the binaries returned bycompile_quoted
Current solution drawbacks
- I think it is possible to compile straight to file (without
File.write!
) - I think this kind of compilation does not call compiler tracers
- I think it can have some problems with protocol consolidation
- It needs to read/write elixir’s compiler manifest, which is some kind of a private structure.
- It is difficult to change the AST because modules can have compile-time code in their bodies, which I need to preevaluate, to change the code inside functions.