From e4a0210af809606470b399fcc5678ce6d37c959a Mon Sep 17 00:00:00 2001 From: Aswin Karthik Date: Sun, 11 Aug 2019 19:24:14 +0530 Subject: [PATCH] Add makefile --- Makefile | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9d46618 --- /dev/null +++ b/Makefile @@ -0,0 +1,76 @@ +-include .env + +VERSION := $(shell git describe --tags) +BUILD := $(shell git rev-parse --short HEAD) +PROJECTNAME := $(shell basename "$(PWD)") + +# Go related variables. +GOBASE := $(shell pwd) +GOPATH := $(GOBASE)/vendor:$(GOBASE) +GOBIN := $(GOBASE)/out +GOFILES := $(wildcard *.go) + +# Use linker flags to provide version/build settings +LDFLAGS=-ldflags "-X=main.Version=$(VERSION) -X=main.Build=$(BUILD)" + +# Make is verbose in Linux. Make it silent. +MAKEFLAGS += --silent + +## install: Install missing dependencies. Runs `go get` internally. e.g; make install get=github.com/foo/bar +install: go-get + +## test: Run all tests +test: + @-$(MAKE) -s go-test + +## compile: Compile the binary. +compile: + @-$(MAKE) -s go-compile + +## exec: Run given command, wrapped with custom GOPATH. e.g; make exec run="go test ./..." +exec: + @GOPATH=$(GOPATH) GOBIN=$(GOBIN) $(run) + +## clean: Clean build files. Runs `go clean` internally. +clean: + @-rm $(GOBIN)/$(PROJECTNAME) 2> /dev/null + @-$(MAKE) go-clean + +go-compile: go-get go-build + +go-build: + @echo " > Building binary..." + @GOPATH=$(GOPATH) GOBIN=$(GOBIN) go build $(LDFLAGS) -o $(GOBIN)/$(PROJECTNAME) $(GOFILES) + +go-generate: + @echo " > Generating dependency files..." + @GOPATH=$(GOPATH) GOBIN=$(GOBIN) go generate $(generate) + +go-get: + @echo " > Checking if there is any missing dependencies..." + @GOPATH=$(GOPATH) GOBIN=$(GOBIN) go get $(get) + +go-install: + @GOPATH=$(GOPATH) GOBIN=$(GOBIN) go mod tidy + +go-vendor: + @GOPATH=$(GOPATH) GOBIN=$(GOBIN) go mod vendor + +go-test: + @GOPATH=$(GOPATH) GOBIN=$(GOBIN) go test -v ./... + +richgo-test: + @GOPATH=$(GOPATH) GOBIN=$(GOBIN) richgo test -v ./... + +go-clean: + @echo " > Cleaning build cache" + @GOPATH=$(GOPATH) GOBIN=$(GOBIN) go clean + +.PHONY: help +all: help +help: Makefile + @echo + @echo " Choose a command run in "$(PROJECTNAME)":" + @echo + @sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /' + @echo