devs/bin/claude_install.sh

160 lines
5.1 KiB
Bash
Executable file

#!/bin/bash
#Script : claude_install.sh
#Apps : MRDEVS TOOLS
#Description : Instalar o desinstalar Anthropoc Claude Code con npm
#Author : Mauro Rosero Pérez
#Company Email : mauro@rosero.one
#Personal Email : mauro.rosero@gmail.com
#Created : 2024/12/01 15:27:00
#Modified : 2025/03/19 11:57:08
#Version : 1.2.0
#Use Notes :
# ./claude_install.sh -> Instalación
# ./claude_install -u -> Desinstalación
#==============================================================================
# Derechos de Autor [2025] [Mauro Rosero P. <mauro@rosero.one>]
#==============================================================================
# Este programa es software libre: usted puede redistribuirlo y/o modificarlo
# bajo los términos de la Licencia Pública Affero General de GNU tal como
# lo publica la Free Software Foundation, ya sea la versión 3 de la licencia,
# o (a su elección) cualquier versión posterior.
#
# Este programa se distribuye con la esperanza de que sea útil,
# pero SIN NINGUNA GARANTÍA; sin siquiera la garantía implícita de
# COMERCIABILIDAD o IDONEIDAD PARA UN PROPÓSITO PARTICULAR. Consulte la
# Licencia Pública Affero General de GNU para obtener más detalles.
#
# Debería haber recibido una copia de la Licencia Pública Affero General
# junto con este programa. Si no la recibió, consulte <https://www.gnu.org/licenses/>.
# Configuración inicial
# Usar DEVELOPER_DIR de base.lib
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BIN_BASE="bin"
BIN_LIBS="lib"
BIN_MESG="msg"
BIN_CFGS="config"
# Leer DEVSPATH desde el archivo de configuración o usar "devs" por defecto
if [ -f "$SCRIPT_DIR/config/devspath.dat" ]; then
DEVSPATH=$(cat "$SCRIPT_DIR/$BIN_CFGS/devspath.dat")
else
DEVSPATH="devs"
fi
BIN_HOME="$HOME/$DEVSPATH"
VERSION=$(cat "$BIN_HOME/$BIN_BASE/$BIN_CFGS/version")
# CHECK SHELL LANGUAGE
BIN_LANG=${LANG:0:2}
# Importar bibliotecas necesarias
source "${BIN_HOME}/${BIN_BASE}/${BIN_LIBS}/base.lib"
# Cargar mensajes en el idioma del sistema o español por defecto
load_messages "${BIN_HOME}/${BIN_BASE}" "${BIN_MESG}" "${BIN_LANG}" "head"
title="${head_000} ${head_002}"
# Verificar que 'dialog' esté instalado
if ! command -v dialog &>/dev/null; then
echo "${npm_001}"
exit 1
fi
# Verificar que 'npm' esté instalado
if ! command -v npm &>/dev/null; then
dialog --backtitle "${title}" --title "${head_error}" --msgbox "${npm_022}" 7 50
${BIN_HOME}/${BIN_BASE}/npm_install.sh
fi
# Función para preparar pre-requisitos para instalación de Claude Code
pre_anthropic() {
# First, save a list of your existing global packages for later migration
npm list -g --depth=0 > ~/npm-global-packages.txt
# Create a directory for your global packages
mkdir -p ~/.npm-global
# Configure npm to use the new directory path
npm config set prefix ~/.npm-global
# Determinar el shell actual y su archivo de configuración
local shell_config=""
case "$SHELL" in
*/bash)
shell_config="$HOME/.bashrc"
;;
*/zsh)
shell_config="$HOME/.zshrc"
;;
*)
# Fallback a bashrc
shell_config="$HOME/.bashrc"
;;
esac
# Verificar si la entrada PATH ya existe
if ! grep -q "export PATH=~/.npm-global/bin:\$PATH" "$shell_config"; then
# Agregar la configuración PATH al archivo de shell
echo 'export PATH=~/.npm-global/bin:$PATH' >> "$shell_config"
fi
# Exportar la variable PATH en la sesión actual
export PATH=~/.npm-global/bin:$PATH
# Informar al usuario que debe reiniciar su terminal o usar source
dialog --backtitle "${title}" --infobox "${npm_060}\n\nsource $shell_config" 8 60
sleep 4
}
# Función para instalar Anthropoc Claude Code
install_anthropic() {
dialog --backtitle "${title}" --title "${npm_018}" --msgbox "${npm_023}" 7 60
# Ejecutar la instalación
pre_anthropic
npm install -g @anthropic-ai/claude-code
if [[ $? -eq 0 ]]; then
cortana_alias
dialog --backtitle "${title}" --title "${npm_014}" --msgbox "${npm_024}" 7 50
else
dialog --backtitle "${title}" --title "${head-error}" --msgbox "${npm_025}" 7 50
fi
}
# Función para desinstalar Anthropoc Claude Code
uninstall_anthropic() {
dialog --backtitle "${title}" --title "${npm_026}" --msgbox "${npm_027}" 7 60
# Ejecutar la desinstalación
npm uninstall -g @anthropic-ai/claude-code
if [[ $? -eq 0 ]]; then
remove_cortana_alias
dialog --backtitle "${title}" --title "${npm_028}" --msgbox "${npm_029}" 7 50
else
dialog --backtitle "${title}" --title "${head_error}" --msgbox "${npm_030}" 7 50
fi
}
# Evaluar el parámetro posicional
if [[ "$1" == "-u" ]]; then
dialog --backtitle "${title}" --title "${npm_031}" --yesno "${npm_032}" 7 50
if [[ $? -eq 0 ]]; then
uninstall_anthropic
else
dialog --backtitle "${title}" --title "${head_canceled}" --msgbox "${npm_008}" 6 40
clear
exit 1
fi
else
dialog --backtitle "${title}" --title "${npm_031}" --yesno "${npm_033}" 7 50
if [[ $? -eq 0 ]]; then
install_anthropic
else
dialog --backtitle "${title}" --title "${head_canceled}" --msgbox "${npm_008}" 6 40
clear
exit 1
fi
fi
# Limpiar consola
clear