Skip to content

Commit

Permalink
online: support scan_dir for custom template
Browse files Browse the repository at this point in the history
  • Loading branch information
DMwangnima committed Jan 21, 2025
1 parent 48fcf42 commit 347186b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tool/internal_pkg/generator/custom_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package generator

import (
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -47,6 +48,8 @@ type Update struct {
AppendTpl string `yaml:"append_tpl,omitempty"`
// Append import template. Use it to render import content to append.
ImportTpl []string `yaml:"import_tpl,omitempty"`
// Using this field to scan the current directory and all the subdirecotries recursively.
ScanDirRecursively bool `yaml:"scan_dir_recursively,omitempty"`
}

type Template struct {
Expand Down Expand Up @@ -97,6 +100,18 @@ func (c *customGenerator) loopGenerate(tpl *Template) error {
if util.Exists(filePath) && updateType(tpl.UpdateBehavior.Type) == skip {
continue
}
// using scanDir to substitute the default directory
scanPath, scanErr := scanDir(filePath, tpl.UpdateBehavior)
if scanErr != nil {
return scanErr
}
if scanPath != "" {
if updateType(tpl.UpdateBehavior.Type) == skip {
continue
}
filePath = scanPath
}

task := &Task{
Name: path.Base(renderPath),
Path: filePath,
Expand All @@ -111,6 +126,36 @@ func (c *customGenerator) loopGenerate(tpl *Template) error {
return nil
}

// scanDir scans the directory of the filePath recursively and returns the path of the file with the same name.
func scanDir(filePath string, behavior *Update) (res string, err error) {
if behavior == nil || !behavior.ScanDirRecursively {
return "", nil
}
fileName := filepath.Base(filePath)
dirPath := filepath.Dir(filePath)
var found bool
if err := filepath.Walk(dirPath, func(path string, info fs.FileInfo, walkErr error) error {
if walkErr != nil {
return walkErr
}
if !info.IsDir() && info.Name() == fileName {
found = true
res = path
return nil
}
return nil
}); err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", err
}
if found {
return res, nil
}
return "", nil
}

func (c *customGenerator) commonGenerate(tpl *Template) error {
// Use all services including base service.
tmp := c.pkg.Methods
Expand All @@ -132,6 +177,17 @@ func (c *customGenerator) commonGenerate(tpl *Template) error {
log.Infof("skip generate file %s", tpl.Path)
return nil
}
// using scanDir to substitute the default directory
scanPath, scanErr := scanDir(filePath, tpl.UpdateBehavior)
if scanErr != nil {
return scanErr
}
if scanPath != "" {
if updateType(tpl.UpdateBehavior.Type) == skip {
return nil
}
filePath = scanPath
}
var f *File
if update && updateType(tpl.UpdateBehavior.Type) == incrementalUpdate {
cc := &commonCompleter{
Expand Down
29 changes: 29 additions & 0 deletions tool/internal_pkg/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,32 @@ func Test_needCallOpt(t *testing.T) {
test.Assert(t, needCallOpt(pkg))
})
}

func Test_scanDir(t *testing.T) {
testcases := []struct {
filePath string
update *Update
expectPath string
expectErr error
}{
{
filePath: "./test_dir/test1.txt",
update: &Update{ScanDirRecursively: true, Type: "skip"},
expectPath: "test_dir/test_dir1/test1.txt",
},
{
filePath: "./test_dir/test2.txt",
update: &Update{ScanDirRecursively: true, Type: "skip"},
},
{
filePath: "./non_exist_dir/test3.txt",
update: &Update{ScanDirRecursively: true, Type: "skip"},
},
}

for _, tc := range testcases {
res, err := scanDir(tc.filePath, tc.update)
test.Assert(t, err == tc.expectErr, err)
test.Assert(t, res == tc.expectPath, res)
}
}
Empty file.
Empty file.

0 comments on commit 347186b

Please sign in to comment.