Skip to content

Commit

Permalink
improve and fix #2060 (#2063)
Browse files Browse the repository at this point in the history
* fix(new): service generation closes #2060

* refactor(new): more advanced makefile
universal makefile usage, `make init` to install `protoc`
  • Loading branch information
alex-dna-tech committed Apr 16, 2024
1 parent 7f4bc57 commit c41ab10
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 34 deletions.
18 changes: 12 additions & 6 deletions cmd/client/new/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ import (

func protoComments(goDir, alias string) []string {
return []string{
"\ndownload protoc zip packages (protoc-$VERSION-$PLATFORM.zip) and install:\n",
"visit https://github.com/protocolbuffers/protobuf/releases",
"\ncompile the proto file " + alias + ".proto:\n",
"cd " + alias,
"make init",
"go mod vendor",
"make proto\n",
"go mod tidy",
"make proto",
"micro run .\n",
}
}

Expand All @@ -35,6 +32,8 @@ type config struct {
Alias string
// github.com/micro/foo
Dir string
// UseGoVersion
GoVersion string
// $GOPATH/src/github.com/micro/foo
GoDir string
// $GOPATH
Expand Down Expand Up @@ -171,17 +170,24 @@ func Run(ctx *cli.Context) error {
}
goDir = filepath.Join(goPath, "src", path.Clean(dir))

goVersion := runtime.Version()
if strings.HasPrefix(goVersion, "go") {
goVersion = strings.TrimPrefix(goVersion, "go")
}

c := config{
Alias: dir,
Comments: protoComments(goDir, dir),
Dir: dir,
GoVersion: goVersion,
GoDir: goDir,
GoPath: goPath,
UseGoPath: false,
Files: []file{
{"main.go", tmpl.MainSRV},
{"handler/" + dir + ".go", tmpl.HandlerSRV},
{"proto/" + dir + ".proto", tmpl.ProtoSRV},
{"dep-install.mk", tmpl.DepInstall},
{"Makefile", tmpl.Makefile},
{"README.md", tmpl.Readme},
{".gitignore", tmpl.GitIgnore},
Expand Down
61 changes: 61 additions & 0 deletions cmd/client/new/template/dep-install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package template

var (
DepInstall = `PROTOC_GEN_GO := $(GOBIN)/protoc-gen-go
PROTOC_GEN_MICRO := $(GOBIN)/protoc-gen-micro
PROTOC = $(shell which protoc || echo "$(GOBIN)/protoc")
# Generate protoc latest binary url
PROTOC_RELEASE_BIN := protoc
ifeq ($(GOOS), linux)
ifeq ($(GOARCH), amd64)
PROTOC_RELEASE = linux-x86_64
else ifeq ($(GOARCH), arm64)
PROTOC_RELEASE = linux-aarch_64
else ifeq ($(GOARCH), 386)
PROTOC_RELEASE = linux-x86_32
else
echo >&2 "only support amd64, arm64 or 386 GOARCH" && exit 1
endif
else ifeq ($(GOOS), darwin)
ifeq ($(GOARCH), amd64)
PROTOC_RELEASE = osx-x86_64
else ifeq ($(GOARCH), arm64)
PROTOC_RELEASE = osx-aarch_64
else
echo >&2 "only support amd64 or arm64 GOARCH" && exit 1
endif
else ifeq ($(GOOS), windows)
ifeq ($(GOARCH), amd64)
PROTOC_RELEASE = win64
else ifeq ($(GOARCH), 386)
PROTOC_RELEASE = win32
else
echo >&2 "only support amd64 or 386 GOARCH" && exit 1
endif
PROTOC_RELEASE_BIN = protoc.exe
endif
PROTOC_LATEST = https://github.com/protocolbuffers/protobuf/releases/latest
PROTOC_VERSION = $(shell curl -Ls -o /dev/null -w %{url_effective} $(PROTOC_LATEST) | sed 's|.*/v||')
PROTOC_RELEASE_URL = https://github.com/protocolbuffers/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-$(PROTOC_RELEASE).zip
# Install dependencies
.PHONY: init
init: $(PROTOC) $(PROTOC_GEN_GO) $(PROTOC_GEN_MICRO)
# Install protoc latest binary
# from release https://github.com/protocolbuffers/protobuf/releases
# to $(GOBIN)
$(PROTOC):
curl -LSs $(PROTOC_RELEASE_URL) -o protoc.zip
unzip -qqj protoc.zip "bin/$(PROTOC_RELEASE_BIN)" -d "$(GOBIN)" && rm protoc.zip
# Install protoc-gen-go
$(PROTOC_GEN_GO):
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
# Install protoc-gen-micro
$(PROTOC_GEN_MICRO):
go install micro.dev/v4/cmd/protoc-gen-micro@master
`
)
47 changes: 29 additions & 18 deletions cmd/client/new/template/makefile.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
package template

var (
Makefile = `
GOPATH:=$(shell go env GOPATH)
.PHONY: init
init:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install micro.dev/v4/cmd/protoc-gen-micro@latest
go install micro.dev/v4/cmd/protoc-gen-openapi@latest
.PHONY: api
api:
protoc --openapi_out=. --proto_path=. proto/{{.Alias}}.proto
Makefile = `GOOS := $(shell go env GOOS)
GOARCH := $(shell go env GOARCH)
GOBIN := $(shell go env GOPATH)/bin
NAME := $(shell basename $(CURDIR))
ifeq ($(GOOS),windows)
TARGET := $(NAME).exe
else
TARGET := $(NAME)
endif
PROTO_FILES := $(wildcard proto/*.proto)
PROTO_GO := $(PROTO_FILES:%.proto=%.pb.go)
PROTO_GO_MICRO := $(PROTO_FILES:%.proto=%.pb.micro.go)
.DEFAULT_GOAL := $(TARGET)
# Build binary
$(TARGET): proto/$(NAME).pb.micro.go
go build -o $(TARGET) *.go
.PHONY: clean
clean:
rm -f $(TARGET) $(PROTO_GO) $(PROTO_GO_MICRO)
include dep-install.mk
.PHONY: proto
proto:
protoc --proto_path=. --micro_out=. --go_out=:. proto/{{.Alias}}.proto
.PHONY: build
build:
go build -o {{.Alias}} *.go
proto: proto/$(NAME).pb.micro.go
%.pb.go %.pb.micro.go:
protoc --proto_path=. --go_out=:. --micro_out=. $(*).proto
.PHONY: test
test:
go test -v ./... -cover
.PHONY: docker
docker:
docker build . -t {{.Alias}}:latest
docker build . -t $(NAME):latest
`
)
12 changes: 2 additions & 10 deletions cmd/client/new/template/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@ package template
var (
Module = `module {{.Dir}}
go 1.18
go {{.GoVersion}}
require (
github.com/golang/protobuf latest
micro.dev/v4 latest
google.golang.org/protobuf latest
)
// This can be removed once etcd becomes go gettable, version 3.4 and 3.5 is not,
// see https://github.com/etcd-io/etcd/issues/11154 and https://github.com/etcd-io/etcd/issues/11931.
replace google.golang.org/grpc => google.golang.org/grpc v1.27.1
require micro.dev/v4 latest
`
)

0 comments on commit c41ab10

Please sign in to comment.