2018-08-01 06:54:59 +01:00
|
|
|
#! /usr/bin/env nix-shell
|
|
|
|
#! nix-shell -i bash -p curl jq unzip
|
2021-11-14 15:55:01 +00:00
|
|
|
# shellcheck shell=bash
|
2018-08-01 06:54:59 +01:00
|
|
|
set -eu -o pipefail
|
|
|
|
|
2021-11-14 15:55:01 +00:00
|
|
|
# can be added to your configuration with the following command and snippet:
|
|
|
|
# $ ./pkgs/misc/vscode-extensions/update_installed_exts.sh > extensions.nix
|
|
|
|
#
|
|
|
|
# packages = with pkgs;
|
|
|
|
# (vscode-with-extensions.override {
|
|
|
|
# vscodeExtensions = map
|
|
|
|
# (extension: vscode-utils.buildVscodeMarketplaceExtension {
|
|
|
|
# mktplcRef = {
|
|
|
|
# inherit (extension) name publisher version sha256;
|
|
|
|
# };
|
|
|
|
# })
|
|
|
|
# (import ./extensions.nix).extensions;
|
|
|
|
# })
|
|
|
|
# ]
|
|
|
|
|
2018-08-01 06:54:59 +01:00
|
|
|
# Helper to just fail with a message and non-zero exit code.
|
|
|
|
function fail() {
|
|
|
|
echo "$1" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:50:26 +00:00
|
|
|
# Helper to clean up after ourselves if we're killed by SIGINT.
|
2018-08-01 06:54:59 +01:00
|
|
|
function clean_up() {
|
|
|
|
TDIR="${TMPDIR:-/tmp}"
|
|
|
|
echo "Script killed, cleaning up tmpdirs: $TDIR/vscode_exts_*" >&2
|
|
|
|
rm -Rf "$TDIR/vscode_exts_*"
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_vsixpkg() {
|
|
|
|
N="$1.$2"
|
|
|
|
|
2021-01-18 17:50:26 +00:00
|
|
|
# Create a tempdir for the extension download.
|
2018-08-01 06:54:59 +01:00
|
|
|
EXTTMP=$(mktemp -d -t vscode_exts_XXXXXXXX)
|
|
|
|
|
|
|
|
URL="https://$1.gallery.vsassets.io/_apis/public/gallery/publisher/$1/extension/$2/latest/assetbyname/Microsoft.VisualStudio.Services.VSIXPackage"
|
|
|
|
|
|
|
|
# Quietly but delicately curl down the file, blowing up at the first sign of trouble.
|
|
|
|
curl --silent --show-error --fail -X GET -o "$EXTTMP/$N.zip" "$URL"
|
|
|
|
# Unpack the file we need to stdout then pull out the version
|
|
|
|
VER=$(jq -r '.version' <(unzip -qc "$EXTTMP/$N.zip" "extension/package.json"))
|
|
|
|
# Calculate the SHA
|
|
|
|
SHA=$(nix-hash --flat --base32 --type sha256 "$EXTTMP/$N.zip")
|
|
|
|
|
|
|
|
# Clean up.
|
|
|
|
rm -Rf "$EXTTMP"
|
2021-01-18 17:50:26 +00:00
|
|
|
# I don't like 'rm -Rf' lurking in my scripts but this seems appropriate.
|
2018-08-01 06:54:59 +01:00
|
|
|
|
|
|
|
cat <<-EOF
|
|
|
|
{
|
|
|
|
name = "$2";
|
|
|
|
publisher = "$1";
|
|
|
|
version = "$VER";
|
|
|
|
sha256 = "$SHA";
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:50:26 +00:00
|
|
|
# See if we can find our `code` binary somewhere.
|
2018-08-01 06:54:59 +01:00
|
|
|
if [ $# -ne 0 ]; then
|
|
|
|
CODE=$1
|
|
|
|
else
|
2021-07-16 09:36:35 +01:00
|
|
|
CODE=$(command -v code || command -v codium)
|
2018-08-01 06:54:59 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$CODE" ]; then
|
|
|
|
# Not much point continuing.
|
|
|
|
fail "VSCode executable not found"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Try to be a good citizen and clean up after ourselves if we're killed.
|
|
|
|
trap clean_up SIGINT
|
|
|
|
|
|
|
|
# Begin the printing of the nix expression that will house the list of extensions.
|
|
|
|
printf '{ extensions = [\n'
|
|
|
|
|
|
|
|
# Note that we are only looking to update extensions that are already installed.
|
|
|
|
for i in $($CODE --list-extensions)
|
|
|
|
do
|
|
|
|
OWNER=$(echo "$i" | cut -d. -f1)
|
|
|
|
EXT=$(echo "$i" | cut -d. -f2)
|
|
|
|
|
|
|
|
get_vsixpkg "$OWNER" "$EXT"
|
|
|
|
done
|
|
|
|
# Close off the nix expression.
|
|
|
|
printf '];\n}'
|