#!/usr/bin/env bash # shellcheck disable=SC2016 set -euo pipefail treesitter_rev=$(jq -r '.["nvim-treesitter/nvim-treesitter"].rev' ../plugins/sources.json) lockfile=$(curl -L -sf "https://github.com/nvim-treesitter/nvim-treesitter/raw/$treesitter_rev/lockfile.json") grammars=$(< grammars.json) get_revision() { jq -r ".$1.revision" <<<"$lockfile" } q() { jq -r "$@" <<<"$grammars" } u() { grammars=$(jq "$@" <<<"$grammars") } update_grammar() { name=$1 repo=$(q --arg name "$name" '.[$name].repo // "tree-sitter/tree-sitter-\($name)"') echo "Updating $name" >&2 revision=$(get_revision "$name") if branch=$(q -e --arg name "$name" '.[$name].branch'); then revision=$(git ls-remote "https://github.com/$repo" "$branch" | cut -b -40) fi sha256=$(nix-prefetch-url --unpack "https://github.com/$repo/archive/$revision.tar.gz") u --arg name "$name" --arg rev "$revision" --arg sha256 "$sha256" ' .[$name] = (.[$name] // {}) * { rev: $rev, sha256: $sha256, } ' } update_all() { for grammar in $(q 'to_entries | .[] | .key'); do update_grammar "$grammar" done } if [ "$#" -eq 0 ]; then update_all else while [ "$#" -gt 0 ]; do update_grammar "$1" shift done fi echo "$grammars" > grammars.json