|
| 1 | +# Copyright 2022 Ashok Pon Kumar |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +BINNAME ?= github-org-stats |
| 16 | +ORGNAME ?= ashokponkumar |
| 17 | +BINDIR := $(CURDIR)/bin |
| 18 | +DISTDIR := $(CURDIR)/_dist |
| 19 | +TARGETS := darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 windows/amd64 |
| 20 | + |
| 21 | +GOPATH = $(shell go env GOPATH) |
| 22 | +GOX = $(GOPATH)/bin/gox |
| 23 | +GOTEST = ${GOPATH}/bin/gotest |
| 24 | +GOLANGCILINT = $(GOPATH)/bin/golangci-lint |
| 25 | +GOLANGCOVER = $(GOPATH)/bin/goveralls |
| 26 | + |
| 27 | +PKG := ./... |
| 28 | +LDFLAGS := -w -s |
| 29 | + |
| 30 | +SRC = $(shell find . -type f -name '*.go' -print) |
| 31 | +ASSETS = $(shell find assets -type f -name '*' -print) |
| 32 | +ARCH = $(shell uname -p) |
| 33 | +GIT_COMMIT = $(shell git rev-parse HEAD) |
| 34 | +GIT_SHA = $(shell git rev-parse --short HEAD) |
| 35 | +GIT_TAG = $(shell git tag --points-at | tail -n 1) |
| 36 | +GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean") |
| 37 | +HAS_UPX = $(shell command -v upx >/dev/null && echo true || echo false) |
| 38 | + |
| 39 | +GOGET := cd / && GO111MODULE=on go install |
| 40 | + |
| 41 | +ifdef VERSION |
| 42 | + BINARY_VERSION = $(VERSION) |
| 43 | +endif |
| 44 | +BINARY_VERSION ?= ${GIT_TAG} |
| 45 | +ifneq ($(BINARY_VERSION),) |
| 46 | + LDFLAGS += -X github.com/${ORGNAME}/${BINNAME}/info.version=${BINARY_VERSION} |
| 47 | + VERSION ?= $(BINARY_VERSION) |
| 48 | +endif |
| 49 | +VERSION ?= latest |
| 50 | + |
| 51 | +VERSION_METADATA = unreleased |
| 52 | +ifneq ($(GIT_TAG),) |
| 53 | + VERSION_METADATA = |
| 54 | +endif |
| 55 | +LDFLAGS += -X github.com/${ORGNAME}/${BINNAME}/types/info.buildmetadata=${VERSION_METADATA} |
| 56 | + |
| 57 | +LDFLAGS += -X github.com/${ORGNAME}/${BINNAME}/types/info.gitCommit=${GIT_COMMIT} |
| 58 | +LDFLAGS += -X github.com/${ORGNAME}/${BINNAME}/types/info.gitTreeState=${GIT_DIRTY} |
| 59 | +LDFLAGS += -extldflags "-static" |
| 60 | + |
| 61 | +# HELP |
| 62 | +# This will output the help for each task |
| 63 | +.PHONY: help |
| 64 | +help: ## This help. |
| 65 | + @awk 'BEGIN {FS = ":.*?## "} /^[0-9a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) |
| 66 | + |
| 67 | +# -- Build -- |
| 68 | + |
| 69 | +.PHONY: build |
| 70 | +build: get $(BINDIR)/$(BINNAME) ## Build go code |
| 71 | + |
| 72 | +$(BINDIR)/$(BINNAME): $(SRC) $(ASSETS) |
| 73 | + go build -ldflags '$(LDFLAGS)' -o $(BINDIR)/$(BINNAME) . |
| 74 | +ifeq ($(HAS_UPX),true) |
| 75 | + @echo 'upx detected. compressing binary...' |
| 76 | + upx $(BINDIR)/$(BINNAME) |
| 77 | +else |
| 78 | + @echo 'For smaller binaries, please install upx:' |
| 79 | + @echo 'MacOS: brew install upx' |
| 80 | + @echo 'Linux: sudo apt-get install upx' |
| 81 | +endif |
| 82 | + mkdir -p $(GOPATH)/bin/ |
| 83 | + cp $(BINDIR)/$(BINNAME) $(GOPATH)/bin/ |
| 84 | + |
| 85 | +.PHONY: get |
| 86 | +get: go.mod |
| 87 | + go mod download |
| 88 | + |
| 89 | +.PHONY: generate |
| 90 | +generate: |
| 91 | + go generate ${PKG} |
| 92 | + |
| 93 | +# -- Test -- |
| 94 | + |
| 95 | +.PHONY: test |
| 96 | +test: ## Run tests |
| 97 | + go test -run . $(PKG) -race |
| 98 | + |
| 99 | +${GOTEST}: |
| 100 | + ${GOGET} github.com/rakyll/ [email protected] |
| 101 | + |
| 102 | +.PHONY: test-verbose |
| 103 | +test-verbose: ${GOTEST} |
| 104 | + gotest -run . $(PKG) -race -v |
| 105 | + |
| 106 | +${GOLANGCOVER}: |
| 107 | + ${GOGET} github.com/mattn/ [email protected] |
| 108 | + |
| 109 | +.PHONY: test-coverage |
| 110 | +test-coverage: ${GOLANGCOVER} ## Run tests with coverage |
| 111 | + go test -run . $(PKG) -coverprofile=coverage.txt -covermode=atomic |
| 112 | + |
| 113 | +${GOLANGCILINT}: |
| 114 | + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.31.0 |
| 115 | + |
| 116 | +.PHONY: test-style |
| 117 | +test-style: ${GOLANGCILINT} |
| 118 | + ${GOLANGCILINT} run --timeout 3m |
| 119 | + |
| 120 | +# -- CI -- |
| 121 | + |
| 122 | +.PHONY: ci |
| 123 | +ci: clean build test test-style ## Run CI routine |
| 124 | + |
| 125 | +# -- Release -- |
| 126 | + |
| 127 | +$(GOX): |
| 128 | + ${GOGET} github.com/mitchellh/ [email protected] |
| 129 | + |
| 130 | +.PHONY: build-cross |
| 131 | +build-cross: $(GOX) clean |
| 132 | + CGO_ENABLED=0 $(GOX) -parallel=3 -output="$(DISTDIR)/{{.OS}}-{{.Arch}}/$(BINNAME)" -osarch='$(TARGETS)' -ldflags '$(LDFLAGS)' ./ |
| 133 | + |
| 134 | +.PHONY: dist |
| 135 | +dist: clean build-cross ## Build distribution |
| 136 | +ifeq ($(HAS_UPX),true) |
| 137 | + @echo 'upx detected. compressing binary...' |
| 138 | + upx $(shell find . -type f -name '$(BINNAME)') |
| 139 | +else |
| 140 | + @echo 'For smaller binaries, please install upx:' |
| 141 | + @echo 'MacOS: brew install upx' |
| 142 | + @echo 'Linux: sudo apt-get install upx' |
| 143 | +endif |
| 144 | + |
| 145 | +.PHONY: clean |
| 146 | +clean: |
| 147 | + rm -rf $(BINDIR) $(DISTDIR) |
| 148 | + go clean -cache |
| 149 | + |
| 150 | +.PHONY: info |
| 151 | +info: ## Get version info |
| 152 | + @echo "Version: ${VERSION}" |
| 153 | + @echo "Git Tag: ${GIT_TAG}" |
| 154 | + @echo "Git Commit: ${GIT_COMMIT}" |
| 155 | + @echo "Git Tree State: ${GIT_DIRTY}" |
0 commit comments