#!/bin/bash #/ Usage: script/packages/build [VERSION] #/ #/ WARNING: Do not call this directly. Please create packages using #/ `script/package [platform]` or `bundle exec rake package[platform]` #/ #/ Builds a distributable package for licensed for a given RUBYC compiler and licensed VERSION. #/ Packages are of the form licensed-$VERSION-$PLATFORM-x64.tar.gz and contain a `./licensed` executable #/ Built Packages are placed in the /pkg/$VERSION directory. #/ #/ OPTIONS: #/ The path to a rubyc compiler that should be used to compile the target executable #/ [VERSION] (optional, default to current git branch or SHA1) version of licensed to build exe at #/ #/ EXAMPLES: #/ #/ Builds a package for version 1.1.0 using a local rubyc compiler #/ $ script/packages/build RUBYC="./rubyc-darwin" VERSION="1.1.0" #/ set -euo pipefail BASE_DIR="$(cd "$(dirname $0)/../.." && pwd)" RUBYC=${RUBYC:=""} if [ ! -f "$RUBYC" ]; then echo "Specify a rubyc compiler using the RUBYC environment variable" >&2 exit 127 fi # if a version is not provided, get an identifier from the current HEAD VERSION=${VERSION:="$(git rev-parse --abbrev-ref HEAD)"} BUILD_DIR="$(mktemp -d)" COPY_DIR="$(mktemp -d)" trap "rm -rf $BUILD_DIR; rm -rf $COPY_DIR" EXIT # copy the repo to a separate directory. determining license metadata # will require a clean environment with no Gemfile.lock. using a work location # is preferred to messing with a development repository rsync -r --exclude="test/" --exclude=".licenses/" --exclude="vendor/" --exclude="Gemfile.lock" --exclude="pkg/" $BASE_DIR/ $COPY_DIR cd $COPY_DIR # ensure repo is at $VERSION and build executable, restoring the repo to previous # state after build. ( # run in a subshell for ease of returning to the current branch after building # the executable CURRENT_BRANCH="$(git rev-parse --abbrev-ref HEAD)" if [[ "$VERSION" != "$CURRENT_BRANCH" ]]; then git checkout "$VERSION" trap "git checkout $CURRENT_BRANCH" EXIT fi # get the openssl dir to use when building based on ruby's default ssl cert dir OPENSSL_DIR="$(cd "$(ruby -e 'require "net/https"; puts OpenSSL::X509::DEFAULT_CERT_DIR')/.." && pwd)" # build the licensed rubyc executable "$RUBYC" --openssl-dir "$OPENSSL_DIR" --clean-tmpdir -o "$BUILD_DIR/licensed" "$COPY_DIR/exe/licensed" chmod +x $BUILD_DIR/licensed ) # non-executable content will be stored in a `meta` directory mkdir -p "$BUILD_DIR/meta" # include dependency license data in package; run bundle update in temp work dir # and then run licensed on itself in that directory to grab license data # NOTE: this does not produce accurate license information if any licensed # depends on any platform-specific gems. ( # run in a subshell so that `unset BUNDLER_VERSION` is contained. the ENV var # is required to successfully build a distributable executable on # dockerized linux unset BUNDLER_VERSION # determining dependency license data needs to be done from a clean environment # without previous dependencies or a Gemfile.lock so as to pull in the latest # bundler version, which is what the distributed executable uses script/bootstrap bundle exec exe/licensed cache cp -R "$COPY_DIR/.licenses" "$BUILD_DIR/meta" ) # copy static metadata to build directory mkdir -p "$BUILD_DIR/meta/ruby" curl -o "$BUILD_DIR/meta/ruby/license.txt" "https://www.ruby-lang.org/en/about/license.txt" cp "$BASE_DIR/LICENSE" "$BUILD_DIR/meta" cp "$BASE_DIR/README.md" "$BUILD_DIR/meta" # create release archive PLATFORM="$(uname -s | tr '[:upper:]' '[:lower:]')" TARGET="$BASE_DIR/pkg/$VERSION/licensed-$VERSION-$PLATFORM-x64.tar.gz" mkdir -p "$(dirname $TARGET)" tar -C "$BUILD_DIR" -czf "$TARGET" . echo "licensed package built to $TARGET"