Skip to content

Commit 657b156

Browse files
committed
Revert "fix: fix env escaping (#25)"
This reverts commit 0adfe53.
1 parent 1689df6 commit 657b156

File tree

1 file changed

+26
-102
lines changed

1 file changed

+26
-102
lines changed

bin/compile

Lines changed: 26 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -34,99 +34,47 @@ trap 'finish_error $LINENO' ERR
3434

3535
logstamp() { printf "[%s]" "$(TZ='America/Chicago' date +'%Y-%m-%d %H:%M:%S')" ; }
3636
topic() { echo "-----> $(logstamp) $*" ; }
37-
info() { echo " $*" ; }
38-
HEROKU_FREETDS_BUILDPACK_DEBUG="${HEROKU_FREETDS_BUILDPACK_DEBUG:-false}"
39-
debug() {
40-
if [ "$HEROKU_FREETDS_BUILDPACK_DEBUG" = "true" ]; then
41-
echo "[DEBUG] $*"
42-
fi
43-
}
44-
debug_topic() {
45-
if [ "$HEROKU_FREETDS_BUILDPACK_DEBUG" = "true" ]; then
46-
echo "[DEBUG] -----> $*"
47-
fi
48-
}
49-
50-
error() { echo " ! $*" ; >&2 exit 1 ; }
51-
indent() {
52-
local command; command='s/^/ /'
53-
case $(uname) in
54-
Darwin) sed -l "$command";;
55-
*) sed -u "$command";;
56-
esac
57-
}
37+
info() { echo " $*" ; }
38+
indent() { sed -u 's/^/ /' ; } # it always runs on linux. on darwin is 'sed -l'
5839

5940
BUILD_DIR="$1"
6041
CACHE_DIR="$2"
42+
mkdir -p "$BUILD_DIR" "$CACHE_DIR"
6143
# shellcheck disable=SC2034
6244
ENV_DIR="${3}"
6345
# echo "$STACK"
6446
# echo "$SOURCE_VERSION"
6547
# See https://devcenter.heroku.com/articles/buildpack-api#bin-compile-summary
6648

67-
mkdir -p "$BUILD_DIR" "$CACHE_DIR"
68-
# Store which STACK we are running on in the cache to bust the cache if it changes
69-
if [ -f "${CACHE_DIR}/.freetds/STACK" ]; then
70-
CACHED_STACK=$(cat "$CACHE_DIR/.freetds/STACK")
71-
else
72-
CACHED_STACK=$STACK
73-
fi
74-
# Ensure we store the STACK in the cache for next time.
75-
mkdir -p "$CACHE_DIR/.freetds"
76-
echo "$STACK" > "$CACHE_DIR/.freetds/STACK"
77-
7849
BIN_DIR="$(cd "$(dirname "$0")"; pwd)" # absolute path
7950
LP_DIR="$(cd "$(dirname "$0")"; cd ..; pwd)"
8051
ROOT_DIR="$(dirname "$BIN_DIR")"
8152

82-
info "BIN_DIR=${BIN_DIR}, ROOT_DIR=${ROOT_DIR}, BUILD_DIR=${BUILD_DIR}, CACHE_DIR=${CACHE_DIR}, CACHED_STACK=${CACHED_STACK} pwd=$(pwd)"
83-
84-
debug_exported_env_vars() {
85-
debug "ENV VARS"
86-
debug "ENV VARS: PATH=${PATH:-}"
87-
debug "ENV VARS: FREETDS_DIR=${FREETDS_DIR:-}"
88-
debug "ENV VARS: LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-}"
89-
debug "ENV VARS: LD_RUN_PATH=${LD_RUN_PATH:-}"
90-
debug "ENV VARS: LIBRARY_PATH=${LIBRARY_PATH:-}"
91-
debug "ENV VARS: SYBASE=${SYBASE:-}"
92-
}
93-
94-
debug_topic "debug env vars before load_env_vars"
95-
debug_exported_env_vars
53+
info "BIN_DIR=${BIN_DIR}, ROOT_DIR=${ROOT_DIR}, BUILD_DIR=${BUILD_DIR}, CACHE_DIR=${CACHE_DIR}, pwd=$(pwd)"
9654

9755
load_env_vars() {
9856
local env_var; env_var="${1:-}"
9957
until [ -z "$env_var" ]; do [ -f "$ENV_DIR/$env_var" ] && export "$env_var=$(cat "$ENV_DIR/$env_var")"; shift ; env_var="${1:-}" ; done
10058
}
10159
load_env_vars "FREETDS_VERSION" "FREETDS_ARCHIVE_NAME" "TDS_VERSION" "FREETDS_REBUILD" "USE_GNUTLS"
10260

103-
debug_topic "debug env vars after load_env_vars"
104-
debug_exported_env_vars
105-
10661
FREETDS_VERSION="${FREETDS_VERSION:-1.00.109}"
10762
FREETDS_ARCHIVE_NAME="${FREETDS_ARCHIVE_NAME:-freetds-${FREETDS_VERSION}}"
10863
TDS_VERSION="${TDS_VERSION:-7.3}" # or TDSVER
10964
USE_GNUTLS="${USE_GNUTLS:---with-gnutls}"
11065

