2016-02-26 02:21:20 -05:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
|
|
|
# Caddy build script. Automates proper versioning.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# $ ./build.bash [output_filename]
|
|
|
|
#
|
|
|
|
# Outputs compiled program in current directory.
|
|
|
|
# Default file name is 'ecaddy'.
|
2016-03-18 17:10:24 -05:00
|
|
|
|
2016-03-18 17:45:56 -05:00
|
|
|
set -euo pipefail
|
2016-02-26 02:21:20 -05:00
|
|
|
|
2016-03-18 17:45:56 -05:00
|
|
|
: ${output_filename:="${1:-}"}
|
2016-03-18 17:10:24 -05:00
|
|
|
: ${output_filename:="ecaddy"}
|
2016-02-26 02:21:20 -05:00
|
|
|
|
|
|
|
pkg=main
|
2016-03-18 17:10:24 -05:00
|
|
|
ldflags=()
|
2016-02-26 02:21:20 -05:00
|
|
|
|
|
|
|
# Timestamp of build
|
2016-03-18 17:10:24 -05:00
|
|
|
name="${pkg}.buildDate"
|
2016-03-20 15:13:50 -05:00
|
|
|
value=$(date -u +"%a %b %d %H:%M:%S %Z %Y")
|
2016-03-18 17:10:24 -05:00
|
|
|
ldflags+=("-X" "\"${name}=${value}\"")
|
2016-02-26 02:21:20 -05:00
|
|
|
|
|
|
|
# Current tag, if HEAD is on a tag
|
2016-03-18 17:10:24 -05:00
|
|
|
name="${pkg}.gitTag"
|
2016-02-26 02:21:20 -05:00
|
|
|
set +e
|
2016-03-18 17:10:24 -05:00
|
|
|
value="$(git describe --exact-match HEAD 2>/dev/null)"
|
2016-02-26 02:21:20 -05:00
|
|
|
set -e
|
2016-03-18 17:10:24 -05:00
|
|
|
ldflags+=("-X" "\"${name}=${value}\"")
|
2016-02-26 02:21:20 -05:00
|
|
|
|
|
|
|
# Nearest tag on branch
|
2016-03-18 17:10:24 -05:00
|
|
|
name="${pkg}.gitNearestTag"
|
|
|
|
value="$(git describe --abbrev=0 --tags HEAD)"
|
|
|
|
ldflags+=("-X" "\"${name}=${value}\"")
|
2016-02-26 02:21:20 -05:00
|
|
|
|
|
|
|
# Commit SHA
|
2016-03-18 17:10:24 -05:00
|
|
|
name="${pkg}.gitCommit"
|
|
|
|
value="$(git rev-parse --short HEAD)"
|
|
|
|
ldflags+=("-X" "\"${name}=${value}\"")
|
2016-02-26 02:21:20 -05:00
|
|
|
|
2016-03-18 17:10:24 -05:00
|
|
|
# Summary of uncommitted changes
|
|
|
|
name="${pkg}.gitShortStat"
|
|
|
|
value="$(git diff-index --shortstat HEAD)"
|
|
|
|
ldflags+=("-X" "\"${name}=${value}\"")
|
2016-02-26 02:21:20 -05:00
|
|
|
|
|
|
|
# List of modified files
|
2016-03-18 17:10:24 -05:00
|
|
|
name="${pkg}.gitFilesModified"
|
2016-03-20 15:13:50 -05:00
|
|
|
value="$(git diff-index --name-only HEAD)"
|
2016-03-18 17:10:24 -05:00
|
|
|
ldflags+=("-X" "\"${name}=${value}\"")
|
|
|
|
|
2016-03-20 15:13:50 -05:00
|
|
|
go build -ldflags "${ldflags[*]}" -o "${output_filename}"
|