Skip to content

Commit

Permalink
upd: rewrite and support stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
sionta committed Sep 3, 2023
1 parent b8db5f0 commit f9ff451
Showing 1 changed file with 50 additions and 35 deletions.
85 changes: 50 additions & 35 deletions scripts/changelog.ps1
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
#!/usr/bin/pwsh -c
param ([Alias('p')][string]$Path)
try {
[System.IO.Directory]::SetCurrentDirectory("$PSScriptRoot/../")
$ROOT_PATH = [System.IO.Directory]::GetCurrentDirectory()
$CHANGELOG = [System.IO.Path]::Combine($ROOT_PATH, 'CHANGELOG.md')
$readLines = [System.IO.File]::ReadAllLines($CHANGELOG)
$header = $readLines -match '^## \[([\d.]+)\] - \d{4}-\d{2}-\d{2}'
} finally {
if ($header[0]) {
$writelines = @()
foreach ($line in $readLines) {
if ($line -eq $header[1]) { break }
if ($line -ne $header[0] -and $line.Length -ne 0) {
if ($line.StartsWith('[Full Changelog]')) { $writelines += '' }
$writelines += $line
}
}
if ($Path -and !([System.IO.File]::Exists($Path))) {
$dirPath = [System.IO.Path]::GetDirectoryName($Path)
if ([System.IO.Directory]::Exists($dirPath)) {
[System.IO.Directory]::CreateDirectory($dirPath) | Out-Null
}
$fileName = [System.IO.Path]::GetFileName($Path)
$newChangelogPath = [System.IO.Path]::Combine($dirPath, $fileName)
} else {
$newChangelogPath = [System.IO.Path]::Combine($ROOT_PATH, 'CHANGES.MD')
param (
# Save as output to file name.
[Alias('o')][string]$OutFile,
# Update header date to current date.
[Alias('u')][switch]$NowDate
)

$ROOT_PATH = [System.IO.Path]::GetFullPath("$PSScriptRoot/..")
$inFileChangelog = [System.IO.Path]::Combine($ROOT_PATH, 'CHANGELOG.md')
if (!([System.IO.File]::Exists($inFileChangelog))) {
"File could not be found '$inFileChangelog'."
exit 1
}

if ($OutFile -and $OutFile.EndsWith('CHANGELOG.md')) {
"File cannot be the same as '$inFileChangelog'."
exit 1
}

[System.Console]::InputEncoding = [System.Console]::OutputEncoding = $OutputEncoding = [System.Text.UTF8Encoding]::new()
$readLines = [System.IO.File]::ReadAllLines($inFileChangelog).Trim()
$headings = $readLines -match '^##.\[([\d.]+)\].-.\d{4}-\d{2}-\d{2}'
$footers = $readLines -match '^\[Full Changelog\]\(.*\)'
$currentDate = [string]::Format('{0:yyyy-MM-dd}', [datetime]::Now)

$startHeader = $headings[0]

if ($startHeader) {
$writeLines = [System.Collections.Generic.List[string]]::new()
foreach ($line in $readLines) {
if ($line -eq $footers[0]) { $writeLines.Add($line); break }
if ($line -eq $headings[1]) { break }; $writeLines.Add($line)
}
if ($NowDate) {
$semanticVersion = $startHeader.Split(' ')[1].Trim('[]')
$newStartHeader = "## [$semanticVersion] - $currentDate"
if ($startHeader -ne $newStartHeader) {
$writeLines = $writeLines -replace $startHeader, $newStartHeader
}
[System.IO.File]::WriteAllLines($newChangelogPath, $writelines)
# replacing old date to current date
$version = $header[0].Split(' ')[1].Trim('[]')
$newDate = [string]::Format('{0:yyyy-MM-dd}', [datetime]::Now)
$oldHeader = "^## \[$version\] - \d{4}-\d{2}-\d{2}"
$newHeader = "## [$version] - $newDate"
if ($header[0] -ne $newHeader) {
$newChangelogDate = $readLines -replace $oldHeader, $newHeader
[System.IO.File]::WriteAllLines($CHANGELOG, $newChangelogDate)
}
if ($OutFile) {
$DirPath = [System.IO.Path]::GetDirectoryName([System.IO.Path]::GetFullPath($OutFile))
if (!([System.IO.Directory]::Exists($DirPath))) {
[System.IO.Directory]::CreateDirectory($DirPath) | Out-Null
}
[System.IO.File]::WriteAllLines($OutFile, $writeLines)
} else {
return $writeLines
}
} else {
"Mismatched heading like '## [semver] - yyyy-MM-dd'."
"example heading: ## [1.0.0] - $currentDate"
exit 1
}

0 comments on commit f9ff451

Please sign in to comment.