protoc-gen-go-micro/clean.go

51 lines
953 B
Go
Raw Permalink Normal View History

package main
import (
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
)
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
}
func cleanDir(dir string) error {
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if dir == "vendor" {
return filepath.SkipDir
}
if isGenerated(path) {
err = os.Remove(path)
}
return err
})
}