|
32 | 32 | Write-Debug "Force: [$Force]"
|
33 | 33 | if ($Force) {
|
34 | 34 | Write-Debug 'Forcing initialization of GitHubConfig.'
|
35 |
| - $context = Set-Context -Context $script:GitHub.DefaultConfig -Vault $script:GitHub.ContextVault -PassThru |
36 |
| - $script:GitHub.Config = [GitHubConfig]$context |
| 35 | + $config = Set-Context -Context $script:GitHub.DefaultConfig -Vault $script:GitHub.ContextVault -PassThru |
| 36 | + $script:GitHub.Config = [GitHubConfig]$config |
37 | 37 | return
|
38 | 38 | }
|
39 | 39 |
|
40 |
| - Write-Debug "GitHubConfig ID: [$($script:GitHub.Config.ID)]" |
41 | 40 | if ($null -ne $script:GitHub.Config) {
|
42 | 41 | Write-Debug 'GitHubConfig already initialized and available in memory.'
|
43 | 42 | return
|
44 | 43 | }
|
45 | 44 |
|
46 | 45 | Write-Debug 'Attempt to load the stored GitHubConfig from ContextVault'
|
47 |
| - $context = Get-Context -ID $script:GitHub.DefaultConfig.ID -Vault $script:GitHub.ContextVault |
48 |
| - if ($context) { |
| 46 | + $config = Get-Context -ID $script:GitHub.DefaultConfig.ID -Vault $script:GitHub.ContextVault |
| 47 | + if ($config) { |
49 | 48 | Write-Debug 'GitHubConfig loaded into memory.'
|
50 | 49 |
|
51 |
| - Write-Debug 'Checking if new default properties are available in the stored context.' |
| 50 | + Write-Debug 'Synchronizing stored context with GitHubConfig class definition.' |
52 | 51 | $needsUpdate = $false
|
53 |
| - $defaultProperties = $script:GitHub.DefaultConfig.PSObject.Properties.Name |
54 |
| - foreach ($propName in $defaultProperties) { |
55 |
| - if (-not $context.PSObject.Properties.Name.Contains($propName)) { |
| 52 | + $validProperties = [GitHubConfig].GetProperties().Name |
| 53 | + $storedProperties = $config.PSObject.Properties.Name |
| 54 | + |
| 55 | + # Add missing properties from DefaultConfig |
| 56 | + foreach ($propName in $validProperties) { |
| 57 | + Write-Debug "Validating property [$propName]" |
| 58 | + if (-not $storedProperties.Contains($propName)) { |
56 | 59 | Write-Debug "Adding missing property [$propName] from DefaultConfig"
|
57 |
| - $context | Add-Member -MemberType NoteProperty -Name $propName -Value $script:GitHub.DefaultConfig.$propName |
| 60 | + $defaultValue = $script:GitHub.DefaultConfig.$propName |
| 61 | + $config | Add-Member -MemberType NoteProperty -Name $propName -Value $defaultValue |
| 62 | + $needsUpdate = $true |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + # Remove obsolete properties that are no longer supported |
| 67 | + $propertiesToRemove = @() |
| 68 | + foreach ($propName in $storedProperties) { |
| 69 | + Write-Debug "Checking property [$propName] for obsolescence" |
| 70 | + if (-not $validProperties.Contains($propName)) { |
| 71 | + Write-Debug "Removing obsolete property [$propName] from stored context" |
| 72 | + $propertiesToRemove += $propName |
58 | 73 | $needsUpdate = $true
|
59 | 74 | }
|
60 | 75 | }
|
| 76 | + |
| 77 | + # Remove the obsolete properties |
| 78 | + foreach ($propName in $propertiesToRemove) { |
| 79 | + $config.PSObject.Properties.Remove($propName) |
| 80 | + } |
| 81 | + |
61 | 82 | if ($needsUpdate) {
|
62 |
| - Write-Debug 'Updating stored context with new default properties' |
63 |
| - $context = Set-Context -Context $context -Vault $script:GitHub.ContextVault -PassThru |
| 83 | + Write-Debug 'Updating stored context with synchronized properties' |
| 84 | + $config = Set-Context -Context $config -Vault $script:GitHub.ContextVault -PassThru |
64 | 85 | }
|
65 | 86 |
|
66 |
| - $script:GitHub.Config = [GitHubConfig]$context |
| 87 | + $script:GitHub.Config = [GitHubConfig]$config |
67 | 88 | return
|
68 | 89 | }
|
69 | 90 | Write-Debug 'Initializing GitHubConfig from defaults'
|
70 |
| - $context = Set-Context -Context $script:GitHub.DefaultConfig -Vault $script:GitHub.ContextVault -PassThru |
71 |
| - $script:GitHub.Config = [GitHubConfig]$context |
| 91 | + $config = Set-Context -Context $script:GitHub.DefaultConfig -Vault $script:GitHub.ContextVault -PassThru |
| 92 | + $script:GitHub.Config = [GitHubConfig]$config |
72 | 93 | }
|
73 | 94 |
|
74 | 95 | end {
|
75 | 96 | Write-Debug "[$stackPath] - End"
|
76 | 97 | }
|
77 | 98 | }
|
78 | 99 | #Requires -Modules @{ ModuleName = 'Context'; RequiredVersion = '8.1.3' }
|
79 |
| - |
|
0 commit comments