146 lines
4.7 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
ENV_DIR="${HOME}/.4nk_template"
ENV_FILE="${ENV_DIR}/.env"
TEMPLATE_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
TEMPLATE_IN_REPO="${TEMPLATE_ROOT}/scripts/env/.env.template"
usage() {
cat <<USAGE
Usage: $0 <git_url> [--dest DIR] [--force]
Actions:
1) Provisionne ~/.4nk_template/.env (si absent)
2) Clone le dépôt cible si le dossier n'existe pas
3) Copie la structure normative 4NK_template dans le projet cible:
- .gitea/** (workflows, templates issues/PR)
- AGENTS.md
- .cursor/rules/** (si présent)
- scripts/agents/**, scripts/env/ensure_env.sh, scripts/deploy/setup.sh
- docs/templates/** et docs/INDEX.md (table des matières)
4) Ne remplace pas les fichiers existants sauf si --force
Exemples:
$0 https://git.example.com/org/projet.git
$0 git@host:org/projet.git --dest ~/work --force
USAGE
}
GIT_URL="${1:-}"
DEST_PARENT="$(pwd)"
FORCE_COPY=0
shift || true
while [[ $# -gt 0 ]]; do
case "$1" in
--dest)
DEST_PARENT="${2:-}"; shift 2 ;;
--force)
FORCE_COPY=1; shift ;;
-h|--help)
usage; exit 0 ;;
*)
echo "Option inconnue: $1" >&2; usage; exit 2 ;;
esac
done
if [[ -z "${GIT_URL}" ]]; then
usage; exit 2
fi
mkdir -p "${ENV_DIR}"
chmod 700 "${ENV_DIR}" || true
if [[ ! -f "${ENV_FILE}" ]]; then
if [[ -f "${TEMPLATE_IN_REPO}" ]]; then
cp "${TEMPLATE_IN_REPO}" "${ENV_FILE}"
else
cat >"${ENV_FILE}" <<'EOF'
# Fichier d'exemple d'environnement pour 4NK_template
# Copiez ce fichier vers ~/.4nk_template/.env puis complétez les valeurs.
# Ne committez jamais de fichier contenant des secrets.
# OpenAI (agents IA)
OPENAI_API_KEY=
OPENAI_MODEL=
OPENAI_API_BASE=https://api.openai.com/v1
OPENAI_TEMPERATURE=0.2
# Gitea (release via API)
BASE_URL=https://git.4nkweb.com
RELEASE_TOKEN=
EOF
fi
chmod 600 "${ENV_FILE}" || true
echo "Fichier créé: ${ENV_FILE}. Complétez les valeurs requises (ex: OPENAI_API_KEY, OPENAI_MODEL, RELEASE_TOKEN)." >&2
fi
# 2) Clonage du dépôt si nécessaire
repo_name="$(basename -s .git "${GIT_URL}")"
target_dir="${DEST_PARENT%/}/${repo_name}"
if [[ ! -d "${target_dir}" ]]; then
echo "Clonage: ${GIT_URL}${target_dir}" >&2
git clone --depth 1 "${GIT_URL}" "${target_dir}"
else
echo "Dossier existant, pas de clone: ${target_dir}" >&2
fi
copy_item() {
local src="$1" dst="$2"
if [[ ! -e "$src" ]]; then return 0; fi
if [[ -d "$src" ]]; then
mkdir -p "$dst"
if (( FORCE_COPY )); then
cp -a "$src/." "$dst/"
else
(cd "$src" && find . -type f -print0) | while IFS= read -r -d '' f; do
if [[ ! -e "$dst/$f" ]]; then
mkdir -p "$(dirname "$dst/$f")"
cp -a "$src/$f" "$dst/$f"
fi
done
fi
else
if [[ -e "$dst" && $FORCE_COPY -eq 0 ]]; then return 0; fi
mkdir -p "$(dirname "$dst")" && cp -a "$src" "$dst"
fi
}
# 3) Copie de la structure normative
copy_item "${TEMPLATE_ROOT}/.gitea" "${target_dir}/.gitea"
copy_item "${TEMPLATE_ROOT}/AGENTS.md" "${target_dir}/AGENTS.md"
copy_item "${TEMPLATE_ROOT}/.cursor" "${target_dir}/.cursor"
copy_item "${TEMPLATE_ROOT}/.cursorignore" "${target_dir}/.cursorignore"
copy_item "${TEMPLATE_ROOT}/.gitignore" "${target_dir}/.gitignore"
copy_item "${TEMPLATE_ROOT}/.markdownlint.json" "${target_dir}/.markdownlint.json"
copy_item "${TEMPLATE_ROOT}/LICENSE" "${target_dir}/LICENSE"
copy_item "${TEMPLATE_ROOT}/CONTRIBUTING.md" "${target_dir}/CONTRIBUTING.md"
copy_item "${TEMPLATE_ROOT}/CODE_OF_CONDUCT.md" "${target_dir}/CODE_OF_CONDUCT.md"
copy_item "${TEMPLATE_ROOT}/SECURITY.md" "${target_dir}/SECURITY.md"
copy_item "${TEMPLATE_ROOT}/TEMPLATE_VERSION" "${target_dir}/TEMPLATE_VERSION"
copy_item "${TEMPLATE_ROOT}/security" "${target_dir}/security"
copy_item "${TEMPLATE_ROOT}/scripts" "${target_dir}/scripts"
copy_item "${TEMPLATE_ROOT}/docs/templates" "${target_dir}/docs/templates"
# Génération docs/INDEX.md dans le projet cible (si absent ou --force)
INDEX_DST="${target_dir}/docs/INDEX.md"
if [[ ! -f "${INDEX_DST}" || $FORCE_COPY -eq 1 ]]; then
mkdir -p "$(dirname "${INDEX_DST}")"
cat >"${INDEX_DST}" <<'IDX'
# Documentation du projet
Cette table des matières oriente vers:
- Documentation spécifique au projet: `docs/project/`
- Modèles génériques à adapter: `docs/templates/`
## Sommaire
- À personnaliser: `docs/project/README.md`, `docs/project/INDEX.md`, `docs/project/ARCHITECTURE.md`, `docs/project/USAGE.md`, etc.
## Modèles génériques
- Voir: `docs/templates/`
IDX
fi
echo "Template 4NK appliqué à: ${target_dir}" >&2
exit 0