Use gox for building

This commit is contained in:
Seth Vargo 2015-10-22 14:16:01 -04:00
parent 002ff0c838
commit 7b2fc2f580
6 changed files with 167 additions and 95 deletions

5
.gitignore vendored
View File

@ -21,7 +21,7 @@ _cgo_export.*
_testmain.go
/dist
/pkg
*.exe
*.test
bin/
@ -41,3 +41,6 @@ ui/dist/
website/.bundle
website/vendor
ui/.bundle
ui/vendor

View File

@ -2,11 +2,24 @@ DEPS = $(shell go list -f '{{range .TestImports}}{{.}} {{end}}' ./...)
PACKAGES = $(shell go list ./...)
VETARGS?=-asmdecl -atomic -bool -buildtags -copylocks -methods \
-nilfunc -printf -rangeloops -shift -structtags -unsafeptr
VERSION?=$(shell awk -F\" '/^const Version/ { print $$2; exit }' version.go)
all: deps format
@mkdir -p bin/
@bash --norc -i ./scripts/build.sh
# bin generates the releasable binaries
bin: generate
@sh -c "'$(CURDIR)/scripts/build.sh'"
# dev creates binares for testing locally. There are put into ./bin and $GOPATH
dev: generate
@CONSUL_DEV=1 sh -c "'$(CURDIR)/scripts/build.sh'"
# dist creates the binaries for distibution
dist: bin
@sh -c "'$(CURDIR)/scripts/dist.sh' $(VERSION)"
cov:
gocov test ./... | gocov-html > /tmp/coverage.html
open /tmp/coverage.html
@ -16,8 +29,14 @@ deps:
@go get -d -v ./... $(DEPS)
updatedeps: deps
@echo "--> Updating build dependencies"
@go get -d -f -u ./... $(DEPS)
go get -u github.com/mitchellh/gox
go get -u golang.org/x/tools/cmd/stringer
go list ./... \
| xargs go list -f '{{join .Deps "\n"}}' \
| grep -v github.com/hashicorp/consul \
| grep -v '/internal/' \
| sort -u \
| xargs go get -f -u -v
test: deps
@./scripts/verify_no_uuid.sh
@ -46,10 +65,15 @@ vet:
echo "and fix them if necessary before submitting the code for reviewal."; \
fi
# generate runs `go generate` to build the dynamically generated source files
generate: deps
find . -type f -name '.DS_Store' -delete
go generate ./...
web:
./scripts/website_run.sh
web-push:
./scripts/website_push.sh
.PHONY: all cov deps integ test vet web web-push test-nodep
.PHONY: all bin dev dist cov deps integ test vet web web-push generate test-nodep

View File

@ -1,6 +1,6 @@
#!/usr/bin/env bash
#!/bin/bash
#
# This script builds the application from source.
# This script builds the application from source for multiple platforms.
set -e
# Get the parent directory of where this script is.
@ -16,37 +16,67 @@ GIT_COMMIT=$(git rev-parse HEAD)
GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)
GIT_DESCRIBE=$(git describe --tags)
# If we're building on Windows, specify an extension
EXTENSION=""
if [ "$(go env GOOS)" = "windows" ]; then
EXTENSION=".exe"
fi
GOPATHSINGLE=${GOPATH%%:*}
if [ "$(go env GOOS)" = "windows" ]; then
GOPATHSINGLE=${GOPATH%%;*}
fi
if [ "$(go env GOOS)" = "freebsd" ]; then
export CC="clang"
fi
# On OSX, we need to use an older target to ensure binaries are
# compatible with older linkers
if [ "$(go env GOOS)" = "darwin" ]; then
export MACOSX_DEPLOYMENT_TARGET=10.6
fi
# Determine the arch/os combos we're building for
XC_ARCH=${XC_ARCH:-"386 amd64 arm"}
XC_OS=${XC_OS:-"darwin freebsd linux netbsd openbsd windows"}
# Install dependencies
echo "--> Installing dependencies to speed up builds..."
go get \
-ldflags "${CGO_LDFLAGS}" \
./...
echo "==> Getting dependencies..."
go get ./...
# Delete the old dir
echo "==> Removing old directory..."
rm -f bin/*
rm -rf pkg/*
mkdir -p bin/
# If its dev mode, only build for ourself
if [ "${CONSUL_DEV}x" != "x" ]; then
XC_OS=$(go env GOOS)
XC_ARCH=$(go env GOARCH)
fi
# Build!
echo "--> Building..."
go build \
-ldflags "${CGO_LDFLAGS} -X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -X main.GitDescribe=${GIT_DESCRIBE}" \
-v \
-o bin/consul${EXTENSION}
cp bin/consul${EXTENSION} "${GOPATHSINGLE}/bin"
echo "==> Building..."
gox \
-os="${XC_OS}" \
-arch="${XC_ARCH}" \
-ldflags "-X main.GitCommit ${GIT_COMMIT}${GIT_DIRTY} -X main.GitDescribe ${GIT_DESCRIBE}" \
-output "pkg/{{.OS}}_{{.Arch}}/consul" \
.
# Move all the compiled things to the $GOPATH/bin
GOPATH=${GOPATH:-$(go env GOPATH)}
case $(uname) in
CYGWIN*)
GOPATH="$(cygpath $GOPATH)"
;;
esac
OLDIFS=$IFS
IFS=: MAIN_GOPATH=($GOPATH)
IFS=$OLDIFS
# Copy our OS/Arch to the bin/ directory
DEV_PLATFORM="./pkg/$(go env GOOS)_$(go env GOARCH)"
for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f); do
cp ${F} bin/
cp ${F} ${MAIN_GOPATH}/bin/
done
if [ "${CONSUL_DEV}x" = "x" ]; then
# Zip and copy to the dist dir
echo "==> Packaging..."
for PLATFORM in $(find ./pkg -mindepth 1 -maxdepth 1 -type d); do
OSARCH=$(basename ${PLATFORM})
echo "--> ${OSARCH}"
pushd $PLATFORM >/dev/null 2>&1
zip ../${OSARCH}.zip ./*
popd >/dev/null 2>&1
done
fi
# Done!
echo
echo "==> Results:"
ls -hl bin/

View File

@ -8,12 +8,6 @@ if [ -z $VERSION ]; then
exit 1
fi
# Make sure we have a bintray API key
if [ -z $BINTRAY_API_KEY ]; then
echo "Please set your bintray API key in the BINTRAY_API_KEY env var."
exit 1
fi
# Get the parent directory of where this script is.
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
@ -22,53 +16,45 @@ DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
# Change into that dir because we expect that
cd $DIR
# Generate the tag
if [ -z $NOTAG ]; then
echo "==> Tagging..."
git commit --allow-empty -a --gpg-sign=348FFC4C -m "Release v$VERSION"
git tag -a -m "Version $VERSION" -s -u 348FFC4C "v${VERSION}" master
fi
# Generate the UI
if [ -z $NOUI ]; then
echo "==> Generating Consul UI..."
make -f ui/Makefile dist
fi
# Zip all the files
rm -rf ./dist/pkg
mkdir -p ./dist/pkg
for FILENAME in $(find ./dist -mindepth 1 -maxdepth 1 -type f); do
rm -rf ./pkg/dist
mkdir -p ./pkg/dist
for FILENAME in $(find ./pkg -mindepth 1 -maxdepth 1 -type f); do
FILENAME=$(basename $FILENAME)
EXTENSION="${FILENAME##*.}"
PLATFORM="${FILENAME%.*}"
if [ "${EXTENSION}" != "exe" ]; then
EXTENSION=""
else
EXTENSION=".${EXTENSION}"
fi
CONSULNAME="consul${EXTENSION}"
pushd ./dist
if [ "${FILENAME}" = "ui.zip" ]; then
cp ${FILENAME} ./pkg/${VERSION}_web_ui.zip
else
if [ "${EXTENSION}" = "" ]; then
chmod +x ${FILENAME}
fi
cp ${FILENAME} ${CONSULNAME}
zip ./pkg/${VERSION}_${PLATFORM}.zip ${CONSULNAME}
rm ${CONSULNAME}
fi
popd
cp ./pkg/${FILENAME} ./pkg/dist/consul_${VERSION}_${FILENAME}
done
# Make the checksums
pushd ./dist/pkg
shasum -a256 * > ./${VERSION}_SHA256SUMS
pushd ./pkg/dist
shasum -a256 * > ./consul_${VERSION}_SHA256SUMS
if [ -z $NOSIGN ]; then
echo "==> Signing..."
gpg --default-key 348FFC4C --detach-sig ./consul_${VERSION}_SHA256SUMS
fi
popd
# Upload
for ARCHIVE in ./dist/pkg/*; do
ARCHIVE_NAME=$(basename ${ARCHIVE})
# # Upload
# for ARCHIVE in ./pkg/dist/*; do
# ARCHIVE_NAME=$(basename ${ARCHIVE})
echo Uploading: $ARCHIVE_NAME
curl \
-T ${ARCHIVE} \
-umitchellh:${BINTRAY_API_KEY} \
"https://api.bintray.com/content/mitchellh/consul/consul/${VERSION}/${ARCHIVE_NAME}"
done
# echo Uploading: $ARCHIVE_NAME
# curl \
# -T ${ARCHIVE} \
# -umitchellh:${BINTRAY_API_KEY} \
# "https://api.bintray.com/content/mitchellh/consul/consul/${VERSION}/${ARCHIVE_NAME}"
# done
exit 0
# exit 0

View File

@ -1,3 +1,5 @@
ROOT:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
server:
python -m SimpleHTTPServer
@ -5,16 +7,6 @@ watch:
sass styles:static --watch
dist:
@echo clean dist
@rm -rf dist/index.html
@rm -rf dist/static
@echo "compile styles/*.scss"
@sass styles/base.scss static/base.css
@ruby scripts/compile.rb
cp -R ./static dist/static/
cp index.html dist/index.html
sed -E -e "/ASSETS/,/\/ASSETS/ d" -ibak dist/index.html
sed -E -e "s#<\/body>#<script src=\"static/application.min.js\"></script></body>#" -ibak dist/index.html
rm dist/index.htmlbak
@sh -c "'$(ROOT)/scripts/dist.sh'"
.PHONY: server watch dist

37
ui/scripts/dist.sh Executable file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env bash
set -e
# Get the parent directory of where this script is.
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
# Change into that directory
cd "$DIR"
# Generate the tag
DEPLOY="../pkg/web_ui"
rm -rf $DEPLOY
mkdir -p $DEPLOY
bundle check >/dev/null 2>&1 || bundle install
bundle exec sass styles/base.scss static/base.css
bundle exec ruby scripts/compile.rb
# Copy into deploy
shopt -s dotglob
cp -r $DIR/static/* $DEPLOY/
cp index.html $DEPLOY/
# Magic scripting
sed -E -e "/ASSETS/,/\/ASSETS/ d" -ibak $DEPLOY/index.html
sed -E -e "s#<\/body>#<script src=\"static/application.min.js\"></script></body>#" -ibak $DEPLOY/index.html
# Remove the backup file from sed
rm $DEPLOY/index.htmlbak
pushd $DEPLOY >/dev/null 2>&1
zip ../web_ui.zip ./*
popd >/dev/null 2>&1