From aa332d958ce66424b711739d3d6c730cb60c54fc Mon Sep 17 00:00:00 2001 From: snowmerak Date: Mon, 22 May 2023 23:44:27 +0900 Subject: [PATCH] make and apply subdirectories --- internal/executor/generate.go | 164 +++++++++++++++++++--------------- internal/executor/new.go | 3 +- 2 files changed, 92 insertions(+), 75 deletions(-) diff --git a/internal/executor/generate.go b/internal/executor/generate.go index 7b1cce2..dd83c30 100644 --- a/internal/executor/generate.go +++ b/internal/executor/generate.go @@ -8,8 +8,11 @@ import ( "log" "os" "path/filepath" + "strings" ) +var subDirectories = []string{"lib", "cmd", "internal", "model"} + func Generate(root string) error { moduleName, err := check.GetModuleName(root) if err != nil { @@ -27,105 +30,120 @@ func Generate(root string) error { return err } - if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { - if info.IsDir() { - return nil - } + for _, subDir := range subDirectories { + if err := filepath.Walk(filepath.Join(root, subDir), func(path string, info os.FileInfo, err error) error { + if info.IsDir() { + return nil + } - relativePath := filepath.ToSlash(path)[len(filepath.ToSlash(root))+1:] - prevGenTime, ok := cc.Get(relativePath) - if ok && prevGenTime >= info.ModTime().Unix() { - log.Printf("skip: %s", relativePath) - return nil - } + if strings.HasPrefix(info.Name(), ".") { + return nil + } - switch filepath.Ext(path) { - case ".go": - pkg, err := parser.ParseFile(path) - if err != nil { - return err + sp := strings.Split(filepath.ToSlash(path), "/") + for _, s := range sp { + if strings.HasPrefix(s, ".") { + return nil + } } - beans, err := check.HasBean(pkg) - if err != nil { - return err + relativePath := filepath.ToSlash(path)[len(filepath.ToSlash(root))+1:] + prevGenTime, ok := cc.Get(relativePath) + if ok && prevGenTime >= info.ModTime().Unix() { + log.Printf("skip: %s", relativePath) + return nil } - if len(beans) > 0 { - if err := generate.Bean(path, beans); err != nil { + log.Println("check:", relativePath) + switch filepath.Ext(path) { + case ".go": + pkg, err := parser.ParseFile(path) + if err != nil { return err } - log.Printf("generate bean: %s", relativePath) - } - parameters, err := check.HasOptionalParameter(pkg) - if err != nil { - return err - } + beans, err := check.HasBean(pkg) + if err != nil { + return err + } - if len(parameters) > 0 { - if err := generate.OptionalParameter(path, parameters); err != nil { + if len(beans) > 0 { + if err := generate.Bean(path, beans); err != nil { + return err + } + log.Printf("generate bean: %s", relativePath) + } + + parameters, err := check.HasOptionalParameter(pkg) + if err != nil { return err } - log.Printf("generate parameter: %s", relativePath) - } - pools, err := check.HasPool(pkg) - if err != nil { - return err - } + if len(parameters) > 0 { + if err := generate.OptionalParameter(path, parameters); err != nil { + return err + } + log.Printf("generate parameter: %s", relativePath) + } - if len(pools) > 0 { - if err := generate.Pool(path, pools); err != nil { + pools, err := check.HasPool(pkg) + if err != nil { return err } - log.Printf("generate pool: %s", relativePath) - } - optionals, err := check.HasOptional(pkg) - if err != nil { - return err - } + if len(pools) > 0 { + if err := generate.Pool(path, pools); err != nil { + return err + } + log.Printf("generate pool: %s", relativePath) + } - if len(optionals) > 0 { - if err := generate.Option(path, optionals); err != nil { + optionals, err := check.HasOptional(pkg) + if err != nil { return err } - log.Printf("generate optional: %s", relativePath) - } - case ".json": - if err := generate.ConvertJson(path); err != nil { - return err - } - log.Printf("generate json: %s", relativePath) - case ".yml": - fallthrough - case ".yaml": - if err := generate.ConvertYaml(path); err != nil { - return err - } - log.Printf("generate yaml: %s", relativePath) - case ".proto": - if err := generate.BuildProtobuf(root, path); err != nil { - return err + + if len(optionals) > 0 { + if err := generate.Option(path, optionals); err != nil { + return err + } + log.Printf("generate optional: %s", relativePath) + } + case ".json": + if err := generate.ConvertJson(path); err != nil { + return err + } + log.Printf("generate json: %s", relativePath) + case ".yml": + fallthrough + case ".yaml": + if err := generate.ConvertYaml(path); err != nil { + return err + } + log.Printf("generate yaml: %s", relativePath) + case ".proto": + if err := generate.BuildProtobuf(root, path); err != nil { + return err + } + log.Printf("generate protobuf/grpc: %s", relativePath) + case ".fbs": + if err := generate.BuildFlatbuffers(root, path); err != nil { + return err + } + log.Printf("generate flatbuffers: %s", relativePath) + default: + return nil } - log.Printf("generate protobuf/grpc: %s", relativePath) - case ".fbs": - if err := generate.BuildFlatbuffers(root, path); err != nil { + + log.Println("update:", relativePath) + if err := cc.Set(relativePath, info.ModTime().Unix()); err != nil { return err } - log.Printf("generate flatbuffers: %s", relativePath) - default: - return nil - } - if err := cc.Set(relativePath, info.ModTime().Unix()); err != nil { + return nil + }); err != nil { return err } - - return nil - }); err != nil { - return err } return nil diff --git a/internal/executor/new.go b/internal/executor/new.go index 2a279b4..da60131 100644 --- a/internal/executor/new.go +++ b/internal/executor/new.go @@ -57,8 +57,7 @@ func New(root string, moduleName string, kind int) error { return err } - subDirs := []string{"lib", "cmd", "internal", "model"} - for _, subDir := range subDirs { + for _, subDir := range subDirectories { if err := generate.MakeDocGo(filepath.Join(root, subDir)); err != nil { return err }