Reputation: 966
We use some code templates in our repo, and we rely on go:generate
to generate all the needed codes from these templates.
Because there are several templates and from each template we generate several actual codes, consistency of generated codes has become an issue: we needed a command to run and be sure that all generated codes are up to date with their corresponding templates. To do this, we have a simple bash command that find all files that have go:generate
in them, and runs go generate
in their directory.
The problem is that this approach is really slow: most of the templates have not changed, so there is no need to generate their codes again.
Is there any way to tell go generate
to only run if the template is newer than the file? Or, is there any better approach other than this?
Thank you very much.
Upvotes: 4
Views: 2204
Reputation: 669
Check go-generate-fast, a drop in replacement to “go generate” that makes re-generations really fast. It caches the output when the inputs are the same, has support for multiple (a growing list of) tools, and supports custom scripts.
https://github.com/oNaiPs/go-generate-fast
Disclosure: I am the main developer.
Upvotes: 0
Reputation: 417402
First, you don't need that bash script that looks for go:generate
in files, the go
tool itself can do that for you. Just use
go generate ./...
to recurse in subfolders.
Next, when a code generation runs, I would store the template file's last modification timestamp in the generated code, either in a comment or in a const / var. The app that is run by go generate
could first check if the template's last mod time is newer that the stored one in the code, and only proceed if so.
This file last mod time should be enough. But if for some reason you can't rely on it, the template could have a "version", which should be incremented when the template is changed. And have the generator "transfer" this version into the final code, which can be used to check if the template is newer than the code generated from it.
Upvotes: 0