System specific update

Reload-Scripts now loads Platform/OS specific scripts depending on
current state.
This required the SystemName class

sranger is an example of running ranger as sudo, but only in Linux.

Moved Get-MyAliases into Reload-MyScripts, and changed Reload-MyScripts
to load Aliases dynamically via the Aliases directory.
This commit is contained in:
lksz 2020-10-02 12:45:04 -04:00
parent 45b53dc110
commit 2d2a7bba5d
8 changed files with 150 additions and 43 deletions

View File

@ -9,6 +9,16 @@
[FunctionName]::_GetValidValues($wordToComplete,$true)
})]
[string[]]$ImportFunction,
[ArgumentCompleter({ param (
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
[SystemName]::_GetValidValues($wordToComplete,$false,$true)
})]
[string]$System,
[switch]$ForceImport,
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
[ArgumentCompleter({ param (
@ -18,16 +28,24 @@
$commandAst,
$fakeBoundParameters
)
[MyScript]::_GetValidValues($wordToComplete,$true)
$local:possibleValues = [MyScript]::_GetValidValues($wordToComplete,$true)
if( $fakeBoundParameters.ContainsKey("System") ) {
$possibleValues = $possibleValues | Where-Object { $_ -match "^sys\.$($fakeBoundParameters.System)" } | ForEach-Object { $_ -replace "^sys\.$($fakeBoundParameters.System).",'' }
}
$possibleValues
})]
[string[]]$ScriptName
)
$local:EditRootPath=$MyPSScriptRoot
if( $System ) {
$EditRootPath = Join-Path $EditRootPath "sys.$System"
}
$local:ScriptPaths = @()
foreach( $local:p in $ImportFunction ) {
$local:f = Get-Command $p -Type Function
if( $f ) {
$local:sp = Join-Path $MyPSScriptRoot "$p`.ps1"
$local:sp = Join-Path $EditRootPath "$p`.ps1"
if( $ForceImport -or -not (Test-Path $sp) ) {
Export-FunctionSource $p -NoHeader > $sp
} elseif ( -not $ForceImport ) {
@ -37,12 +55,19 @@ foreach( $local:p in $ImportFunction ) {
}
}
foreach( $local:p in $ScriptName ) {
$local:sp = Join-Path $MyPSScriptRoot "$p`.ps1"
$local:sp = Join-Path $EditRootPath "$p`.ps1"
$ScriptPaths += $sp
}
if( -not $ScriptPaths ) {
$ScriptPaths += $MyPSScriptRoot
$ScriptPaths += $EditRootPath
}
foreach( $local:p in $ScriptPaths ) {
$local:parentPath = Split-Path -Parent $p;
if( -not (Test-Path $p) -and -not (Test-Path $parentPath) ) {
New-Item -Directory $parentPath
}
}
Edit-TextFile $ScriptPaths

View File

@ -1,26 +0,0 @@
[CmdletBinding(SupportsShouldProcess)]param()
$local:allAliases = @()
$MyAliasScope = 0
$local:_scope = 0
$local:_done = $false
do {
try {
$local:newAliases += Get-Alias -Scope $_scope |
Where-Object {
($_.Definition -match "^$MyPSScriptRoot") -or ($_.Description -match '#MyAlias')
}
if( $newAliases ) {
$allAliases += $newAliases
$MyAliasScope = $_scope;
Write-Verbose "`$MyAliasScope is now set to $MyAliasScope"
}
} catch {
Write-Verbose "catch: $($_.Exception.Message)"
$_done = $_.Exception.Message -match 'The scope .* exceeds'
}
$_scope += 1
} until ( $_done )
$allAliases

View File

@ -1,21 +1,55 @@
[CmdletBinding(SupportsShouldProcess)]param()
function Get-MyAliases {
[CmdletBinding(SupportsShouldProcess)]param([switch]$ScriptsOnly)
$local:allAliases = @()
$MyAliasScope = 0
$local:_scope = 0
$local:_done = $false
do {
try {
$local:newAliases += Get-Alias -Scope $_scope |
Where-Object {
($_.Definition -match "^$MyPSScriptRoot") -or (-not $ScriptsOnly -and ($_.Description -match '#MyAlias'))
}
if( $newAliases ) {
$allAliases += $newAliases
$MyAliasScope = $_scope;
Write-Verbose "`$MyAliasScope is now set to $MyAliasScope"
}
} catch {
Write-Verbose "catch: $($_.Exception.Message)"
$_done = $_.Exception.Message -match 'The scope .* exceeds'
}
$_scope += 1
} until ( $_done )
$allAliases
}
$local:myAliases = [ordered]@{}
$myAliases.sudo = 'Invoke-Sudo'
$myAliases.vi = 'Edit-TextFile'
$myAliases.vim = 'Edit-TextFile'
$myAliases.nvim = 'Edit-TextFile'
$myAliases.nvim = 'Edit-TextFile'
$myAliases.l = 'ls.ps1'
$myAliases.ll = 'ls.ps1'
if( Test-Path $(Join-Path $MyPSScriptRoot Aliases) ) {
Get-ChildItem Join-Path (Join-Path $MyPSScriptRoot Aliases) | ForEach-Object {
$myAliases[$_.BaseName] = Get-Content $_
}
}
# $myAliases.sudo = 'Invoke-Sudo'
# $myAliases.vi = 'Edit-TextFile'
# $myAliases.vim = 'Edit-TextFile'
# $myAliases.nvim = 'Edit-TextFile'
# $myAliases.nvim = 'Edit-TextFile'
# $myAliases.l = 'ls.ps1'
# $myAliases.ll = 'ls.ps1'
#######################################################################
$local:IsVerbose = [bool]($PSBoundParameters['Verbose'])
$local:MyAliasScope = 1
$script:MyAliasScope = 0
$local:oldAliases = . Get-MyAliases
$local:oldAliases = Get-MyAliases
$oldAliases = Get-Alias -Scope $MyAliasScope |
Where-Object Name -in $($oldAliases.Name + $myAliases.Keys)
@ -29,8 +63,21 @@ if( Get-Command Remove-Alias -ErrorAction SilentlyContinue ) {
$oldAliases | ForEach-Object { Remove-Item "Alias:$($_.Name)" }
}
Get-ChildItem (Join-Path $MyPSScriptRoot '*.ps1') |
ForEach-Object {
if( $(. Get-ScopeDepth) -gt 0 ) { Write-Host -ForegroundColor Red "Try sourcing Reload-MyScripts instead of just running it" }
Get-ChildItem $(Join-Path $MyPSScriptRoot profile.d) -Filter '*.ps1' | ForEach-Object { ". '$($_.FullName)';" } | Invoke-Expression
$local:CommandsToAlias = (
@( $MyPSScriptRoot ) + $(
[SystemName]::_GetValidValues("",$true,$true) | ForEach-Object {
Join-Path $MyPSScriptRoot $_
}
)) | ForEach-Object {
if( Test-Path $_ ) {
Get-ChildItem (Join-Path $_ '*.ps1') | Where-Object Name -notmatch '\.inc\.ps1$'
}
}
$CommandsToAlias | ForEach-Object {
Set-Alias $($_.BaseName) $_.FullName -Scope $MyAliasScope
}
@ -38,5 +85,3 @@ foreach( $local:alias in $myAliases.Keys ) {
Set-Alias -Name $alias -Value $myAliases[$alias] -Description '#MyAlias' -Scope $MyAliasScope
}
if( $(. Get-ScopeDepth) -gt 0 ) { Write-Host -ForegroundColor Red "Try sourcing Reload-MyScripts instead of just running it" }
Get-ChildItem $(Join-Path $MyPSScriptRoot profile.d) -Filter '*.ps1' | ForEach-Object { ". '$($_.FullName)';" } | Invoke-Expression

View File

@ -0,0 +1,18 @@
class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator {
static [hashtable]$configDirectory = [ordered]@{
'vi' = '~/.virc'
'vim' = '~/.vimrc','~/.vim/vimrc'
'neovim' = '~/.config/nvim/init.vim','~/.config/nvim/vim-plug/plugins.vim','#vim','#vi'
}
static [string[]] GetConfigPaths([string[]]$ConfigNames) {
return $null
}
static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) {
$local:possibleValues = [MyConfig]::configDirectory.Keys
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict );
}
[String[]] GetValidValues() {
return [MyConfig]::_GetValidValues('',$true)
}
}

2
profile.d/Style.ps1 Normal file
View File

@ -0,0 +1,2 @@
Get-Command Set-PSReadLineKeyHandler | ForEach-Object { Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete }
Get-Command Set-Theme -ErrorAction SilentlyContinue | ForEach-Object { Set-Theme Paradox }

View File

@ -0,0 +1,37 @@
class SystemName { #: System.Management.Automation.IValidateSetValuesGenerator {
static [string[]] _GetValidValues([string]$wordToComplete,[bool]$CurrentOnly,[bool]$Strict) {
$MyPSScriptRoot = "$global:MyPSScriptRoot";
$local:possibleValues = [ordered]@{}
$possibleValues.out = @()
$possibleValues.Current = @(
$($global:PSVersionTable.OS -split ' '|Select-Object -First 1).Replace('Microsoft','Windows'),
$global:PSVersionTable.Platform,
$global:PSVersionTable.PSEdition
);
$possibleValues.out = $possibleValues.Current
if( -not $CurrentOnly ) {
$possibleValues.Platform = @( 'Windows','Linux','Unix' );
$possibleValues.Edition = @( 'Desktop', 'Core' );
$possibleValues.Exist = Get-ChildItem (Join-Path $MyPSScriptRoot "sys.*") | ForEach-Object { $_.Name -replace 'sys\.','' };
$possibleValues.out += $possibleValues.Platform + $possibleValues.Edition + $possibleValues.Exist
}
$possibleValues.out = $possibleValues.out |
Select-Object -Unique |
Sort-Object {
$local:sortName = "zz_$_";
if( $_ -in $possibleValues.Exist -and $_ -in $possibleValues.Current ) { $sortName[0] = "a" }
elseif( $_ -in $possibleValues.Exist ) { $sortName[0] = "b" }
elseif( $_ -in $possibleValues.Current ) { $sortName[0] = "c" }
if( $_ -in $possibleValues.Platform ) { $sortName[1] = "a" }
if( $_ -in $possibleValues.Edition ) { $sortName[1] = "b" }
$sortName;
}
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues.out -Strict:$Strict );
}
[String[]] GetValidValues() {
return [SystemName]::_GetValidValues('',$false,$true)
}
}

View File

@ -0,0 +1,4 @@
Get-Command Import-UnixCompleters -ErrorAction SilentlyContinue | ForEach-Object {
Import-UnixCompleters
Set-UnixCompleter -ShellType Zsh
}

2
sys.Linux/sranger.ps1 Normal file
View File

@ -0,0 +1,2 @@
Invoke-ExpressionEx -sudo "`$env:TERM='tmux-256color'; `$env:EDITOR='$env:EDITOR'; ranger"