mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
refactor(cmd/gf): improve code structure for readability and maintainability (#4771)
Refactor the code structure to enhance readability and maintainability without altering functionality. fixed #4750 #4757 #4767 另外使用`gf init -r github.com/gogf/template-single my-project`方式没有问题 合并后v2.10.2生效
This commit is contained in:
1
Makefile
1
Makefile
@ -34,6 +34,7 @@ branch:
|
||||
version:
|
||||
@set -e; \
|
||||
newVersion=$(to); \
|
||||
$(MAKE) -C cmd/gf pack; \
|
||||
./.make_version.sh ./ $$newVersion; \
|
||||
echo "make version to=$(to) done"
|
||||
|
||||
|
||||
@ -18,9 +18,6 @@ pack.template-mono:
|
||||
@cd temp && gf pack template-mono ../internal/packed/template-mono.go -n=packed -y
|
||||
@rm -fr temp
|
||||
|
||||
# Note:
|
||||
# command `sed` only works on MacOS.
|
||||
# use `grep -irl 'template-single' temp| xargs sed -i'' -e 's/template-single/template-mono-app/g'` on other platforms.
|
||||
pack.template-mono-app:
|
||||
@rm -fr temp
|
||||
@mkdir temp || exit 0
|
||||
@ -31,6 +28,6 @@ pack.template-mono-app:
|
||||
@rm -fr temp/template-mono-app/.gitignore
|
||||
@rm -fr temp/template-mono-app/go.mod
|
||||
@rm -fr temp/template-mono-app/go.sum
|
||||
@grep -irl 'template-single' temp| xargs sed -i '' -e 's/template-single/template-mono-app/g'
|
||||
@grep -irl 'template-single' temp| xargs perl -pi -e 's/template-single/template-mono-app/g'
|
||||
@cd temp && gf pack template-mono-app ../internal/packed/template-mono-app.go -n=packed -y
|
||||
@rm -fr temp
|
||||
@ -232,14 +232,17 @@ func (c cInit) initFromBuiltin(ctx context.Context, in cInitInput) (out *cInitOu
|
||||
return content
|
||||
}
|
||||
}
|
||||
mlog.Debugf("replace %s %s to %s", path, cInitRepoPrefix+templateRepoName, in.Module)
|
||||
return gstr.Replace(gfile.GetContents(path), cInitRepoPrefix+templateRepoName, in.Module)
|
||||
}, in.Name, "*", true)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Format the generated Go files.
|
||||
utils.GoFmt(in.Name)
|
||||
// Format the generated Go files using go/format (not goimports).
|
||||
// utils.GoFmt uses imports.Process which may remove local import paths that cannot
|
||||
// be resolved in the GOPATH or module cache right after generation (e.g. "myapp/api/hello/v1").
|
||||
geninit.FormatGoFiles(in.Name)
|
||||
|
||||
// Update the GoFrame version.
|
||||
if in.Update {
|
||||
|
||||
@ -85,7 +85,7 @@ func generateProject(ctx context.Context, srcPath, name, oldModule, newModule st
|
||||
// 6. Format the generated Go files using go/format (not imports.Process)
|
||||
// Note: We use formatGoFiles instead of utils.GoFmt because imports.Process
|
||||
// may incorrectly "fix" local import paths by replacing them with cached module paths.
|
||||
formatGoFiles(dstPath)
|
||||
FormatGoFiles(dstPath)
|
||||
|
||||
mlog.Print("Project generated successfully!")
|
||||
return nil
|
||||
@ -115,10 +115,10 @@ func upgradeDependencies(ctx context.Context, projectDir string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// formatGoFiles formats all Go files in the directory using go/format.
|
||||
// FormatGoFiles formats all Go files in the directory using go/format.
|
||||
// Unlike imports.Process, this only formats code without modifying imports,
|
||||
// which prevents incorrect "fixing" of local import paths.
|
||||
func formatGoFiles(dir string) {
|
||||
func FormatGoFiles(dir string) {
|
||||
files, err := findGoFiles(dir)
|
||||
if err != nil {
|
||||
mlog.Printf("Failed to find Go files for formatting: %v", err)
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user