PowerShell_Scripts/profile.d/MyConfig.class.ps1

62 рядки
2.6 KiB
PowerShell

class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator {
hidden static [hashtable]$_HardCodedConfigDict = @{
'myconfig' = @($(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'config.files.json'),
$(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'config.files.local.json'))
'mymodules' = @($(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'modules.json'),
$(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'modules.local.json'))
}
static [hashtable]GetConfigDictionary() {
$local:result = @{}
[MyConfig]::_HardCodedConfigDict.Keys | ForEach-Object { $result[$_] = [MyConfig]::_HardCodedConfigDict[$_] }
foreach( $local:jsonSource in $result.myconfig ) {
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) {
$local:configDirectory = [MyConfig]::GetConfigDictionary()
$local:result = [string[]]@()
$local:flat = $false
while(-not $flat) {
$flat = $true
$ConfigNames += $ConfigNames | ForEach-Object {
$configDirectory[$_]
} | Where-Object { $_ -match '^#[\w-\.]+$' } | ForEach-Object {
$_.Substring(1)
} | Where-Object { $_ -notin $ConfigNames } | ForEach-Object {
$flat = false; $_;
}
}
$local:flatConfigList = $ConfigNames | ForEach-Object {
$configDirectory[$_]
} | Where-Object { $_ -notmatch '^#[\w-\.]+$' }
$local:exists = $false;
$local:first = $null
foreach( $local:configPath in $flatConfigList ) {
if( -not $first ) { $first = $configPath }
if( $Force -or (Test-Path $configPath) ) {
$result += Get-Path $configPath
$exists = $true
}
}
if( -not $exists -and -not $Force ) {
$result += $first
}
return $result
}
static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) {
$local:possibleValues = [MyConfig]::GetConfigDictionary().Keys
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict );
}
[String[]] GetValidValues() {
return [MyConfig]::_GetValidValues('',$true)
}
}