Skip to content

Commit

Permalink
modify scripts for mono-repo
Browse files Browse the repository at this point in the history
  • Loading branch information
zhufuyi committed Sep 20, 2024
1 parent 9218f10 commit 621bb71
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 96 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ graph:
.PHONY: docs
# Generate swagger docs, only for ⓵ Web services created based on sql
docs:
go mod tidy
@gofmt -s -w .
@bash scripts/swag-docs.sh $(HOST)
# delete the templates code end

Expand Down
4 changes: 1 addition & 3 deletions Makefile-for-http
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ graph:


.PHONY: docs
# Generate swagger docs, , only for ⓵ Web services created based on sql
# Generate swagger docs, only for ⓵ Web services created based on sql
docs:
go mod tidy
@gofmt -s -w .
@bash scripts/swag-docs.sh $(HOST)


Expand Down
42 changes: 30 additions & 12 deletions cmd/sponge/commands/generate/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ const (
undeterminedDBDriver = "undetermined" // used in services created based on protobuf.

// code name
codeNameModel = "model"
codeNameCache = "cache"
codeNameDao = "dao"
codeNameHandler = "handler"
codeNameHandlerPb = "handler-pb"
codeNameService = "service"
codeNameServiceHTTP = "service-handler"
codeNameHTTP = "http"
codeNameHTTPPb = "http-pb"
codeNameGRPC = "grpc"
codeNameHTTPPb = "http-pb"
codeNameGRPCPb = "grpc-pb"
codeNameGRPCGW = "grpc-gw-pb"
codeNameGRPCHTTP = "grpc-http-pb"
codeNameGRPCConn = "grpc-conn"
codeNameHandler = "handler"
codeNameHandlerPb = "handler-pb"
codeNameService = "service"
codeNameServiceHTTP = "service-handler"
codeNameDao = "dao"
codeNameProtobuf = "protobuf"
codeNameModel = "model"
codeNameGRPCConn = "grpc-conn"
codeNameCache = "cache"

wellPrefix = "## "
mgoSuffix = ".mgo"
Expand Down Expand Up @@ -629,17 +629,27 @@ func moveProtoFileToAPIDir(moduleName string, serverName string, suitedMonoRepo

var (
// for protoc.sh and protoc-doc.sh
monoRepoAPIPath = `bash scripts/init.sh
monoRepoAPIPath = `bash scripts/patch-mono.sh
cd ..
protoBasePath="api"`

// for patch-mono.sh
monoRepoHTTPPatch = `bash scripts/patch-mono.sh
HOST_ADDR=$1`

// for patch.sh
typePbShellCode = ` if [ ! -d "../api/types" ]; then
typePbShellCode = `
if [ ! -d "../api/types" ]; then
sponge patch gen-types-pb --out=./
checkResult $?
mv -f api/types ../api
rmdir api
fi`
dupCodeMark = "--dir=internal/ecode"

dupCodeMark = "--dir=internal/ecode"

adaptDupCode = func(serverType string, serverName string) string {
if serverType == codeNameHTTP {
return dupCodeMark
Expand Down Expand Up @@ -710,6 +720,14 @@ func serverCodeFields(serverType string, moduleName string, serverName string) [
Old: `protoBasePath="api"`,
New: monoRepoAPIPath,
},
{
Old: `HOST_ADDR=$1`,
New: monoRepoHTTPPatch,
},
{
Old: `genServerType=$1`,
New: fmt.Sprintf(`genServerType="%s"`, serverType),
},
{
Old: fmt.Sprintf("go get %s@", moduleName),
New: fmt.Sprintf("go get %s@", "github.com/zhufuyi/sponge"),
Expand Down
2 changes: 1 addition & 1 deletion cmd/sponge/commands/generate/grpc-http-pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (g *httpAndGRPCPbGenerator) addFields(r replacer.Replacer) []replacer.Field
}...)

if g.suitedMonoRepo {
fs := serverCodeFields(r.GetOutputDir(), g.moduleName, g.serverName)
fs := serverCodeFields(codeNameGRPCHTTP, g.moduleName, g.serverName)
fields = append(fields, fs...)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/sponge/commands/generate/http-pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (g *httpPbGenerator) addFields(r replacer.Replacer) []replacer.Field {
}...)

if g.suitedMonoRepo {
fs := serverCodeFields(r.GetOutputDir(), g.moduleName, g.serverName)
fs := serverCodeFields(codeNameHTTPPb, g.moduleName, g.serverName)
fields = append(fields, fs...)
}

Expand Down
4 changes: 1 addition & 3 deletions cmd/sponge/commands/generate/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ Examples:
codes: codes,
outPath: outPath,
isExtendedAPI: sqlArgs.IsExtendedAPI,
serverType: codeNameHTTP,

suitedMonoRepo: suitedMonoRepo,
}
Expand Down Expand Up @@ -183,7 +182,6 @@ type httpGenerator struct {
isEmbed bool
isExtendedAPI bool
suitedMonoRepo bool
serverType string

fields []replacer.Field
}
Expand Down Expand Up @@ -498,7 +496,7 @@ func (g *httpGenerator) addFields(r replacer.Replacer) []replacer.Field {
}...)

if g.suitedMonoRepo {
fs := serverCodeFields(g.serverType, g.moduleName, g.serverName)
fs := serverCodeFields(codeNameHTTP, g.moduleName, g.serverName)
fields = append(fields, fs...)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/sponge/commands/generate/rpc-gw-pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (g *rpcGwPbGenerator) addFields(r replacer.Replacer) []replacer.Field {
}...)

if g.suitedMonoRepo {
fs := serverCodeFields(r.GetOutputDir(), g.moduleName, g.serverName)
fs := serverCodeFields(codeNameGRPCGW, g.moduleName, g.serverName)
fields = append(fields, fs...)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/sponge/commands/generate/rpc-pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (g *rpcPbGenerator) addFields(r replacer.Replacer) []replacer.Field {
}...)

if g.suitedMonoRepo {
fs := serverCodeFields(r.GetOutputDir(), g.moduleName, g.serverName)
fs := serverCodeFields(codeNameGRPCPb, g.moduleName, g.serverName)
fields = append(fields, fs...)
}

Expand Down
8 changes: 1 addition & 7 deletions cmd/sponge/commands/generate/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,6 @@ func (g *rpcGenerator) generateCode() (string, error) {
if err := moveProtoFileToAPIDir(g.moduleName, g.serverName, g.suitedMonoRepo, r.GetOutputDir()); err != nil {
return "", err
}
//apiDir := g.serverName + gofile.GetPathDelimiter() + "api"
//protoFiles, _ := gofile.ListFiles(apiDir, gofile.WithNoAbsolutePath(), gofile.WithSuffix(".proto"))
//if err := saveProtobufFiles(g.moduleName, g.serverName, g.suitedMonoRepo, r.GetOutputDir(), protoFiles); err != nil {
// return "", err
//}
//_ = os.RemoveAll(apiDir)
}
_ = saveGenInfo(g.moduleName, g.serverName, g.suitedMonoRepo, r.GetOutputDir())

Expand Down Expand Up @@ -521,7 +515,7 @@ func (g *rpcGenerator) addFields(r replacer.Replacer) []replacer.Field {
}...)

if g.suitedMonoRepo {
fs := serverCodeFields(r.GetOutputDir(), g.moduleName, g.serverName)
fs := serverCodeFields(codeNameGRPC, g.moduleName, g.serverName)
fields = append(fields, fs...)
}

Expand Down
23 changes: 16 additions & 7 deletions cmd/sponge/commands/patch/copy-go-mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package patch
import (
"errors"
"fmt"
"os"
"strings"

"github.com/fatih/color"
"github.com/spf13/cobra"
Expand All @@ -14,9 +16,10 @@ import (
// CopyGOModCommand copy go mod files
func CopyGOModCommand() *cobra.Command {
var (
moduleName string // module name for go.mod
outPath string // output directory
isLogExist bool
moduleName string // module name for go.mod
outPath string // output directory
isLogExist bool
isForceReplace bool
)

cmd := &cobra.Command{
Expand Down Expand Up @@ -44,10 +47,15 @@ Examples:

goModFile := outPath + gofile.GetPathDelimiter() + "go.mod"
if gofile.IsExists(goModFile) {
if isLogExist {
fmt.Printf("%s already exists, skip copying.\n", goModFile)
if !isForceReplace {
if isLogExist {
fmt.Printf("%s already exists, skip copying.\n", goModFile)
}
return nil
}
return nil
// delete the go.mod and go.sum file if it exists
_ = os.RemoveAll(goModFile)
_ = os.RemoveAll(strings.TrimSuffix(goModFile, ".mod") + ".sum")
}

out, err := runCopyGoModCommand(moduleName, outPath)
Expand All @@ -62,7 +70,8 @@ Examples:

cmd.Flags().StringVarP(&moduleName, "module-name", "m", "", "module-name is the name of the module in the go.mod file")
cmd.Flags().StringVarP(&outPath, "out", "o", ".", "output directory")
cmd.Flags().BoolVarP(&isLogExist, "is-log-exist", "l", false, "is log file exist")
cmd.Flags().BoolVarP(&isLogExist, "is-log-exist", "l", false, "whether to log file exist")
cmd.Flags().BoolVarP(&isForceReplace, "is-force-replace", "f", false, "whether to force replace the go.mod file")

return cmd
}
Expand Down
24 changes: 0 additions & 24 deletions scripts/init.sh

This file was deleted.

36 changes: 36 additions & 0 deletions scripts/patch-mono.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

goModFile="go.mod"
thirdPartyProtoDir="third_party"
genServerType=$1

function checkResult() {
result=$1
if [ ${result} -ne 0 ]; then
exit ${result}
fi
}

if [ ! -f "../$goModFile" ]; then
sponge patch copy-go-mod -f
checkResult $?
mv -f go.mod ..
mv -f go.sum ..
fi

if [ "$genServerType"x != "http"x ]; then
if [ ! -d "../$thirdPartyProtoDir" ]; then
sponge patch copy-third-party-proto
checkResult $?
mv -f $thirdPartyProtoDir ..
fi
fi

if [ "$genServerType"x = "grpc"x ]; then
if [ ! -d "../api/types" ]; then
sponge patch gen-types-pb --out=.
checkResult $?
mv -f api/types ../api
rmdir api
fi
fi
1 change: 0 additions & 1 deletion scripts/protoc.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/bash

# the directory where the proto files are located
protoBasePath="api"
allProtoFiles=""

Expand Down
9 changes: 7 additions & 2 deletions scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ serverName="serverNameExample_mixExample"

binaryFile="cmd/${serverName}/${serverName}"

if [ -f "${serverName}" ] ;then
rm "${serverName}"
osType=$(uname -s)
if [ "${osType%%_*}"x = "MINGW64"x ];then
binaryFile="${binaryFile}.exe"
fi

if [ -f "${binaryFile}" ] ;then
rm "${binaryFile}"
fi

function checkResult() {
Expand Down
5 changes: 5 additions & 0 deletions scripts/swag-docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ function checkResult() {
fi
}

echo "go mod tidy"
go mod tidy
checkResult $?
gofmt -s -w .

# change host addr
if [ "X${HOST_ADDR}" = "X" ];then
HOST_ADDR=$(cat cmd/serverNameExample_mixExample/main.go | grep "@host" | awk '{print $3}')
Expand Down
1 change: 1 addition & 0 deletions test/auto-test/clean_mono_repo.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

rm -rf go.mod go.sum \
api \
third_party \
mono_01_http_mysql \
mono_02_http_postgresql \
Expand Down
Loading

0 comments on commit 621bb71

Please sign in to comment.