Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Process ¶
How to use this library to create your own preprocessor:
Start a new project for your preprocessor.
Define a struct that implements the Modifier interface. The Modifier interface has a single method, [Modify], which accepts the following arguments: (*dst.File, *decorator.Decorator, *decorator.Restorer) and returns a modified *dst.File.
All modifications should be performed within the Modify method.
In the main function of your preprocessor project, simply invoke:
goinject.Process(YourModifierStruct{})
Build your preprocessor using `go build`.
To run your preprocessor on a target project, use the following command:
go build -toolexec="absolute/path/to/your/preprocessor/binary" main.go
The Process function provides a generalized approach to preprocessing Go code. It:
- Verifies if we are at the correct stage of the compilation;
- If not, runs the original command and returns;
- Extracts the files that Go is about to compile;
- Modifies the AST of all files (without altering the source code);
- Writes the modified files to a temporary directory;
- Resolves any missing imports added during the modification;
- Replaces the paths to the original files with the modified file paths, passing them to the compiler;
- Executes the original command with the substituted files for compilation.
func ResolvePkg ¶ added in v0.0.19
ResolvePkg attempts to collect and return the paths to the compiled Go packages corresponding to the given package name. It runs the `go list -deps -export -json -- <pkgName>` command to retrieve package details, where the `-export` flag is crucial for obtaining the actual path to the compiled package by its name. Special handling is applied for the "unsafe" package since it doesn't follow the standard module export format.