· 6 years ago · Oct 07, 2019, 10:04 PM
1#Requirements:
2#Requires -Version 6.0
3
4# Get root directory of "Nevada" frameWork:
5$root = ((Get-Item -Path "Env:/Nevada" -ErrorAction SilentlyContinue -ErrorVariable RootError).value -Replace "[\\/]", "/")
6if ($RootError) {
7 Write-Error -ErrorAction Stop -Message "This script Requires the environment variable `"Nevada`" to exist and point to the root directory of the `"Nevada`" framework. Exiting Now."
8}
9
10# Import:
11Import-Module -Name "$($root)/scripts/.impl/check"
12Import-Module -Name "$($root)/scripts/.impl/Utils"
13Import-Module -Name "$($root)/scripts/.impl/Get-Shared"
14
15####################################################################################################
16# Implementation:
17####################################################################################################
18
19# Get pwd-independent, absolute path to "/Scripts/.config/*.jsonc" file.
20function Get-PathToConfigJson(
21 [String]$file)
22{
23 # Get absolute path to "/Scripts/.config/*.jsonc":
24 $path = [String]"$((Get-Shared)["paths"]["scr/cfg"])/$($file)"
25 # Check:
26 check_if_path_exists -Path $path
27 check_if_path_is_a_file -Path $path
28 check_if_path_is_a_json_file -Path $path
29 # Exit:
30 return [String]$path
31}
32
33# load data from the "/Scripts/.config/*.jsonc" file.
34# WARNING: DO NOT USE !!!
35# USE `Get-Config` INSTEAD !!!
36function Get-ConfigJson(
37 [String]$file,
38 [String]$property,
39 [switch]$all)
40{
41 # Check validity of input:
42 check_if_string_is_null_or_empty -string $file
43 # Load data into memory:
44 $data = (Convert-JsonToData -path (Get-PathToConfigJson -file $file))
45 if ($all) {
46 return $data
47 }
48 else {
49 check_if_json_has_property -json $data -property $property
50 return $data.$property
51 }
52}
53
54# This function provides a single "access-point" to data stored within "/Scripts/.config/env.jsonc" file, within property "vars".
55# WARNING: if the layout of "/Scripts/.config/env.jsonc" or "vars" changes,
56# this functions will need to change as well !!!
57# WARNING: DO NOT USE !!!
58# USE `Get-Config` INSTEAD !!!
59function Get-ConfigEnvVars()
60{
61 $hash = [ordered]@{}
62 Get-ConfigJson -file "env.jsonc" -property "vars" | foreach {
63 $hash[$_."name"] = $_."value"
64 }
65 return $hash
66}
67
68# This function provides a single "access-point" to data stored within "/Scripts/.config/env.jsonc" file.
69# WARNING: if the layout of "/Scripts/.config/env.jsonc" changes,
70# this functions will need to change as well !!!
71# WARNING: DO NOT USE !!!
72# USE `Get-Config` INSTEAD !!!
73function Get-ConfigEnv()
74{
75 $hash = [ordered]@{
76 "vars" = (Get-ConfigEnvVars)
77 }
78 return $hash
79}
80
81# This function provides a single "access-point" to data stored within "/Scripts/.config/registry.jsonc" file, within property "PowerShell".
82# WARNING: if the layout of "/Scripts/.config/registry.jsonc" or "PowerShell" changes,
83# this functions will need to change as well !!!
84# WARNING: DO NOT USE !!!
85# USE `Get-Config` INSTEAD !!!
86function Get-ConfigRegistryPowerShell()
87{
88 $hash = [ordered]@{}
89 Get-ConfigJson -file "registry.jsonc" -property "PowerShell" | foreach {
90 $path = [String]"$($_.'path')"
91 $values = [Ordered]@{}
92 $_.'values' | foreach {
93 $key = [String]"$($_.'key')"
94 $val = [String]"$($_.'val')"
95 $values[$key] = $val
96 }
97 $hash[$path] = $values
98 }
99 return $hash
100}
101
102# This function provides a single "access-point" to data stored within "/Scripts/.config/registry.jsonc" file.
103# WARNING: if the layout of "/Scripts/.config/registry.jsonc" changes,
104# this functions will need to change as well !!!
105# WARNING: DO NOT USE !!!
106# USE `Get-Config` INSTEAD !!!
107function Get-ConfigRegistry()
108{
109 $hash = [ordered]@{
110 "PowerShell" = (Get-ConfigRegistryPowerShell)
111 }
112 return $hash
113}
114
115# globals:
116$config = [Ordered]@{}
117
118# initialize the module:
119function Init-Module()
120{
121 $config["env"] = (Get-ConfigEnv)
122 $config["reg"] = (Get-ConfigRegistry)
123}
124Init-Module
125
126####################################################################################################
127# API:
128####################################################################################################
129
130# This function provides a single "access-point" to data stored within "/Scripts/.config/" directory.
131# WARNING: if the layout of any "/Scripts/.config/*.jsonc" changes,
132# this functions will need to change as well !!!
133function Get-Config()
134{
135 return $config
136}
137
138####################################################################################################
139# Export:
140####################################################################################################
141
142Export-ModuleMember -Function Get-Config