git.iko.soy/heroku-buildpack-stack
on master help sign in
LICENSEnew file+8
since splitsnapshots
unifiedsplit
@@ -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+58−5
@@ -1,7 +1,60 @@
1−# Heroku Buildpack for Haskell's Stack
1+# [Heroku Buildpack for Stack][1]
2
2
3−Mostly stolen taken from [heroku-buildpack-ghc](https://github.com/begriffs/heroku-buildpack-ghc).
3+[Heroku buildpack][2] for [Stack][3]. Based on the excellent [heroku-buildpack-ghc][4]
4
4
5−```bash
5+### Usage
6+
7+Create an app with this buildpack:
8+
6−$ heroku create --buildpack https://github.com/mfine/heroku-buildpack-stack.git
9+ $ heroku create --buildpack https://github.com/mfine/heroku-buildpack-stack.git
7−```
10+
11+Set this buildpack on an existing app:
12+
13+ $ heroku buildpacks:set https://github.com/mfine/heroku-buildpack-stack
14+
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+
57+[1]: https://github.com/mfine/heroku-buildpack-stack
58+[2]: http://devcenter.heroku.com/articles/buildpacks
59+[3]: https://github.com/commercialhaskell/stack
60+[4]: https://github.com/begriffs/heroku-buildpack-ghc
bin/compile+84−64
@@ -9,94 +9,114 @@ BUILD_DIR="$1"
9CACHE_DIR="$2"
9CACHE_DIR="$2"
10ENV_DIR="$3"
10ENV_DIR="$3"
11
11
12−# Default configuration which you can change by setting environment variables
13−PREBUILT=${PREBUILT:-https://s3.amazonaws.com/heroku-ghc}
14−
15−function export_env_dir() {
12+function speak (){
16− blacklist_regex="^(PATH|GIT_DIR|CPATH|CPPATH|LD_PRELOAD|LIBRARY_PATH)$"
13+ echo "-----> $(date -u '+%Y-%m-%d_%H:%M:%S') $1"
17− if [ -d "$ENV_DIR" ]; then
18− echo "-----> Exporting config vars"
19− for e in $(ls "$ENV_DIR"); do
20− echo "$e" | grep -qvE "$blacklist_regex" &&
21− export "$e=$(cat $ENV_DIR/$e)"
22− :
23− done
24− fi
25}
14}
26
15
27−export_env_dir
16+speak "Starting..."
28
17
29−WORK_DIR=/app
18+BLACKLIST_REGEX="^(PATH|GIT_DIR|CPATH|CPPATH|LD_PRELOAD|LIBRARY_PATH)$"
30−PROFILE_PATH="$BUILD_DIR/.profile.d/haskell.sh"
19+if [ -d "$ENV_DIR" ]; then
20+ speak "Exporting config vars"
21+ for e in $(ls "$ENV_DIR"); do
22+ echo "$e" | grep -qvE "$BLACKLIST_REGEX" &&
23+ export "$e=$(cat $ENV_DIR/$e)"
24+ :
25+ done
26+fi
31
27
28+WORK_DIR=/app
29+cp -Rp "$BUILD_DIR/." "$WORK_DIR"
32−mkdir -p $WORK_DIR/vendor
30+mkdir -p "$WORK_DIR/vendor/bin"
33−mkdir -p $(dirname $PROFILE_PATH)
31+mkdir -p "$WORK_DIR/vendor/ghc-libs"
34
32
35−########## gmp #####################################################
33+PROFILE_DIR="$BUILD_DIR/.profile.d"
36−mkdir -p $WORK_DIR/vendor/ghc-libs
34+PROFILE_PATH="$PROFILE_DIR/stack.sh"
37−if [ ! -e $CACHE_DIR/libgmp-6.0.0a ]; then
35+mkdir -p "$PROFILE_DIR"
38− PREBUILT_LIB_URL=$PREBUILT/heroku-libgmp-6.0.0a.tar.gz # contains ghc-libs directories
39− echo "-----> Installing prebuilt libgmp-6.0.0a into cache"
40− mkdir -p $CACHE_DIR/libgmp-6.0.0a
41− curl -# -L "$PREBUILT_LIB_URL" | tar xz -C $CACHE_DIR/libgmp-6.0.0a
42−fi
43−echo "-----> Restoring libgmp-6.0.0a files from cache"
44−find $CACHE_DIR/libgmp-6.0.0a/ghc-libs/ -exec cp -rP '{}' $WORK_DIR/vendor/ghc-libs/ \;
45
36
46# Usage: $ set-env key value
37# Usage: $ set-env key value
47function set-env (){
38function set-env (){
48− echo "export $1=$2" >> $PROFILE_PATH
39+ echo "export $1=$2" >> "$PROFILE_PATH"
49}
40}
50
41
51# Usage: $ set-default-env key value
42# Usage: $ set-default-env key value
52function set-default-env (){
43function set-default-env (){
53− echo "export $1=\${$1:-$2}" >> $PROFILE_PATH
44+ echo "export $1=\${$1:-$2}" >> "$PROFILE_PATH"
54}
45}
55
46
56export LANG=en_US.UTF-8
47export LANG=en_US.UTF-8
57−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
58export LD_LIBRARY_PATH=$LIBRARY_PATH
49export LD_LIBRARY_PATH=$LIBRARY_PATH
59export PATH=$WORK_DIR/.local/bin:$WORK_DIR/vendor/bin:$PATH
50export PATH=$WORK_DIR/.local/bin:$WORK_DIR/vendor/bin:$PATH
51+export STACK_ROOT=$CACHE_DIR/.stack
60
52
61−########## stack ###################################################
53+########## gmp #####################################################
62−mkdir -p $WORK_DIR/vendor/bin
54+LIBGMP_VER=${LIBGMP_VER:-6.0.0a}
63−if [ ! -e $CACHE_DIR/stack ]; then
55+LIBGMP_URL=${LIBGMP_URL:-https://s3.amazonaws.com/heroku-ghc/heroku-libgmp-"$LIBGMP_VER".tar.gz}
64− echo "-----> Downloading Stack"
56+LIBGMP_DIR="$CACHE_DIR/libgmp-$LIBGMP_VER"
65− curl -# -L https://github.com/commercialhaskell/stack/releases/download/v0.1.3.1/stack-0.1.3.1-x86_64-linux.gz | gunzip > $CACHE_DIR/stack
57+if [ ! -e "$LIBGMP_DIR" ]; then
66− cp $CACHE_DIR/stack $WORK_DIR/vendor/bin/stack
58+ speak "Installing libgmp-$LIBGMP_VER into cache"
67− chmod a+x $WORK_DIR/vendor/bin/stack
59+ mkdir -p "$LIBGMP_DIR"
68−else
60+ curl -# -L "$LIBGMP_URL" | tar xz -C "$LIBGMP_DIR"
69− echo "-----> Restoring Stack"
70− rm -f $WORK_DIR/vendor/bin/stack
71− cp $CACHE_DIR/stack $WORK_DIR/vendor/bin/stack
72− chmod a+x $WORK_DIR/vendor/bin/stack
73fi
61fi
74−
62+speak "Restoring $LIBGMP_VER files from cache"
75−if [ -e $CACHE_DIR/.stack ]; then
63+cp -Rp "$LIBGMP_DIR/ghc-libs/." "$WORK_DIR/vendor/ghc-libs"
64+
65+########## stack exe ###############################################
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"
70+if [ ! -e "$STACK_EXE" ]; then
71+ speak "Downloading stack-$STACK_VER"
72+ mkdir -p "$STACK_DIR"
73+ curl -# -L "$STACK_URL" | tar xz -C "$STACK_DIR" --strip-components=1
74+fi
76− echo "-----> Restoring .stack"
75+speak "Restoring stack-$STACK_VER"
77− rm -fr $WORK_DIR/.stack
76+cp "$STACK_EXE" "$WORK_DIR/vendor/bin/stack"
78− cp -R $CACHE_DIR/.stack $WORK_DIR
77+chmod a+x "$WORK_DIR/vendor/bin/stack"
78+
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"
79fi
83fi
80
84
81########## project build ###########################################
85########## project build ###########################################
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
82
93
83−echo "-----> Stack"
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)
84−cd $BUILD_DIR
97+if [ -f "$BUILD_DIR/Makefile" ]; then
98+ speak "Makefile detected"
99+ make
100+ make install
101+else
85−stack setup
102+ stack setup
103+ stack clean
86−stack build --copy-bins
104+ stack build --ghc-options='-O2' --copy-bins
87−
105+fi
88−echo "-----> Caching .stack"
89−rm -fr $CACHE_DIR/.stack
90−cp -R $WORK_DIR/.stack $CACHE_DIR
91
106
92# Set context environment variables.
107# Set context environment variables.
93−set-env PATH "/app/.local/bin:$PATH"
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"
111+
112+speak "Making stack binaries available"
113+mkdir -p "$BUILD_DIR/vendor"
114+mkdir -p "$BUILD_DIR/.local"
115+mv "$WORK_DIR/vendor/ghc-libs" "$BUILD_DIR/vendor"
116+mv "$WORK_DIR/.local/bin" "$BUILD_DIR/.local"
94
117
95−set-default-env LIBRARY_PATH /app/vendor/ghc-libs:/usr/lib
118+speak "Caching .stack-work"
96−set-default-env LD_LIBRARY_PATH /app/vendor/ghc-libs:/usr/lib
119+rsync -avq "$WORK_DIR/.stack-work/" "$SANDBOX_DIR"
120+cp -Rp "$WORK_DIR/.stack-work" "$BUILD_DIR"
97
121
98−echo "-----> Making GHC binaries available to Heroku command line"
122+speak "Finished!"
99−mkdir -p $BUILD_DIR/vendor
100−mkdir -p $BUILD_DIR/.local
101−mv $WORK_DIR/vendor/ghc-libs $BUILD_DIR/vendor
102−mv $WORK_DIR/.local/bin $BUILD_DIR/.local
bin/detect+1−1
@@ -1,6 +1,6 @@
1#!/usr/bin/env bash
1#!/usr/bin/env bash
2
2
3−if [ -f "$1"/stack.yaml ]; then
3+if [ -n "$(find "$1" -name stack.yaml -maxdepth 1 -print -quit)" ]; then
4 echo "Haskell"
4 echo "Haskell"
5 exit 0
5 exit 0
6else
6else