From 0e986cfaa73c505c2bceac697441c8dc41e0a2a2 Mon Sep 17 00:00:00 2001 From: lksz Date: Thu, 18 Feb 2021 12:27:50 -0500 Subject: [PATCH] Module loading upgrade This all started because I wanted the UnixCompleters module and initialization to move to the base.linux package. * MyConfig.class modified to load from all modules * Test-MyModules suppress empty entries * removed unix entries from base/src/modules.json + created base.linux/src/modules.json --- .../profile.d/UnixCompleters.ps1 | 2 +- base.linux/src/modules.json | 5 ++++ base/profile.d/MyConfig.class.ps1 | 26 ++++++++++++++----- base/profile.d/Test-MyModules.ps1 | 11 ++++---- base/src/modules.json | 9 +------ 5 files changed, 32 insertions(+), 21 deletions(-) rename {base => base.linux}/profile.d/UnixCompleters.ps1 (75%) create mode 100644 base.linux/src/modules.json diff --git a/base/profile.d/UnixCompleters.ps1 b/base.linux/profile.d/UnixCompleters.ps1 similarity index 75% rename from base/profile.d/UnixCompleters.ps1 rename to base.linux/profile.d/UnixCompleters.ps1 index aeb3a6d..061b232 100644 --- a/base/profile.d/UnixCompleters.ps1 +++ b/base.linux/profile.d/UnixCompleters.ps1 @@ -1,4 +1,4 @@ Get-Command Import-UnixCompleters -ErrorAction SilentlyContinue | ForEach-Object { Import-UnixCompleters - Set-UnixCompleter -ShellType Zsh + Set-UnixCompleter -ShellType Bash } diff --git a/base.linux/src/modules.json b/base.linux/src/modules.json new file mode 100644 index 0000000..d51d435 --- /dev/null +++ b/base.linux/src/modules.json @@ -0,0 +1,5 @@ +{ + "Unix": [ + "Microsoft.PowerShell.UnixCompleters" + ] +} diff --git a/base/profile.d/MyConfig.class.ps1 b/base/profile.d/MyConfig.class.ps1 index eea89f3..7d9ad0e 100644 --- a/base/profile.d/MyConfig.class.ps1 +++ b/base/profile.d/MyConfig.class.ps1 @@ -1,17 +1,29 @@ +filter _ExpandConfigPath { + $_ | ForEach-Object { + $local:c = Join-Path $(Join-Path $MyPSScriptRoot $(Join-Path '###' 'src')) $_ + $c -replace "###","base" + $c -replace "###","*" | Get-Item -ErrorAction SilentlyContinue | + Select-Object -ExpandProperty FullName + } | Select-Object -Unique + } + class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator { - hidden static [hashtable]$_HardCodedConfigDict = @{ - 'myconfig' = @($(Join-Path $(Join-Path $PackagePath 'src') 'config.files.json'), - $(Join-Path $(Join-Path $PackagePath 'src') 'config.files.local.json')) - 'mymodules' = @($(Join-Path $(Join-Path $PackagePath 'src') 'modules.json'), - $(Join-Path $(Join-Path $PackagePath 'src') 'modules.local.json')) + hidden static [hashtable]$_HardCodedConfigDict = @{ + 'myconfig' = @('config.files.json','config.files.local.json') | _ExpandConfigPath + 'mymodules' = @('modules.json', 'modules.local.json') | _ExpandConfigPath } static [hashtable]GetConfigDictionary() { $local:result = @{} - [MyConfig]::_HardCodedConfigDict.Keys | ForEach-Object { $result[$_] = [MyConfig]::_HardCodedConfigDict[$_] } + [MyConfig]::_HardCodedConfigDict.Keys | + ForEach-Object { + $result[$_] = [MyConfig]::_HardCodedConfigDict[$_] + } + if( Test-Path Function:/_ExpandConfigPath ) { Remove-Item Function:/_ExpandConfigPath } foreach( $local:jsonSource in $result.myconfig ) { if( -not ( Test-Path $jsonSource ) ) { continue } - foreach( $local:prop in $(Get-Content $jsonSource | ConvertFrom-Json).PSObject.Properties ) { + $local:props = $(Get-Item $jsonSource | Get-Content | ConvertFrom-Json).PSObject.Properties + foreach( $local:prop in $props ) { $result[$prop.Name] = $prop.Value } } diff --git a/base/profile.d/Test-MyModules.ps1 b/base/profile.d/Test-MyModules.ps1 index 7ac05fc..c2f5ce2 100644 --- a/base/profile.d/Test-MyModules.ps1 +++ b/base/profile.d/Test-MyModules.ps1 @@ -5,12 +5,13 @@ $local:currentSys = [SystemName]::_GetValidValues('',$true,$true); if( -not $global:MyModulesWarning -or $Force ) { $global:MyModulesWarning = @() } + [MyConfig]::GetConfigPaths('mymodules',$false) | - ForEach-Object { + ForEach-Object { $local:modules = $( Get-Content $_ | ConvertFrom-Json ); - $modules.Always; - $currentSys | ForEach-Object { $modules."$_" } | Where-Object { $_} - } | Where-Object { + $modules.Always + $currentSys | ForEach-Object { $modules."$_" } + } | Where-Object { $_ } | Where-Object { $local:m = $_ if( $( try { -not (Get-Module -ListAvailable $m -ErrorAction stop) @@ -21,4 +22,4 @@ if( -not $global:MyModulesWarning -or $Force ) { } | ForEach-Object { $global:MyModulesWarning += $_ Write-Warning "Missing module: $_" - } \ No newline at end of file + } diff --git a/base/src/modules.json b/base/src/modules.json index 4c33ec3..f4bba93 100644 --- a/base/src/modules.json +++ b/base/src/modules.json @@ -4,11 +4,4 @@ "oh-my-posh", "posh-git", "z" - ], - "Desktop": [ - ], - "Unix": [ - "Microsoft.PowerShell.UnixCompleters" - ] - -} + ]}