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 9, 2025
1 parent 48fcf42 commit eac7ee6
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 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,28 @@ func (c *customGenerator) loopGenerate(tpl *Template) error {
if util.Exists(filePath) && updateType(tpl.UpdateBehavior.Type) == skip {
continue
}
// using scanDir to substitute the default directory
if tpl.UpdateBehavior.ScanDirRecursively {
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
filePath = path
return nil
}
return nil
}); err != nil {
return err
}
if found && updateType(tpl.UpdateBehavior.Type) == skip {
continue
}
}
task := &Task{
Name: path.Base(renderPath),
Path: filePath,
Expand Down Expand Up @@ -132,6 +157,28 @@ func (c *customGenerator) commonGenerate(tpl *Template) error {
log.Infof("skip generate file %s", tpl.Path)
return nil
}
// using scanDir to substitute the default directory
if tpl.UpdateBehavior.ScanDirRecursively {
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
filePath = path
return nil
}
return nil
}); err != nil {
return err
}
if found && updateType(tpl.UpdateBehavior.Type) == skip {
return nil
}
}
var f *File
if update && updateType(tpl.UpdateBehavior.Type) == incrementalUpdate {
cc := &commonCompleter{
Expand Down

0 comments on commit eac7ee6

Please sign in to comment.