install_manual.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env bash
  2. # Requirements to install fonts
  3. REQUIREMENTS="awk curl fc-cache mkdir mktemp unlink unzip"
  4. readonly REQUIREMENTS
  5. # Fonts local directory
  6. LOCAL_FONTS_DIR="${HOME}/.local/share/fonts"
  7. readonly LOCAL_FONTS_DIR
  8. # Latest fonts release info in JSON
  9. LATEST_RELEASE_INFO="https://api.github.com/repos/JetBrains/JetBrainsMono/releases/latest"
  10. readonly LATEST_RELEASE_INFO
  11. RED=$(echo -en '\033[00;31m')
  12. readonly RED
  13. RESTORE=$(echo -en '\033[0m')
  14. readonly RESTORE
  15. # Print error message to STDERR and exit
  16. function die() {
  17. echo >&2 "${RED}$*${RESTORE}"
  18. exit 1
  19. }
  20. # Check requirements
  21. function check_requirements() {
  22. echo "Checking requirements..."
  23. for tool in ${REQUIREMENTS}; do
  24. echo -n "${tool}... "
  25. if command -v "${tool}" >/dev/null 2>&1; then
  26. echo "Found"
  27. else
  28. die "Not found. Please install \"${tool}\" to fix it."
  29. fi
  30. done
  31. }
  32. # Download URL
  33. function download() {
  34. url="${1}"
  35. save_as="${2}"
  36. curl -sL "${1}" -o "${save_as}" || die "Unable to download: ${url}"
  37. }
  38. # Generate temporary filename
  39. function get_tempfile() {
  40. mktemp
  41. }
  42. # Get item from latest release data
  43. function get_item() {
  44. item="${1}"
  45. read_from="${2}"
  46. awk -F '"' "/${item}/ {print \$4}" "${read_from}"
  47. }
  48. # Extract fonts archive
  49. function extract() {
  50. archive="${1}"
  51. extract_to="${2}"
  52. unzip -o "${archive}" -d "${extract_to}" >/dev/null 2>&1
  53. }
  54. # Create fonts directory
  55. function create_fonts_dir() {
  56. if [ ! -d "${LOCAL_FONTS_DIR}" ]; then
  57. echo "Creating fonts directory: ${LOCAL_FONTS_DIR}"
  58. mkdir -p "${LOCAL_FONTS_DIR}" >/dev/null 2>&1 || die "Unable to create fonts directory: ${LOCAL_FONTS_DIR}"
  59. fi
  60. }
  61. # Build fonts cache
  62. function build_fonts_cache() {
  63. fc-cache -f || die "Unable to build fonts cache"
  64. }
  65. # Remove temporary file
  66. function cleanup() {
  67. unlink "${*}" || die "Unable to unlink: ${*}"
  68. }
  69. # Start point
  70. function main() {
  71. echo "Installing latest JetBrains Mono fonts..."
  72. check_requirements
  73. TEMP_LATEST_INFO=$(get_tempfile)
  74. echo "Downloading latest release info: ${LATEST_RELEASE_INFO}"
  75. download "${LATEST_RELEASE_INFO}" "${TEMP_LATEST_INFO}"
  76. TAG_NAME=$(get_item "tag_name" "${TEMP_LATEST_INFO}")
  77. echo "Latest fonts version: ${TAG_NAME}"
  78. BROWSER_URL=$(get_item "browser_download_url" "${TEMP_LATEST_INFO}")
  79. TEMP_FONTS_ARCHIVE=$(get_tempfile)
  80. echo "Downloading fonts archive: ${BROWSER_URL}"
  81. download "${BROWSER_URL}" "${TEMP_FONTS_ARCHIVE}"
  82. create_fonts_dir
  83. echo "Extracting fonts: ${LOCAL_FONTS_DIR}"
  84. extract "${TEMP_FONTS_ARCHIVE}" "${LOCAL_FONTS_DIR}"
  85. echo "Building fonts cache..."
  86. build_fonts_cache
  87. echo "Cleaning up..."
  88. cleanup "${TEMP_LATEST_INFO}"
  89. cleanup "${TEMP_FONTS_ARCHIVE}"
  90. echo "Fonts have been installed"
  91. }
  92. main