Skip to content

Instantly share code, notes, and snippets.

@BendingBender
Last active December 25, 2021 12:07
Show Gist options
  • Save BendingBender/89c3487834ebcf36643a883d93a69e6d to your computer and use it in GitHub Desktop.
Save BendingBender/89c3487834ebcf36643a883d93a69e6d to your computer and use it in GitHub Desktop.
Script to update VSCodium native build for a Mac M1
#!/usr/bin/env bash
set -euo pipefail
INSTALLED_APP=/Applications/VSCodium.app
TMP_DIR=$(mktemp -d)
GITHUB_RELEASES_JSON="${TMP_DIR}/github-releases.json"
LATEST_RELEASE_ZIPBALL="${TMP_DIR}/vscodium.zip"
EXTRACTED_APP="${TMP_DIR}/vscodium/VSCode-darwin-arm64/VSCodium.app"
cleanup() {
if [[ ${TMP_DIR:-} != '' ]]; then
rm -R "${TMP_DIR}"
fi
}
trap cleanup EXIT
check_command() (
command=${1}
link_to_download=${2}
if ! command 1>/dev/null -v "${command}"; then
echo "${command} (${link_to_download}) is needed to run this script"
exit 1
fi
)
check_prerequisites() (
check_command jq https://stedolan.github.io/jq/
check_command curl https://curl.se/
)
get_installed_version() (
path_to_app=${1}
plist_file=${path_to_app}/Contents/Info.plist
if [[ -f ${plist_file} ]]; then
grep --after-context=1 'CFBundleVersion' "${plist_file}" | tail -n 1 | sed -E -n 's|^[[:space:]]*<string>([0-9\.]+)</string>$|\1|p'
else
echo '0.0.0'
fi
)
update_needed() (
old_version=${1}
new_version=${2}
IFS='.' read -r -a old_version_parts <<<"${old_version}"
IFS='.' read -r -a new_version_parts <<<"${new_version}"
# use index from old_version_parts because the tag on GitHub contains the
# architecture as the last part: 1.63.2.darwin_arm64
for index in "${!old_version_parts[@]}"; do
if ((10#${old_version_parts[index]} < 10#${new_version_parts[index]})); then
echo true
return
fi
done
)
patch_marketplace_urls() (
path_to_app=${1}
patch --silent "${path_to_app}/Contents/Resources/app/product.json" <<'EOF'
126,127c126,128
< "serviceUrl": "https://open-vsx.org/vscode/gallery",
< "itemUrl": "https://open-vsx.org/vscode/item"
---
> "serviceUrl": "https://marketplace.visualstudio.com/_apis/public/gallery",
> "cacheUrl": "https://vscode.blob.core.windows.net/gallery/index",
> "itemUrl": "https://marketplace.visualstudio.com/items"
EOF
)
check_prerequisites
installed_version=$(get_installed_version ${INSTALLED_APP})
echo "Locally installed version: ${installed_version}"
curl -s -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/tibeer/vscodium/releases -o "${GITHUB_RELEASES_JSON}"
available_version=$(jq --raw-output .[0].tag_name "${GITHUB_RELEASES_JSON}")
echo "Latest available version on GitHub: ${available_version}"
# need to evaluate the call to update_needed outside of an if expression for script to
# exit when an error occurs in update_needed
update_needed_result=$(update_needed "${installed_version}" "${available_version}")
if [[ -z $update_needed_result ]]; then
echo "Newest version already installed (${installed_version} >= ${available_version})."
echo "Nothing to do."
exit 0
fi
download_url=$(jq --raw-output .[0].assets[0].browser_download_url "${GITHUB_RELEASES_JSON}")
echo "Downloading latest release from $download_url ..."
curl -L -o "${LATEST_RELEASE_ZIPBALL}" "${download_url}"
echo "Downloaded VSCodium to ${LATEST_RELEASE_ZIPBALL}"
unzip -q "${LATEST_RELEASE_ZIPBALL}" -d "${TMP_DIR}"
echo "Successfully unzipped to ${EXTRACTED_APP}"
patch_marketplace_urls "${EXTRACTED_APP}"
echo "Patched marketplace URLs"
rm -R ${INSTALLED_APP} 2>/dev/null || true
mv "${EXTRACTED_APP}" /Applications/
echo "Installed new VSCodium version in /Applications/$(basename "${EXTRACTED_APP}")"
printf '\nYou need to run\n\nsudo xattr -r -d com.apple.quarantine %s\n\nto allow app execution!' "/Applications/$(basename "${EXTRACTED_APP}")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment