add ability to generate vue componenets based on swagger

This commit is contained in:
2025-03-12 23:53:25 -06:00
parent a8f2b832b4
commit d749a32abd
5 changed files with 530 additions and 42 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/urfave/cli/v2"
"golang.org/x/text/cases"
"golang.org/x/text/language"
vue_gen "masonry/vue-gen"
"os"
"os/exec"
"runtime"
@ -16,10 +17,13 @@ import (
)
//go:embed templates/proto/application.proto.tmpl
var protoTemplate string
var protoTemplateSrc string
//go:embed templates/backend/main.go.tmpl
var mainGoTemplate string
var mainGoTemplateSrc string
//go:embed templates/backend/gitignore.tmpl
var gitignoreTemplateSrc string
//go:embed proto_include/*
var protoInclude embed.FS
@ -98,12 +102,26 @@ func createCmd() *cli.Command {
titleMaker := cases.Title(language.English)
// render the main.go file from the template
goTemplate := template.Must(template.New("main").Parse(mainGoTemplate))
goTemplate := template.Must(template.New("main").Parse(mainGoTemplateSrc))
err = goTemplate.Execute(mainFile, map[string]string{"AppName": strings.ToLower(applicationName), "AppNameCaps": titleMaker.String(applicationName)})
if err != nil {
return fmt.Errorf("error rendering main.go file | %w", err)
}
// create a gitignore file
gitignoreFile, err := os.Create(".gitignore")
if err != nil {
return fmt.Errorf("error creating gitignore file | %w", err)
}
defer gitignoreFile.Close()
// render the gitignore file from the template
gitignoreTemplate := template.Must(template.New("gitignore").Parse(gitignoreTemplateSrc))
err = gitignoreTemplate.Execute(gitignoreFile, nil)
if err != nil {
return fmt.Errorf("error rendering gitignore file | %w", err)
}
// create a proto file
protoFile, err := os.Create("proto/service.proto")
if err != nil {
@ -112,7 +130,7 @@ func createCmd() *cli.Command {
defer protoFile.Close()
// render the proto file from the template
t := template.Must(template.New("proto").Parse(protoTemplate))
t := template.Must(template.New("proto").Parse(protoTemplateSrc))
err = t.Execute(protoFile, map[string]string{"AppName": strings.ToLower(applicationName), "AppNameCaps": titleMaker.String(applicationName), "ObjName": "Product"})
if err != nil {
return fmt.Errorf("error rendering proto file | %w", err)
@ -207,24 +225,24 @@ func generateCmd() *cli.Command {
return fmt.Errorf("error generating typescript code | %w", err)
}
// if on windows
if runtime.GOOS == "windows" {
//cmd = exec.Command("npx", "protoc", "--plugin=protoc-gen-ts_proto=.\\node_modules\\.bin\\protoc-gen-ts_proto.cmd", "--ts_proto_out=./src/generated", "--ts_proto_opt=outputServices=generic,esModuleInterop=true", "--proto_path=../proto", "--proto_path=../include", "../proto/service.proto")
//cmd.Stdout = os.Stdout
//cmd.Stderr = os.Stderr
//err = cmd.Run()
//if err != nil {
// return fmt.Errorf("error generating typescript code | %w", err)
//}
} else {
//cmd = exec.Command("npx", "protoc", "--plugin=protoc-gen-ts_proto=./node_modules/.bin/protoc-gen-ts_proto", "--ts_proto_out=./src/generated", "--ts_proto_opt=outputServices=generic,esModuleInterop=true", "--proto_path=../proto", "--proto_path=../include", "../proto/service.proto")
//cmd.Stdout = os.Stdout
//cmd.Stderr = os.Stderr
//err = cmd.Run()
//if err != nil {
// return fmt.Errorf("error generating typescript code | %w", err)
//}
// make sure src/generated-components exists
err = os.Mkdir("src/generated-components", 0755)
if err != nil {
return fmt.Errorf("error creating src/generated-components directory | %w", err)
}
// generate vue components
err = vue_gen.GenVueFromSwagger("../gen/openapi/proto/service.swagger.json", "src/generated-components")
if err != nil {
return fmt.Errorf("error generating vue components | %w", err)
}
cmd := exec.Command("npm", "install", "@masonitestudios/dynamic-vue")
err = cmd.Run()
if err != nil {
return fmt.Errorf("error installing @masonitestudios/dynamic-vue | %w", err)
}
err = os.Chdir("..")
if err != nil {
return fmt.Errorf("error changing directory back to root | %w", err)
@ -299,3 +317,37 @@ func setupCmd() *cli.Command {
},
}
}
func vueGenCmd() *cli.Command {
return &cli.Command{
Name: "vuegen",
Aliases: []string{"vg"},
Usage: "Generate vue components based on a swagger file",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "input",
Aliases: []string{"i"},
Usage: "The input swagger file",
Required: true,
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "The output directory",
Required: true,
},
},
Action: func(c *cli.Context) error {
fmt.Println("Generating vue components")
input := c.String("input")
output := c.String("output")
err := vue_gen.GenVueFromSwagger(input, output)
if err != nil {
return fmt.Errorf("error generating vue components | %w", err)
}
return nil
},
}
}