2022-09-27 20:19:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
2023-12-04 10:22:49 +00:00
|
|
|
"log/slog"
|
2022-09-27 20:19:27 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-12-04 10:22:49 +00:00
|
|
|
"slices"
|
2022-09-27 20:19:27 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultDirPermissions = 0o755
|
|
|
|
|
|
|
|
var (
|
|
|
|
GoSourceFiles []string
|
|
|
|
GeneratedMockFiles []string
|
|
|
|
WorkingDir string
|
|
|
|
OutDir string
|
|
|
|
dirsToIgnore = []string{
|
|
|
|
".git",
|
|
|
|
"magefiles",
|
|
|
|
".concourse",
|
|
|
|
".run",
|
|
|
|
".task",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if wd, err := os.Getwd(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
WorkingDir = wd
|
|
|
|
}
|
|
|
|
|
|
|
|
OutDir = filepath.Join(WorkingDir, "out")
|
|
|
|
|
|
|
|
if err := os.MkdirAll(OutDir, defaultDirPermissions); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, nil)))
|
2022-09-27 20:19:27 +00:00
|
|
|
|
|
|
|
if err := initSourceFiles(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.Info("Completed initialization")
|
2022-09-27 20:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func initSourceFiles() error {
|
|
|
|
return filepath.WalkDir(WorkingDir, func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.IsDir() {
|
|
|
|
if slices.Contains(dirsToIgnore, filepath.Base(path)) {
|
|
|
|
return fs.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ext, found := strings.Cut(filepath.Base(path), ".")
|
|
|
|
if !found {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ext {
|
|
|
|
case "mock.go":
|
|
|
|
GeneratedMockFiles = append(GeneratedMockFiles, path)
|
|
|
|
case "go":
|
|
|
|
GoSourceFiles = append(GoSourceFiles, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|