Skip to content

Commit

Permalink
Refactor Makefile and goreleaser build.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Apr 10, 2023
1 parent 27bef01 commit d4b048e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
16 changes: 10 additions & 6 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
env:
- GO111MODULE=on
- CGO_ENABLED=0
- RELEASE_BUILDS=dist/niltalk_darwin_amd64/niltalk dist/niltalk_linux_amd64/niltalk dist/niltalk_windows_amd64/niltalk.exe

builds:
- binary: niltalk
main: ./
goos:
- linux
- darwin
- windows
- darwin
- linux
- freebsd
- openbsd
- netbsd
goarch:
- amd64
ldflags:
- -s -w -X "main.buildString={{ .Tag }} ({{ .ShortCommit }} {{ .Date }})"
- -s -w -X "main.buildString={{ .Tag }} ({{ .ShortCommit }} {{ .Date }})" -X "main.versionString={{ .Tag }}"

hooks:
# stuff binaries with static assets.
post: make pack-releases
# stuff executables with static assets.
post: make pack-bin BIN={{ .Path }}

archives:
- format: tar.gz
Expand Down
67 changes: 39 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
LAST_COMMIT := $(shell git rev-parse --short HEAD)
LAST_COMMIT_DATE := $(shell git show -s --format=%ci ${LAST_COMMIT})
VERSION := $(shell git describe)
BUILDSTR := ${VERSION} (${LAST_COMMIT} $(shell date -u +"%Y-%m-%dT%H:%M:%S%z"))
# Try to get the commit hash from 1) git 2) the VERSION file 3) fallback.
LAST_COMMIT := $(or $(shell git rev-parse --short HEAD 2> /dev/null),$(shell head -n 1 VERSION | grep -oP -m 1 "^[a-z0-9]+$$"),"UNKNOWN")

# Try to get the semver from 1) git 2) the VERSION file 3) fallback.
VERSION := $(or $(shell git describe --tags --abbrev=0 2> /dev/null),$(shell grep -oP "tag: \K(.*)(?=,)" VERSION),"v0.0.0")

BUILDSTR := ${VERSION} (\#${LAST_COMMIT} $(shell date -u +"%Y-%m-%dT%H:%M:%S%z"))

YARN ?= yarn
GOPATH ?= $(HOME)/go
STUFFBIN ?= $(GOPATH)/bin/stuffbin

BIN := niltalk
STATIC := static/templates static/static:/static config.toml.sample

.PHONY: deps
deps:
# If dependencies are not installed, install.
go get -u github.com/knadh/stuffbin/...

.PHONY: build
build:
go build -o ${BIN} -ldflags="-s -w -X 'main.buildString=${BUILDSTR}'"
build: $(BIN)

.PHONY: run
run: build
./${BIN}
$(STUFFBIN):
go install github.com/knadh/stuffbin/...

.PHONY: dist
dist: build deps
stuffbin -a stuff -in ${BIN} -out ${BIN} ${STATIC}
$(BIN): $(shell find . -type f -name "*.go")
CGO_ENABLED=0 go build -o ${BIN} -ldflags="-s -w -X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}'" *.go

# pack-releases runns stuffbin packing on a given list of
# binaries. This is used with goreleaser for packing
# release builds for cross-build targets.
.PHONY: pack-releases
pack-releases: deps
$(foreach var,$(RELEASE_BUILDS),stuffbin -a stuff -in ${var} -out ${var} ${STATIC} $(var);)
.PHONY: run
run:
CGO_ENABLED=0 go run -ldflags="-s -w -X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}'" *.go

# Run Go tests.
.PHONY: test
test:
go test
go test ./...

.PHONE: clean
clean:
go clean
- rm -f ${BIN}
.PHONY: dist
dist: $(STUFFBIN) build pack-bin

# pack-releases runns stuffbin packing on the given binary. This is used
# in the .goreleaser post-build hook.
.PHONY: pack-bin
pack-bin: $(BIN) $(STUFFBIN)
$(STUFFBIN) -a stuff -in ${BIN} -out ${BIN} ${STATIC}

# Use goreleaser to do a dry run producing local builds.
.PHONY: release-dry
release-dry:
goreleaser --parallelism 1 --rm-dist --snapshot --skip-validate --skip-publish

# Use goreleaser to build production releases and publish them.
.PHONY: release
release:
goreleaser --parallelism 1 --rm-dist --skip-validate

0 comments on commit d4b048e

Please sign in to comment.