Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Sep 26, 2024
1 parent a1a6ae3 commit c5b0049
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bin/
cmd/gf/main
cmd/gf/gf
temp/
example/log
go.work
go.work.sum
!cmd/gf/go.work
Expand Down
2 changes: 1 addition & 1 deletion cmd/gf/internal/cmd/genservice/genservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func (c CGenService) checkAndUpdateMain(srcFolder string) (err error) {
var (
logicPackageName = gstr.ToLower(gfile.Basename(srcFolder))
logicFilePath = gfile.Join(srcFolder, logicPackageName+".go")
importPath = utils.GetImportPath(logicFilePath)
importPath = utils.GetImportPath(srcFolder)
importStr = fmt.Sprintf(`_ "%s"`, importPath)
mainFilePath = gfile.Join(gfile.Dir(gfile.Dir(gfile.Dir(logicFilePath))), "main.go")
mainFileContent = gfile.GetContents(mainFilePath)
Expand Down
15 changes: 6 additions & 9 deletions cmd/gf/internal/utility/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,24 @@ func ReplaceGeneratedContentGFV2(folderPath string) (err error) {
}, folderPath, "*.go", true)
}

// GetImportPath calculates and returns the golang import path for given `filePath`.
// GetImportPath calculates and returns the golang import path for given `dirPath`.
// Note that it needs a `go.mod` in current working directory or parent directories to detect the path.
func GetImportPath(filePath string) string {
func GetImportPath(dirPath string) string {
// If `filePath` does not exist, create it firstly to find the import path.
var realPath = gfile.RealPath(filePath)
var realPath = gfile.RealPath(dirPath)
if realPath == "" {
realPath = gfile.RealPath(filePath)
_ = gfile.Mkdir(dirPath)
realPath = gfile.RealPath(dirPath)
}

var (
newDir = gfile.Dir(realPath)
oldDir string
suffix string
suffix = gfile.Basename(dirPath)
goModName = "go.mod"
goModPath string
importPath string
)

if gfile.IsDir(filePath) {
suffix = gfile.Basename(filePath)
}
for {
goModPath = gfile.Join(newDir, goModName)
if gfile.Exists(goModPath) {
Expand Down

0 comments on commit c5b0049

Please sign in to comment.