update-treesitter.sh 969 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env bash
  2. # shellcheck disable=SC2016
  3. set -euo pipefail
  4. grammars=$(< grammars.json)
  5. q() {
  6. jq -r "$@" <<<"$grammars"
  7. }
  8. u() {
  9. grammars=$(jq "$@" <<<"$grammars")
  10. }
  11. update_grammar() {
  12. name=$1
  13. repo=$(q --arg name "$name" '.[$name].repo // "tree-sitter/tree-sitter-\($name)"')
  14. branch=$(q -e --arg name "$name" '.[$name].branch // "HEAD"')
  15. echo "Updating $name" >&2
  16. revision=$(git ls-remote "https://github.com/$repo" "$branch" | cut -b -40)
  17. sha256=$(nix-prefetch-url --unpack "https://github.com/$repo/archive/$revision.tar.gz")
  18. u --arg name "$name" --arg rev "$revision" --arg sha256 "$sha256" '
  19. .[$name] = (.[$name] // {}) * {
  20. rev: $rev,
  21. sha256: $sha256,
  22. }
  23. '
  24. }
  25. update_all() {
  26. for grammar in $(q 'to_entries | .[] | .key'); do
  27. update_grammar "$grammar"
  28. done
  29. }
  30. if [ "$#" -eq 0 ]; then
  31. update_all
  32. else
  33. while [ "$#" -gt 0 ]; do
  34. update_grammar "$1"
  35. shift
  36. done
  37. fi
  38. echo "$grammars" > grammars.json