add runner

master
kim 4 years ago committed by Kevin Wan
parent 760d3c8c4b
commit b1e8dd62da

@ -47,7 +47,7 @@ func GoCommand(c *cli.Context) error {
lang.Must(util.MkdirIfNotExist(dir))
lang.Must(genEtc(dir, api))
lang.Must(genConfig(dir, api))
lang.Must(genConfig(dir))
lang.Must(genMain(dir, api))
lang.Must(genServiceContext(dir, api))
lang.Must(genTypes(dir, api))

@ -2,10 +2,11 @@ package gogen
import (
"bytes"
"fmt"
"text/template"
"zero/tools/goctl/api/spec"
"zero/tools/goctl/api/util"
"zero/tools/goctl/vars"
)
const (
@ -13,7 +14,6 @@ const (
configTemplate = `package config
import (
"zero/rest"
{{.authImport}}
)
@ -23,7 +23,7 @@ type Config struct {
`
)
func genConfig(dir string, api *spec.ApiSpec) error {
func genConfig(dir string) error {
fp, created, err := util.MaybeCreateFile(dir, configDir, configFile)
if err != nil {
return err
@ -33,7 +33,7 @@ func genConfig(dir string, api *spec.ApiSpec) error {
}
defer fp.Close()
var authImportStr = ""
var authImportStr = fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl)
t := template.Must(template.New("configTemplate").Parse(configTemplate))
buffer := new(bytes.Buffer)
err = t.Execute(buffer, map[string]string{

@ -11,6 +11,7 @@ import (
"zero/tools/goctl/api/spec"
apiutil "zero/tools/goctl/api/util"
"zero/tools/goctl/util"
"zero/tools/goctl/vars"
)
const (
@ -19,7 +20,6 @@ const (
import (
"net/http"
"zero/rest/httpx"
{{.importPackages}}
)
@ -162,6 +162,7 @@ func genHandlers(dir string, api *spec.ApiSpec) error {
func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) string {
var imports []string
imports = append(imports, fmt.Sprintf("\"%s/rest/httpx\"", vars.ProjectOpenSourceUrl))
imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, contextDir)))
if len(route.RequestType.Name) > 0 || len(route.ResponseType.Name) > 0 {
imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, typesDir)))

@ -9,6 +9,7 @@ import (
"zero/tools/goctl/api/spec"
"zero/tools/goctl/api/util"
"zero/tools/goctl/vars"
)
const logicTemplate = `package logic
@ -121,7 +122,7 @@ func genLogicImports(route spec.Route, parentPkg string) string {
var imports []string
imports = append(imports, `"context"`)
imports = append(imports, "\n")
imports = append(imports, `"zero/core/logx"`)
imports = append(imports, fmt.Sprintf("\"%s/core/logx\"", vars.ProjectOpenSourceUrl))
if len(route.ResponseType.Name) > 0 || len(route.RequestType.Name) > 0 {
imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, typesDir)))
}

@ -10,6 +10,7 @@ import (
"zero/tools/goctl/api/spec"
"zero/tools/goctl/api/util"
"zero/tools/goctl/vars"
)
const mainTemplate = `package main
@ -74,8 +75,8 @@ func genMain(dir string, api *spec.ApiSpec) error {
func genMainImports(parentPkg string) string {
imports := []string{
`"zero/core/conf"`,
`"zero/rest"`,
fmt.Sprintf("\"%s/core/conf\"", vars.ProjectOpenSourceUrl),
fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl),
}
imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, configDir)))
imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, handlerDir)))

@ -12,6 +12,7 @@ import (
"zero/tools/goctl/api/spec"
apiutil "zero/tools/goctl/api/util"
"zero/tools/goctl/util"
"zero/tools/goctl/vars"
)
const (
@ -129,7 +130,7 @@ func genRoutes(dir string, api *spec.ApiSpec) error {
func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
var importSet = collection.NewSet()
importSet.AddStr(`"zero/rest"`)
importSet.AddStr(fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl))
importSet.AddStr(fmt.Sprintf("\"%s\"", path.Join(parentPkg, contextDir)))
for _, group := range api.Service.Groups {
for _, route := range group.Routes {

@ -4,13 +4,14 @@ import (
"fmt"
goformat "go/format"
"io"
"os"
"path"
"path/filepath"
"strings"
"zero/core/collection"
"zero/tools/goctl/api/spec"
"zero/tools/goctl/api/util"
"zero/tools/goctl/vars"
)
func getParentPackage(dir string) (string, error) {
@ -18,12 +19,13 @@ func getParentPackage(dir string) (string, error) {
if err != nil {
return "", err
}
pos := strings.Index(absDir, vars.ProjectName)
gopath := os.Getenv("GOPATH")
parent := path.Join(gopath, "src")
pos := strings.Index(absDir, parent)
if pos < 0 {
return "", fmt.Errorf("%s not in project directory", dir)
return "", fmt.Errorf("%s not in GOPATH %s directory", absDir, gopath)
}
return absDir[pos:], nil
return absDir[len(parent)+1:], nil
}
func writeIndent(writer io.Writer, indent int) {

@ -1,3 +1,6 @@
package vars
const ProjectName = "zero"
const (
ProjectName = "zero"
ProjectOpenSourceUrl = "github.com/tal-tech/go-zero"
)

Loading…
Cancel
Save