script-modules/RemoveModuleManifestComments.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function Remove-ModuleManifestComments {
  [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
  param (
    [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)][string]$ManifestPath,
    [switch]$NoConfirm
  )

  $originalManifestContent = Get-Content $ManifestPath
  $cleanManifest = @()
  
  $originalManifestContent | ForEach-Object { 
    if ($_ -match "(?(?=.*#)(.*(?=#))|(.*))" -and -not [string]::IsNullOrWhiteSpace($Matches[0])) { 
      $cleanManifest += $Matches[0]
    } 
  }
  
  for ($i = 1; $i -le $cleanManifest.Length - 2; $i++) {
    if ($cleanManifest[$i][0] -ne "`t") {
      $cleanManifest[$i] = "`t$($cleanManifest[$i])"
    }
  }
  
  $query = "This action will overwrite the existing module manifest file. (And make it look nicer in the process.) Continue?"
  if ($NoConfirm -or $PSCmdlet.ShouldContinue($query, "*****Warning*****")) {
    Set-Content -Path $ManifestPath -Value $cleanManifest
  }
}

Export-ModuleMember *-*