2021-02-18 17:27:50 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-02 16:45:04 +00:00
|
|
|
class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator {
|
2021-02-18 17:27:50 +00:00
|
|
|
hidden static [hashtable]$_HardCodedConfigDict = @{
|
|
|
|
'myconfig' = @('config.files.json','config.files.local.json') | _ExpandConfigPath
|
|
|
|
'mymodules' = @('modules.json', 'modules.local.json') | _ExpandConfigPath
|
2020-11-05 05:00:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 04:31:34 +00:00
|
|
|
static [hashtable]GetConfigDictionary() {
|
|
|
|
$local:result = @{}
|
2021-02-18 17:27:50 +00:00
|
|
|
[MyConfig]::_HardCodedConfigDict.Keys |
|
|
|
|
ForEach-Object {
|
|
|
|
$result[$_] = [MyConfig]::_HardCodedConfigDict[$_]
|
|
|
|
}
|
|
|
|
if( Test-Path Function:/_ExpandConfigPath ) { Remove-Item Function:/_ExpandConfigPath }
|
2020-11-05 05:00:18 +00:00
|
|
|
foreach( $local:jsonSource in $result.myconfig ) {
|
2020-11-05 04:31:34 +00:00
|
|
|
if( -not ( Test-Path $jsonSource ) ) { continue }
|
2021-02-18 17:27:50 +00:00
|
|
|
$local:props = $(Get-Item $jsonSource | Get-Content | ConvertFrom-Json).PSObject.Properties
|
|
|
|
foreach( $local:prop in $props ) {
|
2020-11-05 04:31:34 +00:00
|
|
|
$result[$prop.Name] = $prop.Value
|
|
|
|
}
|
2020-10-31 21:10:32 +00:00
|
|
|
}
|
2020-11-05 04:31:34 +00:00
|
|
|
return $result;
|
2020-10-02 16:45:04 +00:00
|
|
|
}
|
2020-10-31 21:10:32 +00:00
|
|
|
static [string[]] GetConfigPaths([string[]]$ConfigNames,[switch]$Force) {
|
2020-11-05 04:31:34 +00:00
|
|
|
$local:configDirectory = [MyConfig]::GetConfigDictionary()
|
2020-10-31 21:10:32 +00:00
|
|
|
$local:result = [string[]]@()
|
|
|
|
$local:flat = $false
|
|
|
|
while(-not $flat) {
|
|
|
|
$flat = $true
|
|
|
|
$ConfigNames += $ConfigNames | ForEach-Object {
|
2020-11-05 04:31:34 +00:00
|
|
|
$configDirectory[$_]
|
2020-10-31 21:10:32 +00:00
|
|
|
} | Where-Object { $_ -match '^#[\w-\.]+$' } | ForEach-Object {
|
|
|
|
$_.Substring(1)
|
|
|
|
} | Where-Object { $_ -notin $ConfigNames } | ForEach-Object {
|
|
|
|
$flat = false; $_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$local:flatConfigList = $ConfigNames | ForEach-Object {
|
2020-11-05 04:31:34 +00:00
|
|
|
$configDirectory[$_]
|
2020-10-31 21:10:32 +00:00
|
|
|
} | Where-Object { $_ -notmatch '^#[\w-\.]+$' }
|
|
|
|
|
|
|
|
$local:exists = $false;
|
|
|
|
$local:first = $null
|
|
|
|
foreach( $local:configPath in $flatConfigList ) {
|
2020-12-02 04:31:42 +00:00
|
|
|
$configPath = Get-Path $configPath
|
2020-10-31 21:10:32 +00:00
|
|
|
if( -not $first ) { $first = $configPath }
|
|
|
|
if( $Force -or (Test-Path $configPath) ) {
|
2020-12-02 04:31:42 +00:00
|
|
|
$result += $configPath
|
2020-10-31 21:10:32 +00:00
|
|
|
$exists = $true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( -not $exists -and -not $Force ) {
|
|
|
|
$result += $first
|
|
|
|
}
|
|
|
|
return $result
|
2020-10-02 16:45:04 +00:00
|
|
|
}
|
|
|
|
static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) {
|
2020-11-05 04:31:34 +00:00
|
|
|
$local:possibleValues = [MyConfig]::GetConfigDictionary().Keys
|
2020-10-02 16:45:04 +00:00
|
|
|
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict );
|
|
|
|
}
|
|
|
|
|
|
|
|
[String[]] GetValidValues() {
|
|
|
|
return [MyConfig]::_GetValidValues('',$true)
|
|
|
|
}
|
|
|
|
}
|