internal/functions/templateFunctions/Assert-TemplateFunctions.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
87
88
89
90
function Assert-TemplateFunctions
{
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [string] $InputTemplate
    )

    begin
    {
        function Check-StringMappingsExists {
            Param (
                [Parameter(Mandatory = $true)]
                [string] $StringMappingName
            )

            if ($StringMappingName -in $script:desiredConfiguration["stringMappings"].name)
            {
                return $true
            }
            return $false
        }

        function Get-ValueFromStringMappings {
            Param (
                [Parameter(Mandatory = $true)]
                [string] $StringMappingName
            )
            
            if (Check-StringMappingsExists -StringMappingName $StringMappingName) {                
                return ($script:desiredConfiguration["stringMappings"] | Where-Object {$_.name -eq $StringMappingName}).replace
            }
            return $StringMappingName
        }

        [regex] $functionRegex = "(?<pre_comma>,?)\n?\s*`"?(?<to_replace>{% (?<function>for|if) (?<statement>[\w\s\'\-_]*) %}\s?(?<output>[\w\s\-_!§`$&\/\(\)=\?\\`"\[\]:,{}]*)\s?{% (endfor|endif) %})`"?"
        [regex] $ifStatementRegex = "\'?(?<left>[\w\-_]+)\'? (?<operator>eq|ne) \'?(?<right>[\w\-_]+)\'?"
    }
    process
    {    
        $functionMatches = $functionRegex.Matches($InputTemplate)            
        if ($functionMatches.count -eq 0) {
            Write-Debug "Found $($functionMatches.count) matches"
            $InputTemplate
            return
        }

        foreach ($functionMatch in ($functionMatches | Sort-Object -Unique Value )) { 
            # If the same function is used multiple times, we only evaluate it once. We replace all recurrences.
            Write-Debug "Found function $functionMatch"            
            switch ($functionMatch.Groups["function"].Value) {                
                "if" {                    
                    Write-Debug "Is an if-function"
                    $statementMatch = $ifStatementRegex.Match($functionMatch.Groups["statement"].Value)                    
                    $left = if ($statementMatch.Groups["left"].value -notlike "'*'") { Get-ValueFromStringMappings -StringMappingName $statementMatch.Groups["left"].Value } else { $statementMatch.Groups["left"].value }
                    $right = if ($statementMatch.Groups["right"].value -notlike "'*'") { Get-ValueFromStringMappings -StringMappingName $statementMatch.Groups["right"].Value } else { $statementMatch.Groups["right"].value }                                            

                    switch ($statementMatch.Groups["operator"].Value) {
                        "eq" {
                            Write-Debug "Operator is eq"
                            if ($left -eq $right) {
                                Write-Debug "Statement is '$statementMatch' returns true. Replacing with output value"
                                Write-Debug "Replacing $($functionMatch.Groups["to_replace"].Value) with $($functionMatch.Groups["output"].Value)"
                                $InputTemplate = $InputTemplate.replace($functionMatch.Groups["to_replace"].Value, $functionMatch.Groups["output"].Value)
                            }
                            else {
                                Write-Debug "Statement is '$statementMatch' returns false. Replacing with empty"
                                $InputTemplate = $InputTemplate.replace($functionMatch.Value, "")
                            }
                        }
                        "ne" {
                            Write-Debug "Operator is ne"
                            if ($left -ne $right) {
                                Write-Debug "Output value: $($functionMatch.Groups["output"].Value)"
                                Write-Debug "Replacing $($functionMatch.Groups["to_replace"].Value) with $($functionMatch.Groups["output"].Value)"
                                $InputTemplate = $InputTemplate.replace($functionMatch.Groups["to_replace"].Value, $functionMatch.Groups["output"].Value)
                            }
                            else {
                                Write-Debug "Statement is '$statementMatch' returns false. Replacing with empty"
                                $InputTemplate = $InputTemplate.replace($functionMatch.Value, "")
                            }
                        }
                    }
                }                    
            }                
        }
        $InputTemplate
    }
}