Skip to content

Instantly share code, notes, and snippets.

@srghma
Created November 3, 2024 11:01
Show Gist options
  • Save srghma/1a53015ed2c26f725119b1c9dc43a3ab to your computer and use it in GitHub Desktop.
Save srghma/1a53015ed2c26f725119b1c9dc43a3ab to your computer and use it in GitHub Desktop.
zed editor install extension locally script
#!/usr/bin/env bash
set -euo pipefail
# Configuration
EXTENSION_DIR="/home/srghma/projects/zed/extensions/idris2"
INSTALL_DIR="$HOME/.local/share/zed/extensions/installed/idris2"
CACHE_DIR="$HOME/.cache/zed/extensions"
CLANG_PATH="$HOME/.cache/zed/wasi-sdk/wasi-sdk-21.0/bin/clang" # Add clang path
# Ensure cache directory exists
mkdir -p "$CACHE_DIR"
# Compile the Rust extension
echo "Compiling Rust extension..."
cd "$EXTENSION_DIR"
cargo build --target "wasm32-wasip1" --release --target-dir "$EXTENSION_DIR/target" --manifest-path "$EXTENSION_DIR/Cargo.toml"
# Create installation directory structure
echo "Creating installation directory structure..."
rm -rf "$INSTALL_DIR"
mkdir -p "$INSTALL_DIR/languages/idris2"
mkdir -p "$INSTALL_DIR/languages/ipkg"
mkdir -p "$INSTALL_DIR/grammars"
# Copy language configurations
echo "Copying language configurations..."
cp -r "$EXTENSION_DIR/languages/idris2/"*.scm "$INSTALL_DIR/languages/idris2/"
cp "$EXTENSION_DIR/languages/idris2/config.toml" "$INSTALL_DIR/languages/idris2/"
cp -r "$EXTENSION_DIR/languages/ipkg/"*.scm "$INSTALL_DIR/languages/ipkg/"
cp "$EXTENSION_DIR/languages/ipkg/config.toml" "$INSTALL_DIR/languages/ipkg/"
# Copy extension files
echo "Copying extension files..."
cp "$EXTENSION_DIR/extension.wasm" "$INSTALL_DIR/extension.wasm"
cp "$EXTENSION_DIR/extension.toml" "$INSTALL_DIR/"
# Function to compile a grammar
compile_grammar() {
local grammar_name=$1
local repo_path=$2
echo "Compiling grammar: $grammar_name from $repo_path"
# Setup paths
local grammar_dir="$CACHE_DIR/grammars/$grammar_name"
local grammar_wasm_path="$INSTALL_DIR/grammars/$grammar_name.wasm"
# Create grammar directory
mkdir -p "$grammar_dir"
# Check if repository exists and is up to date
if [ ! -d "$repo_path/.git" ]; then
echo "Error: $repo_path is not a git repository"
exit 1
fi
# Compile the grammar
local src_path="$repo_path/src"
local parser_path="$src_path/parser.c"
local scanner_path="$src_path/scanner.c"
if [ ! -f "$parser_path" ]; then
echo "Error: parser.c not found at $parser_path"
exit 1
fi
echo "Compiling $grammar_name grammar with clang..."
local compile_cmd=("$CLANG_PATH" "-fPIC" "-shared" "-Os")
compile_cmd+=("-Wl,--export=tree_sitter_$grammar_name")
compile_cmd+=("-o" "$grammar_wasm_path")
compile_cmd+=("-I" "$src_path" "$parser_path")
# Add scanner.c if it exists
if [ -f "$scanner_path" ]; then
compile_cmd+=("$scanner_path")
fi
# Execute compilation
if ! "${compile_cmd[@]}"; then
echo "Error: Failed to compile $grammar_name grammar"
# Define compile_cmd as an array
local compile_cmd=("$CLANG_PATH" "-fPIC" "-shared" "-Os")
compile_cmd+=("-o" "$grammar_wasm_path")
compile_cmd+=("-I" "$src_path" "$parser_path")
# Add scanner.c if it exists
if [ -f "$scanner_path" ]; then
compile_cmd+=("$scanner_path")
fi
# Execute compilation
if ! "${compile_cmd[@]}"; then
echo "Error: Failed to compile $grammar_name grammar"
exit 1
fi
fi
echo "Successfully compiled $grammar_name grammar"
}
# Compile grammars
compile_grammar "idris2" "/home/srghma/projects/tree-sitter-idris"
compile_grammar "ipkg" "/home/srghma/projects/tree-sitter-ipkg"
echo "Installation completed at $INSTALL_DIR"
echo "Please restart Zed to load the extension"
#!/usr/bin/env bash
# Strict mode
set -euo pipefail
IFS=$'\n\t'
# Configuration
WASI_SDK_URL="https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-21"
# Determine OS-specific asset name
get_asset_name() {
local os_type
os_type="$(uname -s)"
case "${os_type}" in
"Darwin")
echo "wasi-sdk-21.0-macos.tar.gz"
;;
"Linux")
echo "wasi-sdk-21.0-linux.tar.gz"
;;
"MINGW"*|"MSYS"*|"CYGWIN"*)
echo "wasi-sdk-21.0-mingw.tar.gz" # Fixed typo in mingw filename
;;
*)
echo "Error: wasi-sdk is not available for platform ${os_type}" >&2
exit 1
;;
esac
}
# Get the executable suffix based on OS
get_exe_suffix() {
case "${OSTYPE}" in
msys*|cygwin*|win32*)
echo ".exe"
;;
*)
echo ""
;;
esac
}
install_wasi_sdk() {
local cache_dir="${1:-${HOME}/.cache/zed}"
local wasi_sdk_dir="${cache_dir}/wasi-sdk"
local asset_name
asset_name="$(get_asset_name)"
local exe_suffix
exe_suffix="$(get_exe_suffix)"
local clang_path="${wasi_sdk_dir}/wasi-sdk-21.0/bin/clang${exe_suffix}"
local download_url="${WASI_SDK_URL}/${asset_name}"
local tar_out_dir="${wasi_sdk_dir}.archive"
# Create cache directory if it doesn't exist
mkdir -p "${cache_dir}"
# Check if already installed
if [[ -f "${clang_path}" ]] && [[ -x "${clang_path}" ]]; then
echo "WASI SDK already installed at ${clang_path}"
echo "${clang_path}"
return 0
fi
# Clean up any existing installations
rm -rf "${wasi_sdk_dir}" "${tar_out_dir}"
mkdir -p "${tar_out_dir}"
echo "Downloading WASI SDK from ${download_url}"
echo "Installing to ${wasi_sdk_dir}"
# Download and extract
local temp_file
temp_file="$(mktemp)"
trap 'rm -f "${temp_file}"' EXIT
if command -v curl >/dev/null 2>&1; then
if ! curl -fsSL "${download_url}" -o "${temp_file}"; then
echo "Error: Failed to download WASI SDK" >&2
exit 1
fi
elif command -v wget >/dev/null 2>&1; then
if ! wget -q "${download_url}" -O "${temp_file}"; then
echo "Error: Failed to download WASI SDK" >&2
exit 1
fi
else
echo "Error: Neither curl nor wget is available" >&2
exit 1
fi
# Extract archive
echo "Extracting WASI SDK..."
if ! tar xzf "${temp_file}" -C "${tar_out_dir}"; then
echo "Error: Failed to extract WASI SDK archive" >&2
exit 1
fi
# Find and move the extracted directory
local extracted_dir
extracted_dir="$(find "${tar_out_dir}" -maxdepth 1 -name "wasi-sdk*" -type d | head -n 1)"
if [[ -z "${extracted_dir}" ]]; then
echo "Error: Could not find extracted WASI SDK directory" >&2
exit 1
fi
# Move to final location
if ! mv "${extracted_dir}" "${wasi_sdk_dir}"; then
echo "Error: Failed to move WASI SDK to final location" >&2
exit 1
fi
# Clean up
rm -rf "${tar_out_dir}"
# Verify installation
if [[ ! -f "${clang_path}" ]]; then
echo "Error: Installation failed - clang not found at ${clang_path}" >&2
exit 1
fi
if [[ ! -x "${clang_path}" ]]; then
chmod +x "${clang_path}"
fi
echo "Successfully installed WASI SDK"
echo "${clang_path}"
}
# Run the installation if script is executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
install_wasi_sdk "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment