Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Process ¶
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:
goinject.Process(YourModifierStruct{})
Build your preprocessor with just a `go build`
Call a newly compiled preprocessor on your target project like this:
go build -toolexec="absolute/path/to/your/preprocessor/binary" main.go
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.
func ResolvePkg ¶ added in v0.0.19
ResolvePkg will try to collect all the named go packages. It utilizes `go list -deps -export -json -- <pkgName>` command. The most important part here is the -export flag, because it will give us the actual path to the compiled package by its name. Then, we can use this path as a value when adding missing package to importcfg in form of `packagefile {pkgName}={path}`