From b303fa0f88b1aef5ddb18a0f81ebd671db19d4fc Mon Sep 17 00:00:00 2001 From: "lksz@work" Date: Wed, 7 Apr 2021 16:04:46 -0400 Subject: [PATCH] Cleanup of basics to work in Win + fixes to bugs --- base/Edit-MyProfiles.ps1 | 4 +- base/Edit-TextFile.ps1 | 57 ++++++++++++++++---------- base/Reload-MyScripts.ps1 | 85 +++++++++++++++++++++++++-------------- 3 files changed, 91 insertions(+), 55 deletions(-) diff --git a/base/Edit-MyProfiles.ps1 b/base/Edit-MyProfiles.ps1 index f64f1c8..e9ac67f 100644 --- a/base/Edit-MyProfiles.ps1 +++ b/base/Edit-MyProfiles.ps1 @@ -14,10 +14,10 @@ [string[]]$AlsoEditTheseScripts ) -$local:FilePaths = @() + $( Get-Profiles | Where-Object { $_.Exists -or $Force } | Select-Object -ExpandProperty Path ) +[string[]]$local:FilePaths = @() + $( Get-Profiles | Where-Object { $_.Exists -or $Force } | Select-Object -ExpandProperty Path ) foreach( $local:p in $AlsoEditTheseScripts ) { $local:sp = Join-Path $MyPSScriptRoot "$p`.ps1" $FilePaths += $sp } -Edit-TextFile -sudo:$sudo $FilePaths +Edit-TextFile -sudo:$sudo -Path $FilePaths diff --git a/base/Edit-TextFile.ps1 b/base/Edit-TextFile.ps1 index 1d4ca76..b9a8c0c 100644 --- a/base/Edit-TextFile.ps1 +++ b/base/Edit-TextFile.ps1 @@ -1,31 +1,44 @@ [CmdletBinding(SupportsShouldProcess)]param( [switch]$sudo, - [Parameter(Position = 0, ValueFromRemainingArguments = $true)] + [Parameter(Position = 0, ValueFromPipeline, ValueFromRemainingArguments = $true)] [string[]]$Path ) -$local:editors = $env:EDITOR,'nvim','code' -$local:editor = $null -foreach( $local:testEditor in $editors ) { - if( $(try{Get-Command -Type Application $testEditor -ErrorAction SilentlyContinue}catch{}) ) { - $editor = $testEditor - break; +begin { + $PathForEditor = @() +} +process { + foreach( $local:P in $Path ) { + $PathForEditor += $P | Get-Path -Expand } } -if( $editor -match 'vim?$' ) { - $editor += ' -p' -} - -if( $editor -match 'code(\.exe)?$' ) { - if( -not (Get-Process -Name 'code' -ErrorAction SilentlyContinue) ) { - Invoke-ExpressionEx -sudo:$sudo $editor +end { + $local:editors = $env:EDITOR,'nvim','code' + $local:editor = $null + foreach( $local:testEditor in $editors ) { + if( $(try{Get-Command -Type Application $testEditor -ErrorAction SilentlyContinue}catch{}) ) { + $editor = $testEditor + break; + } } -} - -$local:arguments = $Path | Get-Path -Expand | Join-String -Separator "' '" -if( $Path ) { $arguments = "'$arguments'" } - -if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) { - Invoke-ExpressionEx -sudo:$sudo $editor "$arguments" -} + + if( $editor -match 'vim?$' ) { + $editor += ' -p' + } + + if( $editor -match 'code(\.exe)?$' ) { + if( -not (Get-Process -Name 'code' -ErrorAction SilentlyContinue) ) { + Invoke-ExpressionEx -sudo:$sudo $editor + } + } + + Write-Verbose $($Path | Out-String) + + $local:arguments = $PathForEditor | Foreach-Object { "'$_'" } + if( $Path ) { $arguments = "$arguments" } + + if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) { + Invoke-ExpressionEx -sudo:$sudo $editor "$arguments" + } +} \ No newline at end of file diff --git a/base/Reload-MyScripts.ps1 b/base/Reload-MyScripts.ps1 index 162cf65..9c6b868 100644 --- a/base/Reload-MyScripts.ps1 +++ b/base/Reload-MyScripts.ps1 @@ -1,8 +1,31 @@ [CmdletBinding(SupportsShouldProcess)]param() -try { Get-Alias Get-MyAliases -ErrorAction Stop | ForEach-Object { Remove-Item "Alias:$($_.Name)" } } catch {} +function RemoveAlias { + param([string]$Name) + $local:a = $Name | Where-Object { $_ } | Get-Alias -ErrorAction SilentlyContinue + if ( -not $a ) { return } + if ( Get-Command Remove-Alias -ErrorAction SilentlyContinue ) { + $a | Remove-Alias -Scope $MyAliasScope -ErrorAction SilentlyContinue + } + else { + Remove-Item "Alias:$a" + } +} +function SafeSetAlias { + param([string]$Name, [string]$Value, [string]$Description) + + . RemoveAlias $Name + try { + Set-Alias $Name $Value -Scope $MyAliasScope -Description:$Description -ErrorAction Stop + } + catch { + Set-Alias $Name $Value -Scope $MyAliasScope -Description:$Description -Option AllScope + } +} + +try { Get-Alias Get-MyAliases -ErrorAction Stop | ForEach-Object { . RemoveAlias $($_.Name) } } catch {} function Get-MyAliases { -[CmdletBinding(SupportsShouldProcess)]param([switch]$ScriptsOnly) + [CmdletBinding()]param([switch]$ScriptsOnly) $local:allAliases = @() @@ -13,16 +36,17 @@ function Get-MyAliases { try { $local:newAliases += Get-Alias -Scope $_scope | Where-Object { - ($_.Definition -match "^$MyPSScriptRoot") -or (-not $ScriptsOnly -and ($_.Description -match '#MyAlias')) + ($_.Definition -like "$MyPSScriptRoot*") -or (-not $ScriptsOnly -and ($_.Description -match '#MyAlias')) } - if( $newAliases ) { + if ( $newAliases ) { $allAliases += $newAliases - $MyAliasScope = $_scope; + $MyAliasScope = $_scope; Write-Verbose "`$MyAliasScope is now set to $MyAliasScope" } - } catch { + } + catch { Write-Verbose "catch: $($_.Exception.Message)" - $_done = $_.Exception.Message -match 'The scope .* exceeds' + $_done = $_.Exception.Message -match 'The scope .* exceeds' } $_scope += 1 } until ( $_done ) @@ -30,8 +54,9 @@ function Get-MyAliases { $allAliases } -function getScriptName{param([string]$FullPath) - $FullPath -replace '\.ps1$','' -replace "^$([regex]::Escape($MyPSScriptRoot)).",'' +function getScriptName { + param([string]$FullPath) + $FullPath -replace '\.ps1$', '' -replace "^$([regex]::Escape($MyPSScriptRoot)).", '' } $local:IsVerbose = [bool]($PSBoundParameters['Verbose']) @@ -39,7 +64,8 @@ $local:IsVerbose = [bool]($PSBoundParameters['Verbose']) # Loads mandatory Package code try { $null = [Packagesz] -} catch { +} +catch { $local:PackagePath = $(Join-Path $MyPSScriptRoot 'base') Join-Path $(Join-Path $PackagePath profile.d) 'classes.ps1' | Where-Object { Test-Path $_ } | ForEach-Object { . $_ } @@ -62,21 +88,17 @@ $local:oldAliases = Get-MyAliases $oldAliases = Get-Alias -Scope $MyAliasScope | Where-Object Name -in $($oldAliases.Name + $myAliases.Keys) -if( $oldAliases -and $IsVerbose ) { +if ( $oldAliases -and $IsVerbose ) { Write-Verbose "Removing: $($oldAliases.Name -join ', ')" } -if( Get-Command Remove-Alias -ErrorAction SilentlyContinue ) { - $oldAliases | Remove-Alias -Scope $MyAliasScope -} else { - $oldAliases | ForEach-Object { Remove-Item "Alias:$($_.Name)" } -} +$oldAliases | ForEach-Object { . RemoveAlias $($_.Name) } -if( $(. Get-ScopeDepth) -gt 0 ) { +if ( $(. Get-ScopeDepth) -gt 0 ) { Write-Host -ForegroundColor Red "Try sourcing Reload-MyScripts instead of just running it" } -foreach( $local:PackagePath in $myPackages ) { - $local:PackageName = $PackagePath.Name +foreach ( $local:PackagePath in $myPackages ) { + #$local:PackageName = $PackagePath.Name Join-Path $PackagePath 'profile.d' | Where-Object { Test-Path $_ } | Get-ChildItem -Filter '*.ps1' | @@ -88,24 +110,25 @@ foreach( $local:PackagePath in $myPackages ) { $local:CommandsToAlias = ( @( $PackagePath ) + $( - [SystemName]::_GetValidValues("",$true,$true) | + [SystemName]::_GetValidValues("", $true, $true) | ForEach-Object { Join-Path $PackagePath "sys.$_" } - )) | - Where-Object { Test-Path $_ } | - ForEach-Object { - Get-ChildItem (Join-Path $_ '*.ps1') | Where-Object Name -notmatch '\.inc\.ps1$' - } + )) | + Where-Object { Test-Path $_ } | + ForEach-Object { + Get-ChildItem (Join-Path $_ '*.ps1') | Where-Object Name -notmatch '\.inc\.ps1$' + } - $CommandsToAlias | ForEach-Object { - Write-Verbose "Creating alias for $(getScriptName $_.FullName) Script..." - Set-Alias $($_.BaseName) $_.FullName -Scope $MyAliasScope - } + foreach ( $local:newAlias in $CommandsToAlias ) { + Write-Verbose "Creating alias for $(getScriptName $newAlias.FullName) Script..." + . SafeSetAlias $($newAlias.BaseName) $($newAlias.FullName) + } } -foreach( $local:alias in $myAliases.Keys ) { +foreach ( $local:alias in $myAliases.Keys ) { Write-Verbose "Adding $($alias) alias..." - Set-Alias -Name $alias -Value $myAliases[$alias] -Description '#MyAlias' -Scope $MyAliasScope + . SafeSetAlias $alias $myAliases[$alias] '#MyAlias' } +Remove-Item Function:\RemoveAlias, Function:\SafeSetAlias -ErrorAction SilentlyContinue \ No newline at end of file