From 607fff132fda00ebe96d8effc5f48b6359aa2c06 Mon Sep 17 00:00:00 2001 From: lksz Date: Wed, 30 Sep 2020 00:14:51 -0400 Subject: [PATCH] Mending Broken Windows #2 Editor, no Remove-Alias before PowrShell Core. IValidateSetValuesGenerator missing before PowerShell Core --- Edit-TextFile.ps1 | 16 +++++++++++++--- Reload-MyScripts.ps1 | 7 ++++++- profile.d/FunctionName.class.ps1 | 2 +- profile.d/MyScript.class.ps1 | 2 +- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Edit-TextFile.ps1 b/Edit-TextFile.ps1 index fc0542b..e25d0d6 100644 --- a/Edit-TextFile.ps1 +++ b/Edit-TextFile.ps1 @@ -4,9 +4,13 @@ [string[]]$Path ) -$local:editor = '/bin/nvim' -if( (Test-Path env:EDITOR) -and (Test-Path $env:EDITOR) ) { - $editor = $env:EDITOR; +$local:editors = $env:EDITOR,'nvim','code' +$local:editor = $null +foreach( $local:testEditor in $editors ) { + if( Get-Command -Type Application $testEditor -ErrorAction SilentlyContinue ) { + $editor = $testEditor + break; + } } if( $editor -match 'vim?$' ) { @@ -17,6 +21,12 @@ if( $sudo ) { $editor = "/usr/bin/env sudo $editor" } +if( $editor -match 'code(\.exe)?$' ) { + if( -not (Get-Process -Name 'code' -ErrorAction SilentlyContinue) ) { + & $editor + } +} + $local:arguments = $Path -join "' '" if( $Path ) { $arguments = "'$arguments'" } diff --git a/Reload-MyScripts.ps1 b/Reload-MyScripts.ps1 index fad2342..fd0aca3 100644 --- a/Reload-MyScripts.ps1 +++ b/Reload-MyScripts.ps1 @@ -23,7 +23,12 @@ $oldAliases = Get-Alias -Scope $MyAliasScope | if( $oldAliases -and $IsVerbose ) { Write-Verbose "Removing: $($oldAliases.Name -join ', ')" } -$oldAliases | Remove-Alias -Scope $MyAliasScope +if( Get-Command Remove-Alias -ErrorAction SilentlyContinue ) { + $oldAliases | Remove-Alias -Scope $MyAliasScope +} else { + $oldAliases | ForEach-Object { Remove-Item "Alias:$($_.Name)" } +} + Get-ChildItem (Join-Path $MyPSScriptRoot '*.ps1') | ForEach-Object { Set-Alias $($_.BaseName) $_.FullName -Scope $MyAliasScope diff --git a/profile.d/FunctionName.class.ps1 b/profile.d/FunctionName.class.ps1 index 9e51fc4..44fbdd1 100644 --- a/profile.d/FunctionName.class.ps1 +++ b/profile.d/FunctionName.class.ps1 @@ -1,4 +1,4 @@ -class FunctionName: System.Management.Automation.IValidateSetValuesGenerator { +class FunctionName { #: System.Management.Automation.IValidateSetValuesGenerator { static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) { $local:possibleValues = Get-Command -Type Function | Select-Object -ExpandProperty Name return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict ); diff --git a/profile.d/MyScript.class.ps1 b/profile.d/MyScript.class.ps1 index aab3df9..794ee8b 100644 --- a/profile.d/MyScript.class.ps1 +++ b/profile.d/MyScript.class.ps1 @@ -1,4 +1,4 @@ -class MyScript : System.Management.Automation.IValidateSetValuesGenerator { +class MyScript { #: System.Management.Automation.IValidateSetValuesGenerator { static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) { $local:possibleValues = $( Get-ChildItem $global:MyPSScriptRoot -Filter '*.ps1' -Recurse |