You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/tools/goctl/api/ktgen/gen.go

87 lines
1.5 KiB
Go

package ktgen
import (
_ "embed"
"fmt"
"os"
"path/filepath"
"text/template"
4 years ago
"github.com/iancoleman/strcase"
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
)
var (
//go:embed apibase.tpl
apiBaseTemplate string
//go:embed api.tpl
apiTemplate string
)
func genBase(dir, pkg string, api *spec.ApiSpec) error {
e := os.MkdirAll(dir, 0o755)
if e != nil {
return e
}
path := filepath.Join(dir, "BaseApi.kt")
if _, e := os.Stat(path); e == nil {
fmt.Println("BaseApi.kt already exists, skipped it.")
return nil
}
file, e := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
if e != nil {
return e
}
defer file.Close()
t, e := template.New("n").Parse(apiBaseTemplate)
if e != nil {
return e
}
e = t.Execute(file, pkg)
if e != nil {
return e
}
return nil
}
func genApi(dir, pkg string, api *spec.ApiSpec) error {
properties := api.Info.Properties
if properties == nil {
return fmt.Errorf("none properties")
}
title := properties["Title"]
if len(title) == 0 {
return fmt.Errorf("none title")
}
desc := properties["Desc"]
if len(desc) == 0 {
return fmt.Errorf("none desc")
}
name := strcase.ToCamel(title + "Api")
4 years ago
path := filepath.Join(dir, name+".kt")
api.Info.Title = name
api.Info.Desc = desc
e := os.MkdirAll(dir, 0o755)
4 years ago
if e != nil {
return e
}
file, e := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0o644)
4 years ago
if e != nil {
return e
}
defer file.Close()
4 years ago
t, e := template.New("api").Funcs(funcsMap).Parse(apiTemplate)
4 years ago
if e != nil {
return e
}
4 years ago
return t.Execute(file, api)
}