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