2020-11-30 12:11:39 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-09-26 19:44:12 +03:00
|
|
|
"context"
|
2020-11-30 12:11:39 +03:00
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-11-30 16:13:29 +03:00
|
|
|
|
2024-09-26 19:44:12 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
2020-11-30 12:11:39 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func isGenerated(name string) bool {
|
|
|
|
const (
|
|
|
|
genCodeGenerated = "code generated"
|
|
|
|
genDoNotEdit = "do not edit"
|
|
|
|
genAutoFile = "autogenerated file"
|
|
|
|
)
|
|
|
|
|
|
|
|
markers := []string{genCodeGenerated, genDoNotEdit, genAutoFile}
|
|
|
|
|
|
|
|
fileset := token.NewFileSet()
|
|
|
|
syntax, err := parser.ParseFile(fileset, name, nil, parser.PackageClauseOnly|parser.ParseComments)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, comment := range syntax.Comments {
|
|
|
|
for _, marker := range markers {
|
|
|
|
if strings.Contains(strings.ToLower(comment.Text()), marker) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-09-26 19:44:12 +03:00
|
|
|
func cleanDir(ctx context.Context, dir string) error {
|
2020-11-30 12:11:39 +03:00
|
|
|
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if dir == "vendor" {
|
2024-09-26 19:44:12 +03:00
|
|
|
logger.Info(ctx, "skip vendor dir")
|
2020-11-30 12:11:39 +03:00
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
if isGenerated(path) {
|
2024-09-26 19:44:12 +03:00
|
|
|
logger.Infof(ctx, "remove generated file: %s", path)
|
2020-11-30 12:11:39 +03:00
|
|
|
err = os.Remove(path)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|