11package main
22
33import (
4+ "encoding/json"
45 "fmt"
56 "log"
67 "os"
@@ -9,6 +10,27 @@ import (
910 "strings"
1011)
1112
13+ func parseExecCommand (path string ) (string , error ) {
14+ var exec = struct {
15+ Command string `json:"command"`
16+ }{
17+ Command : "generate" ,
18+ }
19+
20+ execJsonPath := filepath .Join (path , "exec.json" )
21+ if _ , err := os .Stat (execJsonPath ); ! os .IsNotExist (err ) {
22+ blob , err := os .ReadFile (execJsonPath )
23+ if err != nil {
24+ return "" , err
25+ }
26+ if err := json .Unmarshal (blob , & exec ); err != nil {
27+ return "" , err
28+ }
29+ }
30+
31+ return exec .Command , nil
32+ }
33+
1234func regenerate (dir string ) error {
1335 return filepath .Walk (dir , func (path string , info os.FileInfo , err error ) error {
1436 if err != nil {
@@ -19,6 +41,15 @@ func regenerate(dir string) error {
1941 }
2042 if strings .HasSuffix (path , "sqlc.json" ) || strings .HasSuffix (path , "sqlc.yaml" ) {
2143 cwd := filepath .Dir (path )
44+ command , err := parseExecCommand (cwd )
45+ if err != nil {
46+ return fmt .Errorf ("failed to parse exec.json: %w" , err )
47+ }
48+
49+ if command != "generate" {
50+ return nil
51+ }
52+
2253 cmd := exec .Command ("sqlc-dev" , "generate" , "--experimental" )
2354 cmd .Dir = cwd
2455 out , failed := cmd .CombinedOutput ()
0 commit comments