Cleanup of basics to work in Win + fixes to bugs

This commit is contained in:
lksz@work 2021-04-07 16:04:46 -04:00
parent b558edacd9
commit b303fa0f88
3 changed files with 91 additions and 55 deletions

View File

@ -14,10 +14,10 @@
[string[]]$AlsoEditTheseScripts [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 ) { foreach( $local:p in $AlsoEditTheseScripts ) {
$local:sp = Join-Path $MyPSScriptRoot "$p`.ps1" $local:sp = Join-Path $MyPSScriptRoot "$p`.ps1"
$FilePaths += $sp $FilePaths += $sp
} }
Edit-TextFile -sudo:$sudo $FilePaths Edit-TextFile -sudo:$sudo -Path $FilePaths

View File

@ -1,9 +1,19 @@
[CmdletBinding(SupportsShouldProcess)]param( [CmdletBinding(SupportsShouldProcess)]param(
[switch]$sudo, [switch]$sudo,
[Parameter(Position = 0, ValueFromRemainingArguments = $true)] [Parameter(Position = 0, ValueFromPipeline, ValueFromRemainingArguments = $true)]
[string[]]$Path [string[]]$Path
) )
begin {
$PathForEditor = @()
}
process {
foreach( $local:P in $Path ) {
$PathForEditor += $P | Get-Path -Expand
}
}
end {
$local:editors = $env:EDITOR,'nvim','code' $local:editors = $env:EDITOR,'nvim','code'
$local:editor = $null $local:editor = $null
foreach( $local:testEditor in $editors ) { foreach( $local:testEditor in $editors ) {
@ -23,9 +33,12 @@ if( $editor -match 'code(\.exe)?$' ) {
} }
} }
$local:arguments = $Path | Get-Path -Expand | Join-String -Separator "' '" Write-Verbose $($Path | Out-String)
if( $Path ) { $arguments = "'$arguments'" }
$local:arguments = $PathForEditor | Foreach-Object { "'$_'" }
if( $Path ) { $arguments = "$arguments" }
if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) { if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) {
Invoke-ExpressionEx -sudo:$sudo $editor "$arguments" Invoke-ExpressionEx -sudo:$sudo $editor "$arguments"
} }
}

View File

@ -1,8 +1,31 @@
[CmdletBinding(SupportsShouldProcess)]param() [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 { function Get-MyAliases {
[CmdletBinding(SupportsShouldProcess)]param([switch]$ScriptsOnly) [CmdletBinding()]param([switch]$ScriptsOnly)
$local:allAliases = @() $local:allAliases = @()
@ -13,14 +36,15 @@ function Get-MyAliases {
try { try {
$local:newAliases += Get-Alias -Scope $_scope | $local:newAliases += Get-Alias -Scope $_scope |
Where-Object { 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 $allAliases += $newAliases
$MyAliasScope = $_scope; $MyAliasScope = $_scope;
Write-Verbose "`$MyAliasScope is now set to $MyAliasScope" Write-Verbose "`$MyAliasScope is now set to $MyAliasScope"
} }
} catch { }
catch {
Write-Verbose "catch: $($_.Exception.Message)" Write-Verbose "catch: $($_.Exception.Message)"
$_done = $_.Exception.Message -match 'The scope .* exceeds' $_done = $_.Exception.Message -match 'The scope .* exceeds'
} }
@ -30,7 +54,8 @@ function Get-MyAliases {
$allAliases $allAliases
} }
function getScriptName{param([string]$FullPath) function getScriptName {
param([string]$FullPath)
$FullPath -replace '\.ps1$', '' -replace "^$([regex]::Escape($MyPSScriptRoot)).", '' $FullPath -replace '\.ps1$', '' -replace "^$([regex]::Escape($MyPSScriptRoot)).", ''
} }
@ -39,7 +64,8 @@ $local:IsVerbose = [bool]($PSBoundParameters['Verbose'])
# Loads mandatory Package code # Loads mandatory Package code
try { try {
$null = [Packagesz] $null = [Packagesz]
} catch { }
catch {
$local:PackagePath = $(Join-Path $MyPSScriptRoot 'base') $local:PackagePath = $(Join-Path $MyPSScriptRoot 'base')
Join-Path $(Join-Path $PackagePath profile.d) 'classes.ps1' | Join-Path $(Join-Path $PackagePath profile.d) 'classes.ps1' |
Where-Object { Test-Path $_ } | ForEach-Object { . $_ } Where-Object { Test-Path $_ } | ForEach-Object { . $_ }
@ -65,18 +91,14 @@ $oldAliases = Get-Alias -Scope $MyAliasScope |
if ( $oldAliases -and $IsVerbose ) { if ( $oldAliases -and $IsVerbose ) {
Write-Verbose "Removing: $($oldAliases.Name -join ', ')" Write-Verbose "Removing: $($oldAliases.Name -join ', ')"
} }
if( Get-Command Remove-Alias -ErrorAction SilentlyContinue ) { $oldAliases | ForEach-Object { . RemoveAlias $($_.Name) }
$oldAliases | Remove-Alias -Scope $MyAliasScope
} else {
$oldAliases | ForEach-Object { Remove-Item "Alias:$($_.Name)" }
}
if ( $(. Get-ScopeDepth) -gt 0 ) { if ( $(. Get-ScopeDepth) -gt 0 ) {
Write-Host -ForegroundColor Red "Try sourcing Reload-MyScripts instead of just running it" Write-Host -ForegroundColor Red "Try sourcing Reload-MyScripts instead of just running it"
} }
foreach ( $local:PackagePath in $myPackages ) { foreach ( $local:PackagePath in $myPackages ) {
$local:PackageName = $PackagePath.Name #$local:PackageName = $PackagePath.Name
Join-Path $PackagePath 'profile.d' | Where-Object { Test-Path $_ } | Join-Path $PackagePath 'profile.d' | Where-Object { Test-Path $_ } |
Get-ChildItem -Filter '*.ps1' | Get-ChildItem -Filter '*.ps1' |
@ -98,14 +120,15 @@ foreach( $local:PackagePath in $myPackages ) {
Get-ChildItem (Join-Path $_ '*.ps1') | Where-Object Name -notmatch '\.inc\.ps1$' Get-ChildItem (Join-Path $_ '*.ps1') | Where-Object Name -notmatch '\.inc\.ps1$'
} }
$CommandsToAlias | ForEach-Object { foreach ( $local:newAlias in $CommandsToAlias ) {
Write-Verbose "Creating alias for $(getScriptName $_.FullName) Script..." Write-Verbose "Creating alias for $(getScriptName $newAlias.FullName) Script..."
Set-Alias $($_.BaseName) $_.FullName -Scope $MyAliasScope . SafeSetAlias $($newAlias.BaseName) $($newAlias.FullName)
} }
} }
foreach ( $local:alias in $myAliases.Keys ) { foreach ( $local:alias in $myAliases.Keys ) {
Write-Verbose "Adding $($alias) alias..." 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