functions/process-publishmap.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
if ($null -eq $global:cache) {
    $global:cache = @{}
}


function import-publishmap {
    [cmdletbinding()]
    param([Parameter(Mandatory=$false)] $maps = $null, [alias("nocache")][switch][bool]$force)
    
    if ($force) {
        $global:cache = @{}
    }

    if ($maps -is [System.Collections.IDictionary] ) {
        return import-publishmapobject $maps
    }    
    else {
        return import-publishmapfile $maps
    }
}



function import-publishmapfile {
    [cmdletbinding()]
    param($maps)
 # Measure-function "$($MyInvocation.MyCommand.Name)" {

        write-verbose "importing publishmaps..."

        $global:publishmap = $null

        if ($null -ne $maps) {
            $maps = @($maps)
        }
        else {
            $maps = get-childitem . -filter "publishmap.*.config.ps1"
        }

        $publishmap = @{}

        foreach($file in $maps) {
            write-verbose "importing publishmap '$file'..."
            try {
                $fullname = $file
                if ($null -ne $fullname.FullName) {
                    $Fullname =$Fullname.FullName 
                }
                $cached = get-cachedobject $fullname
                if ($null -ne $cached) {
                    $pmap = $cached.value
                    write-verbose "loaded publishmap '$fullname' from cache"
                }
                else {
                    $map = & "$FullName"
        
                    #$publishmap_obj = ConvertTo-Object $publishmap
                    $pmap = import-publishmapobject $map
                    set-cachedobject $fullname $pmap
                }
                $publishmap += $pmap
                write-verbose "publishmap '$file' import DONE"
            } catch {
                write-error "failed to import map file '$file': $($_.Exception.Message)"
                throw
            }
        }

        $global:publishmap = $publishmap
        $global:pmap = $global:publishmap 

        write-verbose "processing publishmap... DONE"

        return $publishmap
# }
}

function import-publishmapobject {
    [cmdletbinding()]
    param($map) 
    
    $map = preporcess-publishmap $map
    $pmap = import-mapobject $map
    $pmap = postprocess-publishmap $pmap
    
    return $pmap           
}

<#
.Synopsis
 * inherits properties from global `settings` node
 * generates `_staging` and `swap_` profiles
#>

function preporcess-publishmap($map) {
 # Measure-function "$($MyInvocation.MyCommand.Name)" {

        $globalprofilesname = "global_profiles"
        foreach($groupk in get-propertynames $map) {
            if ($null -ne $map.$groupk.$globalprofilesname) {
                $settings = @{     
                    profiles = $map.$groupk.$globalprofilesname 
                    _strip = $true
                } 
                $null = add-property $map.$groupk -name "settings" -value $settings -merge
            }
        }
        foreach($groupk in get-propertynames $map) {
            foreach($projk in get-propertynames $map.$groupk) { 
                foreach($profk in get-propertynames $map.$groupk.$projk.profiles) { 
                    $shouldcreatestaging = ($profk -notmatch "_staging") -and ($profk -notmatch "swap_") -and ($map.$groupk.$projk.profiles.$profk -is [System.Collections.IDictionary])
                
                    if ($shouldcreatestaging) {
                        $stagingkey = "$($profk)_staging"    
                        $swapkey= "swap_$($profk)"
                        if ($null -eq $map.$groupk.$projk.profiles.$stagingkey) {
                            $map.$groupk.$projk.profiles.$stagingkey = @{ 
                                "_autogenerated" = $true
                                "_inherit_from" = $profk
                                "_postfix" = "-staging"
                            } 
                        }
                        if ($null -eq $map.$groupk.$projk.profiles.$swapkey) {
                            $map.$groupk.$projk.profiles.$swapkey = @{ 
                                "_autogenerated" = $true
                                "_inherit_from" = $profk
                            } 
                        } 
                    }
                }
            }
        }
    
        return $map
# }
}

<#
.Synopsis
* adds profile links at project level
* processes inheritance basing on `_inherit_from` properties
#>

