Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 06569b7

Browse files
committed
Add update-index-state.sh script
This is kinda heavy, but it should work well on 32bit and 64bit arches. The alternative was to copy-paste a lot of cabal-install code that is not exposed. There seems to be no value in that for a development script. The alternative to this script is simply running 'cabal v2-update' twice and then copy-pasting the output manually to cabal.project.
1 parent b202c8b commit 06569b7

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

update-index-state.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu
4+
5+
status_message() {
6+
printf "\\033[0;32m%s\\033[0m\\n" "$1"
7+
}
8+
9+
error_message() {
10+
printf "\\033[0;31m%s\\033[0m\\n" "$1"
11+
}
12+
13+
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
14+
CACHE_LOCATION="${HOME}/.cabal/packages/hackage.haskell.org/01-index.cache"
15+
16+
if [ ! -f "${CACHE_LOCATION}" ] ; then
17+
error_message "${CACHE_LOCATION} does not exist, did you run 'cabal update'?"
18+
exit 1
19+
fi
20+
21+
if [ ! -f "${SCRIPTPATH}/cabal.project" ] ; then
22+
error_message "Could not find ${SCRIPTPATH}/cabal.project, skipping index state update."
23+
exit 3
24+
fi
25+
26+
cabal v2-update
27+
28+
arch=$(getconf LONG_BIT)
29+
30+
case "${arch}" in
31+
32)
32+
byte_size=4
33+
magic_word="CABA1002"
34+
;;
35+
64)
36+
byte_size=8
37+
magic_word="00000000CABA1002"
38+
;;
39+
*)
40+
error_message "Unknown architecture (long bit): ${arch}"
41+
exit 2
42+
;;
43+
esac
44+
45+
# This is the logic to parse the binary format of 01-index.cache.
46+
# The first word is a magic 'caba1002', the second one is the timestamp in unix epoch.
47+
# Better than copying the cabal-install source code.
48+
if [ "$(xxd -u -p -l${byte_size} -s 0 "${CACHE_LOCATION}")" != "${magic_word}" ] ; then
49+
error_message "Magic word does not match!"
50+
exit 4
51+
fi
52+
cache_timestamp=$(echo "ibase=16;obase=A;$(xxd -u -p -l${byte_size} -s ${byte_size} "${CACHE_LOCATION}")" | bc)
53+
54+
# If we got junk from the binary file, this should fail.
55+
cache_date=$(date --utc --date "@${cache_timestamp}" "+%FT%TZ")
56+
57+
58+
status_message "Updating index state in ${SCRIPTPATH}/cabal.project"
59+
60+
if grep -q "^index-state: .*" "${SCRIPTPATH}/cabal.project" ; then
61+
awk '/index-state:/ {gsub(/.*/, "index-state: '${cache_date}'")}; { print }' "${SCRIPTPATH}/cabal.project" > "${SCRIPTPATH}/cabal.project.tmp"
62+
mv "${SCRIPTPATH}/cabal.project.tmp" "${SCRIPTPATH}/cabal.project"
63+
else
64+
printf "index-state: %s\n" "${cache_date}" >> "${SCRIPTPATH}/cabal.project"
65+
fi
66+

0 commit comments

Comments
 (0)