mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-11-26 15:41:40 +00:00
9321785bab
Added better practices to update_exts script. Use `jq` instead of `grep` for more reliable JSON querying. Check for 404 when requesting package.json information to avoid mangled output. Added proper failure points for missing vscode package, unknown version, and if the code executable couldn't be found. Switched to using a `nix-shell` shebang for even better reliability and use the `sh` shell to be that little bit more generic. Script is still clunky and sequential, anything more and I'd need to write a proper program to do this and that's getting a bit silly? But people that have a dozen or so extensions might be in for a long wait. Be explicit about using bash Improve the use of jq to remove unnecessary use of tr. Hat-tip coretemp. Add some comments, finally. Remove the `fetch` function. Change the `get_ver` function to more accurately demonstrate what it is trying to do, as well as add in some better error handling for non-200 http responses. I couldn't make the bash `${param/search/replacement}` work for chopping up the response in the `get_ver` function, hence the use of `sed`. Hopefully it all makes a bit more sense now. Remove github requests. VSIXPackage is just a zip format in disguise so use a tmpdir and unpackage the package.json file for the file in question so we can get the precise version that we're interested in without additional redundant calls to github that may not provide the right answer anyway. Add trap to try to clean up the temp folders and clean up as we go. I can't use 'fetchurl' or even 'nix-prefetch-url' because for the former we don't yet know the hash that we're after and for the latter there isn't a way to tie the predownloaded file into the next part of the workflow. Prevent an unnecessary file from being extracted. Change the unzip command to read the file we're after to stdout so we can use jq on it directly instead of creating a file, reading it, then deleting it. Courtesy of worldofpeace, remove the dependency on coreutils and use the provided nix-hash function to generate the required hash. Fix up a comment Remove use of 'awk' and clean up individual Nix printing with cat to EOF expression.
75 lines
2 KiB
Bash
Executable file
75 lines
2 KiB
Bash
Executable file
#! /usr/bin/env nix-shell
|
|
#! nix-shell -i bash -p curl jq unzip
|
|
set -eu -o pipefail
|
|
|
|
# Helper to just fail with a message and non-zero exit code.
|
|
function fail() {
|
|
echo "$1" >&2
|
|
exit 1
|
|
}
|
|
|
|
# Helper to clean up after ourself if we're killed by SIGINT
|
|
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"
|
|
|
|
# Create a tempdir for the extension download
|
|
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"
|
|
# I don't like 'rm -Rf' lurking in my scripts but this seems appropriate
|
|
|
|
cat <<-EOF
|
|
{
|
|
name = "$2";
|
|
publisher = "$1";
|
|
version = "$VER";
|
|
sha256 = "$SHA";
|
|
}
|
|
EOF
|
|
}
|
|
|
|
# See if can find our code binary somewhere.
|
|
if [ $# -ne 0 ]; then
|
|
CODE=$1
|
|
else
|
|
CODE=$(command -v code)
|
|
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}'
|