From 60a8a86ca27897552c95f171a295cfd53e8b7b6e Mon Sep 17 00:00:00 2001 From: "Mauro Rosero P." Date: Wed, 19 Mar 2025 11:46:59 -0500 Subject: [PATCH] [ADDED] Script para estandarizar encabezados y variables en scripts Bash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Crear plantilla bash.head para encabezados de scripts - Crear plantilla bash.variables para variables comunes en todos los scripts - Implementar script standardize_scripts.sh para aplicar estas plantillas 馃 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- bin/config/bash.head | 25 +++- bin/config/bash.variables | 29 +++++ bin/standardize_scripts.sh | 242 +++++++++++++++++++++++++++++++++++++ 3 files changed, 290 insertions(+), 6 deletions(-) create mode 100644 bin/config/bash.variables create mode 100755 bin/standardize_scripts.sh diff --git a/bin/config/bash.head b/bin/config/bash.head index 74870cd..7690753 100644 --- a/bin/config/bash.head +++ b/bin/config/bash.head @@ -1,10 +1,23 @@ #!/bin/bash # -# Script: cversadm_token.sh -# Description: Administrador de tokens para plataformas de control de versiones -# Created: 2025/01/04 20:00:00 -# Modified: 2025/03/15 16:08:03 -# [Author] Cortana Rosero One -# [Generated] Created by Claude Code (claude-3-7-sonnet-20250219) +# Script: $SCRIPT_NAME +# Description: $DESCRIPTION +# Created: $CREATED_DATE +# Modified: $MODIFIED_DATE +# [Author] $AUTHOR +# [Generated] $GENERATED_BY # # Derechos de Autor (C) [2025] [Mauro Rosero P. ] +# +# 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 . \ No newline at end of file diff --git a/bin/config/bash.variables b/bin/config/bash.variables new file mode 100644 index 0000000..289bbb4 --- /dev/null +++ b/bin/config/bash.variables @@ -0,0 +1,29 @@ +# Configuraci贸n inicial +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# 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/config/devspath.dat") +else + DEVSPATH="devs" +fi + +BIN_HOME="$HOME/$DEVSPATH" +BIN_BASE="bin" +BIN_LIBS="lib" +BIN_MESG="msg" +BIN_CFGS="config" +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" +source "${BIN_HOME}/${BIN_BASE}/${BIN_LIBS}/console.lib" + +# Cargar mensajes en el idioma del sistema o espa帽ol por defecto +load_messages "${BIN_HOME}/${BIN_BASE}" "${BIN_MESG}" "${BIN_LANG}" "head" + +# Variables globales +title="${head_000} ${head_002}" \ No newline at end of file diff --git a/bin/standardize_scripts.sh b/bin/standardize_scripts.sh new file mode 100755 index 0000000..14e4dfa --- /dev/null +++ b/bin/standardize_scripts.sh @@ -0,0 +1,242 @@ +#!/bin/bash +# +# Script: standardize_scripts.sh +# Description: Estandariza los encabezados y estructura de los scripts Bash +# Created: $(date +"%Y/%m/%d %H:%M:%S") +# Modified: $(date +"%Y/%m/%d %H:%M:%S") +# [Author] Cortana Rosero One +# [Generated] Created by Claude Code (claude-3-7-sonnet-20250219) +# +# Derechos de Autor (C) [2025] [Mauro Rosero P. ] +# +# 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 . + +# Configuraci贸n inicial +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# 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/config/devspath.dat") +else + DEVSPATH="devs" +fi + +BIN_HOME="$HOME/$DEVSPATH" +BIN_BASE="bin" +BIN_LIBS="lib" +BIN_MESG="msg" +BIN_CFGS="config" + +# CHECK SHELL LANGUAGE +BIN_LANG=${LANG:0:2} + +# Importar bibliotecas necesarias +source "${BIN_HOME}/${BIN_BASE}/${BIN_LIBS}/base.lib" +source "${BIN_HOME}/${BIN_BASE}/${BIN_LIBS}/console.lib" + +# Cargar mensajes en el idioma del sistema o espa帽ol por defecto +load_messages "${BIN_HOME}/${BIN_BASE}" "${BIN_MESG}" "${BIN_LANG}" "head" + +# Variables globales +title="${head_000} ${head_002}" +HEADER_TEMPLATE="${BIN_HOME}/${BIN_BASE}/${BIN_CFGS}/bash.head" +VARIABLES_TEMPLATE="${BIN_HOME}/${BIN_BASE}/${BIN_CFGS}/bash.variables" +DEFAULT_AUTHOR="Mauro Rosero P. " +CORTANA_AUTHOR="Cortana Rosero One " +CORTANA_GENERATED="Created by Claude Code (claude-3-7-sonnet-20250219)" +TODAY=$(date +"%Y/%m/%d %H:%M:%S") + +# Funci贸n para extraer informaci贸n del script existente +extract_script_info() { + local script_path="$1" + local script_name=$(basename "$script_path") + local description="" + local created_date="" + local modified_date="$TODAY" + local author="$DEFAULT_AUTHOR" + local generated_by="" + + # Extraer descripci贸n si existe + description=$(grep -A 5 "Description:" "$script_path" | grep "Description:" | sed 's/.*Description: \(.*\)/\1/' | head -1) + if [ -z "$description" ]; then + # Intentar extraer del comentario antes de la primera funci贸n o variable + description=$(sed -n '/#.*[Ss]cript/p' "$script_path" | head -1 | sed 's/#.*[Ss]cript[^:]*: \(.*\)/\1/') + if [ -z "$description" ]; then + description="Script para $script_name" + fi + fi + + # Extraer fecha de creaci贸n si existe + created_date=$(grep -A 10 "Created:" "$script_path" | grep "Created:" | sed 's/.*Created: \(.*\)/\1/' | head -1) + if [ -z "$created_date" ]; then + created_date="$TODAY" + fi + + # Extraer fecha de modificaci贸n si existe + mod_date=$(grep -A 10 "Modified:" "$script_path" | grep "Modified:" | sed 's/.*Modified: \(.*\)/\1/' | head -1) + if [ ! -z "$mod_date" ]; then + modified_date="$mod_date" + fi + + # Extraer autor si existe + auth=$(grep -A 10 "\[Author\]" "$script_path" | grep "\[Author\]" | sed 's/.*\[Author\] \(.*\)/\1/' | head -1) + if [ ! -z "$auth" ]; then + author="$auth" + fi + + # Extraer informaci贸n de generaci贸n si existe + gen=$(grep -A 10 "\[Generated\]" "$script_path" | grep "\[Generated\]" | sed 's/.*\[Generated\] \(.*\)/\1/' | head -1) + if [ ! -z "$gen" ]; then + generated_by="$gen" + elif [[ "$author" == *"Cortana"* ]]; then + generated_by="$CORTANA_GENERATED" + fi + + # Crear array con la informaci贸n extra铆da + echo "$script_name|$description|$created_date|$modified_date|$author|$generated_by" +} + +# Funci贸n para aplicar la plantilla a un script +apply_template() { + local script_path="$1" + local info="$2" + + # Separar la informaci贸n en variables + IFS='|' read -r script_name description created_date modified_date author generated_by <<< "$info" + + # Crear encabezado temporal con la informaci贸n extra铆da + local temp_header=$(mktemp) + cat "$HEADER_TEMPLATE" > "$temp_header" + + # Reemplazar las variables en la plantilla + sed -i "s|\$SCRIPT_NAME|$script_name|g" "$temp_header" + sed -i "s|\$DESCRIPTION|$description|g" "$temp_header" + sed -i "s|\$CREATED_DATE|$created_date|g" "$temp_header" + sed -i "s|\$MODIFIED_DATE|$modified_date|g" "$temp_header" + sed -i "s|\$AUTHOR|$author|g" "$temp_header" + + if [ ! -z "$generated_by" ]; then + sed -i "s|\$GENERATED_BY|$generated_by|g" "$temp_header" + else + # Si no hay informaci贸n de generaci贸n, eliminar esa l铆nea + sed -i '/\$GENERATED_BY/d' "$temp_header" + fi + + # Extraer el cuerpo del script (todo excepto el encabezado) + local temp_body=$(mktemp) + # Encontrar l铆nea despu茅s del encabezado (primera l铆nea en blanco despu茅s de los comentarios iniciales) + local header_end=$(grep -n "^$" "$script_path" | head -1 | cut -d: -f1) + if [ -z "$header_end" ]; then + header_end=25 # Valor predeterminado si no se encuentra una l铆nea en blanco + fi + + # Extraer el cuerpo del script + tail -n +$header_end "$script_path" > "$temp_body" + + # Crear el nuevo script combinando el encabezado y el cuerpo + cat "$temp_header" > "$script_path" + echo "" >> "$script_path" # L铆nea en blanco despu茅s del encabezado + cat "$temp_body" >> "$script_path" + + # Limpiar archivos temporales + rm -f "$temp_header" "$temp_body" + + echo "Estandarizado: $script_path" +} + +# Funci贸n para estandarizar variables comunes en el script +standardize_variables() { + local script_path="$1" + + # Buscar y reemplazar patrones comunes de variables + if grep -q "DEVSPATH=" "$script_path"; then + # Determinar d贸nde comienzan las variables + local var_line=$(grep -n "DEVSPATH=" "$script_path" | head -1 | cut -d: -f1) + local next_section=$(grep -n "^# " "$script_path" | awk -v start="$var_line" '$1 > start {print $1; exit}' | cut -d: -f1) + + if [ -z "$next_section" ]; then + # Si no hay otra secci贸n, buscar la primera funci贸n + next_section=$(grep -n "^[a-zA-Z_][a-zA-Z0-9_]*().*{" "$script_path" | head -1 | cut -d: -f1) + if [ -z "$next_section" ]; then + # Si no hay funci贸n, usar el final del archivo + next_section=$(wc -l "$script_path" | awk '{print $1}') + fi + fi + + # Crear un archivo temporal con el contenido antes de las variables + local temp_before=$(mktemp) + head -n $((var_line - 1)) "$script_path" > "$temp_before" + + # Crear un archivo temporal con el contenido despu茅s de las variables + local temp_after=$(mktemp) + tail -n +$next_section "$script_path" > "$temp_after" + + # Crear el nuevo script + cat "$temp_before" > "$script_path" + cat "$VARIABLES_TEMPLATE" >> "$script_path" + echo "" >> "$script_path" # A帽adir l铆nea en blanco + cat "$temp_after" >> "$script_path" + + # Limpiar archivos temporales + rm -f "$temp_before" "$temp_after" + fi + + # Estandarizar la carga de bibliotecas + if ! grep -q "source .*base.lib" "$script_path"; then + echo -e "\n# Importar bibliotecas necesarias\nsource \"\${BIN_HOME}/\${BIN_BASE}/\${BIN_LIBS}/base.lib\"" >> "$script_path" + fi + + # Estandarizar la carga de mensajes si a煤n no existe + if ! grep -q "load_messages" "$script_path"; then + echo -e "\n# Cargar mensajes en el idioma del sistema o espa帽ol por defecto\nload_messages \"\${BIN_HOME}/\${BIN_BASE}\" \"\${BIN_MESG}\" \"\${BIN_LANG}\" \"head\"" >> "$script_path" + fi + + # Estandarizar la definici贸n del t铆tulo si no existe + if ! grep -q "title=" "$script_path"; then + echo -e "\n# Variables globales\ntitle=\"\${head_000} \${head_002}\"" >> "$script_path" + fi +} + +# Funci贸n principal +main() { + echo "Estandarizando scripts Bash en $BIN_HOME/$BIN_BASE" + + # Verificar que existe la plantilla de encabezado + if [ ! -f "$HEADER_TEMPLATE" ]; then + echo "Error: No se encuentra la plantilla de encabezado en $HEADER_TEMPLATE" + exit 1 + fi + + # Recorrer todos los scripts .sh en el directorio bin + for script in "$BIN_HOME/$BIN_BASE"/*.sh; do + # Saltar el script actual para evitar problemas + if [ "$(basename "$script")" == "$(basename "$0")" ]; then + continue + fi + + # Extraer informaci贸n del script + local script_info=$(extract_script_info "$script") + + # Aplicar la plantilla de encabezado + apply_template "$script" "$script_info" + + # Estandarizar variables y estructura com煤n + standardize_variables "$script" + done + + echo "Proceso de estandarizaci贸n completado." +} + +# Ejecutar funci贸n principal +main \ No newline at end of file