Skip to content

Commit

Permalink
feat(template): support regix match for template build (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
liushunkkk authored Dec 3, 2024
1 parent 79ad608 commit 010d8a9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/jzero-io/desc v0.0.1
github.com/jzero-io/jzero-contrib v0.14.1-0.20241202072226-245e83cee9d4
github.com/mitchellh/go-homedir v1.1.0
github.com/moby/patternmatcher v0.6.0
github.com/pkg/errors v0.9.1
github.com/rinchsan/gosimports v0.3.8
github.com/samber/lo v1.47.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkVGiPk=
github.com/moby/patternmatcher v0.6.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg=
Expand Down
8 changes: 7 additions & 1 deletion internal/template/templatebuild/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"strings"

"github.com/moby/patternmatcher"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"golang.org/x/mod/modfile"

Expand Down Expand Up @@ -58,8 +59,13 @@ func build(tc config.TemplateConfig, dirname string, mod *modfile.File) error {
return err
}

pm, err := patternmatcher.New(tc.Build.Ignore)
if err != nil {
return err
}

for _, file := range dir {
if filter(dirname, file.Name(), tc.Build.Ignore) {
if filter(dirname, file.Name(), pm) {
continue
}
if file.IsDir() {
Expand Down
27 changes: 20 additions & 7 deletions internal/template/templatebuild/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,34 @@ package templatebuild
import (
"os"
"path/filepath"

"github.com/moby/patternmatcher"
)

var IgnoreDirs = []string{".git", ".idea", ".vscode", ".DS_Store", "node_modules"}

func filter(dir, name string, ignoreDirs []string) bool {
func filter(dir, name string, matcher *patternmatcher.PatternMatcher) bool {
pwd, err := os.Getwd()
if err != nil {
return false
return true
}
target := filepath.Join(dir, name)
for _, id := range ignoreDirs {
ignore := filepath.Join(pwd, id)
if target == ignore {
return true
}
relFilePath, err := filepath.Rel(pwd, target)
if err != nil {
return true
}
skip, err := filepathMatches(matcher, relFilePath)
if err != nil || skip {
return true
}
return false
}

func filepathMatches(matcher *patternmatcher.PatternMatcher, file string) (bool, error) {
file = filepath.Clean(file)
if file == "." {
// Don't let them exclude everything, kind of silly.
return false, nil
}
return matcher.MatchesOrParentMatches(file)
}

0 comments on commit 010d8a9

Please sign in to comment.