Skip to content

Commit

Permalink
Undo changes that broke the build pack
Browse files Browse the repository at this point in the history
This reverts commit 897a318.
  • Loading branch information
jiaaro committed Jun 4, 2014
1 parent dd7965a commit 23c90bb
Showing 1 changed file with 201 additions and 28 deletions.
229 changes: 201 additions & 28 deletions bin/compilenodejs
Original file line number Diff line number Diff line change
@@ -1,32 +1,205 @@
#!/usr/bin/env bash
set -eo pipefail
# bin/compile <build-dir> <cache-dir>

# fail fast
set -e

# debug
# set -x

# clean up leaking environment
unset GIT_DIR

# config
SCONS_VERSION="1.2.0"
S3_BUCKET="heroku-buildpack-nodejs"

# parse and derive params
BUILD_DIR=$1
CACHE_DIR=$2
LP_DIR=`cd $(dirname $0); cd ..; pwd`

function error() {
echo " ! $*"
exit 1
}

function mktmpdir() {
dir=$(mktemp -t node-$1-XXXX)
rm -rf $dir
mkdir -p $dir
echo $dir
}

function indent() {
c='s/^/ /'
case $(uname) in
Darwin) sed -l "$c";;
*) sed -u "$c";;
esac
}

function run_npm() {
command="$1"

cd $BUILD_DIR
HOME="$BUILD_DIR" $VENDORED_NODE/bin/node $VENDORED_NPM/cli.js >&1 | indent

if [ "${PIPESTATUS[*]}" != "0 0" ]; then
echo " ! Failed to $command dependencies with npm"
exit 1
fi
}

function run_npm_install_global() {
package="$1"

cd $BUILD_DIR
HOME="$BUILD_DIR"
$VENDORED_NODE/bin/node $VENDORED_NPM/cli.js install -g --prefix $BUILD_DIR $package 2>&1 | indent

if [ "${PIPESTATUS[*]}" != "0 0" ]; then
echo " ! Failed to install dependencies with npm"
exit 1
fi
}

function manifest_versions() {
curl "http://${S3_BUCKET}.s3.amazonaws.com/manifest.${1}" -s -o - | tr -s '\n' ' '
}

function resolve_version() {
available_versions="$1"
requested_version="$2"
default_version="$3"

if [ "$2" == "" ]; then
echo $3
else
args=""
for version in $available_versions; do args="${args} -v \"${version}\""; done
for version in $requested_version; do args="${args} -r \"${version}\""; done
eval $bootstrap_node/bin/node $LP_DIR/vendor/node-semver/bin/semver ${args} | tail -n1
fi
}

function package_engine_version() {
version=$(echo "{}" | $bootstrap_node/bin/node $LP_DIR/vendor/json/json engines.$1 2>/dev/null)
if [ $? == 0 ]; then
echo $version | sed -e 's/\([<>=]\) /\1/g'
fi
}

function package_resolve_version() {
engine="$1"
resolved_version=$(resolve_version "${engine_versions[$engine]}" "${engine_requests[$engine]}" "${engine_defaults[$engine]}")

if [ "${resolved_version}" == "" ]; then
error "Requested engine $engine version ${engine_requests[$engine]} does not match available versions: ${engine_versions[$engine]}"
else
echo $resolved_version
fi
}

function package_download() {
engine="$1"
version="$2"
location="$3"

mkdir -p $location
package="http://${S3_BUCKET}.s3.amazonaws.com/$engine-$version.tgz"
curl $package -s -o - | tar xzf - -C $location
}

bootstrap_node=$(mktmpdir bootstrap_node)
package_download "nodejs" "0.4.7" $bootstrap_node

# make some associative arrays
declare -A engine_versions
declare -A engine_defaults
declare -A engine_requests

engine_defaults["node"]="0.8.14"
engine_defaults["npm"]="1.1.65"

engine_versions["node"]=$(manifest_versions "nodejs")
engine_requests["node"]=$(package_engine_version "node")

engine_versions["npm"]=$(manifest_versions "npm")
engine_requests["npm"]=$(package_engine_version "npm")

echo "-----> Resolving engine versions"
NODE_VERSION=$(package_resolve_version "node")
echo "Using Node.js version: ${NODE_VERSION}" | indent

