Skip to content

Instantly share code, notes, and snippets.

@abserari
Last active June 23, 2022 01:17
Show Gist options
  • Save abserari/349f7a15bd37c8de22f4c343d3b6e3d9 to your computer and use it in GitHub Desktop.
Save abserari/349f7a15bd37c8de22f4c343d3b6e3d9 to your computer and use it in GitHub Desktop.
Makefile Template from Temporal
default: help
##@ Main targets
install: update-tools ## Install all tools and builds binaries.
bins: ## Rebuild binaries (used by Dockerfile).
all: update-tools clean proto bins check test ## Install all tools, recompile proto files, run all possible checks and tests (long but comprehensive).
ci-build: update-tools shell-check check proto mocks gomodtidy ensure-no-changes ## Used by Buildkite.
clean: ## Delete all build artefacts.
proto: protoc proto-mocks ## Recompile proto files.
########################################################################
.PHONY: proto
#################### Arguments ####################
GO := go
GIT_COMMIT ?= `git rev-parse --short HEAD`
IMG ?= 'exampleUser/example:latest'
BINDIR ?= $(CURDIR)/bin
MODULE_ROOT := $(lastword $(shell grep -e "^module " go.mod))
COLOR := "\e[1;36m%s\e[0m\n"
RED := "\e[1;31m%s\e[0m\n"
PROTO_ROOT := proto
PROTO_FILES = $(shell find ./$(PROTO_ROOT) -name "*.proto")
PROTO_OUT := .
ALL_SCRIPTS := $(shell find . -name "*.sh")
############################################################
##@ General
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
##@ Docker
docker-build: ## Build docker image.
@docker build -t $(IMG) $(CURDIR) --target=production
docker-push: ## Push docker image to registry.
@docker login docker.sangfor.com -u product_2684_3ced2b -p 63e143b0569c0b3e
@docker push $(IMG)
##@ Proto
protoc: ## Build proto files.
@printf $(COLOR) "Build proto files..."
goctl rpc protoc ./proto/service.proto --go_out=./proto --go-grpc_out=./proto --zrpc_out=.
# All gRPC generated service files pathes relative to PROTO_OUT.
PROTO_GRPC_SERVICES = $(patsubst $(PROTO_OUT)/%,%,$(shell find $(PROTO_OUT) -name "service_grpc.pb.go"))
service_name = $(firstword $(subst /, ,$(1)))
mock_file_name = $(call service_name,$(1))mock/$(subst $(call service_name,$(1))/,,$(1:go=mock.go))
proto-mocks: ## Build proto mocks.
@printf $(COLOR) "Generate proto mocks... $(call service_name,$(1))"
$(foreach PROTO_GRPC_SERVICE,$(PROTO_GRPC_SERVICES),\
cd $(PROTO_OUT) && \
mockgen -package $(call service_name,$(PROTO_GRPC_SERVICE))mock -source $(PROTO_GRPC_SERVICE) -destination $(call mock_file_name,$(PROTO_GRPC_SERVICE)) \
$(NEWLINE))
.PHONY: \
all \
local \
build \
docker-build \
test
##@ Tools
update-framework:
@printf $(COLOR) "Install/update framework tools... [ginkgo,gomega]"
@go install -mod=mod github.com/onsi/ginkgo/v2/ginkgo@latest
@go get github.com/onsi/gomega/...
update-checkers:
@printf $(COLOR) "Install/update check tools..."
@go install golang.org/x/lint/golint@latest
@go install golang.org/x/tools/cmd/goimports@latest
@go install honnef.co/go/tools/cmd/staticcheck@v0.2.2
@go install github.com/kisielk/errcheck@v1.6.0
@go install github.com/googleapis/api-linter/cmd/api-linter@v1.29.4
@go install github.com/bufbuild/buf/cmd/buf@v0.56.0
update-mockgen:
@printf $(COLOR) "Install/update mockgen tool..."
@go install github.com/golang/mock/mockgen@v1.6.0
update-proto-plugins:
@printf $(COLOR) "Install/update proto plugins..."
@go install github.com/temporalio/gogo-protobuf/protoc-gen-gogoslick@latest
# This to download sources of gogo-protobuf which are required to build proto files.
@GO111MODULE=off go get github.com/temporalio/gogo-protobuf/protoc-gen-gogoslick
@go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
update-tools: update-checkers update-mockgen update-proto-plugins ## Update tools and plugins.
get-wire: ## Download wire locally
@go install github.com/google/wire/cmd/wire@latest
AIR = $(BINDIR)/air
.PHONY: get-air
get-air: ## Download air locally if necessary.
$(call go-get-tool,$(AIR),github.com/cosmtrek/air)
# go-get-tool will 'go get' any package $2 and install it to $1.
define go-get-tool
@[ -f $(1) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
$(GO) mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(BINDIR) $(GO) get $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef
##@ Checks
lint:
@printf $(COLOR) "Run linter..."
@golint ./...
vet:
@printf $(COLOR) "Run go vet..."
@go vet ./... || true
goimports-check:
@printf $(COLOR) "Run goimports checks..."
@GO_IMPORTS_OUTPUT=$$(goimports -l .); if [ -n "$${GO_IMPORTS_OUTPUT}" ]; then echo "$${GO_IMPORTS_OUTPUT}" && echo "Please run make goimports" && exit 1; fi
goimports:
@printf $(COLOR) "Run goimports..."
@goimports -w .
staticcheck:
@printf $(COLOR) "Run staticcheck..."
@staticcheck -fail none ./...
errcheck:
@printf $(COLOR) "Run errcheck..."
@errcheck ./... || true
api-linter:
@printf $(COLOR) "Run api-linter..."
@api-linter --set-exit-status $(PROTO_IMPORTS) --config=$(PROTO_ROOT)/api-linter.yaml $(PROTO_FILES)
buf-lint:
@printf $(COLOR) "Run buf linter..."
@(cd $(PROTO_ROOT) && buf lint)
buf-build:
@printf $(COLOR) "Build image.bin with buf..."
@(cd $(PROTO_ROOT) && buf build -o image.bin)
buf-breaking:
@printf $(COLOR) "Run buf breaking changes check against image.bin..."
@(cd $(PROTO_ROOT) && buf check breaking --against image.bin)
shell-check:
@printf $(COLOR) "Run shellcheck for script files..."
@shellcheck $(ALL_SCRIPTS)
check: goimports-check lint vet staticcheck errcheck ## Run Check
##@ Mocks
external-mocks: ##
@printf $(COLOR) "Generate external libraries mocks..."
go-generate: ##
@printf $(COLOR) "Process go:generate directives..."
@go generate ./...
mocks: go-generate external-mocks
##@ Auxiliary
gomodtidy:
@printf $(COLOR) "go mod tidy..."
@go mod tidy
update-dependencies:
@printf $(COLOR) "Update dependencies..."
@go get -u -t $(PINNED_DEPENDENCIES) ./...
@go mod tidy
ensure-no-changes:
@printf $(COLOR) "Check for local changes..."
@printf $(COLOR) "========================================================================"
@git diff --name-status --exit-code || (printf $(COLOR) "========================================================================"; printf $(RED) "Above files are not regenerated properly. Regenerate them and try again."; exit 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment