Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vendor buildpack-stdlib #3

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Changelog

## Main

* Vendor buildpack-stdlib rather than downloading it at build time
6 changes: 4 additions & 2 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ set -ueo pipefail
# clean up leaking environment
unset GIT_DIR

source /dev/stdin <<< "$(curl -s --retry 3 https://lang-common.s3.amazonaws.com/buildpack-stdlib/v4/stdlib.sh)"

BUILD_DIR=$1
CACHE_DIR=$2
HOME=$1 # Required for retaining Heroku CLI plugins
BUILDPACK_DIR="$(dirname $(dirname "$0"))"
HEROKU_CLI_URL="https://cli-assets.heroku.com/heroku-linux-x64.tar.xz"
LIB_DIR="$BUILDPACK_DIR/lib"

puts_step "Vendoring stdlib"
source $LIB_DIR/buildpack-stdlib-v7.sh

puts_step "Fetching and vendoring Heroku CLI into slug"
rm -rf "$BUILD_DIR/.heroku/cli"
Expand Down
164 changes: 164 additions & 0 deletions lib/buildpack-stdlib-v7.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# Buildpack defaults
# ---------------

export BUILDPACK_LOG_FILE=${BUILDPACK_LOG_FILE:-/dev/null}

# Standard Output
# ---------------

# Buildpack Steps.
puts_step() {
if [[ "$@" == "-" ]]; then
read output
else
output=$@
fi
echo -e "\e[1m\e[36m=== $output\e[0m"
unset output
}

# Buildpack Error.
puts_error() {
if [[ "$@" == "-" ]]; then
read output
else
output=$@
fi
echo -e "\e[1m\e[31m=!= $output\e[0m"
}

# Buildpack Warning.
puts_warn() {
if [[ "$@" == "-" ]]; then
read output
else
output=$@
fi
echo -e "\e[1m\e[33m=!= $output\e[0m"
}


# Buildpack Utilities
# -------------------

# Usage: $ set-env key value
# NOTICE: Expects PROFILE_PATH & EXPORT_PATH to be set!
set_env() {
# TODO: automatically create profile path directory if it doesn't exist.
echo "export $1=$2" >> $PROFILE_PATH
echo "export $1=$2" >> $EXPORT_PATH
}

# Usage: $ set-default-env key value
# NOTICE: Expects PROFILE_PATH & EXPORT_PATH to be set!
set_default_env() {
echo "export $1=\${$1:-$2}" >> $PROFILE_PATH
echo "export $1=\${$1:-$2}" >> $EXPORT_PATH
}

# Usage: $ un-set-env key
# NOTICE: Expects PROFILE_PATH to be set!
un_set_env() {
echo "unset $1" >> $PROFILE_PATH
}

# Usage: $ _env-blacklist pattern
# Outputs a regex of default blacklist env vars.
_env_blacklist() {
local regex=${1:-''}
if [ -n "$regex" ]; then
regex="|$regex"
fi
echo "^(PATH|GIT_DIR|CPATH|CPPATH|LD_PRELOAD|LIBRARY_PATH$regex)$"
}

# Usage: $ export-env ENV_DIR WHITELIST BLACKLIST
# Exports the environment variables defined in the given directory.
export_env() {
local env_dir=${1:-$ENV_DIR}
local whitelist=${2:-''}
local blacklist="$(_env_blacklist $3)"
if [ -d "$env_dir" ]; then
for e in $(ls $env_dir); do
echo "$e" | grep -E "$whitelist" | grep -qvE "$blacklist" &&
export "$e=$(cat $env_dir/$e)"
:
done
fi
}

# Usage: $ sub-env command
# Runs a subshell of specified command with user-provided config.
# NOTICE: Expects ENV_DIR to be set. WHITELIST & BLACKLIST are optional.
# Examples:
# WHITELIST=${2:-''}
# BLACKLIST=${3:-'^(GIT_DIR|PYTHONHOME|LD_LIBRARY_PATH|LIBRARY_PATH|PATH)$'}
sub_env() {
(
export_env $ENV_DIR $WHITELIST $BLACKLIST

$1
)
}

# Logging
# -------

# Notice: These functions expect BPLOG_PREFIX and BUILDPACK_LOG_FILE to be defined (BUILDPACK_LOG_FILE can point to /dev/null if not provided by the buildpack).
# Example: BUILDPACK_LOG_FILE=${BUILDPACK_LOG_FILE:-/dev/null}; BPLOG_PREFIX="buildpack.go"

# Returns now, in milleseconds. Useful for logging.
# Example: $ let start=$(nowms); sleep 30; mtime "glide.install.time" "${start}"
nowms() {
date +%s%3N
}

# Log arbitrary data to the logfile (e.g. a packaging file).
# Usage: $ bplog "$(<${vendorJSON})
bplog() {
echo -n ${@} | awk 'BEGIN {printf "msg=\""; f="%s"} {gsub(/"/, "\\\"", $0); printf f, $0} {if (NR == 1) f="\\n%s" } END { print "\"" }' >> ${BUILDPACK_LOG_FILE}
}

# Measures time elapsed for a specific build step.
# Usage: $ let start=$(nowms); mtime "glide.install.time" "${start}"
# https://github.com/heroku/engineering-docs/blob/master/guides/logs-as-data.md#distributions-measure
mtime() {
local key="${BPLOG_PREFIX}.${1}"
local start="${2}"
local end="${3:-$(nowms)}"
echo "${key} ${start} ${end}" | awk '{ printf "measure#%s=%.3f\n", $1, ($3 - $2)/1000 }' >> ${BUILDPACK_LOG_FILE}
}

# Logs a count for a specific built step.
# Usage: $ mcount "tool.govendor"
# https://github.com/heroku/engineering-docs/blob/master/guides/logs-as-data.md#counting-count
mcount() {
local k="${BPLOG_PREFIX}.${1}"
local v="${2:-1}"
echo "count#${k}=${v}" >> ${BUILDPACK_LOG_FILE}
}

# Logs a measure for a specific build step.
# Usage: $ mmeasure "tool.installed_dependencies" 42
# https://github.com/heroku/engineering-docs/blob/master/guides/logs-as-data.md#distributions-measure
mmeasure() {
local k="${BPLOG_PREFIX}.${1}"
local v="${2}"
echo "measure#${k}=${v}" >> ${BUILDPACK_LOG_FILE}
}

# Logs a unuique measurement build step.
# Usage: $ munique "versions.count" 2.7.13
# https://github.com/heroku/engineering-docs/blob/master/guides/logs-as-data.md#uniques-unique
munique() {
local k="${BPLOG_PREFIX}.${1}"
local v="${2}"
echo "unique#${k}=${v}" >> ${BUILDPACK_LOG_FILE}
}

# Measures when an exit path to the buildpack is reached, given a name, then exits 1.
# Usage: $ mcount-exi "binExists"
mcount_exit() {
mcount "error.${1}"
exit 1
}