diff --git a/Makefile b/Makefile index 0b29db4..53fd9bd 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/Makefile-for-http b/Makefile-for-http index f5ef193..33e7f17 100644 --- a/Makefile-for-http +++ b/Makefile-for-http @@ -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) diff --git a/cmd/sponge/commands/generate/common.go b/cmd/sponge/commands/generate/common.go index c923d26..1b85411 100644 --- a/cmd/sponge/commands/generate/common.go +++ b/cmd/sponge/commands/generate/common.go @@ -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" @@ -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 @@ -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"), diff --git a/cmd/sponge/commands/generate/grpc-http-pb.go b/cmd/sponge/commands/generate/grpc-http-pb.go index 1a2ebdd..5e2f3f2 100644 --- a/cmd/sponge/commands/generate/grpc-http-pb.go +++ b/cmd/sponge/commands/generate/grpc-http-pb.go @@ -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...) } diff --git a/cmd/sponge/commands/generate/http-pb.go b/cmd/sponge/commands/generate/http-pb.go index b4f0458..f7afd9f 100644 --- a/cmd/sponge/commands/generate/http-pb.go +++ b/cmd/sponge/commands/generate/http-pb.go @@ -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...) } diff --git a/cmd/sponge/commands/generate/http.go b/cmd/sponge/commands/generate/http.go index 5326912..6557982 100644 --- a/cmd/sponge/commands/generate/http.go +++ b/cmd/sponge/commands/generate/http.go @@ -99,7 +99,6 @@ Examples: codes: codes, outPath: outPath, isExtendedAPI: sqlArgs.IsExtendedAPI, - serverType: codeNameHTTP, suitedMonoRepo: suitedMonoRepo, } @@ -183,7 +182,6 @@ type httpGenerator struct { isEmbed bool isExtendedAPI bool suitedMonoRepo bool - serverType string fields []replacer.Field } @@ -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...) } diff --git a/cmd/sponge/commands/generate/rpc-gw-pb.go b/cmd/sponge/commands/generate/rpc-gw-pb.go index 1ae70de..0324ec9 100644 --- a/cmd/sponge/commands/generate/rpc-gw-pb.go +++ b/cmd/sponge/commands/generate/rpc-gw-pb.go @@ -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...) } diff --git a/cmd/sponge/commands/generate/rpc-pb.go b/cmd/sponge/commands/generate/rpc-pb.go index 0d5b2ca..f170204 100644 --- a/cmd/sponge/commands/generate/rpc-pb.go +++ b/cmd/sponge/commands/generate/rpc-pb.go @@ -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...) } diff --git a/cmd/sponge/commands/generate/rpc.go b/cmd/sponge/commands/generate/rpc.go index 2b9dc6a..453c2fa 100644 --- a/cmd/sponge/commands/generate/rpc.go +++ b/cmd/sponge/commands/generate/rpc.go @@ -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()) @@ -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...) } diff --git a/cmd/sponge/commands/patch/copy-go-mod.go b/cmd/sponge/commands/patch/copy-go-mod.go index 0a797d5..07f80f3 100644 --- a/cmd/sponge/commands/patch/copy-go-mod.go +++ b/cmd/sponge/commands/patch/copy-go-mod.go @@ -3,6 +3,8 @@ package patch import ( "errors" "fmt" + "os" + "strings" "github.com/fatih/color" "github.com/spf13/cobra" @@ -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{ @@ -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) @@ -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 } diff --git a/scripts/init.sh b/scripts/init.sh deleted file mode 100644 index 91ee335..0000000 --- a/scripts/init.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash - -goModFile="go.mod" -thirdPartyProtoDir="third_party" - -function checkResult() { - result=$1 - if [ ${result} -ne 0 ]; then - exit ${result} - fi -} - -if [ ! -f "../$goModFile" ]; then - sponge patch copy-go-mod - checkResult $? - mv -f go.mod .. - mv -f go.sum .. -fi - -if [ ! -d "../$thirdPartyProtoDir" ]; then - sponge patch copy-third-party-proto - checkResult $? - mv -f $thirdPartyProtoDir .. -fi diff --git a/scripts/patch-mono.sh b/scripts/patch-mono.sh new file mode 100644 index 0000000..4e3a2e4 --- /dev/null +++ b/scripts/patch-mono.sh @@ -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 diff --git a/scripts/protoc.sh b/scripts/protoc.sh index bc4f512..6869830 100644 --- a/scripts/protoc.sh +++ b/scripts/protoc.sh @@ -1,6 +1,5 @@ #!/bin/bash -# the directory where the proto files are located protoBasePath="api" allProtoFiles="" diff --git a/scripts/run.sh b/scripts/run.sh index 5417684..966e586 100644 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -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() { diff --git a/scripts/swag-docs.sh b/scripts/swag-docs.sh index c5dd558..1e1eff4 100644 --- a/scripts/swag-docs.sh +++ b/scripts/swag-docs.sh @@ -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}') diff --git a/test/auto-test/clean_mono_repo.sh b/test/auto-test/clean_mono_repo.sh index ee03d45..a4b5223 100644 --- a/test/auto-test/clean_mono_repo.sh +++ b/test/auto-test/clean_mono_repo.sh @@ -1,6 +1,7 @@ #!/bin/bash rm -rf go.mod go.sum \ +api \ third_party \ mono_01_http_mysql \ mono_02_http_postgresql \ diff --git a/test/auto-test/generate_mono_repo_test.sh b/test/auto-test/generate_mono_repo_test.sh index 1242afc..c56904b 100644 --- a/test/auto-test/generate_mono_repo_test.sh +++ b/test/auto-test/generate_mono_repo_test.sh @@ -42,14 +42,6 @@ function checkResult() { fi } -function checkGoModule() { - if [ -f "go.mod" ]; then - return - else - go mod init edusys - fi -} - function printTestResult() { local errCount=$1 local serverDir=$2 @@ -159,8 +151,6 @@ function generate_http_mysql() { echo -e "\n${colorCyan}sponge web handler --db-driver=mysql --db-dsn=$mysqlDsn --db-table=$mysqlTable2 --embed=true --suited-mono-repo=true --extended-api=$isExtended --out=$outDir ${markEnd}" sponge web handler --db-driver=mysql --db-dsn=$mysqlDsn --db-table=$mysqlTable2 --embed=true --suited-mono-repo=true --extended-api=$isExtended --out=$outDir checkResult $? - - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -190,7 +180,6 @@ function generate_http_postgresql() { echo -e "\n${colorCyan}sponge web handler --db-driver=postgresql --db-dsn=$postgresqlDsn --db-table=$postgresqlTable2 --suited-mono-repo=true --extended-api=$isExtended --out=$outDir ${markEnd}" sponge web handler --db-driver=postgresql --db-dsn=$postgresqlDsn --db-table=$postgresqlTable2 --suited-mono-repo=true --extended-api=$isExtended --out=$outDir checkResult $? - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -222,7 +211,6 @@ function generate_http_sqlite() { checkResult $? sed -E -i 's/\\\\sql\\\\/\\\\\.\.\\\\\sql\\\\/g' ${outDir}/configs/${serverName}.yml - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -252,7 +240,6 @@ function generate_http_mongodb() { echo -e "\n${colorCyan}sponge web handler --db-driver=mongodb --db-dsn=$mongodbDsn --db-table=$mongodbCollection2 --suited-mono-repo=true --extended-api=$isExtended --out=$outDir ${markEnd}" sponge web handler --db-driver=mongodb --db-dsn=$mongodbDsn --db-table=$mongodbCollection2 --suited-mono-repo=true --extended-api=$isExtended --out=$outDir checkResult $? - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -284,8 +271,6 @@ function generate_grpc_mysql() { echo -e "\n${colorCyan}sponge micro service --db-driver=mysql --db-dsn=$mysqlDsn --db-table=$mysqlTable2 --suited-mono-repo=true --extended-api=$isExtended --out=$outDir ${markEnd}" sponge micro service --db-driver=mysql --db-dsn=$mysqlDsn --db-table=$mysqlTable2 --suited-mono-repo=true --extended-api=$isExtended --out=$outDir checkResult $? - - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -315,8 +300,6 @@ function generate_grpc_postgresql() { echo -e "\n${colorCyan}sponge micro service --db-driver=postgresql --db-dsn=$postgresqlDsn --db-table=$postgresqlTable2 --suited-mono-repo=true --extended-api=$isExtended --out=$outDir ${markEnd}" sponge micro service --db-driver=postgresql --db-dsn=$postgresqlDsn --db-table=$postgresqlTable2 --suited-mono-repo=true --extended-api=$isExtended --out=$outDir checkResult $? - - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -348,7 +331,6 @@ function generate_grpc_sqlite() { checkResult $? sed -E -i 's/\\\\sql\\\\/\\\\\.\.\\\\\sql\\\\/g' ${outDir}/configs/${serverName}.yml - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -378,8 +360,6 @@ function generate_grpc_mongodb() { echo -e "\n${colorCyan}sponge micro service --db-driver=mongodb --db-dsn=$mongodbDsn --db-table=$mongodbCollection2 --suited-mono-repo=true --extended-api=$isExtended --out=$outDir ${markEnd}" sponge micro service --db-driver=mongodb --db-dsn=$mongodbDsn --db-table=$mongodbCollection2 --suited-mono-repo=true --extended-api=$isExtended --out=$outDir checkResult $? - - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -414,7 +394,6 @@ function generate_http_pb_mysql() { mysqlDsnTmp=$(echo "$mysqlDsn" | sed -E 's/\(/\\\(/g' | sed -E 's/\)/\\\)/g' | sed -E 's/\//\\\//g') sed -E -i "s/root:123456@\(192.168.3.37:3306\)\/account/${mysqlDsnTmp}/g" ${outDir}/configs/${serverName}.yml - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -451,7 +430,6 @@ function generate_http_pb_mongodb() { sed -E -i 's/mysql:/mongodb:/g' ${outDir}/configs/${serverName}.yml mongodbDsnTmp=$(echo "$mongodbDsn" | sed -E 's/\(/\\\(/g' | sed -E 's/\)/\\\)/g' | sed -E 's/\//\\\//g') sed -E -i "s/root:123456@\(192.168.3.37:3306\)\/account/${mongodbDsnTmp}/g" ${outDir}/configs/${serverName}.yml - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -488,7 +466,6 @@ function generate_grpc_pb_mysql() { mysqlDsnTmp=$(echo "$mysqlDsn" | sed -E 's/\(/\\\(/g' | sed -E 's/\)/\\\)/g' | sed -E 's/\//\\\//g') sed -E -i "s/root:123456@\(192.168.3.37:3306\)\/account/${mysqlDsnTmp}/g" ${outDir}/configs/${serverName}.yml - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -525,7 +502,6 @@ function generate_grpc_pb_mongodb() { sed -E -i 's/mysql:/mongodb:/g' ${outDir}/configs/${serverName}.yml mongodbDsnTmp=$(echo "$mongodbDsn" | sed -E 's/\(/\\\(/g' | sed -E 's/\)/\\\)/g' | sed -E 's/\//\\\//g') sed -E -i "s/root:123456@\(192.168.3.37:3306\)\/account/${mongodbDsnTmp}/g" ${outDir}/configs/${serverName}.yml - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -562,7 +538,6 @@ function generate_grpc_http_pb_mysql() { mysqlDsnTmp=$(echo "$mysqlDsn" | sed -E 's/\(/\\\(/g' | sed -E 's/\)/\\\)/g' | sed -E 's/\//\\\//g') sed -E -i "s/root:123456@\(192.168.3.37:3306\)\/account/${mysqlDsnTmp}/g" ${outDir}/configs/${serverName}.yml - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -599,7 +574,6 @@ function generate_grpc_http_pb_mongodb() { sed -E -i 's/mysql:/mongodb:/g' ${outDir}/configs/${serverName}.yml mongodbDsnTmp=$(echo "$mongodbDsn" | sed -E 's/\(/\\\(/g' | sed -E 's/\)/\\\)/g' | sed -E 's/\//\\\//g') sed -E -i "s/root:123456@\(192.168.3.37:3306\)\/account/${mongodbDsnTmp}/g" ${outDir}/configs/${serverName}.yml - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -628,7 +602,6 @@ function generate_http_pb_mixed() { echo -e "\n${colorCyan}sponge web http-pb --server-name=$serverName --module-name=$projectName --project-name=$projectName --protobuf-file=./files/mixed.proto --suited-mono-repo=true --out=$outDir ${markEnd}" sponge web http-pb --server-name=$serverName --module-name=$projectName --project-name=$projectName --protobuf-file=./files/mixed.proto --suited-mono-repo=true --out=$outDir checkResult $? - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -654,7 +627,6 @@ function generate_grpc_pb_mixed() { echo -e "\n${colorCyan}sponge micro rpc-pb --server-name=$serverName --module-name=$projectName --project-name=$projectName --protobuf-file=./files/mixed.proto --suited-mono-repo=true --out=$outDir ${markEnd}" sponge micro rpc-pb --server-name=$serverName --module-name=$projectName --project-name=$projectName --protobuf-file=./files/mixed.proto --suited-mono-repo=true --out=$outDir checkResult $? - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -682,7 +654,6 @@ function generate_grpc_gw_pb_mixed() { echo -e "\n${colorCyan}sponge micro rpc-gw-pb --server-name=$serverName --module-name=$projectName --project-name=$projectName --protobuf-file=./files/mixed.proto --suited-mono-repo=true --out=$outDir ${markEnd}" sponge micro rpc-gw-pb --server-name=$serverName --module-name=$projectName --project-name=$projectName --protobuf-file=./files/mixed.proto --suited-mono-repo=true --out=$outDir checkResult $? - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then @@ -712,8 +683,6 @@ function generate_grpc_gw_pb() { echo -e "\n${colorCyan}sponge micro rpc-conn --rpc-server-name=user --suited-mono-repo=true --out=$outDir ${markEnd}" sponge micro rpc-conn --rpc-server-name=user --suited-mono-repo=true --out=$outDir checkResult $? - - checkGoModule fi if [ "$isOnlyGenerateCode" == "true" ]; then