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.
This commit is contained in:
lksz 2020-11-04 23:31:34 -05:00
parent d85aec0480
commit 524bce25de
4 changed files with 65 additions and 20 deletions

View File

@ -20,3 +20,5 @@ if( -not $ConfigName ) {
$local:ScriptPaths = [MyConfig]::GetConfigPaths($ConfigName,$Force) $local:ScriptPaths = [MyConfig]::GetConfigPaths($ConfigName,$Force)
Edit-TextFile -sudo:$sudo $ScriptPaths Edit-TextFile -sudo:$sudo $ScriptPaths
$null = [MyConfig]::_GetValidValues('', $true)

8
Get-Path.ps1 Normal file
View File

@ -0,0 +1,8 @@
[CmdletBinding()]param([string]$Path)
try {
get-item $Path -Force -ErrorAction Stop |
Select-Object -ExpandProperty FullName
} catch {
$_.targetObject
}

View File

@ -1,28 +1,27 @@
class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator { class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator {
static [hashtable]$configDirectory = @{}; hidden static [string[]]$_MyConfigDictionaryPath = @(
static ReloadConfigPaths() { $(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'config.files.json'),
[MyConfig]::ConfigDirectory = [ordered]@{ $(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'config.files.local.json')
'vi' = '~/.virc' )
'vim' = '~/.vimrc','~/.vim/vimrc' static [hashtable]GetConfigDictionary() {
'neovim' = '~/.config/nvim/init.vim','~/.config/nvim/vim-plug/plugins.vim','#vim','#vi' $local:result = @{}
'zshrc' = '~/.zshrc' $result['myconfig'] = [MyConfig]::_MyConfigDictionaryPath;
'sz-zshrc' = '~/.sz.zshrc.sh', '#zshrc' foreach( $local:jsonSource in [MyConfig]::_MyConfigDictionaryPath ) {
'sz-local-rc' = '~/.sz.local.sh' if( -not ( Test-Path $jsonSource ) ) { continue }
'sz-aliases-sh' = '~/.sz.aliases.sh' foreach( $local:prop in $(Get-Content $jsonSource | ConvertFrom-Json).PSObject.Properties ) {
'sz-shrc' = '~/.sz.shrc.sh' $result[$prop.Name] = $prop.Value
'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'
} }
} }
return $result;
}
static [string[]] GetConfigPaths([string[]]$ConfigNames,[switch]$Force) { static [string[]] GetConfigPaths([string[]]$ConfigNames,[switch]$Force) {
[MyConfig]::ReloadConfigPaths() $local:configDirectory = [MyConfig]::GetConfigDictionary()
$local:result = [string[]]@() $local:result = [string[]]@()
$local:flat = $false $local:flat = $false
while(-not $flat) { while(-not $flat) {
$flat = $true $flat = $true
$ConfigNames += $ConfigNames | ForEach-Object { $ConfigNames += $ConfigNames | ForEach-Object {
[MyConfig]::configDirectory[$_] $configDirectory[$_]
} | Where-Object { $_ -match '^#[\w-\.]+$' } | ForEach-Object { } | Where-Object { $_ -match '^#[\w-\.]+$' } | ForEach-Object {
$_.Substring(1) $_.Substring(1)
} | Where-Object { $_ -notin $ConfigNames } | ForEach-Object { } | Where-Object { $_ -notin $ConfigNames } | ForEach-Object {
@ -31,7 +30,7 @@ class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator {
} }
$local:flatConfigList = $ConfigNames | ForEach-Object { $local:flatConfigList = $ConfigNames | ForEach-Object {
[MyConfig]::configDirectory[$_] $configDirectory[$_]
} | Where-Object { $_ -notmatch '^#[\w-\.]+$' } } | Where-Object { $_ -notmatch '^#[\w-\.]+$' }
$local:exists = $false; $local:exists = $false;
@ -39,7 +38,7 @@ class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator {
foreach( $local:configPath in $flatConfigList ) { foreach( $local:configPath in $flatConfigList ) {
if( -not $first ) { $first = $configPath } if( -not $first ) { $first = $configPath }
if( $Force -or (Test-Path $configPath) ) { if( $Force -or (Test-Path $configPath) ) {
$result += Resolve-Path $configPath $result += Get-Path $configPath
$exists = $true $exists = $true
} }
} }
@ -49,8 +48,7 @@ class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator {
return $result return $result
} }
static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) { static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) {
[MyConfig]::ReloadConfigPaths() $local:possibleValues = [MyConfig]::GetConfigDictionary().Keys
$local:possibleValues = [MyConfig]::configDirectory.Keys
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict ); return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict );
} }

37
src/config.files.json Normal file
View File

@ -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"
]
}