Skip to content

Instantly share code, notes, and snippets.

@dln
Created July 1, 2021 12:26
Show Gist options
  • Save dln/177f768b6f44abba6a0fe319ad119ea6 to your computer and use it in GitHub Desktop.
Save dln/177f768b6f44abba6a0fe319ad119ea6 to your computer and use it in GitHub Desktop.
SHELL := /usr/bin/env bash -o pipefail
PROJECT := myproject
HTTPS_PROTOS := https://github.com/shelmangroup/myproject.git
SSH_PROTOS := ssh://[email protected]/shelmangroup/myproject.git
BUF_VERSION := 0.43.2
PROTOC_GEN_GO_VERSION := v1.26.0
PROTOC_GEN_GO_GRPC_VERSION := v1.1.0
UNAME_OS := $(shell uname -s)
UNAME_ARCH := $(shell uname -m)
CACHE_BASE := $(HOME)/.cache/$(PROJECT)
CACHE := $(CACHE_BASE)/$(UNAME_OS)/$(UNAME_ARCH)
CACHE_BIN := $(CACHE)/bin
CACHE_VERSIONS := $(CACHE)/versions
export PATH := $(abspath $(CACHE_BIN)):$(PATH)
export GOBIN := $(abspath $(CACHE_BIN))
export GO111MODULE := on
VERSION ?= $(shell git describe --tags 2>/dev/null || git rev-parse --short HEAD)
TESTARGS ?= ./{cmd,pkg}/...
LDFLAGS = -ldflags "\
-X 'github.com/shelmangroup/myproject/pkg/revision.AppVersion=${VERSION}' \
-X 'github.com/shelmangroup/myproject/pkg/revision.GitCommit=$(shell git rev-parse --short HEAD || echo unknown)' \
-X 'github.com/shelmangroup/myproject/pkg/revision.GoVersion=$(shell go version)' \
"
# BUF points to the marker file for the installed version.
#
# If BUF_VERSION is changed, the binary will be re-downloaded.
BUF := $(CACHE_VERSIONS)/buf/$(BUF_VERSION)
$(BUF):
@rm -f $(CACHE_BIN)/buf
@mkdir -p $(CACHE_BIN)
ifeq ($(BUF_INSTALL_FROM_SOURCE),true)
$(eval BUF_TMP := $(shell mktemp -d))
cd $(BUF_TMP); go get github.com/bufbuild/buf/cmd/buf@$(BUF_VERSION)
@rm -rf $(BUF_TMP)
else
curl -sSL \
"https://github.com/bufbuild/buf/releases/download/v$(BUF_VERSION)/buf-$(UNAME_OS)-$(UNAME_ARCH)" \
-o "$(CACHE_BIN)/buf"
chmod +x "$(CACHE_BIN)/buf"
endif
@rm -rf $(dir $(BUF))
@mkdir -p $(dir $(BUF))
@touch $(BUF)
# PROTOC_GEN_GO points to the marker file for the installed version.
#
# If PROTOC_GEN_GO_VERSION is changed, the binary will be re-downloaded.
PROTOC_GEN_GO := $(CACHE_VERSIONS)/protoc-gen-go/$(PROTOC_GEN_GO_VERSION)
$(PROTOC_GEN_GO):
@rm -f $(CACHE_BIN)/protoc-gen-go
@mkdir -p $(CACHE_BIN)
$(eval PROTOC_GEN_GO_TMP := $(shell mktemp -d))
cd $(PROTOC_GEN_GO_TMP); go get google.golang.org/protobuf/cmd/protoc-gen-go@$(PROTOC_GEN_GO_VERSION)
@rm -rf $(PROTOC_GEN_GO_TMP)
@rm -rf $(dir $(PROTOC_GEN_GO))
@mkdir -p $(dir $(PROTOC_GEN_GO))
@touch $(PROTOC_GEN_GO)
# PROTOC_GEN_GO_GRPC points to the marker file for the installed version.
#
# If PROTOC_GEN_GO_GRPC_VERSION is changed, the binary will be re-downloaded.
PROTOC_GEN_GO_GRPC := $(CACHE_VERSIONS)/protoc-gen-go-grpc/$(PROTOC_GEN_GO_GRPC_VERSION)
$(PROTOC_GEN_GO_GRPC):
@rm -f $(CACHE_BIN)/protoc-gen-go-grpc
@mkdir -p $(CACHE_BIN)
$(eval PROTOC_GEN_GO_GRPC_TMP := $(shell mktemp -d))
cd $(PROTOC_GEN_GO_GRPC_TMP); go get google.golang.org/grpc/cmd/protoc-gen-go-grpc@$(PROTOC_GEN_GO_GRPC_VERSION)
@rm -rf $(PROTOC_GEN_GO_GRPC_TMP)
@rm -rf $(dir $(PROTOC_GEN_GO_GRPC))
@mkdir -p $(dir $(PROTOC_GEN_GO_GRPC))
@touch $(PROTOC_GEN_GO_GRPC)
# TS_PROTOC_GEN points to the marker file for the installed version.
#
# If TS_PROTOC_GEN_VERSION is changed, the binary will be re-downloaded.
TS_PROTOC_GEN := $(CACHE_VERSIONS)/ts-protoc-gen/$(TS_PROTOC_GEN_VERSION)
$(TS_PROTOC_GEN):
@rm -f $(CACHE_BIN)/protoc-gen-ts
@mkdir -p $(CACHE_BIN)
$(eval TS_PROTOC_GEN_TMP := $(shell mktemp -d))
cd $(TS_PROTOC_GEN_TMP); npm install --prefix $(CACHE) -g ts-protoc-gen@$(TS_PROTOC_GEN_VERSION)
@rm -rf $(TS_PROTOC_GEN_TMP)
@rm -rf $(dir $(TS_PROTOC_GEN))
@mkdir -p $(dir $(TS_PROTOC_GEN))
@touch $(TS_PROTOC_GEN)
.DEFAULT_GOAL := local
# deps allows us to install deps without running any checks.
.PHONY: deps
deps: buf-deps go-deps
.PHONY: buf-deps
buf-deps: $(BUF) $(PROTOC_GEN_GO) $(PROTOC_GEN_GO_GRPC) $(TS_PROTOC_GEN)
.PHONY: go-deps
go-deps:
go get -v -u github.com/securego/gosec/cmd/gosec
go get -v -u honnef.co/go/tools/cmd/staticcheck
go get -v -u golang.org/x/lint/golint
.PHONY: local
local: buf-local myproject
.PHONY: buf-local
buf-local: buf-deps $(BUF)
buf lint
# buf breaking --against '.git#branch=main'
buf generate
.PHONY: buf-https
buf-https: buf-deps $(BUF)
buf lint --config buf.yaml $(HTTPS_PROTOS)
buf breaking --against "$(HTTPS_PROTOS)#branch=main"
buf generate --template buf.gen.yaml $(HTTPS_PROTOS)
.PHONY: buf-ssh
buf-ssh: buf-deps $(BUF)
buf lint --config buf.yaml $(SSH_PROTOS)
buf breaking --against "$(SSH_PROTOS)#branch=main"
buf generate --template buf.gen.yaml $(SSH_PROTOS)
.PHONY: clean
clean:
git clean -xdf
rm -rf $(CACHE_BASE)
.PHONY: myproject
myproject:
go build -trimpath $(LDFLAGS) -o "$@" ./cmd/myproject
debug:
go build -gcflags="all=-N -l" -o "builds-ui" ./cmd/builds-ui
.PHONY: all
all: check build
.PHONY: check
check: buf-local tidy check-fmt vet staticcheck lint gosec test build
.PHONY: tidy
tidy:
go mod tidy
.PHONY: fmt
fmt:
go fmt ./{cmd,pkg}/...
.PHONY: check-fmt
check-fmt:
@echo gofmt -l ./{cmd,pkg}
@eval "bash -c 'F=\$$(gofmt -l ./{cmd,pkg}) ; if [[ \$$F ]] ; then echo -e \$$F ; exit 1 ; fi'"
.PHONY: vet
vet:
go vet ./{cmd,pkg}/...
.PHONY: gosec
gosec:
gosec -quiet -exclude=G104 ./{cmd,pkg}/...
.PHONY: staticcheck
staticcheck:
staticcheck ./{cmd,pkg}/...
.PHONY: lint
lint:
golint ./{cmd,pkg}/...
.PHONY: test
test:
go test -race $(TESTARGS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment