build: update

This commit is contained in:
Felix Lange 2024-07-01 16:33:01 +02:00
parent 90ea8f7164
commit 5649c022de

View file

@ -349,16 +349,16 @@ func downloadSpecTestFixtures(csdb *build.ChecksumDB, cachedir string) string {
return filepath.Join(cachedir, base) return filepath.Join(cachedir, base)
} }
// hashDir iterates all files under the top-level project directory // hashSourceFiles iterates all files under the top-level project directory
// computing the hash of each file (excluding files within the tests // computing the hash of each file (excluding files within the tests
// subrepo) // subrepo)
func hashDir() (map[string]common.Hash, error) { func hashSourceFiles() (map[string]common.Hash, error) {
res := make(map[string]common.Hash) res := make(map[string]common.Hash)
err := filepath.WalkDir(".", func(path string, d os.DirEntry, err error) error { err := filepath.WalkDir(".", func(path string, d os.DirEntry, err error) error {
if strings.HasPrefix(path, filepath.FromSlash("tests/testdata")) { if strings.HasPrefix(path, filepath.FromSlash("tests/testdata")) {
return nil return filepath.SkipDir
} }
if d.IsDir() { if !d.Type().IsRegular() {
return nil return nil
} }
// open the file and hash it // open the file and hash it
@ -383,28 +383,37 @@ func hashDir() (map[string]common.Hash, error) {
// any mutations in the source file tree: i.e. all generated files were // any mutations in the source file tree: i.e. all generated files were
// updated and committed. Any stale generated files are updated. // updated and committed. Any stale generated files are updated.
func doGenerate() { func doGenerate() {
tc := new(build.GoToolchain) var (
cachedir := flag.String("cachedir", "./build/cache", "directory for caching protoc/protoc-gen-go binaries.") tc = new(build.GoToolchain)
cachedir = flag.String("cachedir", "./build/cache", "directory for caching binaries.")
verify = flag.Bool("verify", false, "check whether any files are changed by go generate")
)
protocPath := downloadProtoc(*cachedir) protocPath := downloadProtoc(*cachedir)
protocGenGoPath := downloadProtocGenGo(*cachedir) protocGenGoPath := downloadProtocGenGo(*cachedir)
preHashes, err := hashDir() var preHashes map[string]common.Hash
if *verify {
var err error
preHashes, err = hashSourceFiles()
if err != nil { if err != nil {
log.Fatal("failed to compute map of source hashes", "err", err) log.Fatal("failed to compute map of source hashes", "err", err)
} }
}
c := tc.Go("generate", "./...") c := tc.Go("generate", "./...")
newPathEnv := os.Getenv("PATH") + string(os.PathListSeparator) + protocPath + string(os.PathSeparator) + "bin" pathList := []string{filepath.Join(protocPath, "bin"), protocGenGoPath, os.Getenv("PATH")}
newPathEnv += string(os.PathListSeparator) + protocGenGoPath c.Env = append(c.Env, "PATH="+strings.Join(pathList, string(os.PathListSeparator)))
c.Env = append(c.Env, "PATH="+newPathEnv)
build.MustRun(c) build.MustRun(c)
postHashes, err := hashDir() if !*verify {
return
}
// Check if files were changed.
postHashes, err := hashSourceFiles()
if err != nil { if err != nil {
log.Fatal("error computing source tree file hashes", "err", err) log.Fatal("error computing source tree file hashes", "err", err)
} }
updates := []string{} updates := []string{}
for path, postHash := range postHashes { for path, postHash := range postHashes {
preHash, ok := preHashes[path] preHash, ok := preHashes[path]
@ -413,7 +422,7 @@ func doGenerate() {
} }
} }
for _, updatedFile := range updates { for _, updatedFile := range updates {
fmt.Fprintf(os.Stderr, "updated generated file %s\n", updatedFile) fmt.Fprintf(os.Stderr, "changed file %s\n", updatedFile)
} }
if len(updates) != 0 { if len(updates) != 0 {
log.Fatal("One or more generated files were updated by running 'go generate ./...'") log.Fatal("One or more generated files were updated by running 'go generate ./...'")