protoc-gen-go-micro/clean.go
Vasiliy Tolstov 9b2c559015 add ability to use repo templates
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2020-12-31 04:10:22 +03:00

51 lines
953 B
Go

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
})
}