Skip to content

Instantly share code, notes, and snippets.

@nilsandrey
Created June 10, 2024 20:38
Show Gist options
  • Save nilsandrey/06d095c6b55c7ed5862a246146a81cc6 to your computer and use it in GitHub Desktop.
Save nilsandrey/06d095c6b55c7ed5862a246146a81cc6 to your computer and use it in GitHub Desktop.
Create a README.md with the list of all ADRs found in the current folder, the status extracted from section "## Status" and the notes from first paragraph of the section "## Context"
## Take adrfolder from -Path switch command line argument and default to current directory
# Define the folder containing the ADR markdown files
$adrFolder = $args[0] -split ' ' | Where-Object { $_ -ne '' } | Select-Object -First 1
if (-not $adrFolder) { $adrFolder = $PSScriptRoot }
# Define the summary markdown file path
$summaryFile = "$adrFolder\README.md"
# Initialize an array to store ADR statuses and notes
$adrDetails = @()
# Function to extract the status from an ADR markdown file
function Get-ADRStatus {
param (
[string]$filePath
)
$statusSection = $false
$status = "Unknown"
# Read the file line by line
Get-Content $filePath | ForEach-Object {
if ($_ -match "## Status") {
$statusSection = $true
} elseif ($statusSection -and $_ -match "^(.*\S.*)$") {
$status = $matches[1].Trim()
$statusSection = $false
}
}
return $status
}
# Function to extract the first paragraph from the "### Context" section
function Get-ADRContext {
param (
[string]$filePath
)
$contextSection = $false
$context = ""
# Read the file line by line
Get-Content $filePath | ForEach-Object {
if ($_ -match "## Context") {
$contextSection = $true
} elseif ($contextSection -and $_ -match "^(.*\S.*)$") {
$context = $matches[1].Trim()
$contextSection = $false
}
}
return $context
}
# Iterate through all markdown files in the folder
Get-ChildItem -Path $adrFolder -Filter *.md | ForEach-Object {
$file = $_.FullName
$filename = $_.Name
if ($filename -eq "README.md") { return }
$status = Get-ADRStatus -filePath $file
$context = Get-ADRContext -filePath $file
$adrDetails += [PSCustomObject]@{
Filename = $filename
Status = $status
Context = $context
}
}
# Create or update the summary markdown file
$summaryContent = @(
"# ADR Summary",
"",
"| Filename | Status | Notes |",
"|----------|--------|-------|"
)
# Append ADR statuses and contexts to the summary content
foreach ($adr in $adrDetails) {
$summaryContent += "| $($adr.Filename) | $($adr.Status) | $($adr.Context) |"
}
# Write the summary content to the summary file
$summaryContent | Set-Content -Path $summaryFile
Write-Output "ADR summary has been updated at $summaryFile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment