imports.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
# grab functions from files
get-childitem $psscriptroot\functions\ -filter "*.ps1" | 
? { -not ($_.name.Contains(".Tests.")) } |
? { -not (($_.name).StartsWith("_")) } |
% { . $_.fullname }


$usenative = $true
$lib = "$psscriptroot\lib\publishmap.core.dll"

function loadLib($lib, [scriptblock] $init) {
    $libdir = split-path -parent $lib 

    $OnAssemblyResolve = [System.ResolveEventHandler] {
        param($sender, $e)
        write-warning "resolving assembly '$($e.Name)' by $sender"
        foreach($a in [System.AppDomain]::CurrentDomain.GetAssemblies()) {
            if ($a.FullName -eq $e.Name) {
                return $a
            }
            $splits = $e.Name.Split(",")
            if ($a.GetName().Name -eq $splits[0]) {
                return $a
            }
        }
        Write-Warning "failed to resolve assembly '$($e.name)'"
        return $null
    }


    foreach($asm in (get-childitem $libdir -Filter "*.dll")) {
        try {
            [System.Reflection.Assembly]::LoadFile($asm.fullname)
            write-host "imported $($asm.Name)"
        } catch {
            write-warning "failed to import $($asm.Name): $($_.Exception.Message)"
        }
    }
    
    try {
       
  
        # [System.AppDomain]::CurrentDomain.add_AssemblyResolve($OnAssemblyResolve)
        Add-Type -Path "$lib"
        if ($init -ne $null) {
            Invoke-Command $init
        }
        write-host "lib $lib imported"
       
    } catch {
        throw $_
    }
    finally {
        # [System.AppDomain]::CurrentDomain.remove_AssemblyResolve($OnAssemblyResolve)

    }
}

if ($usenative) {

    loadlib $lib -init { 
        [Publishmap.Utils.Inheritance.Inheritance]::Init()
    }

    function add-property {
        [CmdletBinding()]
        param(
            [Parameter(ValueFromPipeline = $true, Position=1)] $object, 
            [Parameter(Mandatory=$true,Position=2)] $name, 
            [Parameter(Mandatory=$true,Position=3)] $value, 
            [switch][bool] $ifNotExists,
            [switch][bool] $overwrite,
            [switch][bool] $merge
        )  
        # Measure-function "$($MyInvocation.MyCommand.Name)" {
        if ($object -is [System.Collections.IDictionary]) {
            $r = [Publishmap.Utils.Inheritance.Inheritance]::AddProperty($object, $name, $value, $ifNotExists, $overwrite, $merge)
        }
        else {
            $null = $object | add-member -name $name -membertype noteproperty -value $value
        }
        
        return $object
        # }
    }

    
    function add-properties(
        [Parameter(Mandatory=$true, ValueFromPipeline = $true)] 
        $object,
        [Parameter(Mandatory=$true)]
        $props, 
        [switch][bool] $ifNotExists, 
        [switch][bool] $merge, 
        $exclude = @()
    ) {
        # Measure-function "$($MyInvocation.MyCommand.Name)" {
        $r = [Publishmap.Utils.Inheritance.Inheritance]::AddProperties($object, $props, $ifNotExists, $merge, $exclude)
        return $object
        # }
    }


    function add-metaproperties {
        param($group, $fullpath, $specialkeys = @("settings", "global_prof1iles"))
        #Measure-function "$($MyInvocation.MyCommand.Name)" {
        [Publishmap.Utils.Inheritance.Inheritance]::AddMetaProperties($group, $fullpath, $specialkeys)      
        return $group      
        # }
    }



    function copy-hashtable($hash) {
        # Measure-function "$($MyInvocation.MyCommand.Name)" {
        return [Publishmap.Utils.Inheritance.Inheritance]::CopyHashtable($hash)
        # }
    }

    function Add-InheritedProperties($from, $to, $exclude = @(), [switch][bool] $valuesOnly) {
        # Measure-function "$($MyInvocation.MyCommand.Name)" {
        $r = [Publishmap.Utils.Inheritance.Inheritance]::AddInheritedProperties($from, $to, @($exclude), $valuesOnly)
       
        # }
    }

    function postprocess-publishmap($map) {    
        # Measure-function "$($MyInvocation.MyCommand.Name)" {
        [Publishmap.Utils.Inheritance.Inheritance]::PostProcessPublishmap($map)
        return $map
        # }
    }

    function Add-GlobalSettings($proj, $settings) {
        # Measure-function "$($MyInvocation.MyCommand.Name)" {
        [Publishmap.Utils.Inheritance.Inheritance]::AddGlobalSettings($proj, $settings)
        # }
    }

    function import-genericgroup($group,
        $fullpath, 
        $settings = $null,
        $settingskey = "settings",
        $specialkeys = @("settings", "global_profiles")
    ) {
        # Measure-function "$($MyInvocation.MyCommand.Name)" {
        $g = [Publishmap.Utils.Inheritance.Inheritance]::ImportGenericGroup($group, $fullpath, $settings, $settingskey, $specialkeys)
        return $g
        # }
    }
}