111-
FREETDS_CACHE_DIR="$CACHE_DIR/freetds/cache"
112-
mkdir -p "${FREETDS_CACHE_DIR}"
113-
CACHED_TAR="${FREETDS_CACHE_DIR}/freetds-${FREETDS_VERSION}-heroku.tar.bz2"
114-
if [[ "$CACHED_STACK" != "$STACK" ]] ; then
115-
topic "Stack has changed. Clearing cache"
116-
rm -f "$CACHED_TAR"
117-
elif [ "${FREETDS_REBUILD:-}" = "true" ]; then
118-
# Default rebuild to true since I'm having issues linking the library to tiny_tds gem with a cached build.
119-
topic "FREETDS_REBUILD is true. Clearing cache"
66+
CACHED_TAR="${CACHE_DIR}/freetds-${FREETDS_VERSION}-heroku.tar.bz2"
67+
# Default rebuild to true since I'm having issues linking the library to tiny_tds gem with a cached build.
68+
if [ "${FREETDS_REBUILD:-}" = "true" ]; then
12069
rm -f "$CACHED_TAR"
12170
fi
122-
12371
# This installation target is in the BUILD_DIR, which means that
12472
# it'll end up in the slug at runtime.
125-
BUILD_TARGET_DIR="${BUILD_DIR}/.freetds"
73+
BUILD_TARGET_DIR="${BUILD_DIR}/freetds"
12674
mkdir -p "${BUILD_TARGET_DIR}"
12775

12876
# This is the path that will be used at dyno runtime, and in which we build.
129-
APP_TARGET_DIR="${HOME:-app}/.freetds" # /app/freetds"
77+
APP_TARGET_DIR="/app/freetds"
13078
mkdir -p "${APP_TARGET_DIR}"
13179

