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.

84 lines
2.4 KiB

  1. [CmdletBinding(SupportsShouldProcess)]param(
  2. [ArgumentCompleter({ param (
  3. $commandName,
  4. $parameterName,
  5. $wordToComplete,
  6. $commandAst,
  7. $fakeBoundParameters
  8. )
  9. [FunctionName]::_GetValidValues($wordToComplete,$true)
  10. })]
  11. [string[]]$ImportFunction,
  12. [ArgumentCompleter({ param (
  13. $commandName,
  14. $parameterName,
  15. $wordToComplete,
  16. $commandAst,
  17. $fakeBoundParameters
  18. )
  19. [SystemName]::_GetValidValues($wordToComplete,$false,$true)
  20. })]
  21. [string]$System,
  22. [switch]$ForceImport,
  23. [Parameter(Position = 0, ValueFromRemainingArguments = $true)]
  24. [ArgumentCompleter({ param (
  25. $commandName,
  26. $parameterName,
  27. $wordToComplete,
  28. $commandAst,
  29. $fakeBoundParameters
  30. )
  31. $local:possibleValues = [MyScript]::_GetValidValues($wordToComplete,$true)
  32. if( $fakeBoundParameters.ContainsKey("System") ) {
  33. $possibleValues = $possibleValues | Where-Object { $_ -match "^sys\.$($fakeBoundParameters.System)" } | ForEach-Object { $_ -replace "^sys\.$($fakeBoundParameters.System).",'' }
  34. }
  35. $possibleValues
  36. })]
  37. [string[]]$ScriptName
  38. )
  39. $local:EditRootPath=$MyPSScriptRoot
  40. if( $System ) {
  41. $EditRootPath = Join-Path $EditRootPath "sys.$System"
  42. }
  43. $local:ScriptPaths = @()
  44. foreach( $local:p in $ImportFunction ) {
  45. $local:f = Get-Command $p -Type Function
  46. if( $f ) {
  47. $local:sp = Join-Path $EditRootPath "$p`.ps1"
  48. if( $ForceImport -or -not (Test-Path $sp) ) {
  49. Export-FunctionSource $p -NoHeader > $sp
  50. } elseif ( -not $ForceImport ) {
  51. throw "$p already exists in Script, to import the function again, use the -Force"
  52. }
  53. $ScriptPaths += $sp
  54. }
  55. }
  56. foreach( $local:p in $ScriptName ) {
  57. $local:sp = Join-Path $EditRootPath "$p`.ps1"
  58. $ScriptPaths += $sp
  59. }
  60. if( -not $ScriptPaths ) {
  61. $ScriptPaths += $EditRootPath
  62. }
  63. foreach( $local:p in $ScriptPaths ) {
  64. $local:parentPath = Split-Path -Parent $p;
  65. if( -not (Test-Path $p) -and -not (Test-Path $parentPath) ) {
  66. Write-Verbose "Creating missing path $parentPath..."
  67. New-Item -Type Directory $parentPath
  68. }
  69. }
  70. $local:sw = [System.Diagnostics.Stopwatch]::StartNew();
  71. Edit-TextFile $ScriptPaths
  72. $sw.Stop();
  73. if( $sw.Elapsed.TotalSeconds -lt 1 ) {
  74. $null = Read-Host "Waiting before refreshing. Press <Enter> when you're done editing ( don't forget to save ;) )..."
  75. }
  76. Get-Command Reload-MyScripts -ErrorAction SilentlyContinue |
  77. ForEach-Object { . $_.Definition }