chore(cursor): improve setup script

This commit is contained in:
2025-02-19 19:29:34 +00:00
parent 03eb561f1f
commit 65bee1192e
2 changed files with 106 additions and 60 deletions

View File

@@ -1,44 +1,73 @@
#! /usr/bin/env bash
# Define source directory
# ==============================================================================
# Settings
# ==============================================================================
# List of config files to symlink from current directory.
CONFIG_SOURCES=(
"settings.json"
"keybindings.json"
"snippets"
)
# Detect current script directory.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
_CURSOR_CONFIG_DIR=""
# Determine OS and set config directory
cursor_config_dir() {
if [[ -n "${_CURSOR_CONFIG_DIR}" ]]; then
echo "${_CURSOR_CONFIG_DIR}"
return
fi
# ==============================================================================
# Help
# ==============================================================================
show_help() {
cat <<EOF
Usage: $(basename "$0") COMMAND
Commands:
config, conf Create symlinks for Cursor config files
dump-extensions, dump Export installed Cursor extensions to extensions.txt
extensions, ext Install Cursor extensions from extensions.txt
Description:
This script manages Cursor editor configuration files and extensions.
It can create symlinks for settings, keybindings, and snippets,
as well as backup and restore extensions.
EOF
}
# ==============================================================================
# Functions
# ==============================================================================
# Determine Cursor config directory.
cursor_config_dir() {
case "$(uname -s)" in
"Darwin")
_CURSOR_CONFIG_DIR="${HOME}/Library/Application Support/Cursor/User"
echo "${HOME}/Library/Application Support/Cursor/User"
;;
"Linux")
_CURSOR_CONFIG_DIR="${HOME}/.config/Cursor/User"
echo "${HOME}/.config/Cursor/User"
;;
*)
echo "Error: Unsupported operating system"
exit 1
;;
esac
echo "${_CURSOR_CONFIG_DIR}"
}
# Function to backup and symlink
# Backup and symlink
backup_and_link() {
local source="$1"
local target="$2"
local real_target
local real_source
# Check if target already exists
if [[ -e "${target}" ]]; then
# If it's a symlink, check if it points to the same location
if [[ -L "${target}" ]]; then
local current_target
current_target="$(readlink "${target}")"
if [[ "${current_target}" == "${source}" ]]; then
real_target="$(readlink -f "${target}")"
real_source="$(readlink -f "${source}")"
if [[ "${real_target}" == "${real_source}" ]]; then
echo "Skipping ${target} - already linked to ${source}"
return
fi
@@ -53,48 +82,62 @@ backup_and_link() {
ln -s "${source}" "${target}"
}
# Function to create symlinks
# Create symlinks
do_symlink() {
# Create Cursor config directory if it doesn't exist
local config_dir
config_dir="$(cursor_config_dir)"
mkdir -p "${config_dir}"
# Backup and symlink settings.json
backup_and_link "${SCRIPT_DIR}/settings.json" "${config_dir}/settings.json"
# Backup and symlink keybindings.json
backup_and_link "${SCRIPT_DIR}/keybindings.json" "${config_dir}/keybindings.json"
# Backup and symlink snippets directory
backup_and_link "${SCRIPT_DIR}/snippets" "${config_dir}/snippets"
for path in "${CONFIG_SOURCES[@]}"; do
backup_and_link "${SCRIPT_DIR}/${path}" "${config_dir}/${path}"
done
echo "Symlink setup complete!"
}
# Replace do_dump_extensions function:
do_dump_extensions() {
if ! command -v cursor >/dev/null 2>&1; then
echo "Error: cursor command not found"
# Find the cursor CLI command
find_cursor_cmd() {
local cursor_cmd=""
# Check for cursor CLI in multiple possible locations
for cmd in "cursor" "/Applications/Cursor.app/Contents/Resources/app/bin/cursor" "${HOME}/Applications/Cursor.app/Contents/Resources/app/bin/cursor"; do
if command -v "${cmd}" >/dev/null 2>&1; then
cursor_cmd="${cmd}"
break
fi
done
if [[ -z "${cursor_cmd}" ]]; then
echo "Error: cursor command not found" >&2
exit 1
fi
echo "${cursor_cmd}"
}
# Dump installed extensions to extensions.txt
do_dump_extensions() {
local cursor_cmd
cursor_cmd="$(find_cursor_cmd)"
local extensions_file="${SCRIPT_DIR}/extensions.txt"
local current_date
current_date="$(date)"
{
echo "# Cursor Extensions"
echo "# Generated on ${current_date}"
echo
cursor --list-extensions
"${cursor_cmd}" --list-extensions
} >"${extensions_file}"
echo "Extensions list dumped to ${extensions_file}"
}
# Function to install extensions
# Install extensions from extensions.txt
do_install_extensions() {
local cursor_cmd
cursor_cmd="$(find_cursor_cmd)"
local extensions_file="${SCRIPT_DIR}/extensions.txt"
if [[ ! -f "${extensions_file}" ]]; then
@@ -104,39 +147,42 @@ do_install_extensions() {
# Read extensions file, skip comments and empty lines
while IFS= read -r line; do
if [[ -n "${line}" && ! "${line}" =~ ^# ]]; then
if [[ -n "${line}" && ! "${line}" =~ ^[[:space:]]*# ]]; then
echo "Installing extension: ${line}"
cursor --install-extension "${line}"
"${cursor_cmd}" --install-extension "${line}"
fi
done <"${extensions_file}"
echo "Extensions installation complete!"
}
# Help message function
show_help() {
echo "Available commands: symlink (or link), dump-extensions, install-extensions"
# ==============================================================================
# Main
# ==============================================================================
main() {
case "${1:-}" in
"config" | "conf")
do_symlink
;;
"dump-extensions" | "dump")
do_dump_extensions
;;
"extensions" | "ext")
do_install_extensions
;;
"")
echo "Error: No command provided"
show_help
exit 1
;;
*)
echo "Error: Unknown command '$1'"
show_help
exit 1
;;
esac
}
# Main command handler
case "${1:-}" in
"symlink" | "link")
do_symlink
;;
"dump-extensions" | "dump")
do_dump_extensions
;;
"install-extensions" | "install")
do_install_extensions
;;
"")
echo "Error: No command provided"
show_help
exit 1
;;
*)
echo "Error: Unknown command '$1'"
show_help
exit 1
;;
esac
# Run main function.
main "$@"