-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradlecache
executable file
·266 lines (232 loc) · 6.7 KB
/
gradlecache
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
#!/bin/bash -e
# Set of tools to manage gradle the local gradle cache.
GRADLE_USER_HOME=${GRADLE_USER_HOME:-${HOME}/.gradle}
BACKUP_LOC=${GRADLECACHE_BACKUP_HOME:-${HOME}/.config/gradlecache-backups}
LIST_DATE_FORMAT="%d %b %Y %H:%M"
# the sub-directories under the GRADLE_USER_HOME that we backup
CACHE_DIRS=('caches' 'daemon' 'native' 'wrapper')
function show_help {
echo "Usage: $(basename $0) -[bclh] | -[rp] [timestamp]" >&2
echo -e "\t-b: Backup gradle cache" >&2
echo -e "\t-c: Clear the existing gradle cache (creates a backup)" >&2
echo -e "\t-p: Prune backup" >&2
echo -e "\t-r: Restore gradle cache from timestamp" >&2
echo -e "\t-l: List backups" >&2
echo -e "\t-h: Print help" >&2
}
# Exit cleanly if the user hits Ctrl,c when reading
function ctrl_c() {
echo && exit 0
}
# Exit the program with a message and optional exit code (default exit code=1)
function abort {
return_code=${2:-1}
echo "$1"
exit $return_code
}
function format_timestamp {
date --date="@$1" +"$LIST_DATE_FORMAT"
}
# Estimate the size of a backup
function estimate_backup_size {
# du every backup directory in the array
# emit a total (-c)
# print the numeric part of the last line (the total)
du -hsc "${BACKUP_LOC}/${CACHE_DIRS}$1" \
| tail -n 1 \
| awk '{print $1}'
}
function find_backup_timestamps {
if [ ! -d $BACKUP_LOC ]
then
exit
fi
# find a backup directory in the backup location
# extract the timestamp from the directory name
# sort to ensure oldest backups printed first
find \
$BACKUP_LOC \
-maxdepth 1 \
-type d \
-regex ".*${CACHE_DIRS[0]}[0-9]\{10,11\}" \
| sed 's/.*\([0-9]\{10,11\}\)/\1/' | sort | uniq
}
function list_backups {
for timestamp in $(find_backup_timestamps)
do
date_string=$(format_timestamp $timestamp)
size_string=$(estimate_backup_size $timestamp)
echo "$timestamp ($date_string) ($size_string)"
done
}
# Backup specific directory in gradle cache location.
#
# $1: absolute path to directory to be backed up
# $2: backup timestamp
function backup_dir {
if [ -d $1 ]
then
cp -r $1 ${BACKUP_LOC}/$(basename $1)$2
else
abort "No directory found at $1. Is this a fresh gradle home?"
fi
}
# Restore specific directory in gradle cache location.
#
# $1: absolute path to the directory being restored to
# $2: backup timestamp to restore from
function restore_dir {
if [ -d ${BACKUP_LOC}/$(basename $1)$2 ]
then
rm -rf $1
mv ${BACKUP_LOC}/$(basename $1)$2 $1
else
abort "No backup found with timestamp $2. List available backups with $(basename $0) -l"
fi
}
function clear_dir {
if [ -d $1 ]
then
rm -rf $1
else
abort "No directory found at $1. Is this a fresh gradle home?"
fi
}
function validate_timestamp {
# check the timestamp matches a regex
if [[ ! $1 =~ ^[0-9]{10,11}$ ]]
then
abort "$1 is not a valid timestamp"
fi
}
function backup {
backup_timestamp=$(date +"%s")
if [ -d ${GRADLE_USER_HOME} ]
then
echo "Backing up ${GRADLE_USER_HOME} using timestamp $backup_timestamp to ${BACKUP_LOC}..."
if [ ! -d $BACKUP_LOC ]
then
mkdir -p $BACKUP_LOC
fi
for dir in "${CACHE_DIRS[@]}"
do
backup_dir ${GRADLE_USER_HOME}/$dir $backup_timestamp
done
else
abort "No gradle home found at ${GRADLE_USER_HOME}" 0
fi
}
function restore {
if [[ $1 == "" ]]
then
# if no timestamp specified, present user with list of choices
for timestamp in $(find_backup_timestamps)
do
date_string=$(format_timestamp $timestamp)
size_string=$(estimate_backup_size $timestamp)
echo "$timestamp ($date_string) ($size_string)"
# default to the most recent backup
default_choice=$timestamp
done
if [ -z $default_choice ]
then
abort "No available backups" 0
fi
read -p "Select backup to restore [$default_choice]: " restore_timestamp
restore_timestamp=${restore_timestamp:-${default_choice}}
else
# if the user specified a timestamp, restore using that
restore_timestamp=$1
fi
validate_timestamp $restore_timestamp
if [ -d ${GRADLE_USER_HOME} ]
then
echo "Restoring ${GRADLE_USER_HOME} using timestamp $restore_timestamp..."
for dir in "${CACHE_DIRS[@]}"
do
restore_dir ${GRADLE_USER_HOME}/$dir $restore_timestamp
done
else
abort "No gradle home found at ${GRADLE_USER_HOME}"
fi
}
function clear {
read -p "Are you sure you want to clear your gradle cache (\"yes\")? " reply
if [[ $reply =~ ^yes$ ]]
then
backup
for dir in "${CACHE_DIRS[@]}"
do
clear_dir ${GRADLE_USER_HOME}/$dir
done
echo "Gradle cache cleared"
else
echo "Not clearing cache"
fi
}
function prune {
if [[ $1 == "" ]]
then
# if no timestamp specified, present user with list of choices
for timestamp in $(find_backup_timestamps)
do
date_string=$(format_timestamp $timestamp)
size_string=$(estimate_backup_size $timestamp)
echo "$timestamp ($date_string) ($size_string)"
default_choice=${default_choice:-$timestamp}
done
if [ -z $default_choice ]
then
abort "No available backups" 0
fi
read -p "Select backup to prune [$default_choice]: " prune_timestamp
prune_timestamp=${prune_timestamp:-${default_choice}}
else
# if the user specified a timestamp, prune that
prune_timestamp=$1
fi
validate_timestamp $prune_timestamp
echo "pruning $prune_timestamp..."
for dir in "${CACHE_DIRS[@]}"
do
rm -rf ${BACKUP_LOC}/${dir}$prune_timestamp
done
}
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
# show help if no args specified
if [ $# -lt 1 ]
then
show_help && exit
fi
# process args
while [[ "$1" =~ ^- && ! "$1" == "--" ]];
do
case $1 in
-b | --backup )
backup && exit
;;
-c | --clear )
clear && exit
;;
-p | --prune )
shift
prune $1 && exit
;;
-r | --restore )
shift
restore $1 && exit
;;
-l | --list )
list_backups && exit
;;
-h | --help )
show_help && exit
;;
*)
show_help && abort "Unrecognized option: $1"
;;
esac
shift
done
if [[ "$1" == '--' ]]; then shift; fi