|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Checks AppDir for maximum compatibility with AppImage best practices. |
| 4 | +# This might evolve into a more formal specification one day. |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +HERE="$(dirname "$(readlink -f "${0}")")" |
| 9 | +APPDIR="${1}" |
| 10 | + |
| 11 | +fatal () { |
| 12 | + echo "FATAL: $1" |
| 13 | + exit 1 |
| 14 | +} |
| 15 | + |
| 16 | +warn () { |
| 17 | + echo "WARNING: $1" |
| 18 | +} |
| 19 | + |
| 20 | +which desktop-file-validate >/dev/null |
| 21 | +if [ ! $? -eq 0 ] ; then |
| 22 | + fatal "desktop-file-validate is missing, please install it" |
| 23 | +fi |
| 24 | + |
| 25 | +if [ ! -e "${HERE}/excludelist" ] ; then |
| 26 | + fatal "excludelist missing, please install it" |
| 27 | +fi |
| 28 | + |
| 29 | +if [ ! -d "${APPDIR}" ] ; then |
| 30 | + fatal "${APPDIR} is no directory" |
| 31 | +fi |
| 32 | + |
| 33 | +if [ ! -e "${APPDIR}/AppRun" ] ; then |
| 34 | + fatal "AppRun is missing in ${APPDIR}" |
| 35 | +fi |
| 36 | + |
| 37 | +if [ ! -e "${APPDIR}/.DirIcon" ] ; then |
| 38 | + fatal ".DirIcon is missing in ${APPDIR}" |
| 39 | +fi |
| 40 | + |
| 41 | +DIR_ICON_MIME=$(mimetype $(readlink -f ${APPDIR}/.DirIcon) | awk '{print $2}') |
| 42 | + |
| 43 | +if [[ ! "$DIR_ICON_MIME" = "image/png" ]] ; then |
| 44 | + warn "Icon is not in PNG format. It should be so that it can be used as a thumbnail" |
| 45 | +fi |
| 46 | + |
| 47 | +if [ ! -x "${APPDIR}/AppRun" ] ; then |
| 48 | + fatal "AppRun is not executable" // This seems to generate false alarms? https://travis-ci.org/AppImage/AppImageHub/builds/266084511#L539 |
| 49 | +fi |
| 50 | + |
| 51 | +NUM_DESKTOP=$(ls "${APPDIR}"/*.desktop 2>/dev/null | wc -l) |
| 52 | +if [ ${NUM_DESKTOP} != 1 ] ; then |
| 53 | + fatal "No .desktop file or multiple desktop files present" |
| 54 | +fi |
| 55 | + |
| 56 | +num_keys_fatal () { |
| 57 | + while IFS='=' read key val |
| 58 | + do |
| 59 | + if [[ $key == \[*\] ]] ; then |
| 60 | + # in a new section; confirm we saw the key once in [Desktop Entry] |
| 61 | + if [[ "${raw_section}" == "[Desktop Entry]" ]] ; then |
| 62 | + local seen_key="seen__${section}__${1}" |
| 63 | + if [[ "${!seen_key}" != "1" ]] ; then |
| 64 | + fatal "Key $1 is not in .desktop file exactly once in section $raw_section" |
| 65 | + fi |
| 66 | + fi |
| 67 | + local raw_section=$key |
| 68 | + local section="${key//[\[\]\- ]/_}" |
| 69 | + elif [[ $key == "$1" ]] ; then |
| 70 | + local seen_key="seen__${section}__${key}" |
| 71 | + printf -v "$seen_key" %s "$(( $seen_key + 1 ))" |
| 72 | + fi |
| 73 | + done < "${APPDIR}"/*.desktop |
| 74 | + |
| 75 | + |
| 76 | + # in case there is only one section |
| 77 | + # check for existence of key in [Desktop Entry] |
| 78 | + local seen_key="seen__${section}__${1}" |
| 79 | + if [[ "${section}" == "[Desktop Entry]" && "${!seen_key}" != "1" ]] ; then |
| 80 | + fatal "Key $1 is not in .desktop file exactly once in section $raw_section" |
| 81 | + fi |
| 82 | +} |
| 83 | + |
| 84 | +desktop-file-validate "${APPDIR}"/*.desktop |
| 85 | +if [ ! $? -eq 0 ] ; then |
| 86 | + fatal "desktop-file-validate did not exit cleanly on the .desktop file" |
| 87 | +fi |
| 88 | + |
| 89 | +num_keys_warn () { |
| 90 | + while IFS='=' read key val |
| 91 | + do |
| 92 | + if [[ $key == \[*\] ]] ; then |
| 93 | + # in a new section; confirm we saw the key once in [Desktop Entry] |
| 94 | + if [[ "${raw_section}" == "[Desktop Entry]" ]] ; then |
| 95 | + local seen_key="seen__${section}__${1}" |
| 96 | + if [[ "${!seen_key}" != "1" ]] ; then |
| 97 | + warn "Key $1 is not in .desktop file exactly once in section $raw_section" |
| 98 | + fi |
| 99 | + fi |
| 100 | + local raw_section=$key |
| 101 | + local section="${key//[\[\]\- ]/_}" |
| 102 | + elif [[ $key == "$1" ]] ; then |
| 103 | + local seen_key="seen__${section}__${key}" |
| 104 | + printf -v "$seen_key" %s "$(( $seen_key + 1 ))" |
| 105 | + fi |
| 106 | + done < "${APPDIR}"/*.desktop |
| 107 | + |
| 108 | + |
| 109 | + # in case there is only one section |
| 110 | + # check for existence of key in [Desktop Entry] |
| 111 | + local seen_key="seen__${section}__${1}" |
| 112 | + if [[ "${section}" == "[Desktop Entry]" && "${!seen_key}" != "1" ]] ; then |
| 113 | + warn "Key $1 is not in .desktop file exactly once in section $raw_section" |
| 114 | + fi |
| 115 | +} |
| 116 | + |
| 117 | +# num_keys_fatal Name # This is not a valid test since [Desktop Action ...] sections can also have Name= |
| 118 | +# num_keys_fatal Exec # This is not a valid test since [Desktop Action ...] sections can also have Name= |
| 119 | +# e.g, https://github.com/CDrummond/cantata/blob/master/cantata.desktop.cmake |
| 120 | +num_keys_fatal Icon |
| 121 | +num_keys_fatal Categories |
| 122 | +# num_keys_warn Comment |
| 123 | + |
| 124 | +# Find the relevant appdata.xml file; |
| 125 | +# according to ximion, usr/share/appdata is a legacy path replaced by usr/share/metainfo |
| 126 | +APPDATA=$(ls "${APPDIR}"/usr/share/metainfo/*appdata.xml 2>/dev/null | head -n 1) # TODO: Handle more than one better |
| 127 | +if [ -z "$APPDATA" ] ; then |
| 128 | + APPDATA=$(ls "${APPDIR}"/usr/share/appdata/*appdata.xml 2>/dev/null | head -n 1) # TODO: Handle more than one better |
| 129 | +fi |
| 130 | +if [ -z "$APPDATA" ] ; then |
| 131 | + warn 'No appdata file present. Please provide one in the AppImage as per the instructions on https://www.freedesktop.org/software/appstream/docs/chap-Quickstart.html#sect-Quickstart-DesktopApps' |
| 132 | +else |
| 133 | + if [ ! -z $(which appstreamcli) ] ; then |
| 134 | + appstreamcli validate-tree "${APPDIR}" |
| 135 | + else |
| 136 | + echo "Skipping AppStream validation since appstreamcli is not on the \$PATH" |
| 137 | + fi |
| 138 | +fi |
| 139 | + |
| 140 | + |
| 141 | +BLACKLISTED_FILES=$(cat "${HERE}/excludelist" | sed '/^\s*$/d ; /^#.*$/d ; s/\s*#.*$//') |
| 142 | +for FILE in $BLACKLISTED_FILES ; do |
| 143 | + if [ ! -z $(find "${APPDIR}" -name $FILE) ] ; then |
| 144 | + warn "Blacklisted file $FILE found" |
| 145 | + fi |
| 146 | +done |
| 147 | + |
| 148 | +echo "Lint found no fatal issues" |
| 149 | +exit 0 |
0 commit comments