Skip to content

Commit

Permalink
feat(cutter): 支持基于.gitignore过滤文件
Browse files Browse the repository at this point in the history
  • Loading branch information
morehao committed Jul 26, 2024
1 parent d3198e1 commit 79310c3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# 功能特性
- 在模板项目根路径下执行命令可创建新的Go项目
- 创建新项目时基于.gitignore文件过滤创建的文件
- 自动替换 import 路径
- 自动更新 go.mod 文件中的模块名称
- 自动删除 .git 目录
Expand Down
47 changes: 41 additions & 6 deletions cmd/cutter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"
"strings"

ignore "github.com/morehao/go-gitignore"
"golang.org/x/mod/modfile"
)

Expand Down Expand Up @@ -42,8 +43,15 @@ func cutter(newProjectPath string) error {
return fmt.Errorf("create new project directory: %w", err)
}

// 读取.gitignore文件
gitignorePath := filepath.Join(currentDir, ".gitignore")
gitignore, err := readGitignore(gitignorePath)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("read .gitignore file fail, err: %w", err)
}

// 复制模板项目到新项目目录,并替换import路径
if err := copyAndReplace(currentDir, newProjectPath, templateName, newProjectName); err != nil {
if err := copyAndReplace(currentDir, newProjectPath, templateName, newProjectName, gitignore); err != nil {
return fmt.Errorf("copy and replace fail, err: %w", err)
}
if err := removeGitDir(newProjectPath); err != nil {
Expand All @@ -58,21 +66,48 @@ func isGoProject(path string) bool {
return !os.IsNotExist(err)
}

// readGitignore 读取.gitignore文件并返回IgnoreParser
func readGitignore(filename string) (*ignore.GitIgnore, error) {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return ignore.CompileIgnoreLines(), nil
}
ig, err := ignore.CompileIgnoreFile(filename)
if err != nil {
return nil, err
}
return ig, nil
}

// copyAndReplace copy指定目录,并替换import路径
func copyAndReplace(srcDir, dstDir, oldName, newName string) error {
err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
func copyAndReplace(srcDir, dstDir, oldName, newName string, ig *ignore.GitIgnore) error {
err := filepath.Walk(srcDir, func(path string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}

// 获取相对于源目录的路径
relativePath, err := filepath.Rel(srcDir, path)
if err != nil {
return err
}

// 检查是否排除
if ig.MatchesPath(relativePath) {
fmt.Println("Excluding:", path)
if fileInfo.IsDir() {
return filepath.SkipDir
}
return nil
}

// 创建目标目录
targetPath := strings.Replace(path, srcDir, dstDir, 1)
if info.IsDir() {
return os.MkdirAll(targetPath, info.Mode())
if fileInfo.IsDir() {
return os.MkdirAll(targetPath, fileInfo.Mode())
}

// 复制文件并替换 import 路径
if strings.HasSuffix(info.Name(), ".go") {
if strings.HasSuffix(fileInfo.Name(), ".go") {
return copyAndReplaceGoFile(path, targetPath, oldName, newName)
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/morehao/go-cutter
go 1.21

require (
github.com/morehao/go-gitignore v0.0.0-20240726143401-bffa9ffa53e3
github.com/spf13/cobra v1.8.1
golang.org/x/mod v0.19.0
)
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/morehao/go-gitignore v0.0.0-20240726143401-bffa9ffa53e3 h1:ANaXxExaKWL963++WBfC/p0jEPb0ZUWAiti09zpqzD8=
github.com/morehao/go-gitignore v0.0.0-20240726143401-bffa9ffa53e3/go.mod h1:gThei7O/YvKiDN0wDd9KtH5wB/I8/TmPbRrAHESNwm4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 79310c3

Please sign in to comment.