-
Notifications
You must be signed in to change notification settings - Fork 14
/
deb2appimage.sh
executable file
·412 lines (390 loc) · 16.6 KB
/
deb2appimage.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/bin/bash
# Title: deb2appimage
# Author: simonizor
# License: MIT
# Description: Build AppImages from deb packages on any distro with simple json configuration
# Dependencies: jq, curl, ar, tar
# function to handle exits
function d2aexit() {
case $1 in
# normal exit; remove "$HOME"/.cache/deb2appimage before exiting
0)
rm -rf "$HOME"/.cache/deb2appimage/*
exit 0
;;
# missing dependencies
1)
if [[ ! "$D2A_QUIET" = "TRUE" ]]; then
echo "Dependency Error!"
echo "Missing: $2"
if [[ -n "$3" ]]; then
echo "$3"
fi
echo "Exit code 1"
fi
rm -rf "$HOME"/.cache/deb2appimage/*
exit 1
;;
# user input error
2)
if [[ ! "$D2A_QUIET" = "TRUE" ]]; then
echo "Input Error!"
echo "$2"
if [[ -n "$3" ]]; then
echo "$3"
fi
echo "Exit code 2"
fi
rm -rf "$HOME"/.cache/deb2appimage/*
exit 2
;;
# curl error
3)
if [[ ! "$D2A_QUIET" = "TRUE" ]]; then
echo "Download Error!"
echo "Error while downloading $2"
if [[ -n "$3" ]]; then
echo "$3"
fi
echo "Exit code 3"
fi
rm -rf "$HOME"/.cache/deb2appimage/*
exit 3
;;
# file/dir error
4)
if [[ ! "$D2A_QUIET" = "TRUE" ]]; then
echo "Directory/File Error!"
echo "Error while creating file/directory $2"
if [[ -n "$3" ]]; then
echo "$3"
fi
echo "Exit code 4"
fi
rm -rf "$HOME"/.cache/deb2appimage/*
exit 4
;;
# deb extraction error
5)
if [[ ! "$D2A_QUIET" = "TRUE" ]]; then
echo "Error Extracting Deb!"
echo "Error while extracting $2"
if [[ -n "$3" ]]; then
echo "$3"
fi
echo "Exit code 5"
fi
rm -rf "$HOME"/.cache/deb2appimage/*
exit 5
;;
# appimagetool error
6)
if [[ ! "$D2A_QUIET" = "TRUE" ]]; then
echo "Error Running appimagetool!"
echo "appimagetool failed to build $2"
if [[ -n "$3" ]]; then
echo "$3"
fi
echo "Exit code 6"
fi
rm -rf "$HOME"/.cache/deb2appimage/*
exit 6
;;
esac
}
function d2aprerun() {
if [[ -d "$HOME/.cache/deb2appimage/AppDir" ]]; then
d2aexit 4 "$HOME/.cache/deb2appimage/AppDir" "Directory exists!"
fi
mkdir -p "$HOME"/.cache/deb2appimage/AppDir
mkdir -p "$HOME"/.cache/deb2appimage/debs
mkdir -p "$HOME"/.cache/deb2appimage/other
cp "$D2A_JSON" "$HOME"/.cache/deb2appimage/build.json
}
# execute prerun command if not null
function preruncmd() {
PRERUN_CMD="$(jq -r '.buildinfo[0].prerun[]' "$HOME"/.cache/deb2appimage/build.json | sed "s,^.*,& \&\& ,g" | tr -d '\n' | rev | cut -f3- -d' ' | rev)"
if [[ ! "$PRERUN_CMD" = "null" ]]; then
bash -c "$PRERUN_CMD" || d2aexit 2 "'prerun' failed!" "Failed to execute: $PRERUN_CMD"
fi
}
# function to find latest debs based on user input
function getlatestdeb() {
DEB_NAME="$1"
DEB_DISTRO="$2"
DEB_RELEASE="$3"
DEB_ARCH="$4"
case $DEB_DISTRO in
Debian|debian)
DEB_DISTRO_URL="debian.org"
;;
Ubuntu|ubuntu)
DEB_DISTRO_URL="ubuntu.com"
;;
*)
d2aexit 2 "Invalid 'distrorepo' entered in json file" "Valid choices are Debian and Ubuntu"
;;
esac
# find latest deb url using grep and head -n 1
LATEST_DEB_URL="$(curl -sL "https://packages.$DEB_DISTRO_URL/$DEB_RELEASE/$DEB_ARCH/$DEB_NAME/download" | grep "<li>*..*$DEB_ARCH.deb" | cut -f2 -d'"' | head -n 1)"
curl -sL "$LATEST_DEB_URL" -o "$HOME"/.cache/deb2appimage/debs/"$DEB_NAME".deb || d2aexit 3 "$DEB_NAME.deb" "URL: $LATEST_DEB_URL"
}
# function that uses jq to get package's deps from build.json
function getappdeps() {
if [[ ! "$(jq -r '.buildinfo[0].deps' "$HOME"/.cache/deb2appimage/build.json)" = "null" ]]; then
COUNT_NUM=1
# run a for loop to download the latest version of each deb using getlatestdeb function
for appdep in $(jq -r '.buildinfo[0].deps' "$HOME"/.cache/deb2appimage/build.json | tr ',' '\n'); do
DEB_REPO_DISTRO="$(jq -r '.buildinfo[0].distrorepo' "$HOME"/.cache/deb2appimage/build.json | cut -f${COUNT_NUM} -d',')"
DEB_REPO_VERSION="$(jq -r '.buildinfo[0].repoversion' "$HOME"/.cache/deb2appimage/build.json | cut -f${COUNT_NUM} -d',')"
DEB_REPO_ARCH="$(jq -r '.buildinfo[0].repoarch' "$HOME"/.cache/deb2appimage/build.json | cut -f${COUNT_NUM} -d',')"
if [[ -z "$DEB_REPO_DISTRO" ]]; then
DEB_REPO_DISTRO="$DEB_LAST_DISTRO"
fi
if [[ -z "$DEB_REPO_ARCH" ]]; then
DEB_REPO_ARCH="$DEB_LAST_ARCH"
fi
if [[ -z "$DEB_REPO_VERSION" ]]; then
DEB_REPO_VERSION="$DEB_LAST_VERSION"
fi
getlatestdeb "$appdep" "$DEB_REPO_DISTRO" "$DEB_REPO_VERSION" "$DEB_REPO_ARCH"
COUNT_NUM=$(($COUNT_NUM+1))
DEB_LAST_DISTRO="$DEB_REPO_DISTRO"
DEB_LAST_ARCH="$DEB_REPO_ARCH"
DEB_LAST_VERSION="$DEB_REPO_VERSION"
done
fi
}
# function to extract debs and move them to AppDir folder
function debextract () {
mkdir -p "$HOME"/.cache/deb2appimage/debs/temp
cd "$HOME"/.cache/deb2appimage/debs/temp
mv "$HOME"/.cache/deb2appimage/debs/"$1" "$HOME"/.cache/deb2appimage/debs/temp/"$1"
ar x "$HOME"/.cache/deb2appimage/debs/temp/"$1"
rm -f "$HOME"/.cache/deb2appimage/debs/temp/"$1"
rm -f "$HOME"/.cache/deb2appimage/debs/temp/control.tar.gz
rm -f "$HOME"/.cache/deb2appimage/debs/temp/debian-binary
tar -xf "$HOME"/.cache/deb2appimage/debs/temp/data.tar.* -C "$HOME"/.cache/deb2appimage/debs/temp/
rm -f "$HOME"/.cache/deb2appimage/debs/temp/data.tar.*
chmod -R 755 ~/.cache/deb2appimage
cp -r "$HOME"/.cache/deb2appimage/debs/temp/* "$HOME"/.cache/deb2appimage/AppDir/
rm -rf "$HOME"/.cache/deb2appimage/*/debs/temp/*
}
# function that runs a for loop to find all downloaded debs and extract them
function finddownloadeddebs() {
for debpkg in $(ls -C -w 1 "$HOME"/.cache/deb2appimage/debs); do
debextract "$debpkg" || d2aexit 5 "$debpkg"
done
rm -rf "$HOME"/.cache/deb2appimage/*/debs
}
# function that places files in places where appimagetool expects them to be for building AppImage
function prepareappdir() {
APP_NAME="$(jq -r '.buildinfo[0].name' "$HOME"/.cache/deb2appimage/build.json)"
BINARY_PATH="$(jq -r '.buildinfo[0].binarypath' "$HOME"/.cache/deb2appimage/build.json)"
DESKTOP_PATH="$(jq -r '.buildinfo[0].desktoppath' "$HOME"/.cache/deb2appimage/build.json)"
ICON_PATH="$(jq -r '.buildinfo[0].iconpath' "$HOME"/.cache/deb2appimage/build.json)"
USE_WRAPPER="$(jq -r '.buildinfo[0].usewrapper' "$HOME"/.cache/deb2appimage/build.json)"
if [[ "$APP_NAME" = "null" ]]; then
d2aexit 2 "Missing required 'appname' in json file"
fi
if [[ ! -f "$HOME/.cache/deb2appimage/AppDir$BINARY_PATH" ]]; then
d2aexit 2 "Binary file not found at binarypath in json file"
fi
# Download icon if it does not exist
if [[ ! -f "$HOME/.cache/deb2appimage/AppDir$ICON_PATH" ]]; then
echo "$ICON_PATH not found; downloading generic icon..."
curl -sL "https://raw.githubusercontent.com/iconic/open-iconic/master/png/file-6x.png" -o "$HOME"/.cache/deb2appimage/AppDir/."$APP_NAME".png
ICON_PATH="/.$APP_NAME.png"
fi
# Create .desktop file if it does not exist
if [[ ! -f "$HOME/.cache/deb2appimage/AppDir$DESKTOP_PATH" ]]; then
echo "$DESKTOP_PATH not found; creating generic .desktop file..."
cat > "$HOME"/.cache/deb2appimage/AppDir/"$APP_NAME".desktop << EOL
[Desktop Entry]
Type=Application
Name=$APP_NAME
Comment=$APP_NAME
Exec=$BINARY_PATH
Categories=Utility;
Icon=$APP_NAME
StartupNotify=false
Terminal=false
EOL
else
cp "$HOME"/.cache/deb2appimage/AppDir"$DESKTOP_PATH" "$HOME"/.cache/deb2appimage/AppDir/"$APP_NAME".desktop
DESKTOP_CATEGORIES="$(grep '^Categories=' "$HOME"/.cache/deb2appimage/AppDir/"$APP_NAME".desktop)"
if [[ -z "$DESKTOP_CATEGORIES" ]]; then
echo "Categories=Utility;" >> "$HOME"/.cache/deb2appimage/AppDir/"$APP_NAME".desktop
elif [[ ! "$(echo $DESKTOP_CATEGORIES | rev | cut -c1)" = ";" ]]; then
sed -i 's%^Categories=.*%Categories=Utility;%g' "$HOME"/.cache/deb2appimage/AppDir/"$APP_NAME".desktop
fi
fi
if [[ "$USE_WRAPPER" = "true" ]]; then
curl -sSL "https://raw.githubusercontent.com/simoniz0r/deb2appimage/master/resources/desktopintegration" -o "$HOME"/.cache/deb2appimage/AppDir"$BINARY_PATH".wrapper || d2aexit 3 "wrapper script"
curl -sSL "https://github.com/darealshinji/AppImageKit-dialog/releases/download/continuous/dialog-x86_64" -o "$HOME"/.cache/deb2appimage/AppDir/dialog || d2aexit 3 "wrapper script"
chmod a+x "$HOME"/.cache/deb2appimage/AppDir/dialog
chmod a+x "$HOME"/.cache/deb2appimage/AppDir"$BINARY_PATH".wrapper
fi
ICON_TYPE="$(echo $ICON_PATH | rev | cut -f1 -d'.' | rev)"
ICON_SIZE=$(file "$HOME"/.cache/deb2appimage/AppDir${ICON_PATH} | cut -f2 -d',' | tr -d '[:blank:]' | cut -f1 -d'x')
mkdir -p "$HOME"/.cache/deb2appimage/AppDir/usr/share/icons/default/${ICON_SIZE}x${ICON_SIZE}/apps
cp "$HOME"/.cache/deb2appimage/AppDir"$ICON_PATH" "$HOME"/.cache/deb2appimage/AppDir/usr/share/icons/default/${ICON_SIZE}x${ICON_SIZE}/apps/"$APP_NAME"."$ICON_TYPE"
cp "$HOME"/.cache/deb2appimage/AppDir"$ICON_PATH" "$HOME"/.cache/deb2appimage/AppDir/"$APP_NAME"."$ICON_TYPE"
sed -i "s,^Exec=.*,Exec=.$BINARY_PATH,g;s,^Icon=.*,Icon=$APP_NAME,g" "$HOME"/.cache/deb2appimage/AppDir/"$APP_NAME".desktop
}
# function that downloads AppRun and creates AppRun.conf
function prepareapprun() {
curl -sL "https://raw.githubusercontent.com/simoniz0r/deb2appimage/master/resources/AppRun" -o "$HOME"/.cache/deb2appimage/AppDir/AppRun || d2aexit 3 "AppRun script"
chmod a+x "$HOME"/.cache/deb2appimage/AppDir/AppRun
APPRUN_SET_PATH="$(jq -r '.apprunconf[0].setpath' "$HOME"/.cache/deb2appimage/build.json | tr '[:lower:]' '[:upper:]')"
APPRUN_SET_LIBPATH="$(jq -r '.apprunconf[0].setlibpath' "$HOME"/.cache/deb2appimage/build.json | tr '[:lower:]' '[:upper:]')"
APPRUN_SET_PYTHONPATH="$(jq -r '.apprunconf[0].setpythonpath' "$HOME"/.cache/deb2appimage/build.json | tr '[:lower:]' '[:upper:]')"
APPRUN_SET_PYTHONHOME="$(jq -r '.apprunconf[0].setpythonhome' "$HOME"/.cache/deb2appimage/build.json | tr '[:lower:]' '[:upper:]')"
APPRUN_SET_PYTHONBYTE="$(jq -r '.apprunconf[0].setpythondontwritebytecode' "$HOME"/.cache/deb2appimage/build.json | tr '[:lower:]' '[:upper:]')"
APPRUN_SET_XDG="$(jq -r '.apprunconf[0].setxdgdatadirs' "$HOME"/.cache/deb2appimage/build.json | tr '[:lower:]' '[:upper:]')"
APPRUN_SET_PERL="$(jq -r '.apprunconf[0].setperllib' "$HOME"/.cache/deb2appimage/build.json | tr '[:lower:]' '[:upper:]')"
APPRUN_SET_QT="$(jq -r '.apprunconf[0].setqtpluginpath' "$HOME"/.cache/deb2appimage/build.json | tr '[:lower:]' '[:upper:]')"
APPRUN_EXEC="$(jq -r '.apprunconf[0].exec' "$HOME"/.cache/deb2appimage/build.json)"
cat >"$HOME"/.cache/deb2appimage/AppDir/AppRun.conf << EOL
APPRUN_SET_PATH="$APPRUN_SET_PATH"
APPRUN_SET_LD_LIBRARY_PATH="$APPRUN_SET_LIBPATH"
APPRUN_SET_PYTHONPATH="$APPRUN_SET_PYTHONPATH"
APPRUN_SET_PYTHONHOME="$APPRUN_SET_PYTHONHOME"
APPRUN_SET_PYTHONDONTWRITEBYTECODE="$APPRUN_SET_PYTHONBYTE"
APPRUN_SET_XDG_DATA_DIRS="$APPRUN_SET_XDG"
APPRUN_SET_PERLLIB="$APPRUN_SET_PERL"
APPRUN_SET_GSETTINGS_SCHEMA_DIR="$APPRUN_SET_GSETTINGS"
APPRUN_SET_QT_PLUGIN_PATH="$APPRUN_SET_QT"
APPRUN_EXEC="$APPRUN_EXEC"
EOL
}
# function that runs postruncmd
function postruncmd() {
POSTRUN_CMD="$(jq -r '.buildinfo[0].postrun[]' "$HOME"/.cache/deb2appimage/build.json | sed "s,^.*,& \&\& ,g" | tr -d '\n' | rev | cut -f3- -d' ' | rev)"
if [[ ! "$POSTRUN_CMD" = "null" ]]; then
bash -c "$POSTRUN_CMD" || d2aexit 2 "'postrun' failed!" "Failed to execute: $POSTRUN_CMD"
fi
}
# function that downloads appimagetool and uses it to build the AppImage to the --output directory
function buildappimage() {
APP_VERSION="$(jq -r '.buildinfo[0].version' "$HOME"/.cache/deb2appimage/build.json)"
if [[ -z "$APP_VERSION" ]] || [[ "$APP_VERSION" = "null" ]]; then
APP_VERSION="$(date +'%F')"
fi
curl -sL "https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-x86_64.AppImage" -o "$HOME"/.cache/deb2appimage/appimagetool || d2aexit 3 "appimagetool"
chmod +x "$HOME"/.cache/deb2appimage/appimagetool
if [[ "$D2A_QUIET" = "TRUE" ]]; then
ARCH="$(uname -m)" "$HOME"/.cache/deb2appimage/appimagetool "$@" "$HOME"/.cache/deb2appimage/AppDir "$D2A_OUTPUT"/"$APP_NAME"-"$APP_VERSION"-"$(uname -m)".AppImage > /dev/null 2>&1 || d2aexit 6 "$APP_NAME"
else
ARCH="$(uname -m)" "$HOME"/.cache/deb2appimage/appimagetool "$@" "$HOME"/.cache/deb2appimage/AppDir "$D2A_OUTPUT"/"$APP_NAME"-"$APP_VERSION"-"$(uname -m)".AppImage || d2aexit 6 "$APP_NAME"
fi
}
function d2ahelp() {
printf '%s\n' "deb2appimage 0.0.5
Usage deb2appimage [argument] [input]
deb2appimage is a tool for creating AppImages using deb packages. json files are used
to provide simple configuration for building the AppImage. Contrary to the name, other
package types can also be used as the source of the application (or even its deps), but
those files will have to be placed in '~/.cache/deb2appimage/AppDir/' manually with a
prerun script.
Arguments:
--help, -h Show this output and exit
--json, -j Specify the location of the json file for building the AppImage (required)
--output, -o Specify the output directory of the AppImage (optional; $HOME will be used by default)
--quiet, -q Enable quiet mode
--debug Enable debug mode
Any arguments not listed above will be passed to 'appimagetool' when building the AppImage.
Examples:
deb2appimage -j $HOME/my-app.json
deb2appimage -j $HOME/my-app.json -o $HOME/AppImages
deb2appimage -j $HOME/my-app.json -o $HOME/AppImages -q
deb2appimage -j $HOME/my-app.json -o $HOME/AppImages --debug
"
}
# detect arguments
while [[ $# -gt 0 ]]; do
case "$1" in
# output help
-h|--help)
d2ahelp
d2aexit 0
;;
# set path to json file (required)
-j|--json)
shift
D2A_JSON="$(readlink -f $1)"
shift
;;
# set output directory (optional)
-o|--output)
shift
D2A_OUTPUT="$(readlink -f $1)"
shift
;;
# turn on quiet mode
-q|--quiet)
D2A_QUIET="TRUE"
shift
;;
# turn on debug mode
--debug)
set -x
shift
;;
esac
done
# check for deb2appimage's dependencies and exit if not installed
mkdir -p "$HOME"/.cache/deb2appimage
unset MISSING_DEPS
if [[ ! "$(command -v jq)" ]]; then
MISSING_DEPS+=("jq")
fi
if [[ ! "$(command -v curl)" ]]; then
MISSING_DEPS+=("curl")
fi
if [[ ! "$(command -v ar)" ]]; then
MISSING_DEPS+=("binutils")
fi
if [[ ! "$(command -v tar)" ]]; then
MISSING_DEPS+=("tar")
fi
if [[ ! -z "$MISSING_DEPS" ]]; then
d2aexit 1 "$(echo ${MISSING_DEPS[@]})"
fi
# check required inputs
if [[ -z "$D2A_JSON" ]]; then
d2ahelp
d2aexit 2 "Missing required --json input"
fi
# make sure json input is valid
jq '.' "$D2A_JSON" > /dev/null 2>&1 || d2aexit 2 "$D2A_JSON not valid json file"
# set default output location
if [[ -z "$D2A_OUTPUT" ]]; then
D2A_OUTPUT="$HOME"
fi
d2aprerun
if [[ ! "$D2A_QUIET" = "TRUE" ]]; then
echo "Executing prerun..."
fi
preruncmd
if [[ ! "$D2A_QUIET" = "TRUE" ]]; then
echo "Downloading dependencies..."
fi
getappdeps
if [[ ! "$D2A_QUIET" = "TRUE" ]]; then
echo "Extracting dependencies..."
fi
finddownloadeddebs
if [[ ! "$D2A_QUIET" = "TRUE" ]]; then
echo "Preparing AppImage AppDir..."
fi
prepareappdir
prepareapprun
if [[ ! "$D2A_QUIET" = "TRUE" ]]; then
echo "Executing postrun..."
fi
postruncmd
if [[ ! "$D2A_QUIET" = "TRUE" ]]; then
echo "Using 'appimagetool' to build AppImage for '$APP_NAME' to '$D2A_OUTPUT'..."
fi
buildappimage && d2aexit 0