Files
_Assistant_Lead_Tech/sync-ai-instructions.sh
2026-03-08 15:20:29 +01:00

104 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# sync-ai-instructions.sh
# Génère un unique CLAUDE.md dans le repo Lead_tech depuis _AI_INSTRUCTIONS.md + _projects.conf
# puis recrée les liens symboliques nécessaires vers ce fichier unique
# selon la machine courante (Darwin = Mac, Linux = NUC)
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE="$SCRIPT_DIR/_AI_INSTRUCTIONS.md"
PROJECTS_CONF="$SCRIPT_DIR/_projects.conf"
# --- Détection machine ---
OS="$(uname -s)"
CHANGED=0
# --- Construire la table markdown des projets ---
build_projects_table() {
local os="$1"
echo "| Projet | Stack | Localisation | État |"
echo "|---|---|---|---|"
while IFS='|' read -r nom stack path_nuc path_mac etat; do
# Ignorer lignes vides et commentaires
[[ -z "$nom" || "$nom" == \#* ]] && continue
if [ "$os" = "Darwin" ]; then
path="$path_mac"
else
path="$path_nuc"
fi
if [ -z "$path" ]; then
echo "| $nom | $stack | *non disponible sur cette machine* | $etat |"
else
echo "| $nom | $stack | \`$path\` | $etat |"
fi
done < "$PROJECTS_CONF"
}
generate_repo_claude() {
local header="$1"
local dest="$2"
local projects_table
local tmp
projects_table="$(build_projects_table "$OS")"
tmp="$(mktemp)"
mkdir -p "$(dirname "$dest")"
{
echo "$header"
echo ""
# Remplacer {{PROJECTS_TABLE}} par la table générée
while IFS= read -r line; do
if [ "$line" = "{{PROJECTS_TABLE}}" ]; then
echo "$projects_table"
else
echo "$line"
fi
done < "$SOURCE"
} > "$tmp"
if [ ! -f "$dest" ] || ! cmp -s "$tmp" "$dest"; then
mv "$tmp" "$dest"
CHANGED=1
else
rm -f "$tmp"
fi
}
ensure_symlink() {
local target="$1"
local link_path="$2"
mkdir -p "$(dirname "$link_path")"
if [ -L "$link_path" ]; then
local current_target
current_target="$(readlink "$link_path")"
if [ "$current_target" = "$target" ]; then
return 0
fi
elif [ -e "$link_path" ]; then
rm -f "$link_path"
fi
rm -f "$link_path"
ln -s "$target" "$link_path"
CHANGED=1
}
CLAUDE_HEADER="# Instructions globales — Lead Tech Copilote
Ce fichier est chargé automatiquement par Claude Code ou Codex à chaque session.
Il constitue la porte d'entrée principale de la base de connaissance Lead_tech et oriente vers les fichiers spécialisés utilisés par tous les projets."
generate_repo_claude "$CLAUDE_HEADER" "$SCRIPT_DIR/CLAUDE.md"
ensure_symlink "$SCRIPT_DIR/CLAUDE.md" "$HOME/.claude/CLAUDE.md"
ensure_symlink "$HOME/.claude/CLAUDE.md" "$HOME/.codex/AGENTS.md"
ensure_symlink "$SCRIPT_DIR/CLAUDE.md" "$SCRIPT_DIR/AGENTS.md"
if [ "$CHANGED" -eq 1 ]; then
echo "Sync AI instructions (OS: $OS)"
echo "Sync terminée."
fi