diff --git a/install.sh b/install.sh index 738b4c4..fbe08df 100755 --- a/install.sh +++ b/install.sh @@ -54,6 +54,54 @@ else warn "No .bashrc or .zshrc found — add $BIN_DIR to your PATH manually" fi +# --- TPM & Plugins --- +TPM_DIR="$HOME/.tmux/plugins/tpm" + +# Clone TPM if not present +if [[ ! -d "$TPM_DIR" ]]; then + git clone https://github.com/tmux-plugins/tpm "$TPM_DIR" 2>/dev/null + info "TPM installed to $TPM_DIR" +else + warn "TPM already installed at $TPM_DIR" +fi + +TMUX_CONF="$HOME/.tmux.conf" + +# Create .tmux.conf if it doesn't exist +if [[ ! -f "$TMUX_CONF" ]]; then + touch "$TMUX_CONF" + info "Created $TMUX_CONF" +fi + +# Patch .tmux.conf with plugin declarations +if ! grep -qF "tmux-plugins/tpm" "$TMUX_CONF" 2>/dev/null; then + # Remove any existing run tpm line to re-add at end + sed -i '/run.*tpm\/tpm/d' "$TMUX_CONF" + + # Append plugin block + cat >> "$TMUX_CONF" <<'EOF' + +# tmux plugins (added by tmux-ws install) +set -g @plugin 'tmux-plugins/tpm' +set -g @plugin 'tmux-plugins/tmux-resurrect' +set -g @plugin 'tmux-plugins/tmux-continuum' +set -g @continuum-restore 'on' +run '~/.tmux/plugins/tpm/tpm' +EOF + info "Plugin config added to $TMUX_CONF" +else + warn "Plugin config already present in $TMUX_CONF" +fi + +# Install TPM plugins +if [[ -x "$TPM_DIR/bin/install_plugins" ]]; then + tmux start-server \; set-environment -g TMUX_PLUGIN_MANAGER_PATH "$HOME/.tmux/plugins/" 2>/dev/null + "$TPM_DIR/bin/install_plugins" >/dev/null 2>&1 + info "TPM plugins installed" +else + warn "TPM installer not found — run manually: ~/.tmux/plugins/tpm/bin/install_plugins" +fi + # Install shell completions COMPLETIONS_SRC="$SCRIPT_DIR/completions" diff --git a/tests/test_tmux_ws.bats b/tests/test_tmux_ws.bats index d63431f..5030f6b 100644 --- a/tests/test_tmux_ws.bats +++ b/tests/test_tmux_ws.bats @@ -327,3 +327,59 @@ teardown() { [ "$status" -eq 0 ] [[ "${output}" == *"already exists"* ]] } + +# --- TPM Install --- + +setup_mock_bin() { + mkdir -p "$BATS_TEST_TMPDIR/fake-bin" + cat > "$BATS_TEST_TMPDIR/fake-bin/tmux" <<'EOF' +#!/bin/bash +exit 0 +EOF + chmod +x "$BATS_TEST_TMPDIR/fake-bin/tmux" + + cat > "$BATS_TEST_TMPDIR/fake-bin/git" <<'EOF' +#!/bin/bash +if [[ "$1" == "clone" ]]; then + mkdir -p "$3" + exit 0 +fi +exit 0 +EOF + chmod +x "$BATS_TEST_TMPDIR/fake-bin/git" +} + +@test "install clones TPM" { + setup_mock_bin + export HOME="$BATS_TEST_TMPDIR/home" + mkdir -p "$HOME" + run bash -c "export HOME='$HOME'; export TMUX_WS_CONFIG='$HOME/.config/tmux-ws'; PATH='$BATS_TEST_TMPDIR/fake-bin:$PATH' bash '$BATS_TEST_DIRNAME/../install.sh'" + [ "$status" -eq 0 ] + [ -d "$HOME/.tmux/plugins/tpm" ] +} + +@test "install patches .tmux.conf" { + setup_mock_bin + export HOME="$BATS_TEST_TMPDIR/home" + mkdir -p "$HOME" + run bash -c "export HOME='$HOME'; export TMUX_WS_CONFIG='$HOME/.config/tmux-ws'; PATH='$BATS_TEST_TMPDIR/fake-bin:$PATH' bash '$BATS_TEST_DIRNAME/../install.sh'" + [ "$status" -eq 0 ] + grep -qF "tmux-plugins/tpm" "$HOME/.tmux.conf" + grep -qF "tmux-resurrect" "$HOME/.tmux.conf" + grep -qF "tmux-continuum" "$HOME/.tmux.conf" + grep -qF "continuum-restore" "$HOME/.tmux.conf" +} + +@test "install is idempotent" { + setup_mock_bin + export HOME="$BATS_TEST_TMPDIR/home" + mkdir -p "$HOME" + # Run twice + bash -c "export HOME='$HOME'; export TMUX_WS_CONFIG='$HOME/.config/tmux-ws'; PATH='$BATS_TEST_TMPDIR/fake-bin:$PATH' bash '$BATS_TEST_DIRNAME/../install.sh'" >/dev/null 2>&1 + run bash -c "export HOME='$HOME'; export TMUX_WS_CONFIG='$HOME/.config/tmux-ws'; PATH='$BATS_TEST_TMPDIR/fake-bin:$PATH' bash '$BATS_TEST_DIRNAME/../install.sh'" + [ "$status" -eq 0 ] + # Count plugin declarations — should be exactly 1 + local count + count=$(grep -cF "tmux-plugins/tpm" "$HOME/.tmux.conf") + [ "$count" -eq 1 ] +}