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

@ -13,6 +13,7 @@ func main() {
webappCmd(),
tailwindCmd(),
setupCmd(),
vueGenCmd(),
}
app := &cli.App{

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
},
}
}

View File

@ -0,0 +1,3 @@
local.db
gen/
webapp/src/generated/

View File

@ -1,7 +1,7 @@
package main
import (
"context"
"context"
"log"
"net"
"net/http"
@ -9,40 +9,63 @@ import (
"time"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/payne8/go-libsql-dual-driver"
"github.com/rs/cors"
sqlite "github.com/ytsruh/gorm-libsql"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"
"gorm.io/gorm"
pb "{{ .AppName }}/gen/go"
pb "{{ .AppName }}/gen/go"
// the following line is used for the local database
"gorm.io/driver/sqlite"
// uncomment the following lines to switch to a production ready remote database
//libsqldb "github.com/payne8/go-libsql-dual-driver"
//sqlite "github.com/ytsruh/gorm-libsql"
)
func main() {
logger := log.New(os.Stdout, "{{ .AppName }} ", log.LstdFlags)
primaryUrl := os.Getenv("LIBSQL_DATABASE_URL")
authToken := os.Getenv("LIBSQL_AUTH_TOKEN")
tdb, err := libsqldb.NewLibSqlDB(
primaryUrl,
// libsqldb.WithMigrationFiles(migrationFiles),
libsqldb.WithAuthToken(authToken),
libsqldb.WithLocalDBName("local.db"), // will not be used for remote-only
)
// instantiate the grom ORM
/*
uncomment the following code to switch to a production ready remote database
you will need to set the environment variables
*/
// --------------------- start of remote database code ---------------------
//primaryUrl := os.Getenv("LIBSQL_DATABASE_URL")
//authToken := os.Getenv("LIBSQL_AUTH_TOKEN")
//tdb, err := libsqldb.NewLibSqlDB(
// primaryUrl,
// //libsqldb.WithMigrationFiles(migrationFiles),
// libsqldb.WithAuthToken(authToken),
// libsqldb.WithLocalDBName("local.db"), // will not be used for remote-only
//)
//if err != nil {
// logger.Printf("failed to open db %s: %s", primaryUrl, err)
// log.Fatalln(err)
// return
//}
//gormDB, err := gorm.Open(sqlite.New(sqlite.Config{Conn: tdb.DB}), &gorm.Config{})
//if err != nil {
// logger.Printf("failed to open gorm db %s: %s", primaryUrl, err)
// log.Fatalln(err)
// return
//}
// --------------------- end of remote database code ---------------------
// -- start of local database code --
gormDB, err := gorm.Open(sqlite.Open("local.db"), &gorm.Config{})
if err != nil {
logger.Printf("failed to open db %s: %s", primaryUrl, err)
log.Fatalln(err)
return
}
// Instantiate the gorm ORM
gormDB, err := gorm.Open(sqlite.New(sqlite.Config{Conn: tdb.DB}), &gorm.Config{})
if err != nil {
logger.Printf("failed to open gorm db %s: %s", primaryUrl, err)
logger.Printf("failed to open gorm db: %s", err)
log.Fatalln(err)
return
}
// -- end of local database code --
// Uncomment these lines if you need automatic migration
// err = gormDB.AutoMigrate(&pb.UserORM{})