diff --git a/.gitignore b/.gitignore index 983bfa1..df7ec83 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ wheels/ # MacOS junk .DS_Store + +/.scripts/name diff --git a/.lefthook.yml b/.lefthook.yml index 6234db1..8b55afb 100644 --- a/.lefthook.yml +++ b/.lefthook.yml @@ -2,6 +2,9 @@ pre-commit: parallel: false piped: true # stop at the first fail jobs: + # check personalizzato + - name: 00_precheck + run: bash .scripts/precheck.sh # check for hard errors - name: 01_ruff_syntax glob: "*.py" diff --git a/.scripts/precheck.sh b/.scripts/precheck.sh new file mode 100755 index 0000000..6fbb2cc --- /dev/null +++ b/.scripts/precheck.sh @@ -0,0 +1,116 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +RED='\033[31m' +GRE='\033[32m' +YEL='\033[33m' +BLU='\033[34m' +RST='\033[0m' + +die() { + echo -e "${RED}✖ ${1}${RST}" >&2 + exit 1 +} + +info() { + echo -e "• ${1}${RST}" +} + +ok() { + echo -e "${GRE}✔ ${1}${RST}" +} + +warn() { + echo -e "${YEL}▲ ${1}${RST}" +} + +info "Eseguo precheck…" + +SCRIPT_PATH="$0" + +# Risolvi eventuali symlink manualmente +while [ -h "$SCRIPT_PATH" ]; do + DIR="$(cd -P "$(dirname "$SCRIPT_PATH")" >/dev/null 2>&1 && pwd)" + SCRIPT_PATH="$(readlink "$SCRIPT_PATH")" + [[ $SCRIPT_PATH != /* ]] && SCRIPT_PATH="$DIR/$SCRIPT_PATH" +done + +SCRIPT_DIR="$(cd -P "$(dirname "$SCRIPT_PATH")" >/dev/null 2>&1 && pwd)" + +EXPECTED_PYTHON="$(cd "$SCRIPT_DIR/../.venv/bin" >/dev/null 2>&1 && pwd)/python" +ACTUAL_PYTHON="$(command -v python || true)" + +if [ -z "$ACTUAL_PYTHON" ]; then + warn "Nessun comando 'python' trovato nel PATH. Assicurati che di aver attivato il virtual environment." +fi + +if [ ! -x "$EXPECTED_PYTHON" ]; then + warn "virtualenv non trovato nel progetto, segui la guida di installazione." >&2 +fi + +if [ "$ACTUAL_PYTHON" != "$EXPECTED_PYTHON" ]; then + warn "Attualmente non hai il virtual environment attivo, ricordati di attivarlo!" +fi + +FILE_PATH="${SCRIPT_DIR}/name" +if [ ! -f "$FILE_PATH" ]; then + die "Il file col proprio nome non esiste.\n${BLU}crearlo col seguente comando: $GRE printf '' > .scripts/name\n${BLU}Naturalmente devi sostituire $YEL$BLU con ${YEL}il tuo nome$BLU, lo stesso che usi per autentigarti su git.latentspace.cloud.${NC}" +fi + +CONTENT="$(cat "$FILE_PATH")" + +# sostituisce a capo con spazi +CONTENT="$(echo "$CONTENT" | tr '\n' ' ' | tr -s ' ')" +# rimuove spazi iniziali e finali +CONTENT="$(echo "$CONTENT" | sed 's/^ *//; s/ *$//')" + +if [[ "$CONTENT" == "" ]]; then + die "Errore: $YEL.scripts/name$RED esite ma non contiene il tuo nome." +fi + +if [[ "$CONTENT" == "" ]]; then + die "Non hai sostituito il tuo nome nel comando 🙄, rieseguilo ma sostituendo $YEL$RED con ${YEL}il tuo nome$RED." +fi + +if [[ "$CONTENT" == \<* && "$CONTENT" == *\> ]]; then + die "Naturalmente devi rimuovere le parentesi angolate (<>) ... pensavo fosse ovvio 🙄." +fi + +if [[ ! "$CONTENT" =~ ^[a-z]+$ ]]; then + die "Il nome inserito ($YEL${CONTENT}$RED) non è valido, può contenere solo lettere minuscole bro." +fi + +# isrepo +git rev-parse --is-inside-work-tree >/dev/null 2>&1 || die "Non sembra una repository Git." + +# ruff +command -v uv >/dev/null 2>&1 || die "Comando 'uv' non trovato nel PATH. Installalo con 'curl -LsSf https://astral.sh/uv/install.sh | sh'." +ok "uv trovato." + +# ruff +command -v ruff >/dev/null 2>&1 || die "Comando 'ruff' non trovato nel PATH. Installalo con 'uv tool add ruff'." +ok "ruff trovato." + +# git name +GIT_NAME="$(git config --get user.name || true)" +[[ -n "${GIT_NAME}" ]] || die "git user.name non impostato. Esegui: git config user.name ''" +if [[ "$GIT_NAME" != "$CONTENT" ]]; then + die "git user.name non corrisponde al tuo nome impostato in .scripts/name. Assicurati che sia l'username che usi per accedere a git.latentspace.cloud" +fi +ok "git user.name: \t$BLU${GIT_NAME}$RST" + +# git mail +GIT_EMAIL="$(git config --get user.email || true)" +[[ -n "${GIT_EMAIL}" ]] || die "git user.email non impostato. Esegui: git config user.email '@latentspace.cloud'" + +# git mail latentspace +if [[ ! "${GIT_EMAIL}" =~ @latentspace\.cloud$ ]]; then + die "git user.email non valido: $YEL'${GIT_EMAIL}'$RED. Deve terminare con $YEL'@latentspace.cloud'$RED." +fi +USER_PART="${GIT_EMAIL%@latentspace.cloud}" +if [[ "$USER_PART" != "$CONTENT" ]]; then + die "git user.email non corrisponde al tuo nome impostato in .scripts/name. Assicurati che sia $YEL@latentspace.cloud$RED, dove$YEL $RED è il nome che usi per autenticarti su git.latentspace.cloud." +fi +ok "git user.email: \t$BLU${GIT_EMAIL}$RST" + +ok "Precheck completato."