function postprocess-publishmap($map) {    
  # Measure-function "$($MyInvocation.MyCommand.Name)" {
        write-verbose "postprocessing map..."
        foreach($groupk in get-propertynames $map) {
            # remove generated properties from top-level
            if ($groupk.startswith("_")) {
                $map.Remove($groupk)
                continue
            }
            $group = $map.$groupk
            foreach($projk in get-propertynames $group) {
                $proj = $group.$projk
                if ($null -ne $proj.profiles) {
                    foreach($profk in get-propertynames $proj.profiles) {
                        $prof = $proj.profiles.$profk
                        if ($prof -is [System.Collections.IDictionary]) {
                            write-verbose "adding post-properties to '$groupk.$projk.$profk'" 
                            # set full path as if profiles were created at project level
                            $null = add-property $prof -name _fullpath -value "$groupk.$projk.$profk" -overwrite
                            $null = add-property $prof -name _name -value "$profk" -overwrite
                            # use fullpath for backward compatibility
                            if ($prof._fullpath -eq $null) {
                                write-warning "no fullpath property!"
                            }   
                            $null = add-property $prof -name fullpath -value $prof._fullpath -overwrite
                            # expose project at profile level
                            $null = add-property $prof -name project -value $proj
                        } else {
                            #write-verbose "removing non-profile property '$groupk.$projk.$profk'"
                            #remove every property that isn't a real profile
                            $proj.profiles.Remove($profk)
                        }
                        if ($null -ne $prof._inherit_from) {
                            if ($proj.profiles.$($null -eq $prof._inherit_from)) {
                                write-warning "cannot find inheritance base '$($prof._inherit_from)' for profile '$($prof._fullpath)'"
                            } else { 
                                $cur = $prof
                                $hierarchy = @()
                                while($null -ne $cur._inherit_from -and $null -eq $cur._inherited_from) {                                
                                    $hierarchy += $cur
                                    $base = $proj.profiles.$($cur._inherit_from)
                                    $cur = $base
                                }
                                for($i = ($hierarchy.length - 1); $i -ge 0; $i--) {
                                    $cur = @($hierarchy)[$i]
                                    $base = $proj.profiles.$($cur._inherit_from)
                                   # write-verbose "inheriting properties from '$($cur._inherit_from)' to '$($cur._fullpath)'"
                                    inherit-properties -from $base -to $cur -valuesonly -exclude @("_inherit_from","_inherited_from")
                                    $null = add-property $cur -name _inherited_from  -value $($cur._inherit_from)
                                }                            
                            }
                        }
                    }
                    # expose profiles at project level
                    $null = add-properties $proj $proj.profiles -merge -ifNotExists

                                
                }
                # use fullpath for backward compatibility
                if ($proj._fullpath) {
                    $null = add-property $proj -name fullpath -value $proj._fullpath -overwrite
                }
            }

            # use fullpath for backward compatibility
            if ($group._fullpath) {
                $null = add-property $group -name fullpath -value $group._fullpath -overwrite
            }
                    
        }
        return $pmap
 # }
}

function get-profile($name, $map = $null) {
  # Measure-function "$($MyInvocation.MyCommand.Name)" {

        $pmap = $map
        if ($null -eq $map) {
            $pmap = $global:pmap
        }

        $profName = $name
        $splits = $profName.Split('.')

        $map = $pmap
        $entry = $null
        $parent = $null
        $isGroup = $false
        for($i = 0; $i -lt $splits.length; $i++) {
            $split = $splits[$i]
            $parent = $entry
            if ($i -eq $splits.length-1) {
                $entry = get-entry $split $map -excludeProperties @("project")             
            }
            else {
                $entry = $map.$split
            }
            if ($null -eq $entry) {
                break
            }
            if ($null -ne $entry -and $null -ne $entry.group) {
                $isGroup = $true
                break
            }
            $map = $entry
        }    
        $profile = $entry
        if ($null -eq $profile)  {
            if ($splits[1] -eq "all") {
                $isGroup = $true
                $profile = $parent
                break
            }
            else {
                #write-host "unknown profile $profName"
                return $null
            }
        }

        return new-object -Type pscustomobject -Property @{
            Profile = $profile
            IsGroup = $isGroup
            Project = $splits[0]
            Group = $splits[1]
            TaskName = $splits[2]
        }
  # }
}



function check-profileName($proj, $profk) {
 # Measure-function "$($MyInvocation.MyCommand.Name)" {

        $prof = $proj.profiles.$profk
        if ($null -eq $prof) {
                    
            if ($proj.inherit -ne $false) {
                $prof = @{}
                if ($null -eq $proj.profiles) {
                    $null = add-property $proj -name "profiles" -value @{}
                }
                $null = add-property $proj.profiles -name $profk -value $prof
            }
            else {
                continue
            }
        }
  # }
}

<#

function __import-mapproject($proj) {
            #proj = viewer,website,drmserver,vfs, etc.
            #$proj = $group[$projk]
            
            inherit-globalsettings $proj $settings $stripsettingswrapper
            
            $profiles = @()
            if ($null -ne $proj.profiles) {
                $profiles += get-propertynames $proj.profiles
            }
            if ($null -ne $globalProffiles) {
                $profiles += get-propertynames $globalProffiles
            }
            $profiles = $profiles | select -Unique
            #write-host "$groupk.$projk"
                
            foreach($profk in $profiles) {
                  check-profileName $proj $profk
                  $prof = $proj.profiles.$profk
                  import-mapprofile $prof -parent $proj
                  $null = add-property $proj -name $profk -value $prof
            }
}


function __import-mapprofile($prof, $parent) {
   # make sure all profiles exist
                
                #inherit settings from project
                inherit-properties -from $parent -to $prof -exclude (@("profiles") + $profiles + @("level","fullpath"))

                #inherit global profile settings
                if ($null -ne $globalProffiles -and $null -ne $globalProffiles.$profk -and $prof.inherit -ne $false -and $parent.inherit -ne $false) {
                    # inherit project-specific settings
                    #foreach($prop in $globalProffiles.$profk.psobject.properties | ? { $_.name -eq $projk }) {
                    # if ($prop.name -eq $projk) {
                    $global = $globalProffiles.$profk
                    inherit-properties -from $global -to $prof
                    # }
                    #}
                    # inherit generic settings
                    inherit-properties -from $settings -to $prof
                }
                $null = add-property $prof "_level" 3

                #fill meta properties
                $null = add-property $prof -name _parent -value $parent
                #add-property $prof -name fullpath -value "$groupk.$projk.$profk"
                $null = add-property $prof -name _name -value "$profk"
                
}

#>