vendor.go
54 lines1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package scaffold
import (
"bytes"
"io/fs"
"os"
"path/filepath"
"strings"
"congo.gg"
)
// ExtractPackages copies framework packages from the embedded pkg/ source
// into targetDir/internal/<package>/, rewriting imports to the new module path.
func ExtractPackages(targetDir, newModule string, packages []string) error {
const pkgRoot = "pkg/"
oldImport := []byte("congo.gg/pkg/")
newImport := []byte(newModule + "/internal/")
for _, pkg := range packages {
root := pkgRoot + pkg
err := fs.WalkDir(congo.SourceFS, root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
outRel := "internal/" + strings.TrimPrefix(path, pkgRoot)
outPath := filepath.Join(targetDir, outRel)
if d.IsDir() {
return os.MkdirAll(outPath, 0755)
}
if strings.HasSuffix(path, "_test.go") {
return nil
}
content, err := fs.ReadFile(congo.SourceFS, path)
if err != nil {
return err
}
if strings.HasSuffix(path, ".go") {
content = bytes.ReplaceAll(content, oldImport, newImport)
}
return os.WriteFile(outPath, content, 0644)
})
if err != nil {
return err
}
}
return nil
}