dsh-work-summary 0.0.0-stage → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,388 @@
1
+ # collect-range.ps1 —— 收集一个日期区间的"工作痕迹",供周报/月报聚合引用。
2
+ # 用法:
3
+ # pwsh -NoProfile -File collect-range.ps1 -From 2026-11-18 -To 2026-11-24
4
+ # pwsh -NoProfile -File collect-range.ps1 -From 2026-11-01 -To 2026-11-30 -NoSessions
5
+ # 输出:<ReportDir>\_源数据\周期-YYYY-MM-DD_YYYY-MM-DD.md(同时打印路径)
6
+ # 本脚本属于 dsh-work-summary 插件仓库;扫哪里由 -Workspace 决定,产物写哪由 -ReportDir 决定。
7
+ # 与 collect-day.ps1 的分工:单日采集扫工作区/会话;区间采集以已有日报为主,把跨天事实摊平。
8
+
9
+ param(
10
+ [Parameter(Mandatory = $true)][string]$From,
11
+ [Parameter(Mandatory = $true)][string]$To,
12
+ [string]$DshHome = (Join-Path $HOME '.dsh'),
13
+ [string]$Workspace = (Join-Path $DshHome 'dsh-work-summary'),
14
+ [string]$ReportDir = (Join-Path $DshHome 'dsh-work-summary'),
15
+ [switch]$NoSessions
16
+ )
17
+
18
+ $ErrorActionPreference = 'Stop'
19
+ $ProgressPreference = 'SilentlyContinue'
20
+
21
+ try { $fromDay = [datetime]::ParseExact($From, 'yyyy-MM-dd', $null) }
22
+ catch { throw "起始日期格式应为 yyyy-MM-dd,收到:$From" }
23
+ try { $toDay = [datetime]::ParseExact($To, 'yyyy-MM-dd', $null) }
24
+ catch { throw "结束日期格式应为 yyyy-MM-dd,收到:$To" }
25
+ if ($toDay -lt $fromDay) { throw "结束日期不能早于起始日期:$From ~ $To" }
26
+
27
+ $span = ($toDay - $fromDay).Days + 1
28
+ if ($span -gt 400) { throw "区间最长 400 天,当前 $span 天:$From ~ $To" }
29
+
30
+ $rangeEnd = $toDay.AddDays(1)
31
+ $days = @()
32
+ for ($d = $fromDay; $d -le $toDay; $d = $d.AddDays(1)) { $days += $d.ToString('yyyy-MM-dd') }
33
+
34
+ $outDir = Join-Path $ReportDir '_源数据'
35
+ New-Item -ItemType Directory -Force -Path $outDir | Out-Null
36
+ $outFile = Join-Path $outDir ("周期-{0}_{1}.md" -f $From, $To)
37
+
38
+ $skipDirs = @('node_modules', '.pnpm-store', '.git', 'dist', 'build', '.next', 'target',
39
+ 'temp', 'tmp', '.cache', '__pycache__', '_源数据', '.dsh-vision-router',
40
+ '.dsh', 'anatomy', 'shots', '备份', '_scratch')
41
+
42
+ function Test-SkipPath([string]$rel) {
43
+ foreach ($seg in ($rel -split '[\\/]')) {
44
+ if ($skipDirs -contains $seg) { return $true }
45
+ }
46
+ return $false
47
+ }
48
+
49
+ $sb = [System.Collections.Generic.List[string]]::new()
50
+ function Add-Line([string]$s = '') { $sb.Add($s) }
51
+
52
+ function Get-Section([string[]]$content, [string]$number) {
53
+ $start = -1
54
+ for ($i = 0; $i -lt $content.Count; $i++) {
55
+ if ($content[$i] -match ('^##\s*' + $number + '\s*[、..]')) { $start = $i + 1; break }
56
+ }
57
+ if ($start -lt 0) { return @() }
58
+ $out = @()
59
+ for ($i = $start; $i -lt $content.Count; $i++) {
60
+ if ($content[$i] -match '^##\s') { break }
61
+ $out += $content[$i]
62
+ }
63
+ return $out
64
+ }
65
+
66
+ function Get-NonEmpty([string[]]$lines) {
67
+ $out = @()
68
+ foreach ($l in $lines) {
69
+ $t = $l.Trim()
70
+ if ($t -eq '' -or $t -match '^---+$') { continue }
71
+ $out += $t
72
+ }
73
+ return $out
74
+ }
75
+
76
+ function Get-ListItems([string[]]$lines) {
77
+ $out = @()
78
+ foreach ($l in $lines) {
79
+ if ($l -match '^\s*(?:\d+[\.、]|[-*])\s+(.+)$') { $out += $Matches[1].Trim() }
80
+ }
81
+ return $out
82
+ }
83
+
84
+ function Get-AgeDays([string]$text) {
85
+ if ($text -match '^(\d{4}-\d{2}-\d{2})') {
86
+ try {
87
+ $d = [datetime]::ParseExact($Matches[1], 'yyyy-MM-dd', $null)
88
+ $age = ((Get-Date).Date - $d.Date).Days
89
+ if ($age -ge 0) { return $age }
90
+ } catch { }
91
+ }
92
+ return -1
93
+ }
94
+
95
+ function Format-Size([long]$bytes) {
96
+ if ($bytes -ge 1MB) { return ('{0:N1} MB' -f ($bytes / 1MB)) }
97
+ if ($bytes -ge 1KB) { return ('{0:N1} KB' -f ($bytes / 1KB)) }
98
+ return "$bytes B"
99
+ }
100
+
101
+ # ---------- 读取区间内的日报与源数据 ----------
102
+ $daily = @{}
103
+ $missingDaily = @()
104
+ $dailySizes = @{}
105
+ foreach ($day in $days) {
106
+ $p = Join-Path $ReportDir "日报-$day.md"
107
+ if (Test-Path $p) {
108
+ $daily[$day] = @(Get-Content $p -Encoding UTF8)
109
+ $dailySizes[$day] = (Get-Item $p).Length
110
+ } else {
111
+ $missingDaily += $day
112
+ }
113
+ }
114
+
115
+ $missingSource = @()
116
+ foreach ($day in $days) {
117
+ if (-not (Test-Path (Join-Path $outDir "$day.md"))) { $missingSource += $day }
118
+ }
119
+
120
+ # ---------- 工作区改动 ----------
121
+ $files = @()
122
+ if (Test-Path $Workspace) {
123
+ $files = Get-ChildItem -Path $Workspace -Recurse -File -Force -ErrorAction SilentlyContinue |
124
+ Where-Object {
125
+ $_.LastWriteTime -ge $fromDay -and $_.LastWriteTime -lt $rangeEnd -and
126
+ -not (Test-SkipPath $_.FullName.Substring($Workspace.Length))
127
+ } | Sort-Object LastWriteTime
128
+ }
129
+
130
+ # ---------- Git 区间提交 ----------
131
+ $gitExe = (Get-Command git -ErrorAction SilentlyContinue).Source
132
+ $repos = @()
133
+ if ($gitExe -and (Test-Path $Workspace)) {
134
+ $repos = Get-ChildItem -Path $Workspace -Directory -Recurse -Depth 3 -Force -ErrorAction SilentlyContinue |
135
+ Where-Object { Test-Path (Join-Path $_.FullName '.git') } |
136
+ Select-Object -ExpandProperty FullName
137
+ }
138
+ $gitBlocks = @()
139
+ $gitCommitCount = 0
140
+ foreach ($repo in $repos) {
141
+ $rel = if ($repo -eq $Workspace) { '.' } else { $repo.Substring($Workspace.Length).TrimStart('\') }
142
+ $log = @(& $gitExe -C $repo log --since="$From 00:00:00" --until="$To 23:59:59" --pretty='%h %ad %s' --date=format:'%m-%d' --no-decorate 2>$null)
143
+ $branch = (& $gitExe -C $repo rev-parse --abbrev-ref HEAD 2>$null)
144
+ $dirty = @(& $gitExe -C $repo status --porcelain 2>$null).Count
145
+ if ($log.Count -gt 0 -or $dirty -gt 0) {
146
+ $gitBlocks += [pscustomobject]@{ Rel = $rel; Branch = $branch; Log = $log; Dirty = $dirty }
147
+ $gitCommitCount += $log.Count
148
+ }
149
+ }
150
+
151
+ # ---------- 看板 ----------
152
+ $boardPath = Join-Path $ReportDir '看板.md'
153
+ $boardCounts = @{}
154
+ $openItems = @()
155
+ if (Test-Path $boardPath) {
156
+ $cur = ''
157
+ foreach ($line in (Get-Content $boardPath -Encoding UTF8)) {
158
+ if ($line -match '^##\s*(.+?)\s*$') {
159
+ $cur = $Matches[1]
160
+ if (-not $boardCounts.ContainsKey($cur)) { $boardCounts[$cur] = 0 }
161
+ continue
162
+ }
163
+ if ($line -match '^\s*-\s*\[ \]\s*(.+)$') {
164
+ if ($cur) { $boardCounts[$cur] = [int]$boardCounts[$cur] + 1 }
165
+ $openItems += $Matches[1].Trim()
166
+ }
167
+ }
168
+ }
169
+ $openTotal = 0
170
+ foreach ($v in $boardCounts.Values) { $openTotal += [int]$v }
171
+
172
+ # ---------- 输出 ----------
173
+ Add-Line "# 周期痕迹源数据 · $From ~ $To"
174
+ Add-Line ''
175
+ Add-Line "> 采集窗口:$From 00:00:00 ~ $To 23:59:59($span 天,本地时区 $([DateTime]::Now.ToString('zzz')))"
176
+ Add-Line '> 本文件由 `collect-range.ps1` 只读生成,用于周报/月报的事实依据;不要手工编辑。'
177
+ Add-Line ''
178
+
179
+ Add-Line '## 1. 区间概况'
180
+ Add-Line ''
181
+ Add-Line "- 区间:$From ~ $To($span 天,含首尾)"
182
+ Add-Line "- 日报:有 $(($days.Count - $missingDaily.Count)) 天 / 缺 $($missingDaily.Count) 天"
183
+ Add-Line "- 源数据:有 $(($days.Count - $missingSource.Count)) 天 / 缺 $($missingSource.Count) 天"
184
+ Add-Line "- 工作区改动:$($files.Count) 个文件"
185
+ Add-Line "- Git:$($repos.Count) 个仓库,$gitCommitCount 条提交"
186
+ Add-Line "- 看板:未完成 $openTotal 条"
187
+ Add-Line ''
188
+
189
+ Add-Line '## 2. 日报覆盖'
190
+ Add-Line ''
191
+ if ($daily.Count -eq 0) {
192
+ Add-Line '_区间内没有任何日报。_'
193
+ } else {
194
+ foreach ($day in $days) {
195
+ if ($daily.ContainsKey($day)) {
196
+ Add-Line ("- ``日报-$day.md``({0})" -f (Format-Size $dailySizes[$day]))
197
+ }
198
+ }
199
+ if ($missingDaily.Count -gt 0) {
200
+ Add-Line ''
201
+ Add-Line ("缺日报 {0} 天:{1}" -f $missingDaily.Count, ($missingDaily -join '、'))
202
+ }
203
+ }
204
+ Add-Line ''
205
+
206
+ Add-Line '## 3. 每日概览摘录(一)'
207
+ Add-Line ''
208
+ if ($daily.Count -eq 0) {
209
+ Add-Line '_无日报可摘。_'
210
+ } else {
211
+ foreach ($day in $days) {
212
+ if (-not $daily.ContainsKey($day)) { continue }
213
+ Add-Line "### $day"
214
+ Add-Line ''
215
+ $lines = @(Get-NonEmpty @(Get-Section $daily[$day] '一'))
216
+ if ($lines.Count -eq 0) {
217
+ Add-Line '_该日概览为空。_'
218
+ } else {
219
+ foreach ($l in ($lines | Select-Object -First 10)) { Add-Line "- $l" }
220
+ }
221
+ Add-Line ''
222
+ }
223
+ }
224
+
225
+ Add-Line '## 4. 产出汇总(三)'
226
+ Add-Line ''
227
+ if ($daily.Count -eq 0) {
228
+ Add-Line '_无日报可摘。_'
229
+ } else {
230
+ foreach ($day in $days) {
231
+ if (-not $daily.ContainsKey($day)) { continue }
232
+ Add-Line "### $day"
233
+ Add-Line ''
234
+ $lines = @(Get-NonEmpty @(Get-Section $daily[$day] '三'))
235
+ if ($lines.Count -eq 0) {
236
+ Add-Line '_该日没有产出记录。_'
237
+ } else {
238
+ foreach ($l in ($lines | Select-Object -First 40)) { Add-Line $l }
239
+ }
240
+ Add-Line ''
241
+ }
242
+ }
243
+
244
+ Add-Line '## 5. 问题与风险汇总(四)'
245
+ Add-Line ''
246
+ if ($daily.Count -eq 0) {
247
+ Add-Line '_无日报可摘。_'
248
+ } else {
249
+ foreach ($day in $days) {
250
+ if (-not $daily.ContainsKey($day)) { continue }
251
+ Add-Line "### $day"
252
+ Add-Line ''
253
+ $lines = @(Get-NonEmpty @(Get-Section $daily[$day] '四'))
254
+ if ($lines.Count -eq 0) {
255
+ Add-Line '_该日没有风险记录。_'
256
+ } else {
257
+ foreach ($l in ($lines | Select-Object -First 20)) { Add-Line $l }
258
+ }
259
+ Add-Line ''
260
+ }
261
+ }
262
+
263
+ Add-Line '## 6. 明日开场清单汇总(六)'
264
+ Add-Line ''
265
+ if ($daily.Count -eq 0) {
266
+ Add-Line '_无日报可摘。_'
267
+ } else {
268
+ foreach ($day in $days) {
269
+ if (-not $daily.ContainsKey($day)) { continue }
270
+ $items = @(Get-ListItems @(Get-Section $daily[$day] '六'))
271
+ if ($items.Count -eq 0) { continue }
272
+ Add-Line "### $day"
273
+ Add-Line ''
274
+ foreach ($it in $items) { Add-Line "- $it" }
275
+ Add-Line ''
276
+ }
277
+ }
278
+
279
+ Add-Line '## 7. 工作看板现状'
280
+ Add-Line ''
281
+ if ($boardCounts.Count -eq 0) {
282
+ Add-Line '_看板为空或不存在。_'
283
+ } else {
284
+ $parts = @()
285
+ foreach ($k in @('待办', '进行中', '已完成', '已放弃')) {
286
+ if ($boardCounts.ContainsKey($k)) { $parts += ("{0} {1}" -f $k, $boardCounts[$k]) }
287
+ }
288
+ foreach ($k in $boardCounts.Keys) {
289
+ if (@('待办', '进行中', '已完成', '已放弃') -notcontains $k) { $parts += ("{0} {1}" -f $k, $boardCounts[$k]) }
290
+ }
291
+ Add-Line ("- " + ($parts -join ' / '))
292
+ if ($openItems.Count -gt 0) {
293
+ Add-Line ''
294
+ Add-Line '### 未完成明细(含已挂天数)'
295
+ Add-Line ''
296
+ foreach ($it in ($openItems | Select-Object -First 30)) {
297
+ $age = Get-AgeDays $it
298
+ $tag = if ($age -ge 0) { "已挂 $age 天" } else { '未标日期' }
299
+ Add-Line "- ($tag)$it"
300
+ }
301
+ if ($openItems.Count -gt 30) { Add-Line "- …另有 $($openItems.Count - 30) 条" }
302
+ }
303
+ }
304
+ Add-Line ''
305
+
306
+ Add-Line '## 8. Git 区间提交'
307
+ Add-Line ''
308
+ if (-not $gitExe) {
309
+ Add-Line '_未找到 git,跳过。_'
310
+ } elseif ($gitBlocks.Count -eq 0) {
311
+ Add-Line '_区间内没有 Git 提交或未提交改动。_'
312
+ } else {
313
+ foreach ($b in $gitBlocks) {
314
+ Add-Line ("### ``{0}``(分支 {1},当前未提交 {2})" -f $b.Rel, $b.Branch, $b.Dirty)
315
+ Add-Line ''
316
+ if ($b.Log.Count -gt 0) {
317
+ foreach ($l in ($b.Log | Select-Object -First 80)) { Add-Line "- $l" }
318
+ } else {
319
+ Add-Line '_区间内无提交。_'
320
+ }
321
+ Add-Line ''
322
+ }
323
+ }
324
+ Add-Line ''
325
+
326
+ Add-Line '## 9. 工作区区间改动'
327
+ Add-Line ''
328
+ if ($files.Count -eq 0) {
329
+ Add-Line '_区间内工作区无文件改动。_'
330
+ } else {
331
+ Add-Line "共 $($files.Count) 个文件。"
332
+ Add-Line ''
333
+ Add-Line '### 按顶层目录汇总'
334
+ Add-Line ''
335
+ $files | Group-Object { ($_.FullName.Substring($Workspace.Length).TrimStart('\') -split '[\\/]')[0] } |
336
+ Sort-Object Count -Descending |
337
+ ForEach-Object { Add-Line ("- ``{0}``:{1} 个文件" -f $_.Name, $_.Count) }
338
+ Add-Line ''
339
+ Add-Line '### 最近改动的 40 个文件'
340
+ Add-Line ''
341
+ Add-Line '| 时间 | 大小 | 相对路径 |'
342
+ Add-Line '|------|------|----------|'
343
+ $recent = $files | Sort-Object LastWriteTime -Descending | Select-Object -First 40
344
+ foreach ($f in $recent) {
345
+ $rel = $f.FullName.Substring($Workspace.Length).TrimStart('\')
346
+ Add-Line ("| {0:MM-dd HH:mm} | {1} | {2} |" -f $f.LastWriteTime, (Format-Size $f.Length), $rel)
347
+ }
348
+ if ($files.Count -gt 40) { Add-Line ''; Add-Line "- …另有 $($files.Count - 40) 个更早的文件" }
349
+ }
350
+ Add-Line ''
351
+
352
+ Add-Line '## 10. 源数据覆盖'
353
+ Add-Line ''
354
+ if ($missingSource.Count -eq 0) {
355
+ Add-Line '_区间内每天的源数据都在。_'
356
+ } else {
357
+ Add-Line ("缺源数据 {0} 天:{1}" -f $missingSource.Count, ($missingSource -join '、'))
358
+ }
359
+ Add-Line ''
360
+
361
+ Add-Line '## 11. 复发候选(风险小节跨天重复)'
362
+ Add-Line ''
363
+ $riskDays = @{}
364
+ foreach ($day in $days) {
365
+ if (-not $daily.ContainsKey($day)) { continue }
366
+ foreach ($line in @(Get-ListItems @(Get-Section $daily[$day] '四'))) {
367
+ $key = ($line -replace '^\s*(?:\d+[\.、]|[-*])\s*', '' -replace '[`*]', '' -replace '([^)]*)', '').Trim()
368
+ # 数字与标点归一化:「第 12 天」「第 13 天」算同一件事
369
+ $key = $key -replace '\d+', '#'
370
+ $key = $key -replace '[\s,。;:、!?,.;:!?()()【】\[\]「」“”"-]', ''
371
+ if ($key.Length -lt 6) { continue }
372
+ if ($key.Length -gt 40) { $key = $key.Substring(0, 40) + '…' }
373
+ if (-not $riskDays.ContainsKey($key)) { $riskDays[$key] = @() }
374
+ if (-not ($riskDays[$key] -contains $day)) { $riskDays[$key] += $day }
375
+ }
376
+ }
377
+ $repeat = $riskDays.GetEnumerator() | Where-Object { $_.Value.Count -ge 2 } | Sort-Object { $_.Value.Count } -Descending
378
+ if (-not $repeat) {
379
+ Add-Line '_没有跨天重复的风险项。_'
380
+ } else {
381
+ foreach ($entry in ($repeat | Select-Object -First 10)) {
382
+ Add-Line ("- {0}:{1} 天({2})" -f $entry.Key, $entry.Value.Count, ($entry.Value -join '、'))
383
+ }
384
+ }
385
+ Add-Line ''
386
+
387
+ Set-Content -Path $outFile -Value $sb -Encoding UTF8
388
+ Write-Output $outFile