Functions/GenXdev.Git/Add-LineToREADME.ps1
############################################################################### <# .SYNOPSIS Adds a line to a README.md markdown file in a specified section. .DESCRIPTION Finds and modifies a README.md file by adding a new line to a specified section. Can create the section if it doesn't exist. Supports formatting lines as code blocks and showing the modified section. Will look in current directory first, then walk up directories to find the README location. If not found, will use the README in the PowerShell profile directory. .PARAMETER Line The line of text to add to the README file. .PARAMETER Section The section header where the line should be added. .PARAMETER Prefix The prefix to add before the line (default: "* "). .PARAMETER Code Switch to open the README in Visual Studio Code after modification. .PARAMETER Show Switch to display the modified section after changes. .PARAMETER Done Switch to mark a todo item as completed. .PARAMETER UseHomeREADME Switch to use README in PowerShell profile directory. .PARAMETER UseOneDriveREADME Switch to use README in OneDrive directory. .EXAMPLE Add-LineToREADME -Line "New feature" -Section "## Features" -Prefix "- [ ] " .EXAMPLE Add-LineToREADME "New feature" "## Features" "* " -Show #> function Add-LineToREADME { [CmdletBinding()] param( ####################################################################### [Parameter( Position = 0, Mandatory = $false, ValueFromRemainingArguments = $false, HelpMessage = "The line to add to the README" )] [string] $Line = "", ####################################################################### [Parameter( Position = 1, Mandatory = $true, HelpMessage = "The section to add the line to" )] [string] $Section, ####################################################################### [Parameter( Position = 2, Mandatory = $true, HelpMessage = "The prefix to use for the line" )] [string] $Prefix = "* ", ####################################################################### [Parameter( Mandatory = $false, HelpMessage = "Open in Visual Studio Code after modifying" )] [switch] $Code, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = "Show the modified section" )] [switch] $Show, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = "Mark the item as completed" )] [switch] $Done, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = "Use README in PowerShell profile directory" )] [switch] $UseHomeREADME, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = "Use README in OneDrive directory" )] [switch] $UseOneDriveREADME ) begin { # helper function to find readme path by walking up directories function findReadMePath([string] $startDir) { $startDir = Expand-Path $startDir Push-Location try { Set-Location $startDir # walk up directories looking for README.md do { if ([System.IO.File]::Exists("$pwd\README.md")) { return "$pwd\README.md" } Set-Location .. } while (($startDir.Length -ge 2) -and ($startDir -ne $PWD)) } finally { Pop-Location } # fallback to onedrive readme return "$((Expand-Path -FilePath "~\Onedrive\README.md" -CreateDirectory))"; } # determine which readme file to use if ($UseHomeREADME) { $readmePath = Expand-Path ` -FilePath "$([IO.Path]::GetDirectoryName($Profile))\README.md" } elseif ($UseOneDriveREADME) { $readmePath = Expand-Path ` -FilePath "~\Onedrive\README.md" ` -CreateDirectory } else { $readmePath = findReadMePath $PWD } Write-Verbose "Using README at: $readmePath" # verify readme exists if (![IO.File]::Exists($readmePath)) { throw "README file not found at: $readmePath" } # load readme content $readme = [IO.File]::ReadAllText($readmePath) [int] $insertIndex = $readme.IndexOf("$Section") # create section if it doesn't exist if ($insertIndex -lt 0) { $insertIndex = 0 $readme = "$Section`r`n`r`n$readme" } $insertIndex += "$Section`r".Length } process { if (![string]::IsNullOrWhiteSpace($Line)) { if ($Done) { # mark item as completed $readme = $readme.Replace( "$prefix$Line`r`n", "- [✅] $($Line.Trim())`r`n" ) } else { # insert new line at section $readme = $readme.Substring(0, $insertIndex) + "$prefix$Line`r`n" + $readme.Substring($insertIndex) } } } end { # save changes [IO.File]::WriteAllText($readmePath, $readme) # open in vscode if requested if ($Code) { $codeCmd = Get-Command "code.cmd" -ErrorAction SilentlyContinue if ($null -ne $codeCmd) { cmd.exe /c code.cmd $readmePath } } # show modified section if requested if ($Show) { $readmeMarkDown = "" $inSection = $false $ansiColorStart = [char]27 + "[" $ansiColorYellow = "${ansiColorStart}33m" $ansiBackgroundColorBlue = "${ansiColorStart}44m" $ansiColorReset = "${ansiColorStart}0m" foreach ($readmeLine in [IO.File]::ReadAllLines($readmePath)) { if ($readmeLine.StartsWith($Section)) { $inSection = $true $readmeMarkDown = "$ansiBackgroundColorBlue$ansiColorYellow" + "$readmeMarkDown$($readmeLine.trim("`r`n`t "))" + "$ansiColorReset`r`n`r`n" } elseif ($inSection) { if ($readmeLine.StartsWith("#")) { break } $readmeMarkDown = "$readmeMarkDown" + "$($readmeLine.trim("`r`n`t "))`r`n`r`n" } } $readmeMarkDown } } } |