From 524bce25ded92d53eb26cda69d16abdf9595e6bb Mon Sep 17 00:00:00 2001 From: lksz Date: Wed, 4 Nov 2020 23:31:34 -0500 Subject: [PATCH] MyConfig modifications Moved configuration into src/config.files.json, it will also load src/config.files.local.json (which will not be included in the git repo) Added Git-Path to resolve paths simply, even when Path does not exist. --- Edit-MyConfig.ps1 | 2 ++ Get-Path.ps1 | 8 ++++++++ profile.d/MyConfig.class.ps1 | 38 +++++++++++++++++------------------- src/config.files.json | 37 +++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 20 deletions(-) create mode 100644 Get-Path.ps1 create mode 100644 src/config.files.json diff --git a/Edit-MyConfig.ps1 b/Edit-MyConfig.ps1 index 92a098c..1204eef 100644 --- a/Edit-MyConfig.ps1 +++ b/Edit-MyConfig.ps1 @@ -20,3 +20,5 @@ if( -not $ConfigName ) { $local:ScriptPaths = [MyConfig]::GetConfigPaths($ConfigName,$Force) Edit-TextFile -sudo:$sudo $ScriptPaths + +$null = [MyConfig]::_GetValidValues('', $true) diff --git a/Get-Path.ps1 b/Get-Path.ps1 new file mode 100644 index 0000000..7dccf1f --- /dev/null +++ b/Get-Path.ps1 @@ -0,0 +1,8 @@ +[CmdletBinding()]param([string]$Path) + +try { + get-item $Path -Force -ErrorAction Stop | + Select-Object -ExpandProperty FullName +} catch { + $_.targetObject +} diff --git a/profile.d/MyConfig.class.ps1 b/profile.d/MyConfig.class.ps1 index fed67a7..28d7b2e 100644 --- a/profile.d/MyConfig.class.ps1 +++ b/profile.d/MyConfig.class.ps1 @@ -1,28 +1,27 @@ class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator { - static [hashtable]$configDirectory = @{}; - static ReloadConfigPaths() { - [MyConfig]::ConfigDirectory = [ordered]@{ - 'vi' = '~/.virc' - 'vim' = '~/.vimrc','~/.vim/vimrc' - 'neovim' = '~/.config/nvim/init.vim','~/.config/nvim/vim-plug/plugins.vim','#vim','#vi' - 'zshrc' = '~/.zshrc' - 'sz-zshrc' = '~/.sz.zshrc.sh', '#zshrc' - 'sz-local-rc' = '~/.sz.local.sh' - 'sz-aliases-sh' = '~/.sz.aliases.sh' - 'sz-shrc' = '~/.sz.shrc.sh' - 'sz-rc-all' = '#sz-aliases-sh', '#sz-local-rc', '#sz-shrc', '#sz-zshrc', '#zshrc' - 'tmux' = '~/.tmux.conf', '~/.byobu/.tmux.conf' - 'ansible' = '/etc/ansible/ansible.cfg', '/opt/ansible/ansible.cfg' + hidden static [string[]]$_MyConfigDictionaryPath = @( + $(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'config.files.json'), + $(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'config.files.local.json') + ) + static [hashtable]GetConfigDictionary() { + $local:result = @{} + $result['myconfig'] = [MyConfig]::_MyConfigDictionaryPath; + foreach( $local:jsonSource in [MyConfig]::_MyConfigDictionaryPath ) { + if( -not ( Test-Path $jsonSource ) ) { continue } + foreach( $local:prop in $(Get-Content $jsonSource | ConvertFrom-Json).PSObject.Properties ) { + $result[$prop.Name] = $prop.Value + } } + return $result; } static [string[]] GetConfigPaths([string[]]$ConfigNames,[switch]$Force) { - [MyConfig]::ReloadConfigPaths() + $local:configDirectory = [MyConfig]::GetConfigDictionary() $local:result = [string[]]@() $local:flat = $false while(-not $flat) { $flat = $true $ConfigNames += $ConfigNames | ForEach-Object { - [MyConfig]::configDirectory[$_] + $configDirectory[$_] } | Where-Object { $_ -match '^#[\w-\.]+$' } | ForEach-Object { $_.Substring(1) } | Where-Object { $_ -notin $ConfigNames } | ForEach-Object { @@ -31,7 +30,7 @@ class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator { } $local:flatConfigList = $ConfigNames | ForEach-Object { - [MyConfig]::configDirectory[$_] + $configDirectory[$_] } | Where-Object { $_ -notmatch '^#[\w-\.]+$' } $local:exists = $false; @@ -39,7 +38,7 @@ class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator { foreach( $local:configPath in $flatConfigList ) { if( -not $first ) { $first = $configPath } if( $Force -or (Test-Path $configPath) ) { - $result += Resolve-Path $configPath + $result += Get-Path $configPath $exists = $true } } @@ -49,8 +48,7 @@ class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator { return $result } static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) { - [MyConfig]::ReloadConfigPaths() - $local:possibleValues = [MyConfig]::configDirectory.Keys + $local:possibleValues = [MyConfig]::GetConfigDictionary().Keys return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict ); } diff --git a/src/config.files.json b/src/config.files.json new file mode 100644 index 0000000..7cdc1ac --- /dev/null +++ b/src/config.files.json @@ -0,0 +1,37 @@ +{ + "neovim": [ + "~/.config/nvim/init.vim", + "~/.config/nvim/vim-plug/plugins.vim", + "#vim", + "#vi" + ], + "sz-shrc": "~/.sz.shrc.sh", + "sz-rc-all": [ + "#sz-aliases-sh", + "#sz-local-rc", + "#sz-shrc", + "#sz-zshrc", + "#zshrc" + ], + "vi": "~/.virc", + "sz-local-rc": "~/.sz.local.sh", + "zshrc": "~/.zshrc", + "ansible": [ + "/etc/ansible/ansible.cfg", + "/opt/ansible/ansible.cfg", + "/opt/ansible/hosts" + ], + "sz-aliases-sh": "~/.sz.aliases.sh", + "vim": [ + "~/.vimrc", + "~/.vim/vimrc" + ], + "sz-zshrc": [ + "~/.sz.zshrc.sh", + "#zshrc" + ], + "tmux": [ + "~/.tmux.conf", + "~/.byobu/.tmux.conf" + ] +}