13280
configure_app_env_vars() {
@@ -136,51 +84,44 @@ configure_app_env_vars() {
13684
# These exports must point to /app, because the profile is
13785
# executed in a running dyno, not the buildpack environment
13886
cat <<EOF > "${BUILD_DIR}/.profile.d/freetds.sh"
139-
export PATH="\${HOME}/.freetds/bin:\$PATH"
87+
export PATH="${APP_TARGET_DIR}/bin:\$PATH"
14088
# tiny_tds extconf.rb uses FREETDS_DIR
14189
# https://github.com/rails-sqlserver/tiny_tds/blob/5046755ca91594003f8b3ca541d136f3ed859973/ext/tiny_tds/extconf.rb#L36-L38
142-
export FREETDS_DIR="\${HOME}/.freetds"
143-
export LD_LIBRARY_PATH="\${HOME}/.freetds/lib:\${LD_LIBRARY_PATH:-}"
144-
export LD_RUN_PATH="\${HOME}/.freetds/lib:\${LD_RUN_PATH:-}"
145-
export LIBRARY_PATH="\${HOME}/.freetds/lib:\${LIBRARY_PATH:-}"
146-
export SYBASE="\${HOME}/.freetds"
90+
export FREETDS_DIR="${APP_TARGET_DIR}"
91+
export LD_LIBRARY_PATH="${APP_TARGET_DIR}/lib:${LD_LIBRARY_PATH:-/usr/local/lib}"
92+
export LD_RUN_PATH="${APP_TARGET_DIR}/lib:${LD_RUN_PATH:-/usr/local/lib}"
93+
export LIBRARY_PATH="${APP_TARGET_DIR}/lib:${LIBRARY_PATH:-/usr/local/lib}"
94+
export SYBASE="${APP_TARGET_DIR}"
14795
EOF
14896
chmod +x "${BUILD_DIR}/.profile.d/freetds.sh"
14997
echo "configured APP env vars:" | indent
15098
indent < "${BUILD_DIR}/.profile.d/freetds.sh"
151-
# shellcheck disable=SC1091
99+
# shellcheck disable=SC1090
152100
. "${BUILD_DIR}/.profile.d/freetds.sh"
153101
}
154102

155103
configure_buildpack_env_vars() {
156104
# These exports point to the build directory, not to /app, so that
157105
# they work for later buildpacks.
158-
# Assert $BUILD_TARGET_DIR = $HOME/.freetds # /app/freetds
159-
# Assert $APP_TARGET_DIR = $HOME/.freetds # /app/freetds
160-
export PATH="${HOME}/.freetds/bin:$PATH"
161-
export FREETDS_DIR="${HOME}/.freetds"
162-
export LD_LIBRARY_PATH="${HOME}/.freetds/lib:${LD_LIBRARY_PATH:-}"
163-
export LD_RUN_PATH="${HOME}/.freetds/lib:${LD_RUN_PATH:-}"
164-
export LIBRARY_PATH="${HOME}/.freetds/lib:${LIBRARY_PATH:-}"
165-
export SYBASE="${HOME}/.freetds"
106+
export PATH="${BUILD_TARGET_DIR}/bin:$PATH"
107+
export FREETDS_DIR="${BUILD_TARGET_DIR}"
108+
export LD_LIBRARY_PATH="${BUILD_TARGET_DIR}/lib:${LD_LIBRARY_PATH:-/usr/local/lib}"
109+
export LD_RUN_PATH="${BUILD_TARGET_DIR}/lib:${LD_RUN_PATH:-/usr/local/lib}"
110+
export LIBRARY_PATH="${BUILD_TARGET_DIR}/lib:${LIBRARY_PATH:-/usr/local/lib}"
111+
export SYBASE="${APP_TARGET_DIR}"
166112
# give environment to later buildpacks
167-
export | grep -E -e ' (PATH|LD_LIBRARY_PATH|LD_RUN_PATH|LIBRARY_PATH|FREETDS_DIR|SYBASE)=' > "${LP_DIR}/export"
113+
export | grep -E -e ' (PATH|LD_LIBRARY_PATH|LIBRARY_PATH|FREETDS_DIR|SYBASE)=' > "${LP_DIR}/export"
168114

169115
echo "configured BUILDPACK env vars:" | indent
170116
indent < "${LP_DIR}/export"
171-
172-
topic "Rewrite package-config files"
173-
find "$BUILD_DIR/.freetds" -type f -ipath '*/pkgconfig/*.pc' | xargs --no-run-if-empty -n 1 sed -i -e 's!^prefix=\(.*\)$!prefix='"$BUILD_DIR"'/.freetds\1!g'
174117
}
175118

176119
download_and_extract_freetds_archive() {
177120
FREETDS_FILE="$FREETDS_ARCHIVE_NAME.tar.gz"
178121
FREETDS_URL="https://www.freetds.org/files/stable/${FREETDS_FILE}"
122+
# TODO(BF): Print log when HEROKUR_FREETDS_BUILDPACK_DEBUG is set
179123
# TODO(BF): Consider calculating and verifying SHA256: openssl dgst -sha256 < "$FREETDS_FILE"
180124
curl -s "$FREETDS_URL" | tar xzvf - -C "${BUILD_DIR}" > "${BUILD_DIR}/build_log-unpack.log" # Can't write to the archive dir until it exists
181-
if [ "$HEROKU_FREETDS_BUILDPACK_DEBUG" = "true" ]; then
182-
debug "$(cat "${BUILD_DIR}/build_log-unpack.log")"
183-
fi
184125
mv "${BUILD_DIR}/build_log-unpack.log" "${BUILD_DIR}/${FREETDS_ARCHIVE_NAME}/"
185126
}
186127

@@ -211,20 +152,13 @@ build_and_install_freetds() {
211152
--disable-debug \
212153
"--with-tdsver=${TDS_VERSION}"
213154
EOF
155+
# TODO(BF): Print log when HEROKUR_FREETDS_BUILDPACK_DEBUG is set
214156
/bin/bash .build_options > build_log-configure.stdout.log 2> build_log-configure.stderr.log
215-
if [ "$HEROKU_FREETDS_BUILDPACK_DEBUG" = "true" ]; then
216-
debug "$(cat .build_options)"
217-
debug "$(cat build_log-configure.stdout.log)"
218-
debug "$(cat build_log-configure.stderr.log)"
219-
fi
220157
# Flush make output to log since a file will be faster than printing to the screen.
158+
# TODO(BF): Print log when HEROKUR_FREETDS_BUILDPACK_DEBUG is set
221159
{
222160
make && make install && make clean
223161
} > build_log-make_install.stdout.log 2> build_log-make_install.stderr.log
224-
if [ "$HEROKU_FREETDS_BUILDPACK_DEBUG" = "true" ]; then
225-
debug "$(cat build_log-make_install.stdout.log)"
226-
debug "$(cat build_log-make_install.stderr.log)"
227-
fi
228162
cp .build_options build_log-* "${APP_TARGET_DIR}/"
229163

230164
cd "$BUILD_DIR"
@@ -246,11 +180,7 @@ cache_build_artifacts_for_future_deploys() {
246180
main() {
247181
topic "FreeTDS ${FREETDS_VERSION} building in ${BUILD_DIR}"
248182

249-
debug_topic "debug env vars before configure_app_env_vars"
250-
debug_exported_env_vars
251183
configure_app_env_vars
252-
debug_topic "debug env vars after configure_app_env_vars"
253-
debug_exported_env_vars
254184

255185
if [ ! -f "$CACHED_TAR" ]; then
256186
info "Cached files not found - downloading and unpacking..."
@@ -267,14 +197,8 @@ main() {
267197
info "Unpacking cached files..."
268198
tar xjf "${CACHED_TAR}" -C "${BUILD_TARGET_DIR}" | indent
269199

270-
debug_topic "debug env vars before configure_buildpack_env_vars"
271-
debug_exported_env_vars
272-
273200
configure_buildpack_env_vars
274201

275-
debug_topic "debug env vars after configure_buildpack_env_vars"
276-
debug_exported_env_vars
277-
278202
info "Install of FreeTDS ${FREETDS_VERSION} complete"
279203
}
280204

0 commit comments

Comments
 (0)