Skip to content

Commit

Permalink
Add scripts and instructions for switching between tree-sitter versions
Browse files Browse the repository at this point in the history
  • Loading branch information
mjambon committed Sep 11, 2024
1 parent d70955e commit 0b6cf11
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 24 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ _build
/tree-sitter-config.mk
/tree-sitter-config.sh

# The installation root of tree-sitter
# The default installation root of tree-sitter
/tree-sitter
/tree-sitter-*.*.*

# The file containing the tree-sitter version being used
/tree-sitter-version
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ build:
#
.PHONY: setup
setup:
test -f tree-sitter-version \
|| cp tree-sitter-version.default tree-sitter-version
./scripts/check-prerequisites
./scripts/install-tree-sitter-cli
./scripts/install-tree-sitter-lib
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ $ make setup
$ ./scripts/rebuild-everything # needs root access to install libtree-sitter
```

### tree-sitter version

The default tree-sitter version to use is in the
`tree-sitter-version.default` file.

Under the default configuration used for local development purposes,
the version being actually used is stored in the file
`tree-sitter-version`. This can be changed by invoking
`./scripts/switch-tree-sitter-version` before `make setup`.
We made this available to facilitate the transition from tree-sitter 0.20.6 to
0.22.6 in ocaml-tree-sitter-semgrep where the integration of some
grammars needs to be updated. The latest version of these grammars are
compatible with 0.22.6 but their OCaml integration in Semgrep needs work.

Documentation
--

Expand Down
25 changes: 2 additions & 23 deletions scripts/download-tree-sitter
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ set -eu -o pipefail

prog_name=$(basename "$0")

# The official version of tree-sitter we use for the code generator and
# for the runtime library. Please try to keep this as the single source
# of truth.
default_version="0.22.6"

error() {
echo "Current directory: $(pwd)" >&2
echo "Error: $@" >&2
Expand All @@ -27,16 +22,10 @@ Options:
exists locally for the specified version.
--help
Show this message and exit.
--version VERSION
Install this specific version of tree-sitter. VERSION must be a git tag
or a branch name in the original tree-sitter repository.
See valid tags at https://github.com/tree-sitter/tree-sitter/tags
Current default: $default_version
EOF
}

lazy=false
version="$default_version"
while [[ $# -gt 0 ]]; do
case "$1" in
--lazy)
Expand All @@ -46,16 +35,14 @@ while [[ $# -gt 0 ]]; do
usage
exit 0
;;
--version)
version="$2"
shift
;;
*)
error "Invalid argument passed to '${prog_name}': '$1'"
esac
shift
done

version=$(cat tree-sitter-version)

mkdir -p downloads
(
cd downloads
Expand All @@ -67,14 +54,6 @@ mkdir -p downloads
rm -rf "$src_dir"
fi

unversioned_src_dir=tree-sitter
cat <<EOF
Creating version-independent symlink for homebrew packager:
$(pwd)/$unversioned_src_dir
EOF
rm -f "$unversioned_src_dir"
ln -s "$src_dir" "$unversioned_src_dir"

if [[ -d "$src_dir" ]]; then
cat <<EOF
Re-using tree-sitter sources found locally:
Expand Down
1 change: 1 addition & 0 deletions scripts/install-tree-sitter-cli
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ done
bindir="$prefix"/bin

dir_name=$(dirname "$BASH_SOURCE")
"$dir_name"/update-version-symlinks
"$dir_name"/download-tree-sitter --lazy

(
Expand Down
1 change: 1 addition & 0 deletions scripts/install-tree-sitter-lib
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ done
libdir="$prefix"/lib

dir_name=$(dirname "$BASH_SOURCE")
"$dir_name"/update-version-symlinks
"$dir_name"/download-tree-sitter --lazy

(
Expand Down
35 changes: 35 additions & 0 deletions scripts/switch-tree-sitter-version
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#! /usr/bin/env bash
#
# Change the tree-sitter version used for local development and rebuild
# what's necessary.
#
set -eu

error() {
(
echo "[$0] Error: $*"
echo "Supported versions: 0.20.6 0.22.6"
)
exit 1
}

if [[ $# -ne 1 ]]; then
error "Exactly one argument is expected, the tree-sitter version ID"
fi

version=''
case "$1" in
0.20.6)
version=0.20.6
;;
0.22.6)
version=0.22.6
;;
*)
error "Unsupported version '$1'"
esac

echo "$version" > tree-sitter-version
./scripts/update-version-symlinks

echo "You can now run 'make setup' to download and build tree-sitter $version."
41 changes: 41 additions & 0 deletions scripts/update-version-symlinks
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#! /usr/bin/env bash
#
# Run this script to update local symlinks after changing the tree-sitter
# version in the file 'tree-sitter-version'.
#
# This facilitates development that requires switching between different
# tree-sitter versions. The compiled tree-sitter libraries and binaries
# go into a versioned folder e.g. tree-sitter-0.22.6 and are therefore
# preserved when switching back and forth between versions.
#
# If the user wishes to install the tree-sitter CLI or runtime library
# elsewhere than the default 'tree-sitter/', they can do so by passing the
# '--prefix' option. In this case, the symlinks we create here become
# irrelevant.
#
set -eu

version=$(cat tree-sitter-version)

echo "Updating symlinks 'downloads/tree-sitter' and 'tree-sitter'"

(
cd downloads
rm -f tree-sitter
ln -s tree-sitter-"$version" tree-sitter
)

if [[ -d tree-sitter ]] && [[ ! -L tree-sitter ]]; then
# The issue is that we want to be able to use two different versions of
# the tree-sitter CLI. To make this convenient, the 'tree-sitter' folder
# is now a symlink to the versioned folder name.
echo "*** Your tree-sitter installation is old. Removing it."
rm -rf tree-sitter
fi

# Remove the symlink and set it to the new install folder.
# It allows us to use this script to switch tree-sitter versions without
# rebuilding everything.
rm -f tree-sitter
ln -s tree-sitter-"$version" tree-sitter
mkdir -p tree-sitter-"$version"
1 change: 1 addition & 0 deletions tree-sitter-version.default
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.22.6

0 comments on commit 0b6cf11

Please sign in to comment.