Private/GetSettings.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
function GetSettings {
    [CmdletBinding()]
    param()
    end {
        function GetHashtable {
            if ($script:CSSettings) { return $script:CSSettings }

            $targetPath = Join-Path $psEditor.Workspace.Path -ChildPath 'ESCSSettings.psd1'

            if (Test-Path $targetPath) {
                $script:CSSettings = Import-LocalizedData -BaseDirectory $psEditor.Workspace.Path `
                                                        -FileName 'ESCSSettings.psd1'

                return $script:CSSettings
            }

            $script:CSSettings = $script:DEFAULT_SETTINGS

            return $script:CSSettings
        }

        $settings = GetHashtable

        # Ensure all settings have a default value even if not present in user supplied file.
        if ($settings.PreValidated) { return $settings }

        foreach ($setting in $script:DEFAULT_SETTINGS.GetEnumerator()) {
            if (-not ($settings.ContainsKey($setting.Key))) {
                $settings.Add($setting.Key, $setting.Value)
            }
        }
        $settings.PreValidated = $true
        return $settings
    }
}