Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions ...scode/extensions/update_installed_exts.sh → .../extensions/get_updates_installed_exts.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# shellcheck shell=bash
set -eu -o pipefail

# pass <vscode exec or -> <format> to specify format as either json or nix (default nix)
# note that json format is NOT a json array, but a sequence of json objects

# can be added to your configuration with the following command and snippet:
# $ ./pkgs/applications/editors/vscode/extensions/update_installed_exts.sh > extensions.nix
#
Expand Down Expand Up @@ -40,7 +43,15 @@ function get_vsixpkg() {
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 --retry 3 --fail -X GET -o "$EXTTMP/$N.zip" "$URL"
curl --silent --show-error --retry 3 --fail -X GET -o "$EXTTMP/$N.zip" "$URL" || {
if (($? > 128))
then exit $?
fi
cat >&2 <<EOF
unable to download $N
EOF
return
}
# 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 hash
Expand All @@ -50,33 +61,49 @@ function get_vsixpkg() {
rm -Rf "$EXTTMP"
# I don't like 'rm -Rf' lurking in my scripts but this seems appropriate.

cat <<-EOF
case $FORMAT in
nix)
cat <<-EOF
{
name = "$2";
publisher = "$1";
version = "$VER";
hash = "$HASH";
}
EOF
;;
json)
printf '{"name": "%s", "publisher": "%s", "version": "%s", "hash": "%s"}\n' "$2" "$1" "$VER" "$HASH"
;;
esac
}

# See if we can find our `code` binary somewhere.
if [ $# -ne 0 ]; then
if [ $# -ne 0 ] && [ $1 != "-" ]; then
CODE=$1
else
CODE=$(command -v code || command -v codium)
fi

if [ $# -ge 2 ]; then
FORMAT=$2
else
FORMAT=nix
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
trap clean_up EXIT

# Begin the printing of the nix expression that will house the list of extensions.
printf '{ extensions = [\n'
if [ "$FORMAT" = nix ]
then
printf '{ extensions = [\n'
fi

# Note that we are only looking to update extensions that are already installed.
for i in $($CODE --list-extensions)
Expand All @@ -87,4 +114,7 @@ do
get_vsixpkg "$OWNER" "$EXT"
done
# Close off the nix expression.
printf '];\n}'
if [ "$FORMAT" = nix ]
then
printf '];\n}'
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# I am not putting a nix-shell shebang here because I can't be bothered to figure out how to put a Python package into it

from json import loads
from sys import stderr
from regex import MULTILINE, subn
import fileinput

if __name__ == "__main__":
with open("./default.nix", "r") as file:
contents = file.read()

for line in fileinput.input(encoding="utf-8"):
match loads(line):
case {"name": name, "publisher": publisher, "version": version, "hash": hash}:
pass # only for assignment
case _:
raise RuntimeError

pattern = fr"""(?<="?{publisher}"?\."?{name}"?\s*=\s*buildVscodeMarketplaceExtension\s*{{\s*mktplcRef\s*=\s*){{[^{{}}]*}}(?=;)"""

replacement = f"""{{
publisher = "{publisher}";
name = "{name}";
version = "{version}";
hash = "{hash}";
}}""" # indentation here is somewhat important!

contents, subcount = subn(pattern, replacement, contents, MULTILINE)

assert subcount <= 1
if subcount == 0:
print(f"failed to find match for {line}", file=stderr)

with open("./default.nix", "w") as file:
file.write(contents)