PowerShell_Scripts/base/Edit-MyScript.ps1

140 lines
4.3 KiB
PowerShell

[CmdletBinding(SupportsShouldProcess)]param(
[ArgumentCompleter({ param (
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
[FunctionName]::_GetValidValues($wordToComplete,$true)
})]
[string[]]$ImportFunction,
[ArgumentCompleter({ param (
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
$local:NotAll = $true
if( $fakeBoundParameters.ContainsKey("All") ) { $NotAll = -not $fakeBoundParameters.All }
[Packagesz]::_GetValidValues($wordToComplete,$NotAll,$true)
})]
[string]$Package,
[ArgumentCompleter({ param (
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
$local:NotAll = $true
if( $fakeBoundParameters.ContainsKey("All") ) { $NotAll = -not $fakeBoundParameters.All }
$( (,(
Get-MyPackages -Force |
ForEach-Object { Join-Path (Join-Path $MyPSScriptRoot $_) $(Join-Path "profile.d" "*.format.ps1xml") } |
Where-Object { Test-Path $_ } |
Get-Item |
ForEach-Object {
"$( $_ |
Select-Object -ExpandProperty Directory |
Select-Object -ExpandProperty Parent |
Select-Object -ExpandProperty Name
):$($_.Name -replace '\.format\.ps1xml$','')" # -replace ':TypeData$',''
}
)) + (
[Packagesz]::_GetValidValues($wordToComplete,$NotAll,$true))
) | Select-Object -Unique
})]
[string[]]$TypeData,
[switch]$ForceImport,
[switch]$All,
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
[ArgumentCompleter({ param (
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
$local:NotAll = $true
$local:Pkg = [string]::Empty
if( $fakeBoundParameters.ContainsKey("All") ) { $NotAll = -not $fakeBoundParameters.All }
if( $fakeBoundParameters.ContainsKey("Package") ) { $Pkg = Join-Path $fakeBoundParameters.Package '' }
$local:possibleValues = [MyScript]::_GetValidValues($wordToComplete,$NotAll,$true)
if( $Pkg ) {
$possibleValues = $possibleValues |
Where-Object { $_ -match "^$Pkg" } |
ForEach-Object { $_.Replace($Pkg,'') }
}
$possibleValues
})]
[string[]]$ScriptName,
[switch]$WaitNotRequired = $env:WAIT_NOT_REQUIRED
)
$local:EditRootPath=$MyPSScriptRoot
if( $Package ) {
$EditRootPath = Join-Path $EditRootPath $Package
}
$local:ScriptPaths = @()
foreach( $local:p in $ImportFunction ) {
$local:f = Get-Command -ListImported $p -Type Function
if( $f ) {
$local:sp = Join-Path $EditRootPath "$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 $EditRootPath "$p`.ps1"
$ScriptPaths += $sp
}
foreach( $local:t in $TypeData ) {
$local:pkgName = $t
$local:fileBaseName = 'TypeData'
if( $t.Contains(':') ) {
$pkgName = $t -Split ':' | Select-Object -First 1
$fileBaseName = $t -Split ':' | Select-Object -Skip 1 -First 1
}
$fileBaseName = $fileBaseName -replace '\.format\.ps1xml$',''
Get-Path $(
Join-Path $(Join-Path $EditRootPath $pkgName) $(Join-Path "profile.d" "$fileBaseName.format.ps1xml")
) |
Where-Object { $_ } |
ForEach-Object {
$ScriptPaths += $_
}
}
if( -not $ScriptPaths ) {
$ScriptPaths += $EditRootPath
}
foreach( $local:p in $ScriptPaths ) {
$local:parentPath = Split-Path -Parent $p;
if( -not (Test-Path $p) -and -not (Test-Path $parentPath) ) {
Write-Verbose "Creating missing path $parentPath..."
New-Item -Type Directory $parentPath
}
}
$local:sw = [System.Diagnostics.Stopwatch]::StartNew();
Edit-TextFile -Path $ScriptPaths
$sw.Stop();
if( $sw.Elapsed.TotalSeconds -lt 3 ) {
Write-Host -ForegroundColor DarkGreen "When done editing, you might want to run '. Reload-MyScripts'"
return
}
Get-Command -ListImported Reload-MyScripts -ErrorAction SilentlyContinue |
ForEach-Object { . $_.Definition }