PowerShell_Scripts/Edit-MyScript.ps1

52 行
1.3 KiB
PowerShell

[CmdletBinding(SupportsShouldProcess)]param(
[ArgumentCompleter({ param (
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
[FunctionName]::_GetValidValues($wordToComplete,$true)
})]
[string[]]$ImportFunction,
[switch]$ForceImport,
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
[ArgumentCompleter({ param (
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
[MyScript]::_GetValidValues($wordToComplete,$false)
})]
[string[]]$ScriptName
)
$local:ScriptPaths = @()
foreach( $local:p in $ImportFunction ) {
$local:f = Get-Command $p -Type Function
if( $f ) {
$local:sp = Join-Path $MyPSScriptRoot "$p`.ps1"
if( $ForceImport -or -not (Test-Path $sp) ) {
Export-FunctionSource $p -NoHeader > $sp
} elseif ( -not $ForceImport ) {
throw "$p already exists in Script, to import the function again, use the -Force"
}
$ScriptPaths += $sp
}
}
foreach( $local:p in $ScriptName ) {
$local:sp = Join-Path $MyPSScriptRoot "$p`.ps1"
$ScriptPaths += $sp
}
if( -not $ScriptPaths ) {
$ScriptPaths += $MyPSScriptRoot
}
Edit-TextFile $ScriptPaths
Get-Command Reload-MyScripts -ErrorAction SilentlyContinue |
ForEach-Object { . $_.Definition }