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:
码到功成
2026-05-14 15:34:43 +08:00
committed by GitHub
parent d44e082ff5
commit 72733e0bad
7 changed files with 13 additions and 12 deletions

View File

@ -34,6 +34,7 @@ branch:
version: version:
@set -e; \ @set -e; \
newVersion=$(to); \ newVersion=$(to); \
$(MAKE) -C cmd/gf pack; \
./.make_version.sh ./ $$newVersion; \ ./.make_version.sh ./ $$newVersion; \
echo "make version to=$(to) done" echo "make version to=$(to) done"

View File

@ -18,9 +18,6 @@ pack.template-mono:
@cd temp && gf pack template-mono ../internal/packed/template-mono.go -n=packed -y @cd temp && gf pack template-mono ../internal/packed/template-mono.go -n=packed -y
@rm -fr temp @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: pack.template-mono-app:
@rm -fr temp @rm -fr temp
@mkdir temp || exit 0 @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/.gitignore
@rm -fr temp/template-mono-app/go.mod @rm -fr temp/template-mono-app/go.mod
@rm -fr temp/template-mono-app/go.sum @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 @cd temp && gf pack template-mono-app ../internal/packed/template-mono-app.go -n=packed -y
@rm -fr temp @rm -fr temp

View File

@ -232,14 +232,17 @@ func (c cInit) initFromBuiltin(ctx context.Context, in cInitInput) (out *cInitOu
return content return content
} }
} }
mlog.Debugf("replace %s %s to %s", path, cInitRepoPrefix+templateRepoName, in.Module)
return gstr.Replace(gfile.GetContents(path), cInitRepoPrefix+templateRepoName, in.Module) return gstr.Replace(gfile.GetContents(path), cInitRepoPrefix+templateRepoName, in.Module)
}, in.Name, "*", true) }, in.Name, "*", true)
if err != nil { if err != nil {
return return
} }
// Format the generated Go files. // Format the generated Go files using go/format (not goimports).
utils.GoFmt(in.Name) // 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. // Update the GoFrame version.
if in.Update { if in.Update {

View File

@ -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) // 6. Format the generated Go files using go/format (not imports.Process)
// Note: We use formatGoFiles instead of utils.GoFmt because 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. // may incorrectly "fix" local import paths by replacing them with cached module paths.
formatGoFiles(dstPath) FormatGoFiles(dstPath)
mlog.Print("Project generated successfully!") mlog.Print("Project generated successfully!")
return nil return nil
@ -115,10 +115,10 @@ func upgradeDependencies(ctx context.Context, projectDir string) error {
return nil 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, // Unlike imports.Process, this only formats code without modifying imports,
// which prevents incorrect "fixing" of local import paths. // which prevents incorrect "fixing" of local import paths.
func formatGoFiles(dir string) { func FormatGoFiles(dir string) {
files, err := findGoFiles(dir) files, err := findGoFiles(dir)
if err != nil { if err != nil {
mlog.Printf("Failed to find Go files for formatting: %v", err) 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