Private/Build/Read-BrownserveInitFile.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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
function Read-BrownserveInitFile { [CmdletBinding()] param ( # The path to the init file [Parameter( Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0 )] [string] $InitFilePath ) # Import the _init files content as an array (so we can read line-by-line) try { $InitContent = Get-Content $InitFilePath } catch { throw "Failed to import _init file content from $InitFilePath.`n$($_.Exception.Message)" } # The regex for matching our opening and closing lines $CustomCodeOpener = "\#\#\# Start user defined _init steps" $CustomCodeClosing = "\#\#\# End user defined _init steps" # Set up our special variables for counting lines $LineCount = 0 $CustomCodeStart = $null $CustomCodeEnd = $null # Start reading through the _init script line by line... $InitContent | ForEach-Object { $Line = $_.Trim() # If we haven't already found our custom code opening block then see if this line matches it if (-not $CustomCodeStart) { $RegexMatch = [regex]::Match($Line, $CustomCodeOpener) if ($RegexMatch.Success) { # Our custom code will start on the next line _after_ this one $CustomCodeStart = $LineCount + 1 Write-Verbose "User defined _init content starts on line $CustomCodeStart" } } if (-not $CustomCodeEnd) { $RegexMatch = [regex]::Match($Line, $CustomCodeClosing) if ($RegexMatch.Success) { # Our custom code will end on the next line _before_ this one $CustomCodeEnd = $LineCount - 1 Write-Verbose "User defined _init content ends on line $CustomCodeEnd" } } # Increment our line counter $LineCount ++ } # If we've actually found something then return it if ($CustomCodeStart -and $CustomCodeEnd) { # Extract those lines from the array $CustomCode = $InitContent[$CustomCodeStart..$CustomCodeEnd] # Only return something if we have something to return if ($CustomCode) { # This covers cases where people have accidentally deleted the line in the in-between if (-not ($CustomCode -match $CustomCodeOpener)) { $Return = [pscustomobject]@{ CustomCode = $CustomCode } Return $Return } } } # Otherwise raise an error else { throw "Unable to find 'user defined _init steps' block in $InitFilePath." } } |