|
SHELL := /usr/bin/env bash |
|
|
|
# ==================================================================================== |
|
# Project Specific Globals |
|
# ------------------------------------------------------------------------------------ |
|
# |
|
# - It's assumed the $(name) is the same literal as the compiled binary. |
|
# - Override the defaults if not available in a pipeline's environment variables. |
|
# |
|
|
|
name := example |
|
ifdef CI_PROJECT_NAME |
|
override name = $(CI_PROJECT_NAME) |
|
endif |
|
|
|
homebrew-tap := example-organization/example-cli |
|
ifdef HOMEBREW_TAP |
|
override homebrew-tap = $(HOMEBREW_TAP) |
|
endif |
|
|
|
homebrew-tap-repository := gitlab.com:example-organization/group-1/group-2/homebrew-taps.git |
|
ifdef HOMEBREW_TAP_REPOSITORY |
|
override homebrew-tap-repository = $(HOMEBREW_TAP_REPOSITORY) |
|
endif |
|
|
|
type := patch |
|
ifdef RELEASE_TYPE |
|
override type = RELEASE_TYPE |
|
endif |
|
|
|
# ==================================================================================== |
|
# Colors |
|
# ------------------------------------------------------------------------------------ |
|
|
|
black := $(shell printf "\033[30m") |
|
black-bold := $(shell printf "\033[30;1m") |
|
red := $(shell printf "\033[31m") |
|
red-bold := $(shell printf "\033[31;1m") |
|
green := $(shell printf "\033[32m") |
|
green-bold := $(shell printf "\033[32;1m") |
|
yellow := $(shell printf "\033[33m") |
|
yellow-bold := $(shell printf "\033[33;1m") |
|
blue := $(shell printf "\033[34m") |
|
blue-bold := $(shell printf "\033[34;1m") |
|
magenta := $(shell printf "\033[35m") |
|
magenta-bold := $(shell printf "\033[35;1m") |
|
cyan := $(shell printf "\033[36m") |
|
cyan-bold := $(shell printf "\033[36;1m") |
|
white := $(shell printf "\033[37m") |
|
white-bold := $(shell printf "\033[37;1m") |
|
reset := $(shell printf "\033[0m") |
|
|
|
# ==================================================================================== |
|
# Logger |
|
# ------------------------------------------------------------------------------------ |
|
|
|
time-long = $(date +%Y-%m-%d' '%H:%M:%S) |
|
time-short = $(date +%H:%M:%S) |
|
time = $(time-short) |
|
|
|
information = echo $(time) $(blue)[ DEBUG ]$(reset) |
|
warning = echo $(time) $(yellow)[ WARNING ]$(reset) |
|
exception = echo $(time) $(red)[ ERROR ]$(reset) |
|
complete = echo $(time) $(green)[ COMPLETE ]$(reset) |
|
fail = (echo $(time) $(red)[ FAILURE ]$(reset) && false) |
|
|
|
# ==================================================================================== |
|
# Utility Command(s) |
|
# ------------------------------------------------------------------------------------ |
|
|
|
url = $(shell git config --get remote.origin.url | sed -r 's/.*(\@|\/\/)(.*)(\:|\/)([^:\/]*)\/([^\/\.]*)\.git/https:\/\/\2\/\4\/\5/') |
|
|
|
repository = $(shell basename -s .git $(shell git config --get remote.origin.url)) |
|
organization = $(shell git remote -v | grep "(fetch)" | sed 's/.*\/\([^ ]*\)\/.*/\1/') |
|
package = $(shell printf "github.com/%s/%s" "$(organization)" "$(repository)") |
|
|
|
version = $(shell [ -f VERSION ] && head VERSION || echo "0.0.0") |
|
|
|
tag = $(shell echo "v$(version)") |
|
|
|
major = $(shell echo $(version) | sed "s/^\([0-9]*\).*/\1/") |
|
minor = $(shell echo $(version) | sed "s/[0-9]*\.\([0-9]*\).*/\1/") |
|
patch = $(shell echo $(version) | sed "s/[0-9]*\.[0-9]*\.\([0-9]*\).*/\1/") |
|
|
|
zero = $(shell printf "%s" "0") |
|
|
|
major-upgrade = $(shell expr $(major) + 1).$(zero).$(zero) |
|
minor-upgrade = $(major).$(shell expr $(minor) + 1).$(zero) |
|
patch-upgrade = $(major).$(minor).$(shell expr $(patch) + 1) |
|
|
|
dirty = $(shell git diff --quiet) |
|
dirty-contents = $(shell git diff --shortstat 2>/dev/null 2>/dev/null | tail -n1) |
|
|
|
# ==================================================================================== |
|
# Build Command(s) |
|
# ------------------------------------------------------------------------------------ |
|
|
|
compile = go build --mod "vendor" --ldflags "-s -w -X=main.version=$(tag) -X=main.date=$(shell date +%Y-%m-%d:%H-%M-%S) -X=main.source=false" -o "./build/$(name)-$(GOOS)-$(GOARCH)/$(name)" |
|
compile-windows = go build --mod "vendor" --ldflags "-s -w -X=main.version=$(tag) -X=main.date=$(shell date +%Y-%m-%d:%H-%M-%S) -X=main.source=false" -o "./build/$(name)-$(GOOS)-$(GOARCH)/$(name).exe" |
|
|
|
archive = tar -czvf "$(name)-$(GOOS)-$(GOARCH).tar.gz" -C "./build/$(name)-$(GOOS)-$(GOARCH)" . |
|
archive-windows = cd "./build/$(name)-$(GOOS)-$(GOARCH)" && zip -r "../../$(name)-$(GOOS)-$(GOARCH).zip" "." && cd - |
|
|
|
distribute = mkdir -p distribution && mv *.tar.gz distribution |
|
distribute-windows = mkdir -p distribution && mv *.zip distribution |
|
|
|
# ==================================================================================== |
|
# Default |
|
# ------------------------------------------------------------------------------------ |
|
|
|
all: pre-requisites test-release $(type)-release |
|
|
|
# ==================================================================================== |
|
# Pre-Requisites |
|
# ------------------------------------------------------------------------------------ |
|
|
|
pre-requisites: |
|
@command -v brew 2>&1> /dev/null || bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" |
|
@command -v goreleaser 2>&1> /dev/null || brew install goreleaser |
|
@command -v pre-commit 2>&1> /dev/null || brew install pre-commit |
|
@command -v go 2>&1> /dev/null || brew install go |
|
|
|
# ==================================================================================== |
|
# Utilities |
|
# ------------------------------------------------------------------------------------ |
|
|
|
escape-hatch: |
|
@rm -rf ./.upstreams |
|
@sed -i -e "s/using: GitDownloadStrategy/using: GitDownloadStrategy, tag: \"$(tag)\"/g" ./dist/homebrew/Formula/$(name).rb |
|
@mkdir -p .upstreams |
|
@git clone $(homebrew-tap-repository) ./.upstreams/homebrew-taps |
|
@rm -f ./.upstreams/homebrew-taps/Formula/$(name).rb |
|
@cp -f ./dist/homebrew/Formula/$(name).rb ./.upstreams/homebrew-taps/Formula/$(name).rb |
|
@cd ./.upstreams/homebrew-taps && git add ./Formula/$(name).rb && git commit -m "[Chore] - Overwrote URL + Tag" && git push -u origin main |
|
@cd "$(git rev-parse --show-toplevel)" |
|
@rm -rf ./.upstreams |
|
|
|
# ==================================================================================== |
|
# CI-CD Build Targets |
|
# ------------------------------------------------------------------------------------ |
|
|
|
build: build-darwin build-linux build-windows |
|
|
|
# (Darwin) Build Targets |
|
|
|
build-darwin: build-darwin-amd64 build-darwin-arm64 |
|
|
|
build-darwin-arm64: export GOOS := darwin |
|
build-darwin-arm64: export GOARCH := arm64 |
|
build-darwin-arm64: |
|
$(compile) |
|
$(archive) |
|
|
|
build-darwin-amd64: export GOOS := darwin |
|
build-darwin-amd64: export GOARCH := amd64 |
|
build-darwin-amd64: |
|
$(compile) |
|
$(archive) |
|
|
|
# (Linux) Build Targets |
|
|
|
build-linux: build-linux-amd64 build-linux-arm64 build-linux-386 |
|
|
|
build-linux-arm64: export GOOS := linux |
|
build-linux-arm64: export GOARCH := arm64 |
|
build-linux-arm64: |
|
$(compile) |
|
$(archive) |
|
|
|
build-linux-amd64: export GOOS := linux |
|
build-linux-amd64: export GOARCH := amd64 |
|
build-linux-amd64: |
|
$(compile) |
|
$(archive) |
|
|
|
build-linux-386: export GOOS := linux |
|
build-linux-386: export GOARCH := 386 |
|
build-linux-386: |
|
$(compile) |
|
$(archive) |
|
|
|
# (Windows) Build Targets |
|
|
|
build-windows: build-windows-amd64 build-windows-386 |
|
|
|
build-windows-amd64: export GOOS := windows |
|
build-windows-amd64: export GOARCH := amd64 |
|
build-windows-amd64: |
|
$(compile-windows) |
|
$(archive-windows) |
|
|
|
build-windows-386: export GOOS := windows |
|
build-windows-386: export GOARCH := 386 |
|
build-windows-386: |
|
$(compile-windows) |
|
$(archive-windows) |
|
|
|
# Additional Build Targets |
|
|
|
clean: |
|
rm *.tar.gz && rm *.zip |
|
|
|
# ==================================================================================== |
|
# Local Development & Package-Specific Targets |
|
# ------------------------------------------------------------------------------------ |
|
|
|
uninstall: |
|
@rm -rf /opt/homebrew/etc/gitconfig |
|
@brew uninstall $(name) --force || true |
|
@brew untap $(homebrew-tap) --force || true |
|
|
|
install: uninstall |
|
@brew tap $(homebrew-tap) $(homebrew-tap-repository) --force-auto-update --force |
|
@brew update |
|
@brew install $(name) |
|
|
|
tidy: |
|
@go mod tidy && go mod vendor |
|
|
|
test: tidy |
|
@go test ./... |
|
|
|
test-release: |
|
@goreleaser release --snapshot --clean |
|
|
|
# (Patch) Release Targets |
|
|
|
bump-patch: test |
|
@if ! git diff --quiet --exit-code; then \ |
|
echo "$(red-bold)Dirty Working Tree$(reset) - Commit Changes and Try Again"; \ |
|
exit 1; \ |
|
else \ |
|
echo "$(patch-upgrade)" > VERSION; \ |
|
fi |
|
|
|
commit-patch: bump-patch |
|
@echo "$(blue-bold)Tag-Release (Patch)$(reset): \"$(yellow-bold)$(package)$(reset)\" - $(white-bold)$(version)$(reset)" |
|
@git add VERSION |
|
@git commit --message "Chore (Patch) - Tag Release - $(version)" |
|
@git push --set-upstream origin main |
|
@git tag "v$(version)" |
|
@git push origin "v$(version)" |
|
@echo "$(green-bold)Published Tag$(reset): $(version)" |
|
|
|
release-patch: commit-patch |
|
@echo "$(blue-bold)Deployment (Patch)$(reset): \"$(yellow-bold)$(package)$(reset)\" - $(white-bold)$(version)$(reset)" |
|
@goreleaser release --clean |
|
@echo "$(green-bold)Successful$(reset): $(version)" |
|
|
|
patch-release: pre-requisites release-patch escape-hatch install |
|
|
|
# (Minor) Release Targets |
|
|
|
bump-minor: test |
|
@if ! git diff --quiet --exit-code; then \ |
|
echo "$(red-bold)Dirty Working Tree$(reset) - Commit Changes and Try Again"; \ |
|
exit 1; \ |
|
else \ |
|
echo "$(minor-upgrade)" > VERSION; \ |
|
fi |
|
|
|
commit-minor: bump-minor |
|
@echo "$(blue-bold)Tag-Release (Minor)$(reset): \"$(yellow-bold)$(package)$(reset)\" - $(white-bold)$(version)$(reset)" |
|
@git add VERSION |
|
@git commit --message "Chore (Minor) - Tag Release - $(version)" |
|
@git push --set-upstream origin main |
|
@git tag "v$(version)" |
|
@git push origin "v$(version)" |
|
@echo "$(green-bold)Published Tag$(reset): $(version)" |
|
|
|
release-minor: commit-minor |
|
@echo "$(blue-bold)Deployment (Minor)$(reset): \"$(yellow-bold)$(package)$(reset)\" - $(white-bold)$(version)$(reset)" |
|
@goreleaser release --clean |
|
@echo "$(green-bold)Successful$(reset): $(version)" |
|
|
|
minor-release: pre-requisites release-minor escape-hatch install |
|
|
|
# (Major) Release Targets |
|
|
|
bump-major: test |
|
@if ! git diff --quiet --exit-code; then \ |
|
echo "$(red-bold)Dirty Working Tree$(reset) - Commit Changes and Try Again"; \ |
|
exit 1; \ |
|
else \ |
|
echo "$(major-upgrade)" > VERSION; \ |
|
fi |
|
|
|
commit-major: bump-major |
|
@echo "$(blue-bold)Tag-Release (Major)$(reset): \"$(yellow-bold)$(package)$(reset)\" - $(white-bold)$(version)$(reset)" |
|
@git add VERSION |
|
@git commit --message "Chore (Major) - Tag Release - $(version)" |
|
@git push --set-upstream origin main |
|
@git tag "v$(version)" |
|
@git push origin "v$(version)" |
|
@echo "$(green-bold)Published Tag$(reset): $(version)" |
|
|
|
release-major: commit-major |
|
@echo "$(blue-bold)Deployment (Major)$(reset): \"$(yellow-bold)$(package)$(reset)\" - $(white-bold)$(version)$(reset)" |
|
@goreleaser release --clean |
|
@echo "$(green-bold)Successful$(reset): $(version)" |
|
|
|
major-release: pre-requisites release-major escape-hatch install |
|
|
|
|