Files
drmaterac.pl/scripts/changelog.ps1
2026-04-01 01:17:17 +02:00

118 lines
3.3 KiB
PowerShell

param(
[string]$ProjectRoot = (Get-Location).Path,
[string]$Date = (Get-Date -Format 'yyyy-MM-dd'),
[string[]]$WhatDone,
[string[]]$Files,
[switch]$NoGit
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$projectPath = (Resolve-Path -LiteralPath $ProjectRoot).Path
$changelogDir = Join-Path $projectPath 'changelog'
if (-not (Test-Path -LiteralPath $changelogDir)) {
New-Item -ItemType Directory -Path $changelogDir | Out-Null
}
$targetFile = Join-Path $changelogDir ("{0}.md" -f $Date)
function Get-GitChangedFiles {
param([string]$Root)
if ($NoGit) { return @() }
if (-not (Get-Command git -ErrorAction SilentlyContinue)) { return @() }
$inside = & git -C $Root rev-parse --is-inside-work-tree 2>$null
if ($LASTEXITCODE -ne 0 -or $inside -ne 'true') { return @() }
$lines = & git -C $Root status --porcelain
$paths = @()
foreach ($line in $lines) {
if ([string]::IsNullOrWhiteSpace($line) -or $line.Length -lt 4) { continue }
$raw = $line.Substring(3).Trim()
if ($raw -match ' -> ') {
$raw = ($raw -split ' -> ')[-1].Trim()
}
$raw = $raw.Trim('"')
if (-not [string]::IsNullOrWhiteSpace($raw)) {
$paths += $raw
}
}
return $paths | Sort-Object -Unique
}
function Merge-Unique {
param([string[]]$Existing, [string[]]$Incoming)
$combined = @()
$seen = @{}
foreach ($item in @($Existing + $Incoming)) {
if ([string]::IsNullOrWhiteSpace($item)) { continue }
$k = $item.Trim()
if (-not $seen.ContainsKey($k)) {
$seen[$k] = $true
$combined += $k
}
}
return $combined
}
$changedFiles = if ($Files -and $Files.Count -gt 0) { $Files } else { Get-GitChangedFiles -Root $projectPath }
if (-not $WhatDone -or $WhatDone.Count -eq 0) {
$WhatDone = @('Aktualizacja projektu oraz uporzadkowanie zmian w repozytorium.')
}
$existingWhatDone = @()
$existingFiles = @()
if (Test-Path -LiteralPath $targetFile) {
$content = Get-Content -LiteralPath $targetFile
$section = ''
foreach ($line in $content) {
if ($line -match '^##\s+Co zrobiono') { $section = 'done'; continue }
if ($line -match '^##\s+Zmienione pliki') { $section = 'files'; continue }
if ($line -match '^-\s+(.*)$') {
$value = $matches[1].Trim()
if ($section -eq 'done') {
$existingWhatDone += $value
} elseif ($section -eq 'files') {
$existingFiles += $value.Trim('`')
}
}
}
}
$finalWhatDone = Merge-Unique -Existing $existingWhatDone -Incoming $WhatDone
$finalFiles = Merge-Unique -Existing $existingFiles -Incoming $changedFiles
$sb = New-Object System.Text.StringBuilder
[void]$sb.AppendLine("# $Date")
[void]$sb.AppendLine()
[void]$sb.AppendLine('## Co zrobiono')
foreach ($item in $finalWhatDone) {
[void]$sb.AppendLine("- $item")
}
[void]$sb.AppendLine()
[void]$sb.AppendLine('## Zmienione pliki')
if ($finalFiles.Count -eq 0) {
[void]$sb.AppendLine('- Brak wykrytych zmian plikow (uzupelnij recznie, jesli potrzeba).')
} else {
foreach ($file in $finalFiles) {
[void]$sb.AppendLine(('- `{0}`' -f $file))
}
}
Set-Content -LiteralPath $targetFile -Value $sb.ToString() -Encoding UTF8
Write-Output ("Changelog updated: {0}" -f $targetFile)