-
Notifications
You must be signed in to change notification settings - Fork 0
/
apktoys
executable file
·312 lines (260 loc) · 7.79 KB
/
apktoys
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/sh
# apktoys
# Your local aports directory.
if ! [ $APORTS_DIRECTORY ]; then
APORTS_DIRECTORY="$HOME"/aports
fi
# Internal functions
# `__at_cd` - changes the current working directory to the provided dir and
# uses a trap to cd back into the previous directory on exit
__at_cd() {
current_dir="$PWD"
cd "$*"
trap "cd $current_dir" EXIT INT TERM
}
# `__at_cd_aports` - changes the current working directory to aports and
# uses a trap to cd back into the previous directory on exit
__at_cd_aports() {
__at_cd "$APORTS_DIRECTORY"
}
# `__at_argparse` - separates arguments (all that starts with -, unless --
# specified) from regular parameters. Returns the arguments, then :, then
# the parameters, allowing for end users to split the two apart for further
# parsing.
__at_argparse() {
local further_arguments_are_parameters
further_arguments_are_parameters=0
args=''
params=''
for arg in $*; do
if [ "$arg" = '--' ]; then
# -- passed, all further arguments are parameters
further_arguments_are_parameters=1
continue
fi
if [ "$further_arguments_are_parameters" = '1' ] && \
[ "${arg::1}" = '-' ]; then
args="$args $arg"
else
params="$params $arg"
fi
done
args="$(echo $args | sed -e 's/^[[:space:]]*//')"
params="$(echo $params | sed -e 's/^[[:space:]]*//')"
if [ ! "$params" ]; then
echo "No package provided... did you mean 'pwd'?" 1>&2;
return 1
fi
echo "$args:$params"
}
# `at-repoforpkg <package>` - returns a string like `repository/pkgname`.
# usually used internally for the other commands, but may be useful in other
# cases.
at_repoforpkg() {
case $1 in
*'/'*) echo "$1"; return 0;; # already in repository/pkgname format
'') return 1;; # no pkg provided
'pwd') pkgname=$(basename $PWD);;
*) pkgname=$1;;
esac
directory="$(find $APORTS_DIRECTORY -maxdepth 2 -name $pkgname -type d | head -n1)"
if [ ! "$directory" ]; then
echo "Package $pkgname not found" 1>&2
exit 1
fi
clean_name="${directory/$APORTS_DIRECTORY/}"
if [ "${clean_name::1}" = '/' ]; then
echo "${clean_name/\//}"
else
echo "$clean_name"
fi
}
# `at-commit <template> <package> [data] [--amend/-m]` - runs a git commit from
# a given template. `<template>` is one of `upgrade`, `move`.
#
# `[data]` is used differently depending on the template: for `upgrade` it's
# the new version, for `move` it's the old repository.
# If `package` == pwd, assumes current directory.
at_commit() {
argparse="$(__at_argparse $*)"
amend='false'
do_reset='true'
for arg in $args; do
case $arg in
'--amend'|'-m') amend='true';;
'--no-reset'|'-n') do_reset='false';;
esac
done
template="$(echo $argparse | cut -d ' ' -f1)"
template="${template:1}" # strip leading :
raw_package="$(echo $argparse | cut -d ' ' -f2)"
data="$(echo $argparse | cut -d ' ' -f3)"
package="$(at_repoforpkg $raw_package)" || exit 1
case $template in
'new') source "$APORTS_DIRECTORY"/$package/APKBUILD; msg="$package: new aport
$url
$pkgdesc";;
'upgrade') data="$(sed -n 's/^pkgver=//p' $APORTS_DIRECTORY/$package/APKBUILD)"; msg="$package: upgrade to $data";;
'move') msg="$package: move from $data";;
*) echo "Invalid template $template"; exit 1;;
esac
if [ $do_reset = 'true' ]; then
set -e
git reset
git add "$APORTS_DIRECTORY"/$package
if [ "$template" = 'move' ]; then
git add "$APORTS_DIRECTORY"/$data/"$(basename $package)"
fi
set +e
fi
if [ "$amend" = 'true' ]; then
git commit --amend -m "$msg"
else
git commit -m "$msg"
fi
}
# `at-newpkg <package> [<package> <package> ...]` - takes package names and
# commits the packages with the given names.
at_newpkg() {
argparse="$(__at_argparse $*)"
args="$(echo $argparse | cut -d: -f1)"
pkgs="${argparse##$args:}"
pwd_base="$(basename $PWD)"
__at_cd_aports
if [ $2 ]; then
# More than 2 packages; rebase
git reset origin/master
fi
for pkg in $pkgs; do
if [ $pkg = 'pwd' ]; then pkg="$pwd_base"; fi
echo $pkg $PWD
pkg_with_repo="$(at_repoforpkg $pkg)" || exit 1;
echo $pkg_with_repo
git add "$APORTS_DIRECTORY/$pkg_with_repo"
cd "$APORTS_DIRECTORY/$pkg_with_repo"
source APKBUILD
msg="$pkg_with_repo: new aport
$url
$pkgdesc"
if git show HEAD | grep "$pkg_with_repo: new aport" &>/dev/null; then
git commit --amend -m "$msg"
else
git commit -m "$msg"
fi
done
}
# `at-move <package> <target-repository> [--no-commit]` - moves the provided
# package to the target repository (testing, community, main, unmaintained)
# and commits the change.
at_move() {
argparse="$(__at_argparse $*)"
args="$(echo $argparse | cut -d: -f1)"
nonargs="${argparse##$args:}"
source="$(at_repoforpkg $(echo $nonargs | cut -d' ' -f1))" || exit 1;
target="$(echo $nonargs | cut -d' ' -f2)"
pkgname="$(basename $source)"
__at_cd_aports
mv "$source" "$target"/"$pkgname"
nocommit=false
for arg in $args; do
case $arg in
'--no-commit') nocommit=true;;
esac
done
if [ "$nocommit" = 'false' ]; then
git add "$source" "$target"/"$pkgname"
at_commit move "$target"/"$pkgname" "$(dirname $source)"
fi
}
# `at-upgrade <package> [--no-commit]` - sets the pkgver to the provided
# version, updates the checksum, tries to find a changelog, builds the
# app and if all succeeds commits the change
at_upgrade() {
argparse="$(__at_argparse $*)"
args="$(echo $argparse | cut -d: -f1)"
nonargs="${argparse##$args:}"
nocommit=false
nobuild=false
for arg in $args; do
case $arg in
'--no-commit') nocommit=true;;
'--no-build') nobuild=true;;
esac
done
pkg="$(at_repoforpkg $(echo $nonargs | cut -d' ' -f1))" || exit 1;
pkgver="$(echo $nonargs | cut -d' ' -f2)"
pkgname="$(basename $pkg)"
__at_cd "$APORTS_DIRECTORY"/"$pkg"
sed -i "s/^pkgver=.*/pkgver=$pkgver/" APKBUILD
sed -i 's/^pkgrel=.*/pkgrel=0/' APKBUILD
abuild checksum || exit 1
# Try to guess changelog path
# TODO: use sources for this, do some fancy extraction or whatever. this is
# good enough for now
url=$(sed -n 's/^url=//p' APKBUILD)
changelog=none
case $url in
*github.com*) changelog=$url/releases/tag/$pkgver;;
*) changelog=$url;;
esac
echo "You may be able to find a changelog at the following URL:"
echo $changelog
if [ "$nobuild" = 'false' ]; then
read -p "Build and commit right away? [Y/n] " answer
case $answer in
''|'y'|'Y') ;;
*) exit 0;;
esac
abuild -r || exit 1
fi
if [ "$nocommit" = 'false' ]; then
git add .
at_commit upgrade "$pkg" "$pkgver"
fi
}
# `at-install-makedepends [<package>]` - installs depends, makedepends and
# checkdepends for the package. if package name is not provided, assumes
# name from current working directory.
at_install_makedepends() {
if [ ! $1 ]; then
package="$(basename $PWD)"
elif [ $pkg = 'pwd' ]; then
package="$(basename $PWD)"
else
package=$1
fi
package_path="$APORTS_DIRECTORY"/$(at_repoforpkg $package) || exit 1
source "$package_path"/APKBUILD
# yes, abuild -r uses .makedepends-$pkgname, but it also autoremoves it,
# which is kind of counterintuitive... so we use our own custom name
$SUDO apk add --virtual .at-makedepends-$pkgname $depends $checkdepends $makedepends
}
# `at-uninstall-makedepends [<package>]` - uninstalls makedepends installed by
# `at-install-makedepends`.
at_uninstall_makedepends() {
if [ ! $1 ]; then
package="$(basename $PWD)"
else
package=$1
fi
package_path="$APORTS_DIRECTORY"/$(at_repoforpkg $package) || exit 1
source "$package_path"/APKBUILD
$SUDO apk del .at-makedepends-$pkgname
}
if [ $1 ]; then
if [ ! $SUDO ]; then
SUDO=sudo
fi
command=$1
shift
case $command in
'repoforpkg') at_repoforpkg $1 || exit 1;;
'newpkg') at_newpkg $@ || exit 1;;
'commit') at_commit $@ || exit 1;;
'move') at_move $@ || exit 1;;
'upgrade') at_upgrade $@ || exit 1;;
'install-makedepends') at_install_makedepends $1 || exit 1;;
'uninstall-makedepends') at_uninstall_makedepends $1 || exit 1;;
*) echo "Unknown command $command"; exit 1;;
esac
fi