12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package tsgen
- import (
- "path"
- "strings"
- "text/template"
- "github.com/tal-tech/go-zero/tools/goctl/api/spec"
- apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
- "github.com/tal-tech/go-zero/tools/goctl/util"
- )
- const (
- componentsTemplate = `// Code generated by goctl. DO NOT EDIT.
- {{.componentTypes}}
- `
- )
- func genComponents(dir string, api *spec.ApiSpec) error {
- types := api.Types
- if len(types) == 0 {
- return nil
- }
- val, err := buildTypes(types)
- if err != nil {
- return err
- }
- outputFile := apiutil.ComponentName(api) + ".ts"
- filename := path.Join(dir, outputFile)
- if err := util.RemoveIfExist(filename); err != nil {
- return err
- }
- fp, created, err := apiutil.MaybeCreateFile(dir, ".", outputFile)
- if err != nil {
- return err
- }
- if !created {
- return nil
- }
- defer fp.Close()
- t := template.Must(template.New("componentsTemplate").Parse(componentsTemplate))
- return t.Execute(fp, map[string]string{
- "componentTypes": val,
- })
- }
- func buildTypes(types []spec.Type) (string, error) {
- var builder strings.Builder
- first := true
- for _, tp := range types {
- if first {
- first = false
- } else {
- builder.WriteString("\n")
- }
- if err := writeType(&builder, tp); err != nil {
- return "", apiutil.WrapErr(err, "Type "+tp.Name()+" generate error")
- }
- }
- return builder.String(), nil
- }
|