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

Commit 92ac896

Browse files
authored
Merge pull request #1561 from hasufell/PR/cabal-freeze-files
* Add `index-state` to `cabal.project` to help make builds reproducible * sh script to regenerate it
2 parents 059f354 + f2881f2 commit 92ac896

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

cabal.project

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ constraints:
1515
hie-plugin-api +pedantic
1616

1717
write-ghc-environment-files: never
18+
19+
index-state: 2020-01-07T22:09:46Z

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)