refactor(fonts): follow code guidelines better, document code better, fix google fonts

This commit is contained in:
xynydev 2024-03-29 12:58:58 +02:00
parent b866a27cf1
commit be2bcc530e
5 changed files with 39 additions and 85 deletions

View file

@ -1,39 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
NAME=$1
FORMAT=$2
URL=$3
DEST=$4
mkdir -p "$DEST"
DOWNLOAD=$(ls "$DEST" | wc -l)
FILE="$NAME.$FORMAT"
if [[ $DOWNLOAD -eq 0 ]] && [[ -n $NAME ]]; then
echo "Downloading $FILE"
curl -o "$FILE" -OL "$URL"
if [[ -f "$FILE" ]]; then
case $FORMAT in
tar.xz) tar xvJf "$FILE" -C "$DEST" ;;
zip) unzip "$FILE" -d "$DEST" ;;
esac
rm -rf "$FILE"
echo "$FILE downloaded"
else
echo "Unable to download $FILE"
fi
fi

View file

@ -7,16 +7,14 @@ get_yaml_array() {
readarray "$1" < <(echo "$3" | yq -I=0 "$2")
}
export FONTS_MODULE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
for SOURCE in "$MODULE_DIRECTORY"/fonts/sources/*.sh; do
chmod +x "${SOURCE}"
for source in "$FONTS_MODULE_DIR"/sources/*.sh; do
chmod +x "$source"
filename=$(basename -- "$source")
get_yaml_array FONTS ".fonts.${filename%.*}[]" "$1"
bash "$source" "${FONTS[@]}"
# get array of fonts for current source
FILENAME=$(basename -- "${SOURCE}")
get_yaml_array FONTS ".fonts.${FILENAME%.*}[]" "$1"
if [ ${#FONTS[@]} -gt 0 ]; then
bash "${SOURCE}" "${FONTS[@]}"
fi
done

View file

@ -5,7 +5,7 @@ example: |
type: fonts
fonts:
nerd-fonts:
- FiraCode # don't add "Nerd Font" suffix.
- FiraCode # don't add spaces or "Nerd Font" suffix.
- Hack
- SourceCodePro
- Terminus

View file

@ -2,26 +2,28 @@
set -euo pipefail
mapfile -t FONTS <<< "$@"
URL="https://fonts.google.com/download?family="
DIR_PRINCIPAL=/usr/share/fonts/google-fonts
COMPACT_FORMAT="zip"
DEST="/usr/share/fonts/google-fonts"
# To download google-fonts it is necessary to enter the name of the font replacing the spaces with '%20'. See the 3rd parameter of the download script.
echo "Installation of google-fonts started"
rm -rf "${DEST}"
if [ ${#FONTS[@]} -gt 0 ]; then
for FONT in "${FONTS[@]}"; do
mkdir -p "${DEST}/${FONT}"
echo "Installation of google-fonts started"
readarray -t "FILE_REFS" < <(
curl -s "https://fonts.google.com/download/list?family=${FONT// /%20}" | # spaces are replaced with %20 for the URL
tail -n +2 | # remove first line, which as of March 2024 contains ")]}'" and breaks JSON parsing
jq -c '.manifest.fileRefs[]' # -c option makes output bash parsable
)
rm -rf "$DIR_PRINCIPAL"
for font in "${FONTS[@]}"; do
font="$(echo "$font" | sed -e 's|^[[:blank:]]||g' | tr -d '\n')"
bash "$(dirname "$0")"/../download.sh "$font" "$COMPACT_FORMAT" "$URL${font// /%20}" "$DIR_PRINCIPAL/$font"
for FILE_REF in "${FILE_REFS[@]}"; do
FILENAME=$(echo "${FILE_REF}" | jq -r '.filename')
URL=$(echo "${FILE_REF}" | jq -r '.url')
echo "Downloading ${FILENAME} from ${URL}..."
curl "${URL}" -o "${DEST}/${FONT}/${FILENAME##*/}" # everything before the last / is removed to get the filename
done
done
fc-cache -f $DIR_PRINCIPAL
fi
fc-cache -f "${DEST}"

View file

@ -2,26 +2,19 @@
set -euo pipefail
mapfile -t FONTS <<< "$@"
URL="https://github.com/ryanoasis/nerd-fonts/releases/latest/download/"
DIR_PRINCIPAL=/usr/share/fonts/nerd-fonts
COMPACT_FORMAT="zip"
URL="https://github.com/ryanoasis/nerd-fonts/releases/latest/download"
DEST="/usr/share/fonts/nerd-fonts"
# To download nerd-fonts you need to enter the name of the font and the format you want. See the 3rd parameter of the download script.
echo "Installation of nerd-fonts started"
rm -rf "$DEST"
if [ ${#FONTS[@]} -gt 0 ]; then
for FONT in "${FONTS[@]}"; do
mkdir -p "${DEST}/${FONT}"
echo "Installation of nerd-fonts started"
echo "Downloading ${FONT} from ${URL}/${FONT}.tar.xz..."
curl "${URL}/${FONT}.tar.xz" -L -o "/tmp/fonts/${FONT}.tar.xz"
tar -xf "/tmp/fonts/${FONT}.tar.xz" -C "${DEST}/${FONT}"
done
rm -rf "$DIR_PRINCIPAL"
for font in "${FONTS[@]}"; do
font="$(echo "$font" | sed -e 's|^[[:blank:]]||g' | tr -d '\n')"
bash "$(dirname "$0")"/../download.sh "$font" "$COMPACT_FORMAT" "$URL$font.$COMPACT_FORMAT" "$DIR_PRINCIPAL/$font"
done
fc-cache -f $DIR_PRINCIPAL
fi
fc-cache -f "${DEST}"