Skip to content

Commit df5fe48

Browse files
committed
Upgrade cabal-extras
1 parent 124db4e commit df5fe48

File tree

1 file changed

+92
-26
lines changed

1 file changed

+92
-26
lines changed

cabal

+92-26
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ usage () {
88
Commands:
99
build Build this project, including all executables and test suites.
1010
test Test this project, by default this runs all test suites.
11+
testci Test this project, but process control characters (\b, \r) which
12+
reposition the cursor, prior to emitting each line of output.
1113
repl Start the repl, by default on the the main library source.
1214
quick Start the repl directly skipping cabal, this is useful developing
1315
across multiple source trees at once.
@@ -62,35 +64,47 @@ initialize () {
6264

6365
PROJECT_ROOT=$(git rev-parse --show-toplevel)
6466

65-
# init any submodules that we need. Optional $project.submodules files.
66-
MODULE_LIST=$(cd "$PROJECT_ROOT" && git submodule | grep "^-" | cut -d ' ' -f 2)
67-
[ ! -f "$project.submodules" ] || {
68-
MODULE_LIST=$(cat $project.submodules)
67+
# init any submodules that we need. Don't worry about explicit submodules
68+
# file here, we just want to trust git to tell us things that haven't been
69+
# initialized, we really _don't_ want to run this just because a module is
70+
# dirty from development changes etc...
71+
72+
UNINITIALIZED=$(cd "$PROJECT_ROOT" && git submodule | grep "^-" | cut -d ' ' -f 2)
73+
for submodule in $UNINITIALIZED; do
74+
(cd "$PROJECT_ROOT" && git submodule update --init $submodule) || exit $?
75+
done
76+
77+
# if there is an explicit submodules file, then use it (hysterical
78+
# raisins, compatability or something...), otherwise auto-magicically
79+
# discover the sources from the lib directory.
80+
81+
if [ -f "$project.submodules" ]; then
82+
CABAL_SOURCES=$(cat $project.submodules)
6983
echo "Using $project.submodules, however this is now optional, You can remove this file if you like and it will be calculated for you."
70-
}
71-
for submodule in $MODULE_LIST
72-
do
73-
if [ -d "$submodule" ]; then
74-
find "$submodule" -maxdepth 2 -name \*.cabal
75-
fi
76-
done | grep -q . || (
77-
cd $PROJECT_ROOT
78-
git submodule update --init
79-
)
80-
81-
# discover and add cabal sources
82-
CABAL_SOURCES=$(cd "$PROJECT_ROOT" && find lib -maxdepth 4 ! -path "lib/*/bin/*" -name \*.cabal | xargs -L 1 dirname)
83-
[ ! -f "$project.submodules" ] || {
84-
CABAL_SOURCES=$MODULE_LIST
85-
}
86-
for submodule in $CABAL_SOURCES;
87-
do
84+
else
85+
CABAL_SOURCES=$(cd "$PROJECT_ROOT" && find lib -maxdepth 4 ! -path "lib/*/bin/*" ! -path "lib/*/lib/*" -name \*.cabal | xargs -L 1 dirname)
86+
fi
87+
88+
# Remove any cabal sources that are no longer available
89+
90+
for source in $(cabal sandbox list-sources | tail -n +4 | sed '$d' | sed '$d'); do
91+
source_path=$(echo $source | sed s#$(pwd)/##)
92+
if ! $(echo "${CABAL_SOURCES}" | grep -q "${source_path}"); then
93+
echo "${source_path} no longer exists - removing from the sandbox"
94+
cabal sandbox delete-source "$source" 1> /dev/null
95+
fi
96+
done
97+
98+
# We add-sources only if they haven't been added before. We should also
99+
# remove them if they aren't needed, but @charleso has promised to do this
100+
# so for now we live in anticipation.
101+
102+
for submodule in $CABAL_SOURCES; do
88103
if ! grep -q "${submodule}\"" ${SANDBOX_DIR}/add-source-timestamps; then
89-
cabal sandbox add-source ${SANDBOX:-} "$submodule"
104+
cabal sandbox add-source ${SANDBOX:-} "$PROJECT_ROOT/$submodule"
90105
fi
91106
done
92107
93-
94108
# all dependencies installed, and configure been run?
95109
cabal install -j --only-dependencies --force-reinstalls --enable-tests
96110
cabal configure --enable-tests
@@ -112,6 +126,56 @@ run_test () {
112126
cabal test --show-details=streaming "$@"
113127
}
114128
129+
#
130+
# Cabal test for CI build bots.
131+
#
132+
run_testci () {
133+
# Create a temporary directory which will be deleted when the script
134+
# terminates. Unfortunately `mktemp` doesn't behave the same on
135+
# Linux and OS/X so we need to try two different approaches.
136+
CABAL_EXTRAS_TEMP=$(mktemp -d 2>/dev/null || mktemp -d -t 'cabal-extras')
137+
trap "rm -rf $CABAL_EXTRAS_TEMP" EXIT
138+
139+
# Write out a haskell program which performs the control character
140+
# processing.
141+
UNCTL=$CABAL_EXTRAS_TEMP/unctl.hs
142+
cat << EOF > $UNCTL
143+
import System.IO
144+
145+
main :: IO ()
146+
main = do
147+
hSetBuffering stdin LineBuffering
148+
hSetBuffering stdout LineBuffering
149+
interact (go [])
150+
where
151+
go :: [Char] -- ^ current line
152+
-> [Char] -- ^ stdin
153+
-> [Char] -- ^ stdout
154+
155+
-- backspace - delete previous character
156+
go (_ : line) ('\b' : xs) = go line xs
157+
go [] ('\b' : xs) = go [] xs
158+
159+
-- carriage return - delete the whole line
160+
go _ ('\r' : xs) = go [] xs
161+
162+
-- line feed - emit the current line
163+
go line ('\n' : xs) = reverse ('\n' : line) ++ go [] xs
164+
165+
-- normal character - add to current line
166+
go line (x : xs) = go (x : line) xs
167+
168+
-- end of stream - emit the current line
169+
go line [] = line
170+
EOF
171+
172+
# Run the tests, but fix up their output before emitting it.
173+
PIPE=$CABAL_EXTRAS_TEMP/testci.pipe
174+
mkfifo $PIPE
175+
runghc $UNCTL < $PIPE &
176+
run_test "$@" > $PIPE
177+
}
178+
115179
#
116180
# Cabal repl.
117181
#
@@ -148,6 +212,7 @@ run_quick () {
148212
[ -f "$1" ] || fail "The entry point does not exist."
149213
[ ! -d src ] || SRC_DIRS="-isrc"
150214
[ ! -d test ] || SRC_DIRS="${SRC_DIRS:-} -itest"
215+
[ ! -d gen ] || SRC_DIRS="${SRC_DIRS:-} -igen"
151216
[ ! -d dist/build/autogen ] || SRC_DIRS="${SRC_DIRS:-} -idist/build/autogen"
152217
ghci -package-db=$(find .cabal-sandbox -name '*-packages.conf.d') ${SRC_DIRS:-} "$1"
153218
}
@@ -162,6 +227,7 @@ run_watch () {
162227
[ -f "$1" ] || fail "The entry point does not exist."
163228
[ ! -d src ] || SRC_DIRS="-isrc"
164229
[ ! -d test ] || SRC_DIRS="${SRC_DIRS:-} -itest"
230+
[ ! -d gen ] || SRC_DIRS="${SRC_DIRS:-} -igen"
165231
[ ! -d dist/build/autogen ] || SRC_DIRS="${SRC_DIRS:-} -idist/build/autogen"
166232
ENTRY=$1
167233
shift
@@ -330,7 +396,7 @@ esac
330396
331397
MODE="$1"; shift
332398
case "$MODE" in
333-
build|test|repl|quick|tags|lint|update|clean|depend|watch|upgrade) run_$MODE "$@" ;;
399+
build|test|testci|repl|quick|tags|lint|update|clean|depend|watch|upgrade) run_$MODE "$@" ;;
334400
*) fail "Unknown mode: $MODE"
335401
esac
336-
# Version: 4b1e8dd46040f08f813dec9e792a8d9db44a03b1
402+
# Version: fb83ed7a2f70e6b1b0547118c1dc515021ff024d

0 commit comments

Comments
 (0)