diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c77069e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +testrunner diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0bf1339 --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +current_dir := $(shell pwd) + +sync-lib-and-bin-from-testrunner: + # usage: + # cd ../ && git clone https://github.com/heroku/heroku-buildpack-testrunner + # make sync-lib-and-bin-from-testrunner testrunner-dir=../heroku-buildpack-testrunner + mkdir -p ${current_dir}/testrunner + + rm -rf ${current_dir}/testrunner/lib + cp -rf ${testrunner-dir}/lib ${current_dir}/testrunner/lib + + rm -rf ${current_dir}/testrunner/bin + cp -rf ${testrunner-dir}/bin ${current_dir}/testrunner/bin + +prepare-testrunner-image: + # Prepare test-runner image for "Different OS" + # + # You will get following images by build this target + # + # 1. heroku-16-buildpack-test-runner + # 2. heroku-20-buildpack-test-runner + docker build . \ + -f $(current_dir)/docker/Dockerfile.testrunner \ + -t heroku-16-buildpack-test-runner \ + --build-arg ubuntu_version=16 + + docker build . \ + -f $(current_dir)/docker/Dockerfile.testrunner \ + -t heroku-20-buildpack-test-runner \ + --build-arg ubuntu_version=20 + +docker-test: + # usage + # make docker-test image=heroku-20-buildpack-test-runner + docker run -it -v $(current_dir):/app/buildpack:ro ${image} diff --git a/bin/compile b/bin/compile index 5159242..06efed4 100755 --- a/bin/compile +++ b/bin/compile @@ -3,6 +3,10 @@ set -e +WHITE="\e[97m" +GREEN="\e[32m" +YELLOW="\e[33m" + BUILD_DIR=$1 CACHE_DIR=$2 @@ -11,40 +15,45 @@ TMP_PATH="$BUILD_DIR/tmp" mkdir -p $CACHE_DIR $BIN_PATH $TMP_PATH [ -z "$WKHTMLTOPDF_VERSION" ] && WKHTMLTOPDF_VERSION="0.12.4" -WKHTMLTOPDF_URL="https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/${WKHTMLTOPDF_VERSION}/wkhtmltox-${WKHTMLTOPDF_VERSION}_linux-generic-amd64.tar.xz" -WKHTMLTOPDF_TAR="$CACHE_DIR/wkhtmltox.tar.xz" -WKHTMLTOPDF_PATH="$TMP_PATH/wkhtmltox" -WKHTMLTOPDF_BINARIES="$WKHTMLTOPDF_PATH/bin" BIN_DIR=$(cd $(dirname $0); pwd) FONTS_DIR=$(cd "$BIN_DIR/../fonts"; pwd) -if [ -f $WKHTMLTOPDF_TAR ]; then - echo "-----> Using wkhtmltopdf tar from cache" +echo -e "${GREEN}-----> OS${WHITE}" +echo $OSTYPE +echo $(cat /etc/os-release | grep VERSION_CODENAME) + +OS_VERSIONCODE=$(cat /etc/os-release | grep VERSION_CODENAME) + +if [[ "$OS_VERSIONCODE" == *"VERSION_CODENAME=focal"* ]]; then + # focal is codename of ubuntu 20 + WKHTMLTOPDF_DEB="wkhtmltox_0.12.5-1.focal_amd64.deb" +elif [[ "$OS_VERSIONCODE" == *"VERSION_CODENAME=xenial"* ]]; then + # install wkhtmltopdf for ubuntu 16 + WKHTMLTOPDF_DEB="wkhtmltox_0.12.5-1.xenial_amd64.deb" else - echo "-----> Downloading wkhtmltopdf tar" - curl -L $WKHTMLTOPDF_URL -o $WKHTMLTOPDF_TAR + # default version is for ubuntu 16 + echo -e "${YELLOW}WARNING: ${OS_VERSIONCODE} not supported, install default wkhtmltopdf deb for Ubuntu 16${WHITE}" + WKHTMLTOPDF_DEB="wkhtmltox_0.12.5-1.xenial_amd64.deb" fi -echo "-----> Unpacking tar" -tar -xf $WKHTMLTOPDF_TAR -C $TMP_PATH +WKHTMLTOPDF_URL="https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/${WKHTMLTOPDF_DEB}" -echo "-----> Setting permissions" -chmod +x $WKHTMLTOPDF_BINARIES/* +echo -e "${GREEN}-----> Downloading wkhtmltopdf deb${WHITE}" +wget $WKHTMLTOPDF_URL -echo "-----> Moving binaries to the right place" -mv $WKHTMLTOPDF_BINARIES/* $BIN_PATH/ +echo -e "${GREEN}-----> Install wkhtmltopdf${WHITE}" +apt update +yes | apt install xfonts-75dpi xfonts-base +yes | dpkg -i ./$WKHTMLTOPDF_DEB -mkdir -p "$BUILD_DIR"/.profile.d -cat <"$BUILD_DIR"/.profile.d/wkhtmltopdf.sh -export PATH="/app/bin:\$PATH" -EOF +echo -e "${GREEN}-----> Cleaning up${WHITE}" +rm -rf $WKHTMLTOPDF_DEB -echo "-----> Cleaning up" -rm -rf $WKHTMLTOPDF_PATH - -echo "-----> Installing fonts" +echo -e "${GREEN}-----> Installing fonts${WHITE}" mkdir -p $1/.fonts ls $FONTS_DIR cp $FONTS_DIR/* $1/.fonts/ fc-cache -fv $1/.fonts + +exit 0 diff --git a/docker/Dockerfile.testrunner b/docker/Dockerfile.testrunner new file mode 100644 index 0000000..3f01041 --- /dev/null +++ b/docker/Dockerfile.testrunner @@ -0,0 +1,17 @@ +ARG ubuntu_version + +FROM heroku/heroku:$ubuntu_version + +WORKDIR /usr/local/lib + +RUN curl --silent https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/shunit2/shunit2-2.1.6.tgz | tar xz + +ENV SHUNIT_HOME /usr/local/lib/shunit2-2.1.6 + +# content in directory testrunner is copy from https://github.com/heroku/heroku-buildpack-testrunner +# it provide utils to run test of this project +ADD testrunner/bin /app/testrunner/bin +ADD testrunner/lib /app/testrunner/lib + +CMD ["-c", "/app/buildpack"] +ENTRYPOINT ["/app/testrunner/bin/run"] diff --git a/test/compile_test.sh b/test/compile_test.sh new file mode 100644 index 0000000..a8cb26e --- /dev/null +++ b/test/compile_test.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +. ${BUILDPACK_TEST_RUNNER_HOME}/lib/test_utils.sh + +testCompile() { + ${BUILDPACK_HOME}/bin/compile ${BUILD_DIR} ${BUILD_DIR} # this comand can see echo logs + + wkhtmltopdf http://google.com google.pdf + + assertEquals "google.pdf" "$(ls -a | grep google.pdf)" +}