-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcabal2pkgbuild.sh
executable file
·263 lines (230 loc) · 7.26 KB
/
cabal2pkgbuild.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env zsh
# Usage: ./cabal2pkgbuild.sh <HACKAGE_PACKAGES_FILE> <MODE>
usage="Usage: ./cabal2pkgbuild.sh <HACKAGE_PACKAGES_FILE> <MODE>"
# Exit immediately if any errors are found
setopt errexit
# Avoid "no matches found" error if a file does not exist; see
# http://www.zsh.org/mla/users/2008/msg01139.html
setopt local_options no_nomatch
pacman_to_cblrepo () {
# When invoking `pacman -Q PKGNAME`, PKGNAME must be lowercase.
pacman_friendly=(${@:l})
pvs=$(pacman -Q ${pacman_friendly/#/haskell-})
versions=()
typeset -A pkgs1
pkgs1=()
pkgs2=()
pkgs3=()
for pv in ${(f)pvs}; do
v=$(echo $pv | sed -e 's/^.\+ //' -e 's/-/,/')
versions+=($v)
done
# Use an associative array for saner substitutions.
for (( i = 1; i <= $#@; i++ )) do
pkgs1+=($@[i] $versions[i])
done
for k in ${(@k)pkgs1}; do
package="${k#haskell-},${pkgs1[$k]/[_-]/,}"
pkgs2+=($package)
done
# We have to sort $pkgs2, because the array ordering is lost when we use
# Zsh's associative array with `typeset -A pkgs1`.
pkgs3=(${(o)pkgs2})
for pkg in $pkgs3; do
echo " " $pkg
done
echo "cblrepo add" ${pkgs3/#/--distro-pkg }
eval "cblrepo add" ${pkgs3/#/--distro-pkg }
}
if [[ -z $1 ]]; then
echo $usage
exit 1
fi
if [[ ! -f $1 ]]; then
echo "\`$1' does not exist or is not a regular file"
exit 1
fi
if [[ -z $2 ]]; then
echo $usage
exit 1
fi
ghc_pkgs=()
ghc_pkgs_lower_name_only=()
hackage_pkgs=()
hackage_url="http://hackage.haskell.org"
hackage_packages_file=($(<$1))
hackage_lowered=($hackage_packages_file:l)
mode=$2
case $mode in
### Remove any old cblrepo.db file. ###
(initdb|initdb-sync)
rm -fv cblrepo.db
# Add `ghc` itself. Always use 0 as xrev for it.
ghcversion=$(pacman -Q ghc | cut -d " " -f2 | sed 's/-/,0,/')
command="cblrepo add --distro-pkg ghc,$ghcversion"
# Tell user what we are going to do.
echo $command
# Actually execute the command.
eval $command
# Add packages provided by GHC
# Pacman provides information about which modules are exposed by installing the
# 'ghc' package. We put each package into an array.
provided=($(pacman -Qi ghc | grep Provides | cut -d ":" -f2))
echo "GHC packages to be added:"
for p in $provided; do
# Change the syntax to be compatible with cblrepo. The `cut` command here
# removes the 'haskell-' prefix for each package, and `sed` here replaces
# each '=' sign with a ',', as per cblrepo's requirements.
package=$(echo $p | cut -c9- | sed 's/=.\+//')
version=$(echo $p | cut -c9- | sed 's/^.\+=//')
ghc_pkgs+=($package,$version)
ghc_pkgs_lower_name_only+=($package)
echo " " $package,$version
done
echo "cblrepo add" ${ghc_pkgs/#/--ghc-pkg }
eval "cblrepo add" ${ghc_pkgs/#/--ghc-pkg }
echo
# Add packages installed by the user from [haskell-core] or some other Arch
# Linux repository.
pacman_hackage=($(pacman -Qq | grep "^haskell-" | sed 's/^haskell-//'))
# Filter out those packages that were installed from Hackage using this very
# same script (in Arch Linux, the hackage packages, once installed, are in
# the format `haskell-<lowercased_package_name>'). This way, we avoid
# duplicate definitions and the packages added with --distro-pkg will really
# be those packages *only* available from the distribution's official
# haskell repository.
distro=(${pacman_hackage:|hackage_lowered})
hackage_only=(${pacman_hackage:|distro})
echo hackage_only: $hackage_only
echo
echo distro: $distro
echo
# Some packages have upper case letters in their names, but pacman only uses
# lowercase. To make cblrepo understand that, e.g., `QuickCheck` is the
# same as `quickcheck`, we add the uppercase version, NOT the lowercase version.
ghcpkgs_upper=(
$(ghc-pkg list --names-only --simple-output\
| tr ' ' '\n'\
| grep '[A-Z]')
)
ghcpkgs_upper_lowered=(${ghcpkgs_upper:l})
echo ghcpkgs_upper_lowered: $ghcpkgs_upper_lowered
echo
# All pacman-recognized packages (haskell-*), but without the uppercase ones
# from `ghc-pkg list`.
distro_no_upper=(${distro:|ghcpkgs_upper_lowered})
# All pacman-recognized packages (haskell-*), which also happen to be uppercase.
distro_upper_only=(${distro:|distro_no_upper})
echo distro_no_upper: $distro_no_upper
echo
echo distro_upper_only: $distro_upper_only
echo
# Same as distro_upper_only, but with the actual uppercase names.
distro_upper_actual=()
for p in $ghcpkgs_upper; do
p_lower=${p:l}
if [[ -z "${distro[(r)$p_lower]}" ]]; then
continue
fi
distro_upper_actual+=($p)
done
echo distro_upper_actual: $distro_upper_actual
echo
echo "Distribution (originally lowercase) packages to be added (1/2):"
[[ -n $distro_no_upper ]] && pacman_to_cblrepo $distro_no_upper
echo "Distribution (originally mixed case) packages to be added (2/2):"
[[ -n $distro_upper_actual ]] && pacman_to_cblrepo $distro_upper_actual
# We sync cblrepo with Hackage if the user requested it. This is an
# important step because we rely on cblrepo's knowledge of Hackage to
# download the latest packages. If cblrepo's cache is out of date, then this
# script's "latest" Hackage packages will also be out of date.
if [[ $mode == initdb-sync ]]; then
# Sync cblrepo with Hackage
echo -n "Syncing cblrepo with Hackage..."
cblrepo update
echo "done"
fi
# Add packages from Hackage
echo "Adding packages from \`$1'"
mkdir -p cache
typeset -A cabal_latest
cabal_latest=()
cabal_unknown=()
# For packages without explicit version - find latest version
for name in $hackage_packages_file; do
[[ ${name} == *,* ]] && continue
cabal_unknown+=("${name}")
done
for cabal_pkg in $(cblrepo versions -l "${cabal_unknown[@]}"); do
name=${cabal_pkg%%,*}
cabal_latest+=("$name" "$cabal_pkg")
done
# Prepare list of all packages with versions
cabal_pkgs=()
for name in $hackage_packages_file; do
if [[ ${name} == *,* ]]; then
cabal_pkgs+=("${name}")
else
cabal_pkgs+=("${cabal_latest[${name}]}")
fi
done
# Extract cabal files
cabal_files=()
pushd cache
for (( i = 1; i <= $#cabal_pkgs; i++ )) do
name=${cabal_pkgs[i]%%,*}
name_version=${cabal_pkgs[i]/,/-}
cblrepo extract "${cabal_pkgs[i]}"
mv "${name}.cabal" "${name_version}.cabal"
cabal_file="cache/$name_version.cabal"
cabal_files+=($cabal_file)
done
popd
echo "cblrepo add --patchdir patch" ${cabal_files/#/-f }
eval "cblrepo add --patchdir patch" ${cabal_files/#/-f }
# Link the generated cblrepo.db file into ~/.cblrepo
ln -sf $PWD/cblrepo.db ~/.cblrepo/cblrepo.db
;;
### Generate PKGBUILD files for Hackage packages ###
(pkgbuild)
# Remove any old packages.
echo "Deleting old PKGBUILD directories..."
rm -rfv haskell-*
echo "cblrepo pkgbuild --patchdir patch $hackage_packages_file"
eval "cblrepo pkgbuild --patchdir patch $hackage_packages_file"
;;
### Create Arch Linux packages for the Hackage packages ###
(makepkg)
for pkg in ${hackage_lowered}; do
hpkg=haskell-$pkg
install_pkg=0
while true; do
read "reply?Make and install package \`$pkg'? (y/n): "
case $reply in
[Yy])
install_pkg=1
break
;;
[Nn]) break ;;
*) printf '%s\n' 'Please answer y or n.' ;;
esac
done
if (( $install_pkg )); then
cd $hpkg
echo $hpkg
makepkg -sif
cd ..
echo
echo " Finished making/installing package \`$hpkg'"
echo
fi
done
;;
*)
help_msg=(
"Unrecognized <MODE>; valid ones are:"
"initdb initdb-sync pkgbuild makepkg"
)
echo $help_msg
;;
esac