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
bovenliggende d85aec0480
commit 524bce25de
4 gewijzigde bestanden met toevoegingen van 65 en 20 verwijderingen

Bestand weergeven

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

8
Get-Path.ps1 Normal file
Bestand weergeven

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

Bestand weergeven

@ -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 );
}

37
src/config.files.json Normal file
Bestand weergeven

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