-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwikifier.ps1
More file actions
175 lines (154 loc) · 6.03 KB
/
Copy pathwikifier.ps1
File metadata and controls
175 lines (154 loc) · 6.03 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
# wikifier.ps1 — Wikifier PowerShell implementation (Windows)
# Zero-dependency. Mirrors the most important commands from wikifier.sh
param(
[Parameter(Position=0)]
[string]$Command = "help",
[Parameter(ValueFromRemainingArguments=$true)]
[string[]]$Arguments
)
$ErrorActionPreference = "Stop"
$WikifierRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$Staging = Join-Path $WikifierRoot ".wikifier_staging"
$JournalRoot = Join-Path $WikifierRoot "journal"
$HealthFile = Join-Path $WikifierRoot "file_health.md"
$PendingFile = Join-Path $WikifierRoot "pending_updates.md"
$LastCheck = Join-Path $Staging ".last_check"
$Monitored = Join-Path $WikifierRoot "monitored_paths.txt"
$Excludes = Join-Path $WikifierRoot "exclude_patterns.txt"
New-Item -ItemType Directory -Path $Staging -Force | Out-Null
function Write-Log { param([string]$m) Write-Host "[wikifier] $m" }
function Write-Err { param([string]$m) Write-Host "[wikifier ERROR] $m" -ForegroundColor Red }
function Get-Timestamp { Get-Date -Format "yyyy-MM-dd HH:mm:ss" }
function Upsert-Health {
param([string]$file, [string]$status, [string]$reason = "")
$now = Get-Timestamp
if (-not (Test-Path $HealthFile)) {
@"
# Documentation Health Matrix
| File | Status | Last Updated | Reason / Intent |
|------|--------|--------------|-----------------|
"@ | Set-Content $HealthFile
}
$content = Get-Content $HealthFile -Raw
$pattern = [regex]::Escape("| $file |")
if ($content -match $pattern) {
$newLine = "| $file | $status | $now | $reason |"
$content = [regex]::Replace($content, "\| $file \| .*? \| .*? \| .*? \|", $newLine, 1)
Set-Content $HealthFile $content
} else {
Add-Content $HealthFile "| $file | $status | $now | $reason |"
}
}
function Add-Pending {
param([string]$file, [string]$msg)
if (-not (Test-Path $PendingFile)) { " # Pending Updates" | Set-Content $PendingFile }
Add-Content $PendingFile "- $file`: $msg"
}
function Write-Journal {
param([string]$action, [string]$file, [string]$reason)
$datePath = Join-Path $JournalRoot (Get-Date -Format "yyyy/MM")
New-Item -ItemType Directory -Path $datePath -Force | Out-Null
$dayFile = Join-Path $datePath ("{0:dd}.md" -f (Get-Date))
@"
## [$(Get-Timestamp)] $action
**File:** $file
**Reason:** $reason
"@ | Add-Content $dayFile
}
# ---------------- Commands ----------------
switch ($Command.ToLower()) {
"help" {
Write-Host @"
Wikifier (PowerShell/Windows)
Core:
check-changes
record-change <file> "<reason>"
mark-green <file>
health
monitor
serve [port]
update-maps
init
See wikifier.sh for the full Unix implementation (recommended on WSL/macOS/Linux).
"@
}
"init" {
if (-not (Test-Path $Monitored)) { "." | Set-Content $Monitored }
if (-not (Test-Path $Excludes)) {
@"
node_modules
.git
build
dist
"@ | Set-Content $Excludes
}
if (-not (Test-Path $HealthFile)) {
@"
# Documentation Health Matrix
| File | Status | Last Updated | Reason / Intent |
|------|--------|--------------|-----------------|
"@ | Set-Content $HealthFile
}
Upsert-Health "wikifier.ps1" "🟢 Green" "Windows PowerShell CLI initialised."
Write-Log "Wikifier Windows state initialised."
}
"check-changes" {
Write-Log "Running change detection (PowerShell)..."
$last = if (Test-Path $LastCheck) { Get-Content $LastCheck } else { "1970-01-01" }
$roots = if (Test-Path $Monitored) { Get-Content $Monitored | Where-Object { $_ -notmatch '^\s*#' } } else { @(".") }
$changed = 0
foreach ($root in $roots) {
if (-not (Test-Path $root)) { continue }
Get-ChildItem -Path $root -Recurse -File -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTime -gt [datetime]$last } |
ForEach-Object {
$rel = $_.FullName.Replace($WikifierRoot, "").TrimStart('\','/')
if ($rel -match '(\.git|node_modules|Logged_issues|journal|\.wikifier)') { return }
Upsert-Health $rel "🟡 Yellow" "mtime changed (PowerShell auto-detect)"
Add-Pending $rel "Auto-detected change on Windows"
$changed++
}
}
Get-Date -Format "yyyy-MM-dd HH:mm:ss" | Set-Content $LastCheck
Write-Log "Detected $changed file(s) changed."
}
"record-change" {
$file = $Arguments[0]
$reason = if ($Arguments.Count -gt 1) { $Arguments[1] } else { "No reason" }
if (-not $file) { Write-Err "Usage: wikifier record-change <file> <reason>"; exit 1 }
Upsert-Health $file "🟡 Yellow" $reason
Add-Pending $file "record-change: $reason"
Write-Journal "record-change" $file $reason
Write-Log "Recorded change for $file"
}
"mark-green" {
$file = $Arguments[0]
if (-not $file) { Write-Err "Usage: wikifier mark-green <file>"; exit 1 }
Upsert-Health $file "🟢 Green" "Verified on Windows."
if (Test-Path $PendingFile) {
(Get-Content $PendingFile) | Where-Object { $_ -notmatch [regex]::Escape($file) } | Set-Content $PendingFile
}
Write-Log "🟢 $file marked Green."
}
"health" {
if (Test-Path $HealthFile) { Get-Content $HealthFile } else { Write-Host "Run init or check-changes first." }
}
"monitor" {
Write-Log "Starting heartbeat monitor (Windows PowerShell)..."
while ($true) {
& $PSCommandPath check-changes
Start-Sleep -Seconds 30
}
}
"serve" {
# Browsers block fetch() on file:// pages, so index.html must be served.
# wikifier.serve adds the dashboard's Run/Stop buttons (localhost-only).
$port = if ($Arguments -and $Arguments[0]) { $Arguments[0] } else { "8787" }
Set-Location $WikifierRoot
$env:WIKIFIER_PROJECT_ROOT = $WikifierRoot
python -m wikifier.serve $port
}
default {
Write-Err "Unknown command '$Command'. Try 'wikifier help'."
}
}