fix windows support

This commit is contained in:
anserme
2021-06-22 15:20:07 +08:00
parent 8ca47f743d
commit 2c5683cd14
7 changed files with 118 additions and 9 deletions

View File

@@ -3,8 +3,10 @@ package generator_handler
import (
"bytes"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"github.com/xinliangnote/go-gin-api/configs"
"github.com/xinliangnote/go-gin-api/internal/pkg/core"
@@ -14,9 +16,12 @@ type gormExecuteRequest struct {
Tables string `form:"tables"`
}
const gormgenSh = "./scripts/gormgen.sh"
func (h *handler) GormExecute() core.HandlerFunc {
dir, _ := os.Getwd()
projectPath := strings.Replace(dir, "\\", "/", -1)
gormgenSh := projectPath + "/scripts/gormgen.sh"
gormgenBat := projectPath + "/scripts/gormgen.bat"
return func(c core.Context) {
req := new(gormExecuteRequest)
if err := c.ShouldBindPostForm(req); err != nil {
@@ -26,11 +31,12 @@ func (h *handler) GormExecute() core.HandlerFunc {
mysqlConf := configs.Get().MySQL.Read
shellPath := fmt.Sprintf("%s %s %s %s %s %s", gormgenSh, mysqlConf.Addr, mysqlConf.User, mysqlConf.Pass, mysqlConf.Name, req.Tables)
batPath := fmt.Sprintf("%s %s %s %s %s %s", gormgenBat, mysqlConf.Addr, mysqlConf.User, mysqlConf.Pass, mysqlConf.Name, req.Tables)
command := new (exec.Cmd)
command := new(exec.Cmd)
if runtime.GOOS == "windows" {
command = exec.Command("cmd", "/C", shellPath)
command = exec.Command("cmd", "/C", batPath)
} else {
// runtime.GOOS = linux or darwin
command = exec.Command("/bin/bash", "-c", shellPath)

View File

@@ -3,8 +3,10 @@ package generator_handler
import (
"bytes"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"github.com/xinliangnote/go-gin-api/internal/pkg/core"
)
@@ -13,22 +15,25 @@ type handlerExecuteRequest struct {
Name string `form:"name"`
}
const handlergenSh = "./scripts/handlergen.sh"
func (h *handler) HandlerExecute() core.HandlerFunc {
dir, _ := os.Getwd()
projectPath := strings.Replace(dir, "\\", "/", -1)
handlergenSh := projectPath + "/scripts/handlergen.sh"
handlergenBat := projectPath + "./scripts/handlergen.bat"
return func(c core.Context) {
req := new(handlerExecuteRequest)
if err := c.ShouldBindPostForm(req); err != nil {
c.Payload("参数传递有误")
return
}
shellPath := fmt.Sprintf("%s %s", handlergenSh, req.Name)
batPath := fmt.Sprintf("%s %s", handlergenBat, req.Name)
command := new (exec.Cmd)
command := new(exec.Cmd)
if runtime.GOOS == "windows" {
command = exec.Command("cmd", "/C", shellPath)
command = exec.Command("cmd", "/C", batPath)
} else {
// runtime.GOOS = linux or darwin
command = exec.Command("/bin/bash", "-c", shellPath)

45
scripts/gormgen.bat Normal file
View File

@@ -0,0 +1,45 @@
@echo off
chcp 65001
echo.
echo Regenerating file
echo.
go run -v ./cmd/mysqlmd/main.go -addr %1 -user %2 -pass %3 -name %4 -tables %5
if %errorlevel% == 1 (
echo.
echo failed!!!
exit 1
)
echo.
echo create curd code :
echo.
go build -o gormgen ./cmd/gormgen/main.go
if %errorlevel% == 1 (
echo.
echo failed!!!
exit 1
)
move gormgen $GOPATH/bin
if %errorlevel% == 1 (
echo.
echo failed!!!
exit 1
)
go generate ./...
if %errorlevel% == 1 (
echo.
echo failed!!!
exit 1
)
echo.
echo Formatting code
echo.
go run -v ./cmd/mfmt/main.go
if %errorlevel% == 1 (
echo.
echo failed!!!
exit 1
)
echo.
echo Done.

10
scripts/gqlgen.bat Normal file
View File

@@ -0,0 +1,10 @@
@echo off
chcp 65001
echo.
echo Regenerating gqlgen file
echo.
del internal/graph/generated/generated.go internal/graph/model/generated.go internal/graph/resolvers/generated/generated.go
go run -v github.com/99designs/gqlgen $1
echo.
echo Done.
echo.

22
scripts/handlergen.bat Normal file
View File

@@ -0,0 +1,22 @@
@echo off
chcp 65001
echo.
echo Regenerating handler file
echo.
go run -v ./cmd/handlergen/main.go -handler %1
if %errorlevel% == 1 (
echo.
echo failed!!!
exit 1
)
echo.
echo Formatting code
echo.
go run -v ./cmd/mfmt/main.go
if %errorlevel% == 1 (
echo.
echo failed!!!
exit 1
)
echo.
echo Done.

13
scripts/init.bat Normal file
View File

@@ -0,0 +1,13 @@
@echo off
chcp 65001
echo.
echo Init db
echo.
go run -v ./cmd/init/db/main.go -addr %1 -user %2 -pass %3 -name %4
if %errorlevel% == 1 (
echo.
echo failed!!!
exit 1
)
echo.
echo Done.

8
scripts/swagger.bat Normal file
View File

@@ -0,0 +1,8 @@
@echo off
chcp 65001
echo.
echo Regenerating swagger doc
echo.
go run -v github.com/swaggo/swag/cmd/swag init
echo.
echo Done.