Skip to content

Commit

Permalink
make and apply subdirectories
Browse files Browse the repository at this point in the history
  • Loading branch information
snowmerak committed May 22, 2023
1 parent 2389c0d commit aa332d9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 75 deletions.
164 changes: 91 additions & 73 deletions internal/executor/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions internal/executor/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit aa332d9

Please sign in to comment.