git.iko.soy/heroku-buildpack-stack
on master help sign in
5 files changed+98−27
since splitsnapshots
unifiedsplit
LICENSE+8 Procfile.example+1 README.md+44−2 bin/compile+44−24 bin/detect+1−1
LICENSEnew file+8
@@ -0,0 +1,8 @@
1+The MIT License (MIT)
2+Copyright (c) 2016 Mark Fine
3+
4+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5+
6+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
8+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Procfile.examplenew file+1
@@ -0,0 +1 @@
1+web: YOURAPPNAME-exe # $ cat *.cabal | grep executable
README.md+44−2
@@ -2,16 +2,58 @@
22
33[Heroku buildpack][2] for [Stack][3]. Based on the excellent [heroku-buildpack-ghc][4]
44
5−### Useage
5+### Usage
66
77Create an app with this buildpack:
88
99 $ heroku create --buildpack https://github.com/mfine/heroku-buildpack-stack.git
1010
11−Set this buildpack on an exiting app:
11+Set this buildpack on an existing app:
1212
1313 $ heroku buildpacks:set https://github.com/mfine/heroku-buildpack-stack
1414
15+Binary executables are placed in `/app/.local/bin/` after compilation, a directory which Heroku includes in your `$PATH`, so assuming that your application is [binding to `$PORT`](https://devcenter.heroku.com/articles/dynos#web-dynos), you can serve your app by [creating a `Procfile`](https://devcenter.heroku.com/articles/procfile) at your project root that defines a `web` process type for the `executable` defined in your `.cabal` file:
16+
17+ $ cat *.cabal | grep executable
18+ executable YOURAPPNAME-exe
19+
20+ $ echo "web: YOURAPPNAME-exe" >> Procfile
21+
22+
23+### Templating stack.yaml
24+
25+To avoid committing secrets into `stack.yaml` for access to private
26+repos, an app's config vars values can be substituted for tags
27+enclosed in double brackets. For example, given a `stack.yaml` containing:
28+
29+ packages:
30+ -location:
31+ git: https://mfine:{{GITPASS}}@github.com/mfine/heroku-buildpack-stack.git
32+
33+and an application with config vars:
34+
35+ $ heroku config -app calm-storm-51595
36+ === murmuring-beyond-51595 Config Vars
37+ GITPASS: abc123
38+ $
39+
40+before compilation, the `stack.yaml` will be substituted as follows:
41+
42+ packages:
43+ -location:
44+ git: https://mfine:abc123@github.com/mfine/heroku-buildpack-stack.git
45+
46+
47+### Makefile Support
48+
49+This buildpack now supports an optional Makefile, in case you need to coordinate
50+other build steps with Stack. It assumes that the Makefile includes a `make
51+install` target that uses the Stack build command with the flag `--copy-bins`.
52+For example, a possible `install` target configuration in the Makefile could be:
53+
54+ install:
55+ stack build --copy-bins
56+
1557[1]: https://github.com/mfine/heroku-buildpack-stack
1658[2]: http://devcenter.heroku.com/articles/buildpacks
1759[3]: https://github.com/commercialhaskell/stack
bin/compile+44−24
@@ -13,6 +13,8 @@ function speak (){
1313 echo "-----> $(date -u '+%Y-%m-%d_%H:%M:%S') $1"
1414}
1515
16+speak "Starting..."
17+
1618BLACKLIST_REGEX="^(PATH|GIT_DIR|CPATH|CPPATH|LD_PRELOAD|LIBRARY_PATH)$"
1719if [ -d "$ENV_DIR" ]; then
1820 speak "Exporting config vars"
@@ -24,7 +26,7 @@ if [ -d "$ENV_DIR" ]; then
2426fi
2527
2628WORK_DIR=/app
27−mkdir -p "$WORK_DIR/vendor"
29+cp -Rp "$BUILD_DIR/." "$WORK_DIR"
2830mkdir -p "$WORK_DIR/vendor/bin"
2931mkdir -p "$WORK_DIR/vendor/ghc-libs"
3032
@@ -43,9 +45,10 @@ function set-default-env (){
4345}
4446
4547export LANG=en_US.UTF-8
46−export LIBRARY_PATH=$WORK_DIR/vendor/ghc-libs:/usr/lib:$LIBRARY_PATH
48+export LIBRARY_PATH=$WORK_DIR/vendor/ghc-libs:$WORK_DIR/vendor/lib:/usr/lib:$LIBRARY_PATH
4749export LD_LIBRARY_PATH=$LIBRARY_PATH
4850export PATH=$WORK_DIR/.local/bin:$WORK_DIR/vendor/bin:$PATH
51+export STACK_ROOT=$CACHE_DIR/.stack
4952
5053########## gmp #####################################################
5154LIBGMP_VER=${LIBGMP_VER:-6.0.0a}
@@ -57,46 +60,63 @@ if [ ! -e "$LIBGMP_DIR" ]; then
5760 curl -# -L "$LIBGMP_URL" | tar xz -C "$LIBGMP_DIR"
5861fi
5962speak "Restoring $LIBGMP_VER files from cache"
60−#find "$LIBGMP_DIR/ghc-libs/" -exec cp -rP '{}' "$WORK_DIR/vendor/ghc-libs/" \;
61−cp -rP "$LIBGMP_DIR/ghc-libs/*" "$WORK_DIR/vendor/ghc-libs/"
63+cp -Rp "$LIBGMP_DIR/ghc-libs/." "$WORK_DIR/vendor/ghc-libs"
6264
6365########## stack exe ###############################################
64−STACK_VER=${STACK_VER:-0.1.3.1}
65−STACK_URL=${STACK_URL:-https://github.com/commercialhaskell/stack/releases/download/v"$STACK_VER"/stack-"$STACK_VER"-x86_64-linux.gz}
66−STACK_EXE="$CACHE_DIR/stack-$STACK_VER"
66+STACK_VER=${STACK_VER:-2.1.3}
67+STACK_URL=${STACK_URL:-https://github.com/commercialhaskell/stack/releases/download/v"$STACK_VER"/stack-"$STACK_VER"-linux-x86_64.tar.gz}
68+STACK_DIR="$CACHE_DIR/stack-$STACK_VER"
69+STACK_EXE="$STACK_DIR/stack"
6770if [ ! -e "$STACK_EXE" ]; then
6871 speak "Downloading stack-$STACK_VER"
69− curl -# -L "$STACK_URL" | gunzip > "$STACK_EXE"
72+ mkdir -p "$STACK_DIR"
73+ curl -# -L "$STACK_URL" | tar xz -C "$STACK_DIR" --strip-components=1
7074fi
7175speak "Restoring stack-$STACK_VER"
72−cp -P "$STACK_EXE" "$WORK_DIR/vendor/bin/stack"
76+cp "$STACK_EXE" "$WORK_DIR/vendor/bin/stack"
7377chmod a+x "$WORK_DIR/vendor/bin/stack"
7478
75−########## stack dir ###############################################
76−STACK_DIR="$CACHE_DIR/.stack"
77−if [ -e "$STACK_DIR" ]; then
78− speak "Restoring .stack"
79− cp -rP "$STACK_DIR" "$WORK_DIR"
79+########## stack vars ##############################################
80+if [ -d "$ENV_DIR" ]; then
81+ speak "Substituting stack vars"
82+ perl -p -i -e 's/\{\{([^}]+)\}\}/defined$ENV{$1}?$ENV{$1}:$&/eg' "$BUILD_DIR/stack.yaml"
8083fi
8184
8285########## project build ###########################################
83−speak "Running stack"
84−cd "$BUILD_DIR"
85−stack setup
86−stack build --copy-bins
86+SANDBOX_DIR="$CACHE_DIR/.stack-work"
87+if [ -e "$SANDBOX_DIR" ]; then
88+ speak "Restoring .stack-work"
89+ cp -Rp "$SANDBOX_DIR" "$WORK_DIR"
90+else
91+ mkdir -p "$SANDBOX_DIR"
92+fi
8793
88−if [ ! -e "$STACK_DIR" ]; then
89− speak "Caching .stack"
90− cp -rP "$WORK_DIR/.stack" "$CACHE_DIR"
94+speak "Running stack"
95+cd "$WORK_DIR"
96+# Makefile support (if you need other build tools than Stack that you need to use in conjunction)
97+if [ -f "$BUILD_DIR/Makefile" ]; then
98+ speak "Makefile detected"
99+ make
100+ make install
101+else
102+ stack setup
103+ stack clean
104+ stack build --ghc-options='-O2' --copy-bins
91105fi
92106
93107# Set context environment variables.
94−set-env PATH "/app/.local/bin:$PATH"
95−set-default-env LIBRARY_PATH /app/vendor/ghc-libs:/usr/lib
96−set-default-env LD_LIBRARY_PATH /app/vendor/ghc-libs:/usr/lib
108+set-env PATH "$WORK_DIR/.local/bin:$WORK_DIR/vendor/bin:\$PATH"
109+set-default-env LIBRARY_PATH "$WORK_DIR/vendor/ghc-libs:$WORK_DIR/vendor/lib:/usr/lib"
110+set-default-env LD_LIBRARY_PATH "$WORK_DIR/vendor/ghc-libs:$WORK_DIR/vendor/lib:/usr/lib"
97111
98112speak "Making stack binaries available"
99113mkdir -p "$BUILD_DIR/vendor"
100114mkdir -p "$BUILD_DIR/.local"
101115mv "$WORK_DIR/vendor/ghc-libs" "$BUILD_DIR/vendor"
102116mv "$WORK_DIR/.local/bin" "$BUILD_DIR/.local"
117+
118+speak "Caching .stack-work"
119+rsync -avq "$WORK_DIR/.stack-work/" "$SANDBOX_DIR"
120+cp -Rp "$WORK_DIR/.stack-work" "$BUILD_DIR"
121+
122+speak "Finished!"
bin/detect+1−1
@@ -1,6 +1,6 @@
11#!/usr/bin/env bash
22
3−if [ -f "$1"/stack.yaml ]; then
3+if [ -n "$(find "$1" -name stack.yaml -maxdepth 1 -print -quit)" ]; then
44 echo "Haskell"
55 exit 0
66else