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(util.MkdirIfNotExist(dir))
lang.Must(genEtc(dir, api)) lang.Must(genEtc(dir, api))
lang.Must(genConfig(dir, api)) lang.Must(genConfig(dir))
lang.Must(genMain(dir, api)) lang.Must(genMain(dir, api))
lang.Must(genServiceContext(dir, api)) lang.Must(genServiceContext(dir, api))
lang.Must(genTypes(dir, api)) lang.Must(genTypes(dir, api))

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

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

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

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

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

@ -4,13 +4,14 @@ import (
"fmt" "fmt"
goformat "go/format" goformat "go/format"
"io" "io"
"os"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
"zero/core/collection" "zero/core/collection"
"zero/tools/goctl/api/spec" "zero/tools/goctl/api/spec"
"zero/tools/goctl/api/util" "zero/tools/goctl/api/util"
"zero/tools/goctl/vars"
) )
func getParentPackage(dir string) (string, error) { func getParentPackage(dir string) (string, error) {
@ -18,12 +19,13 @@ func getParentPackage(dir string) (string, error) {
if err != nil { if err != nil {
return "", err 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 { 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[len(parent)+1:], nil
return absDir[pos:], nil
} }
func writeIndent(writer io.Writer, indent int) { func writeIndent(writer io.Writer, indent int) {

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

Loading…
Cancel
Save