🚀 Shave off minutes and turn them into seconds for your go generation step 🚀.
go-generate-fast
serves as a drop-in replacement for go
generate.
Smart enough to understand if your generated files have changed or not,
go-generate-fast
can circumvent running unaltered scripts, offering a
significant speed boost by harnessing smart caching mechanisms.
Screen.Recording.2023-10-04.at.02.51.57.mov
- Effortless Integration: Seamlessly integrate it with your workflow through comment directives.
- Tool-awareness: Executes tools only when necessary, based on changes identified in input files.
- Comprehensive Tool Compatibility: Works with a variety of
go:generate
tools, including stringer, mockgen, esc, and more. - Support for Custom Scripts: Manually specify inputs and outputs if needed.
go install github.com/oNaiPs/go-generate-fast
Replace the traditional go generate
command with go-generate-fast
in your
scripts to leverage its benefits.
Typical command invocation is as follows:
go-generate-fast [file.go... | packages]
go-generate-fast
automatically detects the input/output files for the
following tools:
- stringer: Automated
generation of methods satisfying the
fmt.Stringer
interface. - protobuf: Go code generation for protobuf files
- gqlgen: Go code generation for GraphQL APIs
- mockgen: Mock class generation for interfaces
- moq: Interface mocking tool for go generate
- esc: Embedding static files in Go binaries
- go-bindata: Turn data file into go code.
- genny: Elegant generics for Go.
- controller-gen Generates utility code and Kubernetes YAML.
Above commands can be called in binary form, or with go run. E.g.:
//go:generate stringer
OR
//go:generate go run golang.org/x/tools/cmd/stringer[@latest]
If you are using a custom or currently unsupported script/tool, you can manually add files as demonstrated below:
//go:generate_input doc/gen_docs.go commands/**/*.go
//go:generate_output doc/man/*.1 doc/md/*.md
//go:generate go run doc/gen_docs.go
//go:generate_input images/*.png
//go:generate_output images/*.jpeg
//go:generate bash convert_images.sh
Just before your go:generate
command, you can add any go:generate_input
or
go:generate_output
directives accurately. The input files influence the
tool/script (re)execution, while output files contain the results. If one or
more input files change, the command reruns and stores the output files in the
cache directory.
Various environment variables are available for configuration:
-
GO_GENERATE_FAST_DIR
: Sets the base directory for configurations and caching. Default locations differ by OS:- Linux:
$HOME/.config/go-generate-fast
- Darwin:
$HOME/Library/Application Support/go-generate-fast
- Windows:
%AppData%\go-generate-fast
- Linux:
-
GO_GENERATE_FAST_CACHE_DIR
: Defines the cache files location. Default is$GO_GENERATE_FAST_DIR/cache/
. -
GO_GENERATE_FAST_DEBUG
: Enables debugging logs. -
GO_GENERATE_FAST_DISABLE
: Completely ignores caching. -
GO_GENERATE_FAST_READ_ONLY
: Uses the existing cache but prevents any new cache entries. -
GO_GENERATE_FAST_FORCE_USE_CACHE
: Forceably uses cache. If it does not exist, the command fails. -
GO_GENERATE_FAST_RECACHE
: Sets the cache to overwrite existing entries. The new results will be cached.
go-generate-fast
makes regeneration faster by reusing previously outputs when
the same input files are used again. This approach is identified via the
creation of a unique hash that combines inputs,outputs and other metadata. The
underlying concept is similar to the C/C++ compiler cache
ccache.
We highly appreciate community contributions! Please create an issue or submit a pull request for any suggestions or issues.
go-generate-fast
is licensed under the MIT License.