Let’s break down what this script does in detail. The script is named install_latest_jetbrains_mono.sh, authored by GJS (homelab-alpha), and its purpose is to fetch the latest version of JetBrains Mono from the official GitHub repository, download it, install it to the system-wide fonts directory, and clean up the downloaded files.

Here’s a detailed explanation:

Script Metadata

  • Filename: install_latest_jetbrains_mono.sh
  • Author: GJS (homelab-alpha)
  • Date: Jul 05, 2024
  • Version: 1.1.0
  • Description: The script fetches the latest version of JetBrains Mono from the official GitHub repository, downloads it, installs it to the system-wide fonts directory, and cleans up the downloaded files.
  • Usage: sudo ./install_latest_jetbrains_mono.sh
  • RAW Script: install_latest_jetbrains_mono.sh

Fetching the Latest Version

The script starts by fetching the latest version of JetBrains Mono using the GitHub API. It uses curl to send a request to the GitHub API endpoint for JetBrains Mono releases and grep to extract the tag name of the latest release. The tag name, which includes the version number, is then cleaned up using sed to isolate just the version number.

LATEST_VERSION=$(curl -s https://api.github.com/repos/JetBrains/JetBrainsMono/releases/latest | grep '"tag_name":' | sed -E 's/.*"v([^"]+)".*/\1/')

Error Handling for Version Fetching

The script checks if the version fetching was successful by verifying if the LATEST_VERSION variable is not empty. If it is empty, an error message is displayed, and the script exits with a status of 1.

if [ -z "$LATEST_VERSION" ]; then
  echo "Failed to fetch the latest version of JetBrains Mono."
  exit 1
fi

Constructing the Download URL

Using the fetched version number, the script constructs the URL to download the latest JetBrains Mono zip file.

DOWNLOAD_URL="https://github.com/JetBrains/JetBrainsMono/releases/download/v${LATEST_VERSION}/JetBrainsMono-${LATEST_VERSION}.zip"

Defining the Download Directory

The script defines a directory where the downloaded file will be saved. In this case, it uses the Downloads directory in the user’s home directory.

DOWNLOAD_DIR="$HOME/Downloads"

Checking and Creating the Download Directory

The script checks if the download directory exists, and if not, it creates it.

if [ ! -d "$DOWNLOAD_DIR" ]; then
  mkdir -p "$DOWNLOAD_DIR"
  echo "Created directory: $DOWNLOAD_DIR"
fi

Downloading the Latest Version

The script uses wget to download the JetBrains Mono zip file to the specified download directory.

wget -v "$DOWNLOAD_URL" -O "$DOWNLOAD_DIR"/JetBrainsMono-"${LATEST_VERSION}".zip

Error Handling for Downloading

It checks if wget successfully downloaded the file by verifying its existence. If the download fails, an error message is displayed, and the script exits with a status of 1.

if [ ! -f "$DOWNLOAD_DIR"/JetBrainsMono-"${LATEST_VERSION}".zip ]; then
  echo "Failed to download JetBrains Mono version ${LATEST_VERSION}."
  exit 1
fi

echo "Successfully downloaded JetBrains Mono version ${LATEST_VERSION} to $DOWNLOAD_DIR."

Extracting the Downloaded File

The script extracts the contents of the downloaded zip file to the download directory using unzip.

unzip "$DOWNLOAD_DIR"/JetBrainsMono-"${LATEST_VERSION}".zip -d "$DOWNLOAD_DIR/JetBrainsMono-${LATEST_VERSION}"

Preparing the Installation Directory

The script creates the target directory /usr/share/fonts/jetbrains-mono if it does not already exist.

JETBRAINS_MONO_DIR="/usr/share/fonts/jetbrains-mono"

if [ ! -d "$JETBRAINS_MONO_DIR" ]; then
  sudo mkdir -p "$JETBRAINS_MONO_DIR"
  echo "Created directory: $JETBRAINS_MONO_DIR"
fi

Removing Previous Installations

To avoid conflicts, the script removes any previous installations of JetBrains Mono fonts in the target directory.

sudo rm -rf $JETBRAINS_MONO_DIR/*

Moving Files to the Installation Directory

The script moves the font files to the system-wide fonts directory.

sudo mv "$DOWNLOAD_DIR/JetBrainsMono-${LATEST_VERSION}/fonts/"* $JETBRAINS_MONO_DIR

Verifying the Installation

It checks if the font files were successfully moved to the target directory. If the files are not found, an error message is displayed, and the script exits with a status of 1.

if [ ! -f "$JETBRAINS_MONO_DIR/ttf/JetBrainsMono-Regular.ttf" ]; then
  echo "Failed to move JetBrains Mono fonts to $JETBRAINS_MONO_DIR."
  exit 1
fi

echo "Successfully installed JetBrains Mono version ${LATEST_VERSION} to $JETBRAINS_MONO_DIR."

Updating the Font Cache

The script updates the font cache to make the new fonts available system-wide.

sudo fc-cache -f -v

Cleaning Up

The script removes the downloaded zip file and the extracted directory to clean up unnecessary files.

rm "$DOWNLOAD_DIR"/JetBrainsMono-"${LATEST_VERSION}".zip
rm -r "$DOWNLOAD_DIR/JetBrainsMono-${LATEST_VERSION}"

echo "Cleanup complete."
echo ""

Verifying the Installation by Listing Installed Fonts

Finally, the script checks if the JetBrains Mono font was successfully installed by listing installed fonts and checking for its presence. If the font is not found, an error message is displayed; otherwise, a success message is shown.

if ! fc-list | grep -q "JetBrains Mono"; then
  echo "JetBrains Mono font not found. Installation may have failed."
  exit 1
else
  echo "JetBrains Mono font successfully installed."
fi

Conclusion

This script automates the process of downloading and installing the latest version of JetBrains Mono, ensuring that users always have the most up-to-date version with minimal manual intervention. It includes comprehensive error handling and provides clear instructions for users to finalize their setup if necessary.

Last updated 01 Sep 2024, 10:22 CEST . history