graphene-hardened-malloc: 2024123000 -> 2025012700 (#378307)
[NixPkgs.git] / pkgs / applications / editors / vscode / extensions / update_installed_exts.sh
blob17fe106043400d884c0b2654149390eb91ccbbf6
1 #! /usr/bin/env nix-shell
2 #! nix-shell -i bash -p cacert curl jq unzip
3 # shellcheck shell=bash
4 set -eu -o pipefail
6 # can be added to your configuration with the following command and snippet:
7 # $ ./pkgs/applications/editors/vscode/extensions/update_installed_exts.sh > extensions.nix
9 # packages = with pkgs;
10 # (vscode-with-extensions.override {
11 # vscodeExtensions = map
12 # (extension: vscode-utils.buildVscodeMarketplaceExtension {
13 # mktplcRef = {
14 # inherit (extension) name publisher version sha256;
15 # };
16 # })
17 # (import ./extensions.nix).extensions;
18 # })
19 # ]
21 # Helper to just fail with a message and non-zero exit code.
22 function fail() {
23 echo "$1" >&2
24 exit 1
27 # Helper to clean up after ourselves if we're killed by SIGINT.
28 function clean_up() {
29 TDIR="${TMPDIR:-/tmp}"
30 echo "Script killed, cleaning up tmpdirs: $TDIR/vscode_exts_*" >&2
31 rm -Rf "$TDIR/vscode_exts_*"
34 function get_vsixpkg() {
35 N="$1.$2"
37 # Create a tempdir for the extension download.
38 EXTTMP=$(mktemp -d -t vscode_exts_XXXXXXXX)
40 URL="https://$1.gallery.vsassets.io/_apis/public/gallery/publisher/$1/extension/$2/latest/assetbyname/Microsoft.VisualStudio.Services.VSIXPackage"
42 # Quietly but delicately curl down the file, blowing up at the first sign of trouble.
43 curl --silent --show-error --retry 3 --fail -X GET -o "$EXTTMP/$N.zip" "$URL"
44 # Unpack the file we need to stdout then pull out the version
45 VER=$(jq -r '.version' <(unzip -qc "$EXTTMP/$N.zip" "extension/package.json"))
46 # Calculate the SHA
47 SHA=$(nix-hash --flat --base32 --type sha256 "$EXTTMP/$N.zip")
49 # Clean up.
50 rm -Rf "$EXTTMP"
51 # I don't like 'rm -Rf' lurking in my scripts but this seems appropriate.
53 cat <<-EOF
55 name = "$2";
56 publisher = "$1";
57 version = "$VER";
58 sha256 = "$SHA";
60 EOF
63 # See if we can find our `code` binary somewhere.
64 if [ $# -ne 0 ]; then
65 CODE=$1
66 else
67 CODE=$(command -v code || command -v codium)
70 if [ -z "$CODE" ]; then
71 # Not much point continuing.
72 fail "VSCode executable not found"
75 # Try to be a good citizen and clean up after ourselves if we're killed.
76 trap clean_up SIGINT
78 # Begin the printing of the nix expression that will house the list of extensions.
79 printf '{ extensions = [\n'
81 # Note that we are only looking to update extensions that are already installed.
82 for i in $($CODE --list-extensions)
84 OWNER=$(echo "$i" | cut -d. -f1)
85 EXT=$(echo "$i" | cut -d. -f2)
87 get_vsixpkg "$OWNER" "$EXT"
88 done
89 # Close off the nix expression.
90 printf '];\n}'