Skip to content

Instantly share code, notes, and snippets.

@roman
Created August 31, 2013 01:45
Show Gist options
  • Save roman/6395741 to your computer and use it in GitHub Desktop.
Save roman/6395741 to your computer and use it in GitHub Desktop.
A way to build cabal files with multiple executables that share the same exact build-depends, without repeating packages over and over again!
#!/bin/bash
cabal_template="$1"
build_depends="$2"
test_build_depends="$3"
IFS=
handle_comma_ending () {
local line
local replacement_entry
local whitespace
local depends_file
local file_init
local file_last
line="$1"
replacement_entry="$2"
depends_file="$3"
replacement_regex="s/${replacement_entry}//g"
whitespace=$(echo "$line" | sed -e $replacement_regex)
file_init="$(head --lines=-1 $depends_file)"
file_last="$(tail --lines=-1 $depends_file)"
echo "$file_init" | sed -e "s/\(.*\)/`echo ${whitespace}`\1/g"
echo "$file_last" | sed -e "s/\(.*\)/`echo ${whitespace}`\1,/g"
}
main () {
while read line; do
if [[ -f $build_depends ]] && [[ "$line" =~ '__DEPENDENCIES__,' ]]; then
handle_comma_ending $line "__DEPENDENCIES__," $build_depends
elif [[ -f $test_build_depends ]] && [[ "$line" =~ '__TEST_DEPENDENCIES__,' ]]; then
handle_comma_ending $line "__TEST_DEPENDENCIES__," $test_build_depends
elif [[ -f $build_depends ]] && [[ "$line" =~ '__DEPENDENCIES__' ]]; then
whitespace=$(echo "$line" | sed -e 's/__DEPENDENCIES__//g')
cat $build_depends | sed -e "s/\(.*\)/`echo ${whitespace}`\1/g"
elif [[ -f $test_build_depends ]] && [[ "$line" =~ '__TEST_DEPENDENCIES__' ]]; then
whitespace=$(echo "$line" | sed -e 's/__TEST_DEPENDENCIES__//g')
cat $test_build_depends | sed -e "s/\(.*\)/`echo ${whitespace}`\1/g"
else
echo "$line"
fi
done < $cabal_template
}
main
base >= 4.6 && <4.7,
aeson == 0.6.1.0,
attoparsec == 0.10.4.0,
bytestring == 0.10.0.2,
blaze-builder == 0.3.1.1,
conduit == 1.0.7.3,
http-conduit == 1.9.4.2,
http-reverse-proxy == 0.2.0,
http-types == 0.8.0,
lens == 3.9.0.2,
mtl == 2.1.2,
optparse-applicative >= 0.5.2,
regex-pcre >= 0.94 && < 0.95,
regex-pcre-gsub == 0.0.1.0,
network == 2.4.1.2,
resourcet == 0.4.7.2,
wai == 1.4.0.1,
warp >= 1.3.9,
maestro-config == 0.1.0.0,
-- BROKEN DEPENDENCIES
containers == 0.5.0.0,
entropy == 0.2.1
# Assuming all cabal gen files (myproject.cabal.tmpl, build_depends and test_build_depends)
# are in gen/cabal/ folder
CABAL_FILE = myproject.cabal
CABAL_GEN_FILES = $(wildcard gen/cabal/*)
$(CABAL_FILE) : $(CABAL_GEN_FILES)
@echo "Building new cabal file"
@bin/build_cabal_file.sh \
gen/cabal/maestro-proxy.cabal.tmpl \
gen/cabal/build_depends \
gen/cabal/test_build_depends > $(CABAL_FILE)
name: my-project
version: 0.0.1.0
-- synopsis:
-- description:
license: MIT
license-file: LICENSE
author: Roman Gonzalez
maintainer: [email protected]
-- copyright:
category: Other
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
flag testloop
description: Build testloop binary
default: False
library
default-language: Haskell2010
hs-source-dirs: src
exposed-modules:
MyProject.Internal
build-depends:
__DEPENDENCIES__
executable myproject-testloop
default-language: Haskell2010
hs-source-dirs: src, test
main-is: TestLoop.hs
if flag(testloop)
build-depends:
__DEPENDENCIES__,
__TEST_DEPENDENCIES__,
testloop >= 0.1.1 && < 0.2
else
buildable: False
test-suite myproject-tests
default-language: Haskell2010
type: exitcode-stdio-1.0
hs-source-dirs: src, test
main-is: TestSuite.hs
build-depends:
__DEPENDENCIES__,
__TEST_DEPENDENCIES__
executable myproject-bin
default-language: Haskell2010
main-is: Main.hs
hs-source-dirs: src
build-depends:
__DEPENDENCIES__
hspec >= 1.7 && < 1.8,
HUnit >= 1.2 && < 1.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment