functions/inheritance.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
<#

.PARAMETER valuesOnly
Inherit only value types, do not inherit dictionaries (helps prevent infinite inheritance loop)

#>

function Add-InheritedProperties($from, $to, $exclude = @(), [switch][bool] $valuesOnly) {
   # Measure-function "$($MyInvocation.MyCommand.Name)" {

        if ($from -is [System.Collections.IDictionary]) {
        }
        else {
            $from = $from.psobject.properties | % { $d = @{} } { $d[$_.name] = $_.value } { $d }
        }
        $from = $from.getenumerator( ) | % { $h = @{} } {
            $key = $_.key
            $value = $_.value
            $shouldExclude = $false 
            if ($key -in $exclude) {
                $shouldExclude = $true 
            }
            if (@($exclude | ? { $key -match "^$_$" }).Count -gt 0) {
                $shouldExclude = $true 
            }
          
            if ($value -is [System.Collections.IDictionary]) {
                if ($valuesOnly) {
                    $shouldExclude = $true 
                }
                else {
                    $newvalue = copy-hashtable $value
                    $value = $newvalue
                }
            }
            if (!$shouldExclude) {
                $h[$key] = $value    
            }
        } { $h } 
    
        if ($null -ne $from) {
            try {
                $null = add-properties -object $to -props $from -merge -ifNotExists
            } catch {
                throw "failed to inherit properties:$($_.Exception.Message)`r`nfrom:`r`n$($from | format-table | out-string)`r`nto:`r`n$($to | format-table | out-string)"
            }
        
        }
        <# foreach($key in $from.keys) {

        $value = $from[$key]

        if ($value -is [System.Collections.IDictionary]) {
            if ($valuesOnly) {
                $shouldExclude = $true
            }
            else {
                $value = $value.Clone()
            }
         }

        if (!$shouldExclude) {
            add-property $to -name $key -value $value
        }
    }
    #>

 # }
}


function Add-GlobalSettings($proj, $settings) {
 # Measure-function "$($MyInvocation.MyCommand.Name)" {

        if ($null -ne $settings) {
            write-verbose "inheriting global settings to $($proj._fullpath). strip=$stripsettingswrapper"
            $stripsettingswrapper = $settings._strip
            if ($null -ne $stripsettingswrapper -and $stripsettingswrapper) {
                $null = inherit-properties -from $settings -to $proj -ifNotExists -merge -exclude "_strip"
            }
            else {
                $null = add-property $proj "settings" $settings -ifNotExists -merge
            }
        }
# }
}

New-Alias -Name Inherit-Properties -Value Add-InheritedProperties -Force
New-Alias -Name Inherit-GlobalSettings -Value Add-GlobalSettings -Force