Skip to content

Instantly share code, notes, and snippets.

@dlisboa
Forked from alexedwards/Makefile
Last active May 3, 2024 18:35
Show Gist options
  • Save dlisboa/864f8d6fda9a3e1820499b26bab7aec3 to your computer and use it in GitHub Desktop.
Save dlisboa/864f8d6fda9a3e1820499b26bab7aec3 to your computer and use it in GitHub Desktop.
Boilerplate Makefile for Go projects
# !!!!!
# VEJA A VERSÃO SIMPLIFICADA ABAIXO
# !!!!!
# Change these variables as necessary.
MAIN_PACKAGE_PATH := ./cmd/example
BINARY_NAME := example
# ==================================================================================== #
# HELPERS
# ==================================================================================== #
## help: print this help message
.PHONY: help
help:
@echo 'Usage:'
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'
.PHONY: confirm
confirm:
@echo -n 'Are you sure? [y/N] ' && read ans && [ $${ans:-N} = y ]
.PHONY: no-dirty
no-dirty:
git diff --exit-code
# ==================================================================================== #
# QUALITY CONTROL
# ==================================================================================== #
## tidy: format code and tidy modfile
.PHONY: tidy
tidy:
go fmt ./...
go mod tidy -v
## audit: run quality control checks
.PHONY: audit
audit:
go mod verify
go vet ./...
go run honnef.co/go/tools/cmd/staticcheck@latest -checks=all,-ST1000,-U1000 ./...
go run golang.org/x/vuln/cmd/govulncheck@latest ./...
go test -race -buildvcs -vet=off ./...
# ==================================================================================== #
# DEVELOPMENT
# ==================================================================================== #
## test: run all tests
.PHONY: test
test:
go test -v -race -buildvcs ./...
## test/cover: run all tests and display coverage
.PHONY: test/cover
test/cover:
go test -v -race -buildvcs -coverprofile=/tmp/coverage.out ./...
go tool cover -html=/tmp/coverage.out
## build: build the application
.PHONY: build
build:
# Include additional build steps, like TypeScript, SCSS or Tailwind compilation here...
go build -o=/tmp/bin/${BINARY_NAME} ${MAIN_PACKAGE_PATH}
## run: run the application
.PHONY: run
run: build
/tmp/bin/${BINARY_NAME}
## run/live: run the application with reloading on file changes
.PHONY: run/live
run/live:
go run github.com/cosmtrek/air@v1.43.0 \
--build.cmd "make build" --build.bin "/tmp/bin/${BINARY_NAME}" --build.delay "100" \
--build.exclude_dir "" \
--build.include_ext "go, tpl, tmpl, html, css, scss, js, ts, sql, jpeg, jpg, gif, png, bmp, svg, webp, ico" \
--misc.clean_on_exit "true"
# ==================================================================================== #
# OPERATIONS
# ==================================================================================== #
## push: push changes to the remote Git repository
.PHONY: push
push: tidy audit no-dirty
git push
## production/deploy: deploy the application to production
.PHONY: production/deploy
production/deploy: confirm tidy audit no-dirty
GOOS=linux GOARCH=amd64 go build -ldflags='-s' -o=/tmp/bin/linux_amd64/${BINARY_NAME} ${MAIN_PACKAGE_PATH}
upx -5 /tmp/bin/linux_amd64/${BINARY_NAME}
# Include additional deployment steps here...
@dlisboa
Copy link
Author

dlisboa commented May 2, 2024

Outro exemplo de Makefile para projetos Go:

build:
	go build -v ./...

test:
	go test -race -v ./...
watch-test:
	reflex -t 50ms -s -- sh -c 'gotest -race -v ./...'

bench:
	go test -benchmem -count 3 -bench ./...
watch-bench:
	reflex -t 50ms -s -- sh -c 'go test -benchmem -count 3 -bench ./...'

coverage:
	go test -v -coverprofile=cover.out -covermode=atomic ./...
	go tool cover -html=cover.out -o cover.html

tools:
	go install github.com/cespare/reflex@latest
	go install github.com/rakyll/gotest@latest
	go install github.com/psampaz/go-mod-outdated@latest
	go install github.com/jondot/goweight@latest
	go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
	go get -t -u golang.org/x/tools/cmd/cover
	go install github.com/sonatype-nexus-community/nancy@latest
	go mod tidy

lint:
	golangci-lint run --timeout 60s --max-same-issues 50 ./...
lint-fix:
	golangci-lint run --timeout 60s --max-same-issues 50 --fix ./...

audit:
	go list -json -m all | nancy sleuth

outdated:
	go list -u -m -json all | go-mod-outdated -update -direct

weight:
	goweight

@dlisboa
Copy link
Author

dlisboa commented May 2, 2024

A minha versão simplificada:

# Change these
MAIN_PACKAGE_PATH := ./cmd/server
BINARY_NAME := server

## help: print usage
help:
	@echo 'Usage: ' && sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' |  sed -e 's/^/ /'

## tidy: format code and tidy modfile
tidy:
	go fmt ./...

## build: build the app
build:
	go build -o=./bin/${BINARY_NAME} ${MAIN_PACKAGE_PATH}

## clean: clean build artifacts
clean:
	rm ./bin/${BINARY_NAME}

## run: run the app
run:
	go run ${MAIN_PACKAGE_PATH}

## run/live: run the app with reloading
run/live:
	reflex -r '\.go|tmpl' -R '_test\.go' -s go run ${MAIN_PACKAGE_PATH}

## test: run all tests
test:
	go test -v -race -buildvcs ./...

## test: run all tests with reloading
test/live:
	reflex -r '\.go|tmpl' -s go test -v -race -buildvcs ./...

## lint: run linters
lint:
	golangci-lint run ./...

## lint/fix: run linters and fix issues
lint/fix:
	golangci-lint run --fix ./...

## deps: install dev and prod deps
deps:
	go mod tidy
	go install github.com/cespare/reflex@latest
	go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

.PHONY: help tidy build clean run run/live test test/live lint lint/fix deps

O .PHONY é importante, não deve ser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment