-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ps1
More file actions
299 lines (251 loc) · 11.6 KB
/
Copy pathsetup.ps1
File metadata and controls
299 lines (251 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# setup.ps1 - Knowledge Tree Ersteinrichtung
#
# Fuehrt folgende Schritte aus:
# 1. Prueft Voraussetzungen (uv, git)
# 2. .env aus .env.example erstellen (interaktiv)
# 3. Vault-Verzeichnisstruktur anlegen
# 4. Verbindung testen (Azure OpenAI Ping)
# 5. Wiki-Sync als Windows Scheduled Task registrieren (alle 15 Min)
#
# Usage: .\setup.ps1
# Usage: .\setup.ps1 -VaultRoot "C:\MeinVault" -NonInteractive
param(
[string]$VaultRoot = "",
[switch]$NonInteractive
)
$ErrorActionPreference = "Stop"
$ScriptRoot = $PSScriptRoot
Write-Host ""
Write-Host "=================================================" -ForegroundColor Cyan
Write-Host " Knowledge Tree Setup" -ForegroundColor Cyan
Write-Host "=================================================" -ForegroundColor Cyan
Write-Host ""
# -- 1. Voraussetzungen ----------------------------------------------------
Write-Host "[1/5] Pruefe Voraussetzungen..." -ForegroundColor Yellow
$missing = @()
if (-not (Get-Command uv -ErrorAction SilentlyContinue)) { $missing += "uv (winget install astral-sh.uv)" }
if (-not (Get-Command git -ErrorAction SilentlyContinue)) { $missing += "git" }
if ($missing.Count -gt 0) {
Write-Host "Fehlende Tools:" -ForegroundColor Red
$missing | ForEach-Object { Write-Host " - $_" -ForegroundColor Red }
exit 1
}
Write-Host " OK (uv + git vorhanden)" -ForegroundColor Green
# -- 2. .env erstellen -----------------------------------------------------
Write-Host ""
Write-Host "[2/5] Konfiguration (.env)..." -ForegroundColor Yellow
$envFile = Join-Path $ScriptRoot ".env"
$envExample = Join-Path $ScriptRoot ".env.example"
if (Test-Path $envFile) {
Write-Host " .env bereits vorhanden - wird nicht ueberschrieben." -ForegroundColor DarkGray
$existingEnv = Get-Content $envFile -Raw
} else {
if (-not (Test-Path $envExample)) {
Write-Host " .env.example nicht gefunden!" -ForegroundColor Red
exit 1
}
Copy-Item $envExample $envFile
Write-Host " .env aus .env.example erstellt." -ForegroundColor Green
$existingEnv = ""
}
# VaultRoot bestimmen
if (-not $VaultRoot) {
# Aus .env lesen falls schon gesetzt
if ($existingEnv -match 'VAULT_ROOT=(.+)') {
$VaultRoot = $Matches[1].Trim()
}
}
if (-not $VaultRoot -and -not $NonInteractive) {
Write-Host ""
Write-Host " Wo sollen die Vault-Daten liegen (wiki/, raw/, assets/)?" -ForegroundColor Cyan
Write-Host " Empfehlung: OneDrive-Ordner fuer automatische Synchronisation" -ForegroundColor DarkGray
Write-Host ""
$default = "C:\Users\$env:USERNAME\OneDrive - INFORM GmbH\knowledge-wiki"
$input = Read-Host " Vault-Pfad [$default]"
$VaultRoot = if ($input.Trim()) { $input.Trim() } else { $default }
}
if (-not $VaultRoot) {
Write-Host " Kein Vault-Pfad gesetzt. Verwende Skript-Verzeichnis als Fallback." -ForegroundColor Yellow
$VaultRoot = $ScriptRoot
}
# VAULT_ROOT in .env eintragen
$envContent = Get-Content $envFile -Raw
if ($envContent -match 'VAULT_ROOT=<user>') {
$envContent = $envContent -replace 'VAULT_ROOT=.*', "VAULT_ROOT=$VaultRoot"
Set-Content $envFile -Value $envContent -Encoding UTF8 -NoNewline
Write-Host " VAULT_ROOT=$VaultRoot eingetragen." -ForegroundColor Green
} elseif (-not ($envContent -match 'VAULT_ROOT=')) {
Add-Content $envFile "`nVAULT_ROOT=$VaultRoot"
Write-Host " VAULT_ROOT=$VaultRoot ergaenzt." -ForegroundColor Green
} else {
Write-Host " VAULT_ROOT bereits gesetzt: $VaultRoot" -ForegroundColor DarkGray
}
# -- 3. Vault-Struktur anlegen ---------------------------------------------
Write-Host ""
Write-Host "[3/5] Vault-Verzeichnisstruktur anlegen..." -ForegroundColor Yellow
$dirs = @(
"wiki",
"wiki\concepts",
"wiki\entities",
"wiki\sources",
"wiki\syntheses",
"wiki\meta",
"raw\slides",
"raw\docs",
"raw\pdfs",
"raw\.cache",
"assets",
"output",
"logs"
)
foreach ($dir in $dirs) {
$fullPath = Join-Path $VaultRoot $dir
if (-not (Test-Path $fullPath)) {
New-Item -ItemType Directory -Force -Path $fullPath | Out-Null
Write-Host " Erstellt: $dir" -ForegroundColor DarkGray
}
}
# Basis wiki/index.md anlegen falls nicht vorhanden
$indexPath = Join-Path $VaultRoot "wiki\index.md"
if (-not (Test-Path $indexPath)) {
Set-Content $indexPath -Value @"
# Knowledge Tree -- Index
> Automatisch generiert. Stand: $(Get-Date -Format 'yyyy-MM-dd')
| Domain | Beschreibung | Seiten | Zuletzt aktualisiert |
|--------|--------------|--------|---------------------|
"@ -Encoding UTF8
Write-Host " wiki\index.md angelegt." -ForegroundColor DarkGray
}
# wiki/log.md anlegen falls nicht vorhanden
$logPath = Join-Path $VaultRoot "wiki\log.md"
if (-not (Test-Path $logPath)) {
Set-Content $logPath -Value @"
# Aktivitaetslog
> Append-only. Neueste Eintraege oben.
"@ -Encoding UTF8
Write-Host " wiki\log.md angelegt." -ForegroundColor DarkGray
}
Write-Host " Vault-Struktur OK." -ForegroundColor Green
# -- 4. Verbindungstest ----------------------------------------------------
Write-Host ""
Write-Host "[4/5] Verbindung testen..." -ForegroundColor Yellow
# uv schreibt Download-Fortschritt nach stderr. Unter ErrorActionPreference=Stop
# wuerde das als terminierender Fehler interpretiert, obwohl uv sauber laeuft.
$prevEAP = $ErrorActionPreference
$ErrorActionPreference = "Continue"
$testResult = & uv run --with openai --with python-dotenv "$ScriptRoot\test-connection.py" 2>&1
$ErrorActionPreference = $prevEAP
if ($LASTEXITCODE -eq 0) {
Write-Host " Azure OpenAI: OK" -ForegroundColor Green
} else {
Write-Host " Azure OpenAI: FEHLER" -ForegroundColor Red
Write-Host " $testResult" -ForegroundColor Red
Write-Host " Bitte AZURE_OPENAI_ENDPOINT und AZURE_OPENAI_API_KEY in .env pruefen." -ForegroundColor Yellow
}
# -- 5. Watcher-Config in Vault kopieren + MetaSync registrieren -----------
Write-Host ""
Write-Host "[5/5] Watcher-Setup..." -ForegroundColor Yellow
# (a) Default-watchers.json aus Repo in $VAULT_ROOT kopieren (mit cwd).
# Damit koennen weitere Code-Projekte ihre Watcher einfach an die Datei
# im Vault anhaengen.
$vaultWatchersFile = Join-Path $VaultRoot "watchers.json"
$templateFile = Join-Path $ScriptRoot "watchers.json"
if (Test-Path $vaultWatchersFile) {
Write-Host " watchers.json im Vault vorhanden - wird nicht ueberschrieben: $vaultWatchersFile" -ForegroundColor DarkGray
} elseif (-not (Test-Path $templateFile)) {
Write-Host " ! Template $templateFile fehlt - ueberspringe." -ForegroundColor Red
} else {
try {
$tmpl = Get-Content $templateFile -Raw -Encoding UTF8 | ConvertFrom-Json
foreach ($w in $tmpl.watchers) {
if (-not $w.cwd) {
$w | Add-Member -NotePropertyName cwd -NotePropertyValue $ScriptRoot -Force
}
}
$tmpl | ConvertTo-Json -Depth 5 | Set-Content $vaultWatchersFile -Encoding UTF8
Write-Host " watchers.json angelegt: $vaultWatchersFile" -ForegroundColor Green
} catch {
Write-Host " ! watchers.json konnte nicht angelegt werden: $_" -ForegroundColor Red
}
}
# (a2) Default-code-repos.yaml aus Repo-Template in $VAULT_ROOT kopieren.
# Liegt im Vault (nicht im Code-Dir), damit Repo-Liste ueber OneDrive
# zwischen Maschinen synchronisiert wird.
$vaultReposFile = Join-Path $VaultRoot "code-repos.yaml"
$reposTemplate = Join-Path $ScriptRoot "code-repos.yaml.example"
$legacyReposFile = Join-Path $ScriptRoot "code-repos.yaml"
if (Test-Path $vaultReposFile) {
Write-Host " code-repos.yaml im Vault vorhanden - wird nicht ueberschrieben: $vaultReposFile" -ForegroundColor DarkGray
} elseif (Test-Path $legacyReposFile) {
# Migration: alte Datei aus dem Code-Dir uebernehmen
try {
Move-Item -Path $legacyReposFile -Destination $vaultReposFile
Write-Host " code-repos.yaml aus Code-Dir nach Vault migriert: $vaultReposFile" -ForegroundColor Green
} catch {
Write-Host " ! Migration von code-repos.yaml fehlgeschlagen: $_" -ForegroundColor Red
}
} elseif (-not (Test-Path $reposTemplate)) {
Write-Host " ! Template $reposTemplate fehlt - ueberspringe." -ForegroundColor Red
} else {
try {
Copy-Item $reposTemplate $vaultReposFile
Write-Host " code-repos.yaml aus Template angelegt: $vaultReposFile" -ForegroundColor Green
Write-Host " -> Repos eintragen + GITHUB_PAT in .env setzen, dann KnowledgeTree-CodeWatch laeuft." -ForegroundColor DarkGray
} catch {
Write-Host " ! code-repos.yaml konnte nicht angelegt werden: $_" -ForegroundColor Red
}
}
# (b) MetaSync-Task registrieren — liest alle 15 Min watchers.json neu
# und syncht die Scheduled Tasks (create/update/remove).
$metaTaskName = "KnowledgeTree-MetaSync"
$syncScript = Join-Path $ScriptRoot "sync-watchers.ps1"
if (-not (Test-Path $syncScript)) {
Write-Host " ! sync-watchers.ps1 fehlt - MetaSync nicht registriert." -ForegroundColor Red
} else {
try {
$action = New-ScheduledTaskAction `
-Execute "powershell.exe" `
-Argument "-ExecutionPolicy Bypass -NoProfile -File `"$syncScript`" -WatchersFile `"$vaultWatchersFile`"" `
-WorkingDirectory $ScriptRoot
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) `
-RepetitionInterval (New-TimeSpan -Minutes 15) `
-RepetitionDuration (New-TimeSpan -Days 9999)
$settings = New-ScheduledTaskSettingsSet `
-MultipleInstances IgnoreNew `
-ExecutionTimeLimit (New-TimeSpan -Minutes 5) `
-StartWhenAvailable
Register-ScheduledTask `
-TaskName $metaTaskName `
-Action $action `
-Trigger $trigger `
-Settings $settings `
-Description "Synchronisiert watchers.json mit Windows Scheduled Tasks (alle 15 Min)." `
-Force | Out-Null
Write-Host " OK $metaTaskName (alle 15 Min, liest $vaultWatchersFile)" -ForegroundColor Green
} catch {
Write-Host " ! $metaTaskName konnte nicht registriert werden: $_" -ForegroundColor Red
}
# (c) Einmalig jetzt ausfuehren, damit die Watcher nicht bis zum ersten
# MetaSync-Tick warten muessen.
Write-Host " Erste Synchronisation..." -ForegroundColor DarkGray
& powershell -ExecutionPolicy Bypass -NoProfile -File $syncScript -WatchersFile $vaultWatchersFile
}
Write-Host " Verwalten: Aufgabenplanung > Task 'KnowledgeTree-*'" -ForegroundColor DarkGray
Write-Host " Neue Watcher: $vaultWatchersFile editieren - wirkt beim naechsten MetaSync." -ForegroundColor DarkGray
# -- Zusammenfassung -------------------------------------------------------
Write-Host ""
Write-Host "=================================================" -ForegroundColor Cyan
Write-Host " Setup abgeschlossen!" -ForegroundColor Green
Write-Host "=================================================" -ForegroundColor Cyan
Write-Host ""
Write-Host " Code: $ScriptRoot" -ForegroundColor White
Write-Host " Vault: $VaultRoot" -ForegroundColor White
Write-Host ""
Write-Host " Naechste Schritte:" -ForegroundColor Cyan
Write-Host " 1. Dokument ingestieren:" -ForegroundColor White
Write-Host " uv run `"$ScriptRoot\ingest.py`" `"$VaultRoot\raw\slides\Meine-Praesentation.pptx`"" -ForegroundColor DarkGray
Write-Host " 2. Watcher laufen automatisch (Aufgabenplanung > 'KnowledgeTree-*')" -ForegroundColor White
Write-Host " Neue Watcher hinzufuegen: $vaultWatchersFile editieren" -ForegroundColor DarkGray
Write-Host " -> MetaSync registriert/entfernt Tasks innerhalb 15 Min automatisch" -ForegroundColor DarkGray
Write-Host " 3. Fuer Code-Monitoring: GITHUB_PAT in .env + Repos in $vaultReposFile eintragen" -ForegroundColor White
Write-Host ""