Devdeer.Tools.psm1
######################################################################################## # # Devdeer.Tools Module # # -------------------------------------------------------------------------------------- # # DEVDEER GmbH # Alexander Schmidt # # -------------------------------------------------------------------------------------- # # Provides helpers for PowerShell. # ######################################################################################## <# .Synopsis Replaces a function call in a JSON file with the contents of a given file. .Description .Parameter File The path to the file in which the replacement should happen and which has the $FunctionName defined. .Parameter FunctionName The name of the function inside the $File (e.g. `[FunctionName('filePath')]`) .Parameter MakeOneLine Indicates if the line breaks should be removed before replacement. .OUTPUTS System.String. Invoke-JsonContentReplacement returns the original content of the file so that the caller can undo replacement later. .Example # Insert JSON based on the function `getJson` in the `parameters.json` file. You would use # `[getJson('sample.json')]` to define the replace position and the source file for the JSON. PS> Invoke-JsonContentReplacement -File .\parameters.json -FunctionName getJson #> Function Invoke-JsonContentReplacement { param ( [Parameter(Mandatory = $true)] [string] $File, [Parameter(Mandatory = $true)] [string] $FunctionName, [bool] $MakeOneLine = $true ) $path = Split-Path $File $ParameterFileContent = Get-Content $File -Raw $fileName = [regex]::Matches($ParameterFileContent, "$FunctionName\('(.*?)'").captures.groups[1].value $fileName = Join-Path -Path $path -ChildPath $fileName $jsonContent = (Get-Content $fileName -Raw).Replace('"', '\"') if ($MakeOneLine) { $jsonContent = $jsonContent.Replace("`n","").Replace("`r","").Replace("`t","").Replace(" ", "") } $result = [regex]::Replace($ParameterFileContent, "[\[]$FunctionName\('(.*?)'\)[\]]", $jsonContent) Set-Content $File -Value $result return $oldContent } Export-ModuleMember -Function Invoke-JsonContentReplacement |