1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package ktgen
- import (
- _ "embed"
- "fmt"
- "os"
- "path/filepath"
- "text/template"
- "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")
- path := filepath.Join(dir, name+".kt")
- api.Info.Title = name
- api.Info.Desc = desc
- e := os.MkdirAll(dir, 0o755)
- if e != nil {
- return e
- }
- file, e := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0o644)
- if e != nil {
- return e
- }
- defer file.Close()
- t, e := template.New("api").Funcs(funcsMap).Parse(apiTemplate)
- if e != nil {
- return e
- }
- return t.Execute(file, api)
- }
|