MergeTagCheck.psm1
|
Set-StrictMode -Version Latest function Test-MergeTag { <# .SYNOPSIS Check a template's {{ merge_tags }} against a CSV: unknown tags, blank cells per tag, rows ready to send. .DESCRIPTION The template is a text file with merge tags written {{ field_name }}; spaces inside the braces are optional and the name is letters, digits and underscore. Tags with a fallback syntax (for example {{ name | there }}) are not supported and are not recognised as tags. The CSV needs a header row; header matching is case-insensitive. Returns one object with Tags (Tag, ColumnFound, BlankRows), UnknownTags, RowsTotal, RowsReady and Report (the text report). A row is ready when every tag has a non-blank value. A row is never ready if a tag has no column. Throws on a missing file, a template with no tags, or a CSV with no data rows. .EXAMPLE Test-MergeTag -TemplatePath template.txt -CsvPath sample.csv #> [CmdletBinding()] param( [Parameter(Mandatory)][string]$TemplatePath, [Parameter(Mandatory)][string]$CsvPath ) if (-not (Test-Path -LiteralPath $TemplatePath -PathType Leaf)) { throw "file not found: $TemplatePath" } if (-not (Test-Path -LiteralPath $CsvPath -PathType Leaf)) { throw "file not found: $CsvPath" } $text = Get-Content -LiteralPath $TemplatePath -Raw if ([string]::IsNullOrWhiteSpace($text)) { throw 'template is empty: it has no merge tags' } $tags = New-Object System.Collections.Generic.List[string] $seen = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase) foreach ($m in [regex]::Matches($text, '\{\{\s*([A-Za-z0-9_]+)\s*\}\}')) { $name = $m.Groups[1].Value if ($seen.Add($name)) { $tags.Add($name) } } if ($tags.Count -eq 0) { throw 'template has no merge tags (expected {{ field_name }})' } $rows = @(Import-Csv -LiteralPath $CsvPath) if ($rows.Count -eq 0) { throw 'CSV has no data rows' } $headers = @($rows[0].PSObject.Properties.Name) $results = New-Object System.Collections.Generic.List[object] $unknown = New-Object System.Collections.Generic.List[string] $columnOf = @{} foreach ($t in $tags) { $col = $null foreach ($h in $headers) { if ($h -ieq $t) { $col = $h; break } } if ($null -eq $col) { $unknown.Add($t) $results.Add([pscustomobject]@{ Tag = $t; ColumnFound = $false; BlankRows = $null }) } else { $columnOf[$t] = $col $blank = 0 foreach ($r in $rows) { if ([string]::IsNullOrWhiteSpace([string]$r.PSObject.Properties[$col].Value)) { $blank++ } } $results.Add([pscustomobject]@{ Tag = $t; ColumnFound = $true; BlankRows = $blank }) } } $ready = 0 if ($unknown.Count -eq 0) { foreach ($r in $rows) { $ok = $true foreach ($t in $tags) { if ([string]::IsNullOrWhiteSpace([string]$r.PSObject.Properties[$columnOf[$t]].Value)) { $ok = $false; break } } if ($ok) { $ready++ } } } $w = 3 foreach ($t in $tags) { if ($t.Length -gt $w) { $w = $t.Length } } $lines = New-Object System.Collections.Generic.List[string] $lines.Add("Tags in template: $($tags.Count)") $lines.Add(('{0} {1} {2}' -f 'Tag'.PadRight($w), 'Column'.PadRight(6), 'Blank rows')) foreach ($x in $results) { $found = if ($x.ColumnFound) { 'yes' } else { 'no' } $blankText = if ($x.ColumnFound) { [string]$x.BlankRows } else { '-' } $lines.Add(('{0} {1} {2}' -f $x.Tag.PadRight($w), $found.PadRight(6), $blankText)) } $unknownText = if ($unknown.Count -eq 0) { 'none' } else { $unknown -join ', ' } $lines.Add("Unknown tags (no such column): $unknownText") $lines.Add("Rows total: $($rows.Count)") $lines.Add("Rows ready: $ready") [pscustomobject]@{ Tags = $results.ToArray() UnknownTags = $unknown.ToArray() RowsTotal = $rows.Count RowsReady = $ready Report = ($lines -join [Environment]::NewLine) } } Export-ModuleMember -Function Test-MergeTag |