Skip to content

Commit

Permalink
fix: gen move to dyn-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Dec 27, 2024
1 parent 1851e13 commit 8bbfbe6
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 113 deletions.
21 changes: 11 additions & 10 deletions cmd/dyngen/command/api.go → cmd/dyn-gen/command/crud/api.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package command
package crud

import (
"errors"
"log/slog"
"os"

"github.com/spf13/cobra"
"github.com/things-go/dyn/cmd/dyngen/command/api"
"github.com/things-go/dyn/cmd/dyn-gen/command/crud/api"
"github.com/things-go/dyn/cmd/dyn-gen/util"
)

type apiOpt struct {
Expand All @@ -20,13 +21,13 @@ type apiOpt struct {
EnableOpenapiv2Annotation bool // 启用int64的openapiv2注解
}

type apiCmd struct {
cmd *cobra.Command
type ApiCmd struct {
Cmd *cobra.Command
apiOpt
}

func newApiCmd() *apiCmd {
root := &apiCmd{}
func NewApiCmd() *ApiCmd {
root := &ApiCmd{}
cmd := &cobra.Command{
Use: "api",
Short: "Generate api from database",
Expand All @@ -40,7 +41,7 @@ func newApiCmd() *apiCmd {
return errors.New("at least one schema entity")
}
entity := schemaes.Entities[0].IntoProto()
filename := joinFilename(root.OutputDir, entity.TableName, ".proto")
filename := util.JoinFilename(root.OutputDir, entity.TableName, ".proto")
_, err = os.Stat(filename)
if err == nil || os.IsExist(err) {
slog.Warn("🐛 '" + entity.TableName + "' already exists, skipping")
Expand All @@ -56,7 +57,7 @@ func newApiCmd() *apiCmd {
EnableOpenapiv2Annotation: root.EnableOpenapiv2Annotation,
}
data := codegen.Gen().Bytes()
err = WriteFile(filename, data)
err = util.WriteFile(filename, data)
if err != nil {
return err
}
Expand Down Expand Up @@ -84,8 +85,8 @@ func newApiCmd() *apiCmd {
cmd.MarkFlagsOneRequired("url", "input")
cmd.MarkFlagRequired("package")
cmd.MarkFlagRequired("options")
root.cmd = cmd
root.cmd.AddCommand(
root.Cmd = cmd
root.Cmd.AddCommand(
newApiServiceCmd().cmd,
)
return root
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package command
package crud

import (
"errors"
"log/slog"
"os"

"github.com/spf13/cobra"
"github.com/things-go/dyn/cmd/dyngen/command/api"
"github.com/things-go/dyn/cmd/dyn-gen/command/crud/api"
"github.com/things-go/dyn/cmd/dyn-gen/util"
)

type apiServiceOpt struct {
Expand Down Expand Up @@ -37,7 +38,7 @@ func newApiServiceCmd() *apiServiceCmd {
return errors.New("at least one schema entity")
}
entity := schemaes.Entities[0].IntoProto()
filename := joinFilename(root.OutputDir, root.Filename, ".proto")
filename := util.JoinFilename(root.OutputDir, root.Filename, ".proto")
_, err = os.Stat(filename)
if err == nil || os.IsExist(err) {
slog.Warn("🐛 '" + root.Filename + "' already exists, skipping")
Expand All @@ -49,7 +50,7 @@ func newApiServiceCmd() *apiServiceCmd {
Options: root.Options,
}
data := codegen.GenService().Bytes()
err = WriteFile(filename, data)
err = util.WriteFile(filename, data)
if err != nil {
return err
}
Expand Down
25 changes: 13 additions & 12 deletions cmd/dyngen/command/dal.go → cmd/dyn-gen/command/crud/dal.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package crud

import (
"bytes"
Expand All @@ -11,6 +11,7 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/things-go/dyn/cmd/dyn-gen/util"
"github.com/things-go/ens"
"github.com/things-go/ens/utils"
)
Expand All @@ -28,13 +29,13 @@ type dalOpt struct {
ens.Option
}

type dalCmd struct {
cmd *cobra.Command
type DalCmd struct {
Cmd *cobra.Command
dalOpt
}

func newDalCmd() *dalCmd {
root := &dalCmd{}
func NewDalCmd() *DalCmd {
root := &DalCmd{}

cmd := &cobra.Command{
Use: "dal",
Expand All @@ -55,14 +56,14 @@ func newDalCmd() *dalCmd {
buf := bytes.Buffer{}
packageName := cmp.Or(root.PackageName, utils.GetPkgName(root.OutputDir))
queryImportPath := strings.Join([]string{root.DalImportPath, "query"}, "/")
dalOptionFilename := joinFilename(root.OutputDir, "a.dal.ext", ".go")
dalOptionFilename := util.JoinFilename(root.OutputDir, "a.dal.ext", ".go")
_, err = os.Stat(dalOptionFilename)
if !(err == nil || os.IsExist(err)) || root.Override {
err = dalOptionTpl.Execute(&buf, Dal{Package: packageName})
if err != nil {
return err
}
err = WriteFile(dalOptionFilename, buf.Bytes())
err = util.WriteFile(dalOptionFilename, buf.Bytes())
if err != nil {
return fmt.Errorf("dal_option: %v", err)
}
Expand Down Expand Up @@ -91,7 +92,7 @@ func newDalCmd() *dalCmd {
}

for _, entity := range schemaes.Entities {
dalFilename := joinFilename(root.OutputDir, entity.Name, ".go")
dalFilename := util.JoinFilename(root.OutputDir, entity.Name, ".go")
_, err = os.Stat(dalFilename)
if (err == nil || os.IsExist(err)) && !root.Override {
slog.Warn("🐛 '" + entity.Name + "' already exists, skipping")
Expand All @@ -104,7 +105,7 @@ func newDalCmd() *dalCmd {
return err
}

err = WriteFile(dalFilename, buf.Bytes())
err = util.WriteFile(dalFilename, buf.Bytes())
if err != nil {
return fmt.Errorf("%v: %v", entity.Name, err)
}
Expand All @@ -115,8 +116,8 @@ func newDalCmd() *dalCmd {
if err != nil {
return err
}
dalQueryFilename := joinFilename(filepath.Join(root.OutputDir, "query"), entity.Name, ".go")
err = WriteFile(dalQueryFilename, buf.Bytes())
dalQueryFilename := util.JoinFilename(filepath.Join(root.OutputDir, "query"), entity.Name, ".go")
err = util.WriteFile(dalQueryFilename, buf.Bytes())
if err != nil {
return err
}
Expand Down Expand Up @@ -154,6 +155,6 @@ func newDalCmd() *dalCmd {
cmd.MarkFlagsOneRequired("url", "input")
cmd.MarkFlagRequired("modelImportPath")
cmd.MarkFlagRequired("dalImportPath")
root.cmd = cmd
root.Cmd = cmd
return root
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package crud

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package crud

import (
"embed"
Expand Down Expand Up @@ -28,7 +28,6 @@ var (
dalGormTpl = tpl.Lookup("dal_gorm.tpl")
dalQueryTpl = tpl.Lookup("dal_query.tpl")
dalOptionTpl = tpl.Lookup("dal_option.tpl")
curdProtoTpl = tpl.Lookup("curd_proto.tpl")
)

type Dal struct {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package errno

import (
"log/slog"
Expand All @@ -20,18 +20,17 @@ type ErrnoOption struct {
Epk string
}

type RootCmd struct {
cmd *cobra.Command
level string
type ErrnoCmd struct {
Cmd *cobra.Command
ErrnoOption
}

func NewRootCmd() *RootCmd {
root := &RootCmd{}
func NewErrnoCmd() *ErrnoCmd {
root := &ErrnoCmd{}
cmd := &cobra.Command{
Use: "errno-gen",
Short: "errno-gen generate errno from enum",
Long: "errno-gen generate errno from enum",
Use: "errno",
Short: "generate errno from enum",
Long: "generate errno from enum",
Version: meta.BuildVersion(),
SilenceUsage: false,
SilenceErrors: false,
Expand Down Expand Up @@ -87,42 +86,13 @@ func NewRootCmd() *RootCmd {
return nil
},
}
cobra.OnInitialize(func() {
textHandler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
AddSource: false,
Level: level(root.level),
ReplaceAttr: nil,
})
slog.SetDefault(slog.New(textHandler))
})

cmd.PersistentFlags().StringVarP(&root.level, "level", "l", "info", "log level(debug,info,warn,error)")
cmd.Flags().StringSliceVarP(&root.Pattern, "pattern", "p", []string{"."}, "the list of files or a directory.")
cmd.Flags().StringSliceVarP(&root.Type, "type", "t", nil, "the list type of enum names; must be set")
cmd.Flags().StringSliceVar(&root.Tags, "tags", nil, "comma-separated list of build tags to apply")
cmd.Flags().BoolVarP(&root.DisableStringer, "disable-stringer", "d", false, "disable use `stringer` command.")
cmd.Flags().StringVarP(&root.Epk, "epk", "e", "github.com/things-go/dyn/errorx", "errors package import path")

root.cmd = cmd
root.Cmd = cmd
return root
}

// Execute adds all child commands to the root command and sets flags appropriately.
func (r *RootCmd) Execute() error {
return r.cmd.Execute()
}

func level(s string) slog.Level {
switch strings.ToUpper(s) {
case "DEBUG":
return slog.LevelDebug
case "INFO":
return slog.LevelInfo
case "WARN":
return slog.LevelWarn
case "ERROR":
return slog.LevelError
default:
return slog.LevelInfo
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package errno

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package errno

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated by errno-gen. DO NOT EDIT.
// Code generated by dyn-gen. DO NOT EDIT.
// version: {{.Version}}
{{- if .IsDeprecated}}
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package errno

import (
"embed"
Expand Down
15 changes: 8 additions & 7 deletions cmd/dyngen/command/root.go → cmd/dyn-gen/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"strings"

"github.com/spf13/cobra"

"github.com/things-go/dyn/cmd/dyn-gen/command/crud"
"github.com/things-go/dyn/cmd/dyn-gen/command/errno"
"github.com/things-go/dyn/cmd/internal/meta"
_ "github.com/things-go/ens/driver/mysql"
)

type RootCmd struct {
Expand All @@ -19,9 +19,9 @@ type RootCmd struct {
func NewRootCmd() *RootCmd {
root := &RootCmd{}
cmd := &cobra.Command{
Use: "ormat",
Short: "gorm reflect tools",
Long: "database/sql to golang struct",
Use: "ast-gen",
Short: "ast gen tools",
Long: "ast gen tools",
Version: meta.BuildVersion(),
SilenceUsage: false,
SilenceErrors: false,
Expand All @@ -38,8 +38,9 @@ func NewRootCmd() *RootCmd {

cmd.PersistentFlags().StringVarP(&root.level, "level", "l", "info", "log level(debug,info,warn,error)")
cmd.AddCommand(
newDalCmd().cmd,
newApiCmd().cmd,
errno.NewErrnoCmd().Cmd,
crud.NewDalCmd().Cmd,
crud.NewApiCmd().Cmd,
)
root.cmd = cmd
return root
Expand Down
14 changes: 14 additions & 0 deletions cmd/dyn-gen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"os"

"github.com/things-go/dyn/cmd/dyn-gen/command"
)

func main() {
err := command.NewRootCmd().Execute()
if err != nil {
os.Exit(1)
}
}
4 changes: 2 additions & 2 deletions cmd/dyngen/command/helper.go → cmd/dyn-gen/util/util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package util

import (
"os"
Expand All @@ -7,7 +7,7 @@ import (
"strings"
)

func joinFilename(dir, filename, suffix string) string {
func JoinFilename(dir, filename, suffix string) string {
suffix = strings.TrimSpace(suffix)
if suffix != "" && !strings.HasPrefix(suffix, ".") {
suffix = "." + suffix
Expand Down
16 changes: 0 additions & 16 deletions cmd/dyngen/main.go

This file was deleted.

Loading

0 comments on commit 8bbfbe6

Please sign in to comment.