PSCache.psm1

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
$ErrorActionPreference = "Stop"

# Attempt to retrieve relevant script files
$Classes = Get-ChildItem (Join-Path $PSScriptRoot Classes) -ErrorAction SilentlyContinue -Filter *.class.ps1
$Public  = Get-ChildItem (Join-Path $PSScriptRoot Public)  -ErrorAction SilentlyContinue -Filter *.ps1
$Private = Get-ChildItem (Join-Path $PSScriptRoot Private) -ErrorAction SilentlyContinue -Filter *.ps1

# Classes on which other classes might depend, must be specified in order
$ClassDependees = @()

# Import classes on which others depend first
foreach($classDependee in $ClassDependees)
{
    try{
        . (Join-Path (Join-Path $PSScriptRoot .\Classes) "$classDependee.class.ps1")
    }
    catch{
        Write-Error -Message "Failed to import class $($classDependee): $_"
    }
}

# Import any remaining class files
foreach($class in $Classes|Where-Object {($_.Name -replace '\.class\.ps1') -notin $ClassDependees})
{
    try{
        . $class.fullname
    }
    catch{
        Write-Error -Message "Failed to import dependant class $($class.fullname): $_"
    }    
}

# dot source the functions
foreach($import in @($Public;$Private))
{
    try{
        . $import.fullname
    }
    catch{
        Write-Error -Message "Failed to import function $($import.fullname): $_"
    }
}

# Export public functions
Export-ModuleMember -Function $Public.BaseName