NPM_VERSION=$(package_resolve_version "npm")
echo "Using npm version: ${NPM_VERSION}" | indent

# cache directories
CACHE_STORE_DIR="$CACHE_DIR/node_modules/$NODE_VERSION/$NPM_VERSION"
CACHE_TARGET_DIR="$BUILD_DIR/node_modules"

# s3 packages
NODE_PACKAGE="http://${S3_BUCKET}.s3.amazonaws.com/nodejs-${NODE_VERSION}.tgz"
NPM_PACKAGE="http://${S3_BUCKET}.s3.amazonaws.com/npm-${NPM_VERSION}.tgz"
SCONS_PACKAGE="http://${S3_BUCKET}.s3.amazonaws.com/scons-${SCONS_VERSION}.tgz"

# vendor directories
VENDORED_NODE="$(mktmpdir node)"
VENDORED_NPM="$(mktmpdir npm)"
VENDORED_SCONS="$(mktmpdir scons)"

# download and unpack packages
echo "-----> Fetching Node.js binaries"
package_download "nodejs" "${NODE_VERSION}" "${VENDORED_NODE}"
package_download "npm" "${NPM_VERSION}" "${VENDORED_NPM}"
package_download "scons" "${SCONS_VERSION}" "${VENDORED_SCONS}"

# vendor node into the slug
PATH="$BUILD_DIR/bin:$PATH"
echo "-----> Vendoring node into slug"
mkdir -p "$BUILD_DIR/bin"
cp "$VENDORED_NODE/bin/node" "$BUILD_DIR/bin/node"

# setting up paths for building
PATH="$VENDORED_SCONS:$VENDORED_NODE/bin:$PATH"
INCLUDE_PATH="$VENDORED_NODE/include"
export CPATH="$INCLUDE_PATH"
export CPPPATH="$INCLUDE_PATH"

# unpack existing cache
if [ -d $CACHE_STORE_DIR ]; then

# generate a place to put node_modules
TEMP_NODE_MODULES_DIR=$(mktmpdir node_modules)

# move existing node_modules out of the way
if [ -d $CACHE_TARGET_DIR ]; then
mv $CACHE_TARGET_DIR $TEMP_NODE_MODULES_DIR/
fi

# copy the cached node_modules in
mkdir -p $CACHE_TARGET_DIR
cp -R $CACHE_STORE_DIR/* $CACHE_TARGET_DIR/

# move existing node_modules back into place
if [ -d $TEMP_NODE_MODULES_DIR/node_modules ]; then
cp -R $TEMP_NODE_MODULES_DIR/node_modules/* $CACHE_TARGET_DIR/
fi

fi

# install dependencies with npm
echo "-----> Installing dependencies with npm"
for dependency in $(cat $1/npm_requirements.txt)
do
run_npm_install_global $dependency
done
echo "Dependencies installed" | indent

NODE_VERSION=0.10.28
NODE_BASENAME=node-v${NODE_VERSION}-linux-x64
NODE_ARCHIVE="http://nodejs.org/dist/v${NODE_VERSION}/${NODE_BASENAME}.tar.gz"

# make a temp directory
tempdir="$( mktemp -t node_XXXX )"
rm -rf $tempdir
mkdir -p $tempdir

pushd $tempdir
curl -s -L -o tmp-nodejs.tar.gz $NODE_ARCHIVE
tar -zxvf tmp-nodejs.tar.gz > /dev/null
rm tmp-nodejs.tar.gz
popd

mkdir -v -p $BUILD_DIR/.heroku/vendor
pushd $BUILD_DIR/.heroku/vendor
rm -rf node
mv $tempdir/$NODE_BASENAME node
popd

echo $(ls -thora)
echo $(pwd)

ln -sf ../../vendor/node/bin/node .heroku/python/bin/node
ln -sf ../../vendor/node/bin/node-waf .heroku/python/bin/node-waf
ln -sf ../../vendor/node/bin/npm .heroku/python/bin/npm
# repack cache with new assets
if [ -d $CACHE_TARGET_DIR ]; then
rm -rf $CACHE_STORE_DIR
mkdir -p $(dirname $CACHE_STORE_DIR)
cp -a $CACHE_TARGET_DIR $CACHE_STORE_DIR
fi

0 comments on commit 23c90bb

Please sign in to comment.