diff --git a/.gitignore b/.gitignore index 6fc2563..8c1d934 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Makefile b/Makefile index 3c98a4b..3299e3d 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index cd2bba0..47f10fa 100644 --- a/README.md +++ b/README.md @@ -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 -- diff --git a/scripts/download-tree-sitter b/scripts/download-tree-sitter index 6cbf816..7a8f5b7 100755 --- a/scripts/download-tree-sitter +++ b/scripts/download-tree-sitter @@ -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 @@ -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) @@ -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 @@ -67,14 +54,6 @@ mkdir -p downloads rm -rf "$src_dir" fi - unversioned_src_dir=tree-sitter - cat < tree-sitter-version +./scripts/update-version-symlinks + +echo "You can now run 'make setup' to download and build tree-sitter $version." diff --git a/scripts/update-version-symlinks b/scripts/update-version-symlinks new file mode 100755 index 0000000..e95f6b6 --- /dev/null +++ b/scripts/update-version-symlinks @@ -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" diff --git a/tree-sitter-version.default b/tree-sitter-version.default new file mode 100644 index 0000000..18fb7fe --- /dev/null +++ b/tree-sitter-version.default @@ -0,0 +1 @@ +0.22.6