mirror of
https://gitee.com/johng/gf.git
synced 2024-11-29 18:57:44 +08:00
improve command gen ctrl
and gen service
(#2711)
This commit is contained in:
parent
d6433de4a6
commit
c5c2938380
@ -3,13 +3,13 @@ module github.com/gogf/gf/cmd/gf/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.4.4
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
github.com/minio/selfupdate v0.6.0
|
||||
github.com/olekukonko/tablewriter v0.0.5
|
||||
golang.org/x/tools v0.7.0
|
||||
|
@ -33,6 +33,7 @@ gf gen ctrl
|
||||
CGenCtrlBriefSdkPath = `also generate SDK go files for api definitions to specified directory`
|
||||
CGenCtrlBriefSdkStdVersion = `use standard version prefix for generated sdk request path`
|
||||
CGenCtrlBriefSdkNoV1 = `do not add version suffix for interface module name if version is v1`
|
||||
CGenCtrlBriefClear = `auto delete generated and unimplemented controller go files if api definitions are missing`
|
||||
)
|
||||
|
||||
const (
|
||||
@ -56,6 +57,7 @@ func init() {
|
||||
`CGenCtrlBriefSdkPath`: CGenCtrlBriefSdkPath,
|
||||
`CGenCtrlBriefSdkStdVersion`: CGenCtrlBriefSdkStdVersion,
|
||||
`CGenCtrlBriefSdkNoV1`: CGenCtrlBriefSdkNoV1,
|
||||
`CGenCtrlBriefClear`: CGenCtrlBriefClear,
|
||||
})
|
||||
}
|
||||
|
||||
@ -69,13 +71,17 @@ type (
|
||||
SdkPath string `short:"k" name:"sdkPath" brief:"{CGenCtrlBriefSdkPath}"`
|
||||
SdkStdVersion bool `short:"v" name:"sdkStdVersion" brief:"{CGenCtrlBriefSdkStdVersion}" orphan:"true"`
|
||||
SdkNoV1 bool `short:"n" name:"sdkNoV1" brief:"{CGenCtrlBriefSdkNoV1}" orphan:"true"`
|
||||
Clear bool `short:"c" name:"clear" brief:"{CGenCtrlBriefClear}" orphan:"true"`
|
||||
}
|
||||
CGenCtrlOutput struct{}
|
||||
)
|
||||
|
||||
func (c CGenCtrl) Ctrl(ctx context.Context, in CGenCtrlInput) (out *CGenCtrlOutput, err error) {
|
||||
if in.WatchFile != "" {
|
||||
err = c.generateByWatchFile(in.WatchFile, in.SdkPath, in.SdkStdVersion, in.SdkNoV1)
|
||||
err = c.generateByWatchFile(
|
||||
in.WatchFile, in.SdkPath, in.SdkStdVersion, in.SdkNoV1, in.Clear,
|
||||
)
|
||||
mlog.Print(`done!`)
|
||||
return
|
||||
}
|
||||
|
||||
@ -96,7 +102,10 @@ func (c CGenCtrl) Ctrl(ctx context.Context, in CGenCtrlInput) (out *CGenCtrlOutp
|
||||
module = gfile.Basename(apiModuleFolderPath)
|
||||
dstModuleFolderPath = gfile.Join(in.DstFolder, module)
|
||||
)
|
||||
err = c.generateByModule(apiModuleFolderPath, dstModuleFolderPath, in.SdkPath, in.SdkStdVersion, in.SdkNoV1)
|
||||
err = c.generateByModule(
|
||||
apiModuleFolderPath, dstModuleFolderPath, in.SdkPath,
|
||||
in.SdkStdVersion, in.SdkNoV1, in.Clear,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -106,7 +115,7 @@ func (c CGenCtrl) Ctrl(ctx context.Context, in CGenCtrlInput) (out *CGenCtrlOutp
|
||||
return
|
||||
}
|
||||
|
||||
func (c CGenCtrl) generateByWatchFile(watchFile, sdkPath string, sdkStdVersion, sdkNoV1 bool) (err error) {
|
||||
func (c CGenCtrl) generateByWatchFile(watchFile, sdkPath string, sdkStdVersion, sdkNoV1, clear bool) (err error) {
|
||||
// File lock to avoid multiple processes.
|
||||
var (
|
||||
flockFilePath = gfile.Temp("gf.cli.gen.service.lock")
|
||||
@ -133,20 +142,26 @@ func (c CGenCtrl) generateByWatchFile(watchFile, sdkPath string, sdkStdVersion,
|
||||
return nil
|
||||
}
|
||||
// watch file should have api definitions.
|
||||
if !gregex.IsMatchString(PatternApiDefinition, gfile.GetContents(watchFile)) {
|
||||
return nil
|
||||
if gfile.Exists(watchFile) {
|
||||
if !gregex.IsMatchString(PatternApiDefinition, gfile.GetContents(watchFile)) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
projectRootPath = gfile.Dir(gfile.Dir(apiModuleFolderPath))
|
||||
module = gfile.Basename(apiModuleFolderPath)
|
||||
dstModuleFolderPath = gfile.Join(projectRootPath, "internal", "controller", module)
|
||||
)
|
||||
return c.generateByModule(apiModuleFolderPath, dstModuleFolderPath, sdkPath, sdkStdVersion, sdkNoV1)
|
||||
return c.generateByModule(
|
||||
apiModuleFolderPath, dstModuleFolderPath, sdkPath, sdkStdVersion, sdkNoV1, clear,
|
||||
)
|
||||
}
|
||||
|
||||
// parseApiModule parses certain api and generate associated go files by certain module, not all api modules.
|
||||
func (c CGenCtrl) generateByModule(
|
||||
apiModuleFolderPath, dstModuleFolderPath, sdkPath string, sdkStdVersion, sdkNoV1 bool,
|
||||
apiModuleFolderPath, dstModuleFolderPath, sdkPath string,
|
||||
sdkStdVersion, sdkNoV1, clear bool,
|
||||
) (err error) {
|
||||
// parse src and dst folder go files.
|
||||
apiItemsInSrc, err := c.getApiItemsInSrc(apiModuleFolderPath)
|
||||
@ -158,6 +173,12 @@ func (c CGenCtrl) generateByModule(
|
||||
return err
|
||||
}
|
||||
|
||||
// generate api interface go files.
|
||||
if err = newApiInterfaceGenerator().Generate(apiModuleFolderPath, apiItemsInSrc); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// generate controller go files.
|
||||
// api filtering for already implemented api controllers.
|
||||
var (
|
||||
alreadyImplementedCtrlSet = gset.NewStrSet()
|
||||
@ -172,13 +193,6 @@ func (c CGenCtrl) generateByModule(
|
||||
}
|
||||
toBeImplementedApiItems = append(toBeImplementedApiItems, item)
|
||||
}
|
||||
|
||||
// generate api interface go files.
|
||||
if err = newApiInterfaceGenerator().Generate(apiModuleFolderPath, apiItemsInSrc); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// generate controller go files.
|
||||
if len(toBeImplementedApiItems) > 0 {
|
||||
err = newControllerGenerator().Generate(dstModuleFolderPath, toBeImplementedApiItems)
|
||||
if err != nil {
|
||||
@ -186,12 +200,34 @@ func (c CGenCtrl) generateByModule(
|
||||
}
|
||||
}
|
||||
|
||||
// delete unimplemented controllers if api definitions are missing.
|
||||
if clear {
|
||||
var (
|
||||
apiDefinitionSet = gset.NewStrSet()
|
||||
extraApiItemsInCtrl = make([]apiItem, 0)
|
||||
)
|
||||
for _, item := range apiItemsInSrc {
|
||||
apiDefinitionSet.Add(item.String())
|
||||
}
|
||||
for _, item := range apiItemsInDst {
|
||||
if apiDefinitionSet.Contains(item.String()) {
|
||||
continue
|
||||
}
|
||||
extraApiItemsInCtrl = append(extraApiItemsInCtrl, item)
|
||||
}
|
||||
if len(extraApiItemsInCtrl) > 0 {
|
||||
err = newControllerClearer().Clear(dstModuleFolderPath, extraApiItemsInCtrl)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// generate sdk go files.
|
||||
if sdkPath != "" {
|
||||
if err = newApiSdkGenerator().Generate(apiItemsInSrc, sdkPath, sdkStdVersion, sdkNoV1); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
57
cmd/gf/internal/cmd/genctrl/genctrl_generate_ctrl_clear.go
Normal file
57
cmd/gf/internal/cmd/genctrl/genctrl_generate_ctrl_clear.go
Normal file
@ -0,0 +1,57 @@
|
||||
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package genctrl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
type controllerClearer struct{}
|
||||
|
||||
func newControllerClearer() *controllerClearer {
|
||||
return &controllerClearer{}
|
||||
}
|
||||
|
||||
func (c *controllerClearer) Clear(dstModuleFolderPath string, extraApiItemsInCtrl []apiItem) (err error) {
|
||||
for _, item := range extraApiItemsInCtrl {
|
||||
if err = c.doClear(dstModuleFolderPath, item); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *controllerClearer) doClear(dstModuleFolderPath string, item apiItem) (err error) {
|
||||
var (
|
||||
methodNameSnake = gstr.CaseSnake(item.MethodName)
|
||||
methodFilePath = gfile.Join(dstModuleFolderPath, fmt.Sprintf(
|
||||
`%s_%s_%s.go`, item.Module, item.Version, methodNameSnake,
|
||||
))
|
||||
fileContent = gstr.Trim(gfile.GetContents(methodFilePath))
|
||||
)
|
||||
match, err := gregex.MatchString(`.+?Req.+?Res.+?{([\s\S]+?)}`, fileContent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(match) > 1 {
|
||||
implements := gstr.Trim(match[1])
|
||||
// One line.
|
||||
if !gstr.Contains(implements, "\n") && gstr.Contains(implements, `CodeNotImplemented`) {
|
||||
mlog.Printf(
|
||||
`remove unimplemented and of no api definitions controller file: %s`,
|
||||
methodFilePath,
|
||||
)
|
||||
err = gfile.Remove(methodFilePath)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
@ -16,7 +16,6 @@ import (
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gtag"
|
||||
|
||||
@ -180,7 +179,6 @@ type (
|
||||
DB gdb.DB
|
||||
TableNames []string
|
||||
NewTableNames []string
|
||||
ModName string // Module name of current golang project, which is used for import purpose.
|
||||
}
|
||||
)
|
||||
|
||||
@ -204,9 +202,8 @@ func (c CGenDao) Dao(ctx context.Context, in CGenDaoInput) (out *CGenDaoOutput,
|
||||
// doGenDaoForArray implements the "gen dao" command for configuration array.
|
||||
func doGenDaoForArray(ctx context.Context, index int, in CGenDaoInput) {
|
||||
var (
|
||||
err error
|
||||
db gdb.DB
|
||||
modName string // Go module name, eg: github.com/gogf/gf.
|
||||
err error
|
||||
db gdb.DB
|
||||
)
|
||||
if index >= 0 {
|
||||
err = g.Cfg().MustGet(
|
||||
@ -221,21 +218,6 @@ func doGenDaoForArray(ctx context.Context, index int, in CGenDaoInput) {
|
||||
mlog.Fatalf(`path "%s" does not exist`, in.Path)
|
||||
}
|
||||
removePrefixArray := gstr.SplitAndTrim(in.RemovePrefix, ",")
|
||||
if in.ImportPrefix == "" {
|
||||
mlog.Debug(`import prefix is empty, trying calculating the import package path using go.mod`)
|
||||
if !gfile.Exists("go.mod") {
|
||||
mlog.Fatal("go.mod does not exist in current working directory")
|
||||
}
|
||||
var (
|
||||
goModContent = gfile.GetContents("go.mod")
|
||||
match, _ = gregex.MatchString(`^module\s+(.+)\s*`, goModContent)
|
||||
)
|
||||
if len(match) > 1 {
|
||||
modName = gstr.Trim(match[1])
|
||||
} else {
|
||||
mlog.Fatal("module name does not found in go.mod")
|
||||
}
|
||||
}
|
||||
|
||||
// It uses user passed database configuration.
|
||||
if in.Link != "" {
|
||||
@ -287,7 +269,6 @@ func doGenDaoForArray(ctx context.Context, index int, in CGenDaoInput) {
|
||||
DB: db,
|
||||
TableNames: tableNames,
|
||||
NewTableNames: newTableNames,
|
||||
ModName: modName,
|
||||
})
|
||||
// Do.
|
||||
generateDo(ctx, CGenDaoInternalInput{
|
||||
@ -295,7 +276,6 @@ func doGenDaoForArray(ctx context.Context, index int, in CGenDaoInput) {
|
||||
DB: db,
|
||||
TableNames: tableNames,
|
||||
NewTableNames: newTableNames,
|
||||
ModName: modName,
|
||||
})
|
||||
// Entity.
|
||||
generateEntity(ctx, CGenDaoInternalInput{
|
||||
@ -303,7 +283,6 @@ func doGenDaoForArray(ctx context.Context, index int, in CGenDaoInput) {
|
||||
DB: db,
|
||||
TableNames: tableNames,
|
||||
NewTableNames: newTableNames,
|
||||
ModName: modName,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -65,7 +65,7 @@ func generateDaoSingle(ctx context.Context, in generateDaoSingleInput) {
|
||||
importPrefix = in.ImportPrefix
|
||||
)
|
||||
if importPrefix == "" {
|
||||
importPrefix = utils.GetImportPath(in.DaoPath)
|
||||
importPrefix = utils.GetImportPath(gfile.Join(in.Path, in.DaoPath))
|
||||
} else {
|
||||
importPrefix = gstr.Join(g.SliceStr{importPrefix, in.DaoPath}, "/")
|
||||
}
|
||||
|
@ -11,10 +11,10 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
"github.com/gogf/gf/v2/container/gmap"
|
||||
"github.com/gogf/gf/v2/container/gset"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/os/gproc"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
@ -50,7 +50,7 @@ destination file name storing automatically generated go files, cases are as fol
|
||||
`
|
||||
CGenServiceBriefWatchFile = `used in file watcher, it re-generates all service go files only if given file is under srcFolder`
|
||||
CGenServiceBriefStPattern = `regular expression matching struct name for generating service. default: ^s([A-Z]\\\\w+)$`
|
||||
CGenServiceBriefPackages = `produce go files only for given source packages`
|
||||
CGenServiceBriefPackages = `produce go files only for given source packages(source folders)`
|
||||
CGenServiceBriefImportPrefix = `custom import prefix to calculate import path for generated importing go file of logic`
|
||||
CGenServiceBriefClear = `delete all generated go files that are not used any further`
|
||||
)
|
||||
@ -93,21 +93,6 @@ const (
|
||||
)
|
||||
|
||||
func (c CGenService) Service(ctx context.Context, in CGenServiceInput) (out *CGenServiceOutput, err error) {
|
||||
// File lock to avoid multiple processes.
|
||||
var (
|
||||
flockFilePath = gfile.Temp("gf.cli.gen.service.lock")
|
||||
flockContent = gfile.GetContents(flockFilePath)
|
||||
)
|
||||
if flockContent != "" {
|
||||
if gtime.Timestamp()-gconv.Int64(flockContent) < genServiceFileLockSeconds {
|
||||
// If another "gen service" process is running, it just exits.
|
||||
mlog.Debug(`another "gen service" process is running, exit`)
|
||||
return
|
||||
}
|
||||
}
|
||||
defer gfile.Remove(flockFilePath)
|
||||
_ = gfile.PutContents(flockFilePath, gtime.TimestampStr())
|
||||
|
||||
in.SrcFolder = gstr.TrimRight(in.SrcFolder, `\/`)
|
||||
in.SrcFolder = gstr.Replace(in.SrcFolder, "\\", "/")
|
||||
in.WatchFile = gstr.TrimRight(in.WatchFile, `\/`)
|
||||
@ -115,6 +100,21 @@ func (c CGenService) Service(ctx context.Context, in CGenServiceInput) (out *CGe
|
||||
|
||||
// Watch file handling.
|
||||
if in.WatchFile != "" {
|
||||
// File lock to avoid multiple processes.
|
||||
var (
|
||||
flockFilePath = gfile.Temp("gf.cli.gen.service.lock")
|
||||
flockContent = gfile.GetContents(flockFilePath)
|
||||
)
|
||||
if flockContent != "" {
|
||||
if gtime.Timestamp()-gconv.Int64(flockContent) < genServiceFileLockSeconds {
|
||||
// If another "gen service" process is running, it just exits.
|
||||
mlog.Debug(`another "gen service" process is running, exit`)
|
||||
return
|
||||
}
|
||||
}
|
||||
defer gfile.Remove(flockFilePath)
|
||||
_ = gfile.PutContents(flockFilePath, gtime.TimestampStr())
|
||||
|
||||
// It works only if given WatchFile is in SrcFolder.
|
||||
var (
|
||||
watchFileDir = gfile.Dir(in.WatchFile)
|
||||
@ -131,13 +131,10 @@ func (c CGenService) Service(ctx context.Context, in CGenServiceInput) (out *CGe
|
||||
mlog.Fatalf(`%+v`, err)
|
||||
}
|
||||
mlog.Debug("Chdir:", newWorkingDir)
|
||||
_ = gfile.Remove(flockFilePath)
|
||||
var command = fmt.Sprintf(
|
||||
`%s gen service -packages=%s`,
|
||||
gfile.SelfName(), gfile.Basename(watchFileDir),
|
||||
)
|
||||
err = gproc.ShellRun(ctx, command)
|
||||
return
|
||||
|
||||
in.WatchFile = ""
|
||||
in.Packages = []string{gfile.Basename(watchFileDir)}
|
||||
return c.Service(ctx, in)
|
||||
}
|
||||
|
||||
if !gfile.Exists(in.SrcFolder) {
|
||||
@ -173,34 +170,74 @@ func (c CGenService) Service(ctx context.Context, in CGenServiceInput) (out *CGe
|
||||
if len(files) == 0 {
|
||||
continue
|
||||
}
|
||||
// Parse single logic package folder.
|
||||
var (
|
||||
// StructName => FunctionDefinitions
|
||||
srcPkgInterfaceMap = make(map[string]*garray.StrArray)
|
||||
srcImportedPackages = garray.NewSortedStrArray().SetUnique(true)
|
||||
srcPackageName = gfile.Basename(srcFolderPath)
|
||||
ok bool
|
||||
dstFilePath = gfile.Join(in.DstFolder,
|
||||
srcPkgInterfaceMap = make(map[string]*garray.StrArray)
|
||||
srcImportedPackages = garray.NewSortedStrArray().SetUnique(true)
|
||||
importPackageItemAliasMap = gmap.NewStrStrMap() // for conflict imports check. alias => path
|
||||
srcPackageName = gfile.Basename(srcFolderPath)
|
||||
aliasIndex = 0
|
||||
ok bool
|
||||
dstFilePath = gfile.Join(in.DstFolder,
|
||||
c.getDstFileNameCase(srcPackageName, in.DstFileNameCase)+".go",
|
||||
)
|
||||
)
|
||||
generatedDstFilePathSet.Add(dstFilePath)
|
||||
for _, file := range files {
|
||||
var packageItems []packageItem
|
||||
fileContent = gfile.GetContents(file)
|
||||
fileContent, err := gregex.ReplaceString(`/[/|\*](.+)`, "", fileContent)
|
||||
// remove all comments.
|
||||
fileContent, err = gregex.ReplaceString(`/[/|\*](.+)`, "", fileContent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Calculate imported packages of source go files.
|
||||
err = c.calculateImportedPackages(fileContent, srcImportedPackages)
|
||||
packageItems, err = c.calculateImportedPackages(fileContent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// try finding the conflicts imports between files.
|
||||
for _, item := range packageItems {
|
||||
alias := item.Alias
|
||||
if alias == "" {
|
||||
alias = gfile.Basename(gstr.Trim(item.Path, `"`))
|
||||
}
|
||||
importPath := importPackageItemAliasMap.Get(alias)
|
||||
if importPath == "" {
|
||||
importPackageItemAliasMap.Set(alias, item.Path)
|
||||
srcImportedPackages.Add(item.RawImport)
|
||||
continue
|
||||
}
|
||||
if importPath != item.Path {
|
||||
// update the conflicted alias with suffix.
|
||||
item.Alias = fmt.Sprintf(`%s%d`, alias, aliasIndex)
|
||||
aliasIndex++
|
||||
importPackageItemAliasMap.Set(item.Alias, item.Path)
|
||||
item.RawImport = fmt.Sprintf(`%s %s`, item.Alias, item.Path)
|
||||
|
||||
// update the file content with new alias import.
|
||||
fileContent, err = gregex.ReplaceStringFuncMatch(
|
||||
fmt.Sprintf(`([^\w])%s(\.\w+)`, alias), fileContent,
|
||||
func(match []string) string {
|
||||
return match[1] + item.Alias + match[2]
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
srcImportedPackages.Add(item.RawImport)
|
||||
}
|
||||
}
|
||||
// Calculate functions and interfaces for service generating.
|
||||
err = c.calculateInterfaceFunctions(in, fileContent, srcPkgInterfaceMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
initImportSrcPackages = append(
|
||||
initImportSrcPackages,
|
||||
fmt.Sprintf(`%s/%s`, in.ImportPrefix, srcPackageName),
|
||||
@ -213,7 +250,7 @@ func (c CGenService) Service(ctx context.Context, in CGenServiceInput) (out *CGe
|
||||
)
|
||||
continue
|
||||
}
|
||||
// Generating service go file for logic.
|
||||
// Generating service go file for single logic package.
|
||||
if ok, err = c.generateServiceFile(generateServiceFilesInput{
|
||||
CGenServiceInput: in,
|
||||
SrcStructFunctions: srcPkgInterfaceMap,
|
||||
@ -262,6 +299,56 @@ func (c CGenService) Service(ctx context.Context, in CGenServiceInput) (out *CGe
|
||||
utils.GoFmt(in.DstFolder)
|
||||
}
|
||||
|
||||
// auto update main.go.
|
||||
if err = c.checkAndUpdateMain(in.SrcFolder); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mlog.Print(`done!`)
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
importStr = fmt.Sprintf(`_ "%s"`, importPath)
|
||||
mainFilePath = gfile.Join(gfile.Dir(gfile.Dir(gfile.Dir(logicFilePath))), "main.go")
|
||||
mainFileContent = gfile.GetContents(mainFilePath)
|
||||
)
|
||||
if gstr.Contains(mainFileContent, importStr) {
|
||||
return nil
|
||||
}
|
||||
match, err := gregex.MatchString(`import \(([\s\S]+?)\)`, mainFileContent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
lines := garray.NewStrArrayFrom(gstr.Split(match[1], "\n"))
|
||||
for i, line := range lines.Slice() {
|
||||
line = gstr.Trim(line)
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
if line[0] == '_' {
|
||||
continue
|
||||
}
|
||||
// Insert the logic import into imports.
|
||||
if err = lines.InsertBefore(i, fmt.Sprintf("\t%s\n\n", importStr)); err != nil {
|
||||
return err
|
||||
}
|
||||
break
|
||||
}
|
||||
mainFileContent, err = gregex.ReplaceString(
|
||||
`import \(([\s\S]+?)\)`,
|
||||
fmt.Sprintf(`import (%s)`, lines.Join("\n")),
|
||||
mainFileContent,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mlog.Print(`update main.go`)
|
||||
err = gfile.PutContents(mainFilePath, mainFileContent)
|
||||
utils.GoFmt(mainFilePath)
|
||||
return
|
||||
}
|
||||
|
@ -15,25 +15,40 @@ import (
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
)
|
||||
|
||||
func (c CGenService) calculateImportedPackages(fileContent string, srcImportedPackages *garray.SortedStrArray) (err error) {
|
||||
type packageItem struct {
|
||||
Alias string
|
||||
Path string
|
||||
RawImport string
|
||||
}
|
||||
|
||||
func (c CGenService) calculateImportedPackages(fileContent string) (packages []packageItem, err error) {
|
||||
f, err := parser.ParseFile(token.NewFileSet(), "", fileContent, parser.ImportsOnly)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
packages = make([]packageItem, 0)
|
||||
for _, s := range f.Imports {
|
||||
if s.Path != nil {
|
||||
if s.Name != nil {
|
||||
// If it has alias, and it is not `_`.
|
||||
if pkgAlias := s.Name.String(); pkgAlias != "_" {
|
||||
srcImportedPackages.Add(pkgAlias + " " + s.Path.Value)
|
||||
packages = append(packages, packageItem{
|
||||
Alias: pkgAlias,
|
||||
Path: s.Path.Value,
|
||||
RawImport: pkgAlias + " " + s.Path.Value,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// no alias
|
||||
srcImportedPackages.Add(s.Path.Value)
|
||||
packages = append(packages, packageItem{
|
||||
Alias: "",
|
||||
Path: s.Path.Value,
|
||||
RawImport: s.Path.Value,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return packages, nil
|
||||
}
|
||||
|
||||
func (c CGenService) calculateInterfaceFunctions(
|
||||
|
@ -154,27 +154,27 @@ func (c CGenService) isToGenerateServiceGoFile(dstPackageName, filePath string,
|
||||
// generateInitializationFile generates `logic.go`.
|
||||
func (c CGenService) generateInitializationFile(in CGenServiceInput, importSrcPackages []string) (err error) {
|
||||
var (
|
||||
srcPackageName = gstr.ToLower(gfile.Basename(in.SrcFolder))
|
||||
srcFilePath = gfile.Join(in.SrcFolder, srcPackageName+".go")
|
||||
srcImports string
|
||||
logicPackageName = gstr.ToLower(gfile.Basename(in.SrcFolder))
|
||||
logicFilePath = gfile.Join(in.SrcFolder, logicPackageName+".go")
|
||||
logicImports string
|
||||
generatedContent string
|
||||
)
|
||||
if !utils.IsFileDoNotEdit(srcFilePath) {
|
||||
mlog.Debugf(`ignore file as it is manually maintained: %s`, srcFilePath)
|
||||
if !utils.IsFileDoNotEdit(logicFilePath) {
|
||||
mlog.Debugf(`ignore file as it is manually maintained: %s`, logicFilePath)
|
||||
return nil
|
||||
}
|
||||
for _, importSrcPackage := range importSrcPackages {
|
||||
srcImports += fmt.Sprintf(`%s_ "%s"%s`, "\t", importSrcPackage, "\n")
|
||||
logicImports += fmt.Sprintf(`%s_ "%s"%s`, "\t", importSrcPackage, "\n")
|
||||
}
|
||||
generatedContent = gstr.ReplaceByMap(consts.TemplateGenServiceLogicContent, g.MapStrStr{
|
||||
"{PackageName}": srcPackageName,
|
||||
"{Imports}": srcImports,
|
||||
"{PackageName}": logicPackageName,
|
||||
"{Imports}": logicImports,
|
||||
})
|
||||
mlog.Printf(`generating init go file: %s`, srcFilePath)
|
||||
if err = gfile.PutContents(srcFilePath, generatedContent); err != nil {
|
||||
mlog.Printf(`generating init go file: %s`, logicFilePath)
|
||||
if err = gfile.PutContents(logicFilePath, generatedContent); err != nil {
|
||||
return err
|
||||
}
|
||||
utils.GoFmt(srcFilePath)
|
||||
utils.GoFmt(logicFilePath)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -10,14 +10,15 @@ const TemplateGenCtrlSdkPkgNew = `
|
||||
// =================================================================================
|
||||
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
|
||||
// =================================================================================
|
||||
|
||||
package {PkgName}
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/contrib/sdk/httpclient/v2"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/contrib/sdk/httpclient/v2"
|
||||
)
|
||||
|
||||
type implementer struct {
|
||||
@ -62,8 +63,8 @@ package {PkgName}
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/contrib/sdk/httpclient/v2"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
|
||||
{ImportPaths}
|
||||
)
|
||||
|
@ -77,14 +77,22 @@ func ReplaceGeneratedContentGFV2(folderPath string) (err error) {
|
||||
// GetImportPath calculates and returns the golang import path for given `filePath`.
|
||||
// Note that it needs a `go.mod` in current working directory or parent directories to detect the path.
|
||||
func GetImportPath(filePath string) string {
|
||||
// If `filePath` does not exist, create it firstly to find the import path.
|
||||
var realPath = gfile.RealPath(filePath)
|
||||
if realPath == "" {
|
||||
_ = gfile.Mkdir(filePath)
|
||||
realPath = gfile.RealPath(filePath)
|
||||
}
|
||||
|
||||
var (
|
||||
newDir = gfile.Dir(filePath)
|
||||
newDir = gfile.Dir(realPath)
|
||||
oldDir string
|
||||
suffix string
|
||||
goModName = "go.mod"
|
||||
goModPath string
|
||||
importPath string
|
||||
)
|
||||
|
||||
if gfile.IsDir(filePath) {
|
||||
suffix = gfile.Basename(filePath)
|
||||
}
|
||||
@ -93,7 +101,9 @@ func GetImportPath(filePath string) string {
|
||||
if gfile.Exists(goModPath) {
|
||||
match, _ := gregex.MatchString(`^module\s+(.+)\s*`, gfile.GetContents(goModPath))
|
||||
importPath = gstr.Trim(match[1]) + "/" + suffix
|
||||
return gstr.Replace(importPath, `\`, `/`)
|
||||
importPath = gstr.Replace(importPath, `\`, `/`)
|
||||
importPath = gstr.TrimRight(importPath, `/`)
|
||||
return importPath
|
||||
}
|
||||
oldDir = newDir
|
||||
newDir = gfile.Dir(oldDir)
|
||||
|
@ -4,7 +4,7 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/apolloconfig/agollo/v4 v4.1.1
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
)
|
||||
|
||||
replace github.com/gogf/gf/v2 => ../../../
|
||||
|
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/kubecm/v2
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
k8s.io/api v0.25.2
|
||||
k8s.io/apimachinery v0.25.2
|
||||
k8s.io/client-go v0.25.2
|
||||
|
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/nacos/v2
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
github.com/nacos-group/nacos-sdk-go v1.1.2
|
||||
)
|
||||
|
||||
|
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/polaris/v2
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
github.com/polarismesh/polaris-go v1.4.3
|
||||
)
|
||||
|
||||
|
@ -4,7 +4,7 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/ClickHouse/clickhouse-go/v2 v2.0.15
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/shopspring/decimal v1.3.1
|
||||
)
|
||||
|
@ -6,5 +6,5 @@ replace github.com/gogf/gf/v2 => ../../../
|
||||
|
||||
require (
|
||||
gitee.com/chunanyong/dm v1.8.10
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
)
|
||||
|
@ -4,7 +4,7 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/denisenkom/go-mssqldb v0.11.0
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
)
|
||||
|
||||
replace github.com/gogf/gf/v2 => ../../../
|
||||
|
@ -4,7 +4,7 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/go-sql-driver/mysql v1.6.0
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
)
|
||||
|
||||
replace github.com/gogf/gf/v2 => ../../../
|
||||
|
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/drivers/oracle/v2
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
github.com/sijms/go-ora/v2 v2.4.20
|
||||
)
|
||||
|
||||
|
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/drivers/pgsql/v2
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
github.com/lib/pq v1.10.4
|
||||
)
|
||||
|
||||
|
@ -4,7 +4,7 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/glebarez/go-sqlite v1.17.3
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
)
|
||||
|
||||
replace github.com/gogf/gf/v2 => ../../../
|
||||
|
@ -4,7 +4,7 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/go-redis/redis/v8 v8.11.5
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
go.opentelemetry.io/otel v1.7.0
|
||||
go.opentelemetry.io/otel/trace v1.7.0
|
||||
)
|
||||
|
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/registry/etcd/v2
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
go.etcd.io/etcd/client/v3 v3.5.4
|
||||
)
|
||||
|
||||
|
@ -2,6 +2,6 @@ module github.com/gogf/gf/contrib/registry/file/v2
|
||||
|
||||
go 1.15
|
||||
|
||||
require github.com/gogf/gf/v2 v2.4.3
|
||||
require github.com/gogf/gf/v2 v2.4.4
|
||||
|
||||
replace github.com/gogf/gf/v2 => ../../../
|
||||
|
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/registry/polaris/v2
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
github.com/polarismesh/polaris-go v1.4.3
|
||||
)
|
||||
|
||||
|
@ -4,7 +4,7 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/go-zookeeper/zk v1.0.3
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
golang.org/x/sync v0.1.0
|
||||
)
|
||||
|
||||
|
@ -3,8 +3,8 @@ module github.com/gogf/gf/contrib/rpc/grpcx/v2
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/contrib/registry/file/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/registry/file/v2 v2.4.4
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
go.opentelemetry.io/otel v1.10.0
|
||||
go.opentelemetry.io/otel/trace v1.10.0
|
||||
golang.org/x/net v0.0.0-20220919232410-f2f64ebce3c1 // indirect
|
||||
|
@ -2,6 +2,6 @@ module github.com/gogf/gf/contrib/sdk/httpclient/v2
|
||||
|
||||
go 1.15
|
||||
|
||||
require github.com/gogf/gf/v2 v2.4.3
|
||||
require github.com/gogf/gf/v2 v2.4.4
|
||||
|
||||
replace github.com/gogf/gf/v2 => ../../../
|
||||
|
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/trace/jaeger/v2
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
go.opentelemetry.io/otel v1.7.0
|
||||
go.opentelemetry.io/otel/exporters/jaeger v1.7.0
|
||||
go.opentelemetry.io/otel/sdk v1.7.0
|
||||
|
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/trace/otlpgrpc/v2
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
go.opentelemetry.io/otel v1.16.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0
|
||||
|
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/trace/otlphttp/v2
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
go.opentelemetry.io/otel v1.16.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.16.0
|
||||
|
@ -3,20 +3,20 @@ module github.com/gogf/gf/example
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/contrib/config/apollo/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/config/kubecm/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/config/nacos/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/config/polaris/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/nosql/redis/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/registry/etcd/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/registry/file/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/registry/polaris/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/rpc/grpcx/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/trace/jaeger/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/trace/otlpgrpc/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/trace/otlphttp/v2 v2.4.3
|
||||
github.com/gogf/gf/v2 v2.4.3
|
||||
github.com/gogf/gf/contrib/config/apollo/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/config/kubecm/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/config/nacos/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/config/polaris/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/nosql/redis/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/registry/etcd/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/registry/file/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/registry/polaris/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/rpc/grpcx/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/trace/jaeger/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/trace/otlpgrpc/v2 v2.4.4
|
||||
github.com/gogf/gf/contrib/trace/otlphttp/v2 v2.4.4
|
||||
github.com/gogf/gf/v2 v2.4.4
|
||||
github.com/nacos-group/nacos-sdk-go v1.1.2
|
||||
github.com/polarismesh/polaris-go v1.4.3
|
||||
google.golang.org/grpc v1.55.0
|
||||
|
@ -225,11 +225,9 @@ func Test_Async(t *testing.T) {
|
||||
defer gfile.Remove(path)
|
||||
|
||||
Path(path).File(file).Async().Stdout(false).Debug(ctx, 1, 2, 3)
|
||||
content := gfile.GetContents(gfile.Join(path, file))
|
||||
t.Assert(content, "")
|
||||
time.Sleep(1000 * time.Millisecond)
|
||||
|
||||
content = gfile.GetContents(gfile.Join(path, file))
|
||||
content := gfile.GetContents(gfile.Join(path, file))
|
||||
t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_DEBU]), 1)
|
||||
t.Assert(gstr.Count(content, "1 2 3"), 1)
|
||||
})
|
||||
|
@ -2,5 +2,5 @@ package gf
|
||||
|
||||
const (
|
||||
// VERSION is the current GoFrame version.
|
||||
VERSION = "v2.4.3"
|
||||
VERSION = "v2.4.4"
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user