Script directory for a PowerShell profile. https://blog.lksz.me/keeping-track-of-everything/#-mypsscriptroot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.6 KiB

  1. class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator {
  2. hidden static [hashtable]$_HardCodedConfigDict = @{
  3. 'myconfig' = @($(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'config.files.json'),
  4. $(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'config.files.local.json'))
  5. 'mymodules' = @($(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'modules.json'),
  6. $(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'modules.local.json'))
  7. }
  8. static [hashtable]GetConfigDictionary() {
  9. $local:result = @{}
  10. [MyConfig]::_HardCodedConfigDict.Keys | ForEach-Object { $result[$_] = [MyConfig]::_HardCodedConfigDict[$_] }
  11. foreach( $local:jsonSource in $result.myconfig ) {
  12. if( -not ( Test-Path $jsonSource ) ) { continue }
  13. foreach( $local:prop in $(Get-Content $jsonSource | ConvertFrom-Json).PSObject.Properties ) {
  14. $result[$prop.Name] = $prop.Value
  15. }
  16. }
  17. return $result;
  18. }
  19. static [string[]] GetConfigPaths([string[]]$ConfigNames,[switch]$Force) {
  20. $local:configDirectory = [MyConfig]::GetConfigDictionary()
  21. $local:result = [string[]]@()
  22. $local:flat = $false
  23. while(-not $flat) {
  24. $flat = $true
  25. $ConfigNames += $ConfigNames | ForEach-Object {
  26. $configDirectory[$_]
  27. } | Where-Object { $_ -match '^#[\w-\.]+$' } | ForEach-Object {
  28. $_.Substring(1)
  29. } | Where-Object { $_ -notin $ConfigNames } | ForEach-Object {
  30. $flat = false; $_;
  31. }
  32. }
  33. $local:flatConfigList = $ConfigNames | ForEach-Object {
  34. $configDirectory[$_]
  35. } | Where-Object { $_ -notmatch '^#[\w-\.]+$' }
  36. $local:exists = $false;
  37. $local:first = $null
  38. foreach( $local:configPath in $flatConfigList ) {
  39. if( -not $first ) { $first = $configPath }
  40. if( $Force -or (Test-Path $configPath) ) {
  41. $result += Get-Path $configPath
  42. $exists = $true
  43. }
  44. }
  45. if( -not $exists -and -not $Force ) {
  46. $result += $first
  47. }
  48. return $result
  49. }
  50. static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) {
  51. $local:possibleValues = [MyConfig]::GetConfigDictionary().Keys
  52. return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict );
  53. }
  54. [String[]] GetValidValues() {
  55. return [MyConfig]::_GetValidValues('',$true)
  56. }
  57. }