99# - Devcontainer: Can run as root (apt-install feature) or non-root (postCreateCommand)
1010# - Auto-detection: Checks if running as root (id -u = 0), uses sudo if non-root
1111#
12- # This script installs ALL tree-sitter grammars for integration testing
12+ # Grammar building is delegated to tsdl (https://github.com/stackmystack/tsdl).
13+ # Configure grammars and versions in parsers.toml at the project root.
1314#
1415# Options:
1516# --sudo: Force use of sudo (optional, auto-detected by default)
1617# --cli: Install tree-sitter-cli via npm (optional)
1718# --build: Build and install the tree-sitter C runtime from source when distro packages are missing (optional)
18- # --workspace PATH: Workspace root path for informational/debugging purposes only (defaults to /workspaces/tree_haver)
19+ # --tsdl-version VERSION: Pin tsdl release version (default: v2.0.0)
20+ # --workspace PATH: Workspace root path for informational/debugging purposes only
1921
2022SUDO=" "
2123INSTALL_CLI=false
2224BUILD_FROM_SOURCE=false
25+ TSDL_VERSION=" v2.0.0"
2326WORKSPACE_ROOT=" /workspaces/${PWD##*/ } "
2427
2528# Parse arguments properly using while loop
2629while [[ $# -gt 0 ]]; do
2730 case $1 in
28- --sudo)
29- SUDO=" sudo"
30- shift
31- ;;
32- --cli)
33- INSTALL_CLI=true
34- shift
35- ;;
36- --build)
37- BUILD_FROM_SOURCE=true
38- shift
39- ;;
40- --workspace)
41- WORKSPACE_ROOT=" $2 "
42- shift 2
43- ;;
44- --workspace=* )
45- WORKSPACE_ROOT=" ${1#* =} "
46- shift
47- ;;
48- * )
49- echo " Unknown option: $1 " >&2
50- shift
51- ;;
31+ --sudo) SUDO=" sudo" ; shift ;;
32+ --cli) INSTALL_CLI=true; shift ;;
33+ --build) BUILD_FROM_SOURCE=true; shift ;;
34+ --tsdl-version) TSDL_VERSION=" $2 " ; shift 2 ;;
35+ --tsdl-version=* ) TSDL_VERSION=" ${1#* =} " ; shift ;;
36+ --workspace) WORKSPACE_ROOT=" $2 " ; shift 2 ;;
37+ --workspace=* ) WORKSPACE_ROOT=" ${1#* =} " ; shift ;;
38+ * ) echo " Unknown option: $1 " >&2 ; shift ;;
5239 esac
5340done
5441
@@ -62,6 +49,7 @@ echo " Workspace root: $WORKSPACE_ROOT (informational only)"
6249echo " Using sudo: $( [ -n " $SUDO " ] && echo " yes" || echo " no" ) "
6350echo " Install CLI: $INSTALL_CLI "
6451echo " Build from source: $BUILD_FROM_SOURCE "
52+ echo " tsdl version: $TSDL_VERSION "
6553echo " "
6654
6755have_cmd () { command -v " $1 " > /dev/null 2>&1 ; }
@@ -74,29 +62,67 @@ have_tree_sitter() {
7462}
7563
7664install_tree_sitter_from_source () {
77- echo " [ubuntu] Attempting to build and install tree-sitter from source..."
65+ echo " [tree-sitter] Building runtime from source..."
7866 tmpdir=$( mktemp -d /tmp/tree-sitter-src-XXXX)
7967 trap ' rm -rf "$tmpdir"' EXIT
8068 git clone --depth 1 https://github.com/tree-sitter/tree-sitter.git " $tmpdir " || return 1
8169 pushd " $tmpdir " > /dev/null || return 1
8270 if ! make; then
83- echo " [ubuntu ] ERROR: 'make' failed while building tree-sitter " >&2
71+ echo " [tree-sitter ] ERROR: 'make' failed" >&2
8472 popd > /dev/null
8573 return 1
8674 fi
87-
8875 $SUDO mkdir -p /usr/local/include/tree-sitter
8976 $SUDO cp -r lib/include/* /usr/local/include/tree-sitter/ || true
9077 $SUDO cp -a lib/libtree-sitter.* /usr/local/lib/ 2> /dev/null || true
91- if have_cmd ldconfig; then
92- $SUDO ldconfig || true
93- fi
94-
78+ have_cmd ldconfig && $SUDO ldconfig || true
9579 popd > /dev/null
96- echo " [ubuntu] tree-sitter built and installed to /usr/local (headers + libs) ."
80+ echo " [tree-sitter] Runtime installed to /usr/local."
9781 return 0
9882}
9983
84+ install_tsdl () {
85+ if have_cmd tsdl; then
86+ echo " [tsdl] Already installed: $( tsdl --version) "
87+ return 0
88+ fi
89+
90+ echo " [tsdl] Installing tsdl ${TSDL_VERSION} ..."
91+ local arch
92+ arch=" $( uname -m) "
93+ case " $arch " in
94+ x86_64) arch=" x64" ;;
95+ aarch64) arch=" arm64" ;;
96+ armv7l) arch=" arm" ;;
97+ i686) arch=" x86" ;;
98+ * ) echo " [tsdl] ERROR: Unsupported architecture: $arch " >&2 ; return 1 ;;
99+ esac
100+
101+ local os
102+ os=" $( uname -s | tr ' [:upper:]' ' [:lower:]' ) "
103+ case " $os " in
104+ linux) os=" linux" ;;
105+ darwin) os=" macos" ;;
106+ * ) echo " [tsdl] ERROR: Unsupported OS: $os " >&2 ; return 1 ;;
107+ esac
108+
109+ local url=" https://github.com/stackmystack/tsdl/releases/download/${TSDL_VERSION} /tsdl-${os} -${arch} .gz"
110+ local tmpbin
111+ tmpbin=$( mktemp /tmp/tsdl-XXXX)
112+
113+ if ! wget -q " $url " -O " ${tmpbin} .gz" ; then
114+ echo " [tsdl] ERROR: Failed to download from $url " >&2
115+ return 1
116+ fi
117+ gunzip -f " ${tmpbin} .gz"
118+ chmod +x " $tmpbin "
119+ $SUDO mv " $tmpbin " /usr/local/bin/tsdl
120+ echo " [tsdl] Installed: $( tsdl --version) "
121+ }
122+
123+ # --- 1. System dependencies ---
124+ echo " Installing system dependencies..."
125+
100126echo " Installing tree-sitter system library and dependencies..."
101127$SUDO apt-get update -y
102128# libtree-sitter-dev is optional when building from source via --build
@@ -117,39 +143,40 @@ if ! $SUDO apt-get install -y \
117143 libcurl4-openssl-dev \
118144 software-properties-common \
119145 libffi-dev; then
120- echo " ERROR: apt-get failed to install required packages."
121- echo " Please check your network, package sources, and re-run this script."
146+ echo " ERROR: apt-get failed to install required packages." >&2
122147 exit 1
123148fi
124149
125- # If the user requested a source-build, skip installing libtree-sitter-dev
150+ # --- 2. Tree-sitter runtime ---
126151if [ " $BUILD_FROM_SOURCE " = true ]; then
127- echo " [ubuntu ] --build specified; skipping distro package 'libtree-sitter-dev' and building tree-sitter from source."
152+ echo " [tree-sitter ] --build specified; building runtime from source."
128153fi
129154
130155# Ensure tree-sitter is available; if not, attempt to build from source
131156if ! have_tree_sitter; then
132157 if [ " $BUILD_FROM_SOURCE " = true ]; then
133- echo " [ubuntu] tree-sitter not found in system paths; attempting to build from source as requested (--build)."
134158 if ! install_tree_sitter_from_source; then
135- echo " [ubuntu ] ERROR: Failed to provide tree-sitter runtime/library . Aborting." >&2
159+ echo " [tree-sitter ] ERROR: Failed to build runtime. Aborting." >&2
136160 exit 1
137161 fi
138162 else
139- echo " [ubuntu ] ERROR: tree-sitter runtime (headers/libs) not found."
140- echo " Install the appropriate distro package (e.g., libtree-sitter-dev) or re-run this script with --build to compile from source. "
163+ echo " [tree-sitter ] ERROR: Runtime (headers/libs) not found." >&2
164+ echo " Install libtree-sitter-dev or re-run with --build. " >&2
141165 exit 1
142166 fi
143167fi
144168
145- # Install tree-sitter CLI via npm (optional)
169+ # --- 3. tree-sitter-cli (optional) ---
146170if [ " $INSTALL_CLI " = true ]; then
147171 echo " Installing tree-sitter-cli via npm..."
148172 $SUDO npm install -g tree-sitter-cli
149173else
150- echo " Skipping tree-sitter-cli installation (use --cli flag to install)"
174+ echo " Skipping tree-sitter-cli (use --cli to install)"
151175fi
152176
177+ # --- 4. Install tsdl and build grammars ---
178+ install_tsdl
179+
153180# Install all tree-sitter grammars for integration testing
154181GRAMMARS=(" toml" " json" " jsonc" " bash" )
155182
@@ -209,7 +236,20 @@ if ! $SUDO ldconfig; then
209236fi
210237
211238echo " "
239+ echo " Building tree-sitter grammars via tsdl..."
240+ # Use parsers.toml from the project root if it exists, otherwise build defaults.
241+ # tsdl automatically reads parsers.toml in the current directory.
242+ if [ -f parsers.toml ]; then
243+ echo " [tsdl] Using parsers.toml config"
244+ $SUDO tsdl build --out-dir /usr/local/lib --progress plain
245+ else
246+ echo " [tsdl] No parsers.toml found; building default grammars: toml json bash rbs"
247+ $SUDO tsdl build toml json bash rbs --out-dir /usr/local/lib --progress plain
248+ fi
249+
250+ $SUDO ldconfig || echo " WARNING: ldconfig failed" >&2
212251echo " tree-sitter setup complete!"
252+
213253echo " "
214254echo " Detected library paths:"
215255
@@ -225,9 +265,11 @@ elif [ -f /usr/lib/libtree-sitter.so ]; then
225265else
226266 echo " WARNING: Could not find libtree-sitter runtime library!"
227267fi
228-
229268echo " "
230269echo " Grammar libraries:"
231270for grammar in " ${GRAMMARS[@]} " ; do
232271 echo " TREE_SITTER_${grammar^^} _PATH=/usr/local/lib/libtree-sitter-${grammar} .so"
233272done
273+ for lib in /usr/local/lib/libtree-sitter-* .so; do
274+ [ -f " $lib " ] && echo " $lib "
275+ done
0 commit comments