Skip to content

Instantly share code, notes, and snippets.

@lcd047
Last active December 30, 2015 07:29
Show Gist options
  • Save lcd047/7796203 to your computer and use it in GitHub Desktop.
Save lcd047/7796203 to your computer and use it in GitHub Desktop.
Wrapper script for ghc-mod for checking .ghc files
#! /usr/bin/env bash
#
# File: ghc-mod.sh
# Description: Wrapper script allowing syntastic's ghc-mod checker to work with
# .ghc files.
# Maintainer: LCD 47 <lcd047 at gmail dot com>
# License: This program is free software. It comes without any warranty,
# to the extent permitted by applicable law. You can redistribute
# it and/or modify it under the terms of the Do What The Fuck You
# Want To Public License, Version 2, as published by Sam Hocevar.
# See http://sam.zoy.org/wtfpl/COPYING for more details.
# The following is a rough translation of a Vim by @tarruda:
# https://github.com/tarruda/syntastic/commit/ab9d72ab33d055aa3c4025b5386f29268c91ad3a
# PATH=/usr/local/bin:/bin:/usr/bin
for last; do :; done
case "$last" in
*.hsc)
includes=
for f in *.cabal *.buildinfo; do
if [ -f "$f" ]; then
includes="$includes $( awk '
tolower($1) == "include-dirs:" {
in_list = 1
$1 = ""
}
$1 ~ /:$/ {
in_list = 0
next
}
{
if (in_list) {
# XXX this breaks for quoted strings
split($0, dirs, / *, *| +/)
for (inc in dirs)
if (dirs[inc] != "")
includes = includes " -I" dirs[inc]
}
}
END {
print includes
}
' "$f" )" || exit 1
fi
done
# the .hs file has to be created in the same directory as the
# source, to allow for relative include paths
tf=$( mktemp -p "$( dirname "$last" )" "${0##*/}".$$.XXXXXXXX ) || exit 1
# ghc-mod insists on a .hs extension
hs="$tf".hs
mv -f "$tf" "$hs" || exit 1
trap 'rm -f "$hs"' 0 HUP INT QUIT TERM
hsc2hs -o "$hs" $includes "$last" || exit 1
# XXX "${@:1:$((${#}-1))}" is the list of positional arguments
# without the last one; this is a bashism, no idea how to make
# it portable
ghc-mod "${@:1:$((${#}-1))}" "$hs"
exit $?
;;
*)
exec ghc-mod "$@"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment