Skip to content

Commit ed9264d

Browse files
authored
feat: Add script to mirror code to sqlc-gen-go (sqlc-dev#2952)
* feat(scripts): Mirror sqlc-gen-go to second repo For easy forking, mirror the Go plugin to a separate read-only repository. * fix: Use correct import paths
1 parent 5dacaf0 commit ed9264d

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

scripts/mirror-go-plugin/main.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"flag"
6+
"fmt"
7+
"io/fs"
8+
"log"
9+
"os"
10+
"path/filepath"
11+
"strings"
12+
)
13+
14+
func main() {
15+
flag.Parse()
16+
// Assume it exists
17+
loc := flag.Arg(0)
18+
19+
dir := filepath.Join("internal", "codegen", "golang")
20+
err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
21+
if err != nil {
22+
return err
23+
}
24+
if info.IsDir() {
25+
return nil
26+
}
27+
contents, err := os.ReadFile(path)
28+
if err != nil {
29+
return err
30+
}
31+
32+
newdir := filepath.Join(loc, "internal")
33+
newpath := strings.Replace(path, dir, newdir, 1)
34+
35+
os.MkdirAll(filepath.Dir(newpath), 0755)
36+
37+
contents = bytes.ReplaceAll(contents,
38+
[]byte(`"github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"`),
39+
[]byte(`"github.com/sqlc-dev/sqlc-gen-go/internal/opts"`))
40+
41+
contents = bytes.ReplaceAll(contents,
42+
[]byte(`"github.com/sqlc-dev/sqlc/internal/plugin"`),
43+
[]byte(`"github.com/sqlc-dev/plugin-sdk-go/plugin"`))
44+
45+
contents = bytes.ReplaceAll(contents,
46+
[]byte(`"github.com/sqlc-dev/sqlc/internal/codegen/sdk"`),
47+
[]byte(`"github.com/sqlc-dev/plugin-sdk-go/sdk"`))
48+
49+
contents = bytes.ReplaceAll(contents,
50+
[]byte(`"github.com/sqlc-dev/sqlc/internal/metadata"`),
51+
[]byte(`"github.com/sqlc-dev/plugin-sdk-go/metadata"`))
52+
53+
contents = bytes.ReplaceAll(contents,
54+
[]byte(`"github.com/sqlc-dev/sqlc/internal/pattern"`),
55+
[]byte(`"github.com/sqlc-dev/plugin-sdk-go/pattern"`))
56+
57+
contents = bytes.ReplaceAll(contents,
58+
[]byte(`"github.com/sqlc-dev/sqlc/internal/debug"`),
59+
[]byte(`"github.com/sqlc-dev/sqlc-gen-go/internal/debug"`))
60+
61+
contents = bytes.ReplaceAll(contents,
62+
[]byte(`"github.com/sqlc-dev/sqlc/internal/inflection"`),
63+
[]byte(`"github.com/sqlc-dev/sqlc-gen-go/internal/inflection"`))
64+
65+
if err := os.WriteFile(newpath, contents, 0644); err != nil {
66+
return err
67+
}
68+
return nil
69+
})
70+
71+
if err != nil {
72+
fmt.Printf("error walking the path: %v\n", err)
73+
return
74+
}
75+
76+
{
77+
path := filepath.Join("internal", "inflection", "singular.go")
78+
contents, err := os.ReadFile(path)
79+
if err != nil {
80+
log.Fatal(err)
81+
}
82+
newpath := filepath.Join(loc, "internal", "inflection", "singular.go")
83+
if err := os.WriteFile(newpath, contents, 0644); err != nil {
84+
log.Fatal(err)
85+
}
86+
}
87+
88+
}

0 commit comments

Comments
 (0)