Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Process ¶
func Process(modifier Modifier)
How to use this library to build you own preprocessor:
Create a new project for your own preprocessor.
Define a struct that will satisfy Modifier interface. Modifier has only one method [Modify] that must accept (*dst.File, *decorator.Decorator, *decorator.Restorer) as arguments, and must return a modified *dst.File.
All the modifications you want to make should be called inside your Modify method.
In a main function of your preprocessor project simply call:
moonject.Process(YourModifierStruct{})
Build your preprocessor with just a `go build`
Call a newly compiled preprocessor on your target project like this:
go build -a -toolexec="absolute/path/to/your/preprocessor/binary $PWD" main.go
IMPORTANT: pay attention to the -a flag in the above command. It is required to call compilation of all project files. Otherwise go compiler will not compile files that have not been changed since the last compilation. So if you make changes to injector.go and then try to call it as a preprocessor when compiling your project code, the go compiler may not make the changes you need if you have not changed the project code since the last compilation.
IMPORTANT: pay attention to $PWD argument in the above command. When calling your preprocessor, you must specify the absolute path to the root of the project you want to compile as the first argument. If you call go build in the root of the project, it is sufficient to specify $PWD.
Process function represents the generalized approach to preprocessing go code. It:
- Checks if we are at the right stage of compilation;
- If not, runs the original command and return;
- Extract the files that go is about to compile;
- Make the changes to the AST of all the files (this won't affect the source code);
- Writes the modified files to the temporary directory;
- Resolve all missing imports that were added as part of the modification;
- Substitutes the path to the original files with the path to modified files and pass them to the compiler command;
- Runs the original command with an already substituted files to be compiled.