ps/Modules/.build/Update-FormatsToProcessInPSD1.ps1
2023-05-30 22:51:22 -07:00

61 lines
2.1 KiB
PowerShell

Function Update-FormatsToProcessInPSD1 {
<#
.SYNOPSIS
Updates the PSD1 FormatsToProcess entry
.EXAMPLE
Update-FormatsToProcessInPSD1 .\Alkami.PowerShell.IIS\Public .\Alkami.PowerShell.IIS\Alkami.PowerShell.IIS.psd1
.PARAMETER FolderPath
The name of the folder to examine all files under.
.PARAMETER Psd1FilePath
The file to be rewritten. (This is called out in case more than one .psd1 file exists in a folder.)
#>
[CmdletBinding()]
Param (
[String]$FolderPath,
[String]$Psd1FilePath
)
process {
# In the future, may want to ship all the format files separately
# Currently there is Get-ContentFromFormatFilesInPath and Join-PS1XMLFromFiles that smacks these two together
# So for now, we just want to write out the name of the built ps1xml to the psd1
# The names are the same, so that's easy.
$contents = (Get-Content $Psd1FilePath)
$newContents = @()
$projectName = [System.IO.Path]::GetFileNameWithoutExtension($psd1Filepath)
$magicWord = 'FormatsToProcess'
$magicConcat = " = `"$projectName.ps1xml`""
$foundFormatsLine = $false
foreach($line in $contents) {
if ($line.Trim().StartsWith($magicWord)) {
$splits = $line -split '='
$line = $splits[0].TrimEnd() + $magicConcat
$foundFormatsLine = $true
}
$newContents += $line
}
if (!$foundFormatsLine) {
$newContents = @()
## We couldn't find an existing formats line in that PSD1, so we need to add one.
## We are gonna add it after FunctionsToExport
$duplicatableTag = 'FunctionsToExport'
foreach($line in $contents) {
$newContents += $line
if ($line.Trim().StartsWith($duplicatableTag)) {
$splits = $line -split '='
$newLine = $splits[0].TrimEnd().Replace($duplicatableTag,$magicWord) + $magicConcat
$newContents += $newLine
}
}
}
Set-Content -Path $Psd1FilePath -Value $newContents
}
}