mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 17:52:49 +08:00
e76f44a35b
* reactor rpc generation * update flag * update command * update command * update unit test * delete test file * optimize code * update doc * update gen pb * rename target dir * update mysql data type convert rule * add done flag * optimize req/reply parameter * optimize req/reply parameter * remove waste code * remove duplicate parameter * format code * format code * optimize naming * reactor rpcv2 to rpc * remove new line * format code * rename underline to snake * reactor getParentPackage * remove debug log * reactor background
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package ctx
|
|
|
|
import (
|
|
"errors"
|
|
"path/filepath"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
|
)
|
|
|
|
var moduleCheckErr = errors.New("the work directory must be found in the go mod or the $GOPATH")
|
|
|
|
type (
|
|
ProjectContext struct {
|
|
WorkDir string
|
|
// Name is the root name of the project
|
|
// eg: go-zero、greet
|
|
Name string
|
|
// Path identifies which module a project belongs to, which is module value if it's a go mod project,
|
|
// or else it is the root name of the project, eg: github.com/tal-tech/go-zero、greet
|
|
Path string
|
|
// Dir is the path of the project, eg: /Users/keson/goland/go/go-zero、/Users/keson/go/src/greet
|
|
Dir string
|
|
}
|
|
)
|
|
|
|
// Prepare checks the project which module belongs to,and returns the path and module.
|
|
// workDir parameter is the directory of the source of generating code,
|
|
// where can be found the project path and the project module,
|
|
func Prepare(workDir string) (*ProjectContext, error) {
|
|
ctx, err := background(workDir)
|
|
if err == nil {
|
|
return ctx, nil
|
|
}
|
|
|
|
name := filepath.Base(workDir)
|
|
_, err = execx.Run("go mod init "+name, workDir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return background(workDir)
|
|
}
|
|
|
|
func background(workDir string) (*ProjectContext, error) {
|
|
isGoMod, err := IsGoMod(workDir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if isGoMod {
|
|
return projectFromGoMod(workDir)
|
|
}
|
|
return projectFromGoPath(workDir)
|
|
}
|