private/config/Compile-Full-Config.ps1
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
function Compile-Full-Config { param ([string]$MultipleConfig) [Scriptblock]$matchEvaluator = { param ($match) $include_path = $match.Groups[1].Value.Trim() # Check if it's a file or directory if ($include_path -and (Test-Path $include_path)) { $item = Get-Item $include_path if (Test-Path $item.FullName -PathType Container) { # It's a directory. Include content of all files inside it $content = "" Get-ChildItem $item | ForEach-Object { Write-Verbose "CONFIG: Including file $($item.FullName)" Write-Verbose "CONFIG: Reading file $($item.FullName)" $content += Get-Content $_.FullName -Raw } }else { # It's a single file. Include its content Write-Verbose "CONFIG: Including file $($item.FullName)" Write-Verbose "CONFIG: Reading file $($item.FullName)" $content = Get-Content $include_path -Raw } }else { Write-Verbose "CONFIG: Ignoring included path $include_path because it is invalid." } # Return the replacement value if ($content) { "`n$content" } } # Remove all comments (i.e. starting with '#') [Regex]$remove_comments_regex = '#.*' $MultipleConfig = $remove_comments_regex.Replace($MultipleConfig, '') # Remove all within-block 'include' directives [Regex]$include_regex = '({[^}]*?)(include[^\n]*)([^}]*})' $MultipleConfig = $include_regex.Replace($MultipleConfig, '$1$3') # Insert all 'include' directives' paths' content [Regex]$include_regex = '\s*include([^\n]*)' $MultipleConfig = $include_regex.Replace($MultipleConfig, $matchEvaluator) # Remove all comments (i.e. starting with '#') [Regex]$remove_comments_regex = '#.*' $MultipleConfig = $remove_comments_regex.Replace($MultipleConfig, '') # Return compiled config $MultipleConfig } |