diff --git a/.gitignore b/.gitignore index df860f8..cdaaada 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /coverage /bin -rice-box.go \ No newline at end of file +rice-box.go +/release +/vendor \ No newline at end of file diff --git a/Makefile b/Makefile index 59d5249..36dc56e 100644 --- a/Makefile +++ b/Makefile @@ -13,13 +13,16 @@ deps: go get -u github.com/cortesi/modd/cmd/modd go get -u github.com/golangci/golangci-lint/cmd/golangci-lint -coverage: - @script/coverage - lint: golangci-lint run --tests=false --enable-all +vendor: + go mod vendor + +release: clean vendor + ./misc/script/release + clean: rm -rf ./bin ./release ./coverage -.PHONY: test clean lint coverage doc \ No newline at end of file +.PHONY: test clean lint coverage doc release vendor \ No newline at end of file diff --git a/misc/script/release b/misc/script/release new file mode 100755 index 0000000..4291c5a --- /dev/null +++ b/misc/script/release @@ -0,0 +1,102 @@ +#!/bin/bash + +set -eo pipefail + +OS_TARGETS=(linux) +ARCH_TARGETS=${ARCH_TARGETS:-amd64 arm64} + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" +PROJECT_DIR="$DIR/../.." + +function build { + + local name=$1 + local srcdir=$2 + local os=$3 + local arch=$4 + + local dirname="$name-$os-$arch" + local destdir="$PROJECT_DIR/release/$dirname" + + rm -rf "$destdir" + mkdir -p "$destdir" + + echo "building $dirname..." + + CGO_ENABLED=0 GOOS="$os" GOARCH="$arch" go build \ + -mod=vendor \ + -ldflags="-s -w -X 'main.GitRef=$(current_commit_ref)' -X 'main.ProjectVersion=$(current_version)' -X 'main.BuildDate=$(current_date)'" \ + -gcflags=-trimpath="${PWD}" \ + -asmflags=-trimpath="${PWD}" \ + -o "$destdir/bin/$name" \ + "$srcdir" + + if [ ! -z "$(which upx)" ]; then + upx --best "$destdir/bin/$name" + fi + +} + +function current_date { + date '+%Y-%m-%d %H:%M' +} + +function current_commit_ref { + git log -n 1 --pretty="format:%h" +} + +function current_version { + local latest_tag=$(git describe --abbrev=0 2>/dev/null) + echo ${latest_tag:-0.0.0} +} + +function copy { + + local name=$1 + local os=$2 + local arch=$3 + local src=$4 + local dest=$5 + + local dirname="$name-$os-$arch" + local destdir="$PROJECT_DIR/release/$dirname" + + echo "copying '$src' to '$destdir/$dest'..." + + mkdir -p "$(dirname $destdir/$dest)" + + cp -rfL $src "$destdir/$dest" + +} + +function compress { + local name=$1 + local os=$2 + local arch=$3 + + local dirname="$name-$os-$arch" + local destdir="$PROJECT_DIR/release/$dirname" + + echo "compressing $dirname..." + tar -czf "$destdir.tar.gz" -C "$destdir/../" "$dirname" +} + +function release_scaffold { + local os=$1 + local arch=$2 + + build 'scaffold' "$PROJECT_DIR/cmd/scaffold" $os $arch + copy 'scaffold' $os $arch "$PROJECT_DIR/README.md" "README.md" + copy 'scaffold' $os $arch "$PROJECT_DIR/LICENSE" "LICENSE" + compress 'scaffold' $os $arch +} + +function main { + for os in ${OS_TARGETS[@]}; do + for arch in ${ARCH_TARGETS[@]}; do + release_scaffold $os $arch + done + done +} + +main \ No newline at end of file