diff --git a/Edit-MyScript.ps1 b/Edit-MyScript.ps1 index 1cc37a9..ddfbdbe 100644 --- a/Edit-MyScript.ps1 +++ b/Edit-MyScript.ps1 @@ -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 diff --git a/Get-MyAliases.ps1 b/Get-MyAliases.ps1 deleted file mode 100644 index ddcbcef..0000000 --- a/Get-MyAliases.ps1 +++ /dev/null @@ -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 diff --git a/Reload-MyScripts.ps1 b/Reload-MyScripts.ps1 index fd0aca3..0f2e337 100644 --- a/Reload-MyScripts.ps1 +++ b/Reload-MyScripts.ps1 @@ -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 diff --git a/profile.d/MyConfig.class.ps1 b/profile.d/MyConfig.class.ps1 new file mode 100644 index 0000000..19f3d52 --- /dev/null +++ b/profile.d/MyConfig.class.ps1 @@ -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) + } +} diff --git a/profile.d/Style.ps1 b/profile.d/Style.ps1 new file mode 100644 index 0000000..7b7f354 --- /dev/null +++ b/profile.d/Style.ps1 @@ -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 } diff --git a/profile.d/SystemName.class.ps1 b/profile.d/SystemName.class.ps1 new file mode 100644 index 0000000..022340f --- /dev/null +++ b/profile.d/SystemName.class.ps1 @@ -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) + } +} + diff --git a/profile.d/UnixCompleters.ps1 b/profile.d/UnixCompleters.ps1 new file mode 100644 index 0000000..aeb3a6d --- /dev/null +++ b/profile.d/UnixCompleters.ps1 @@ -0,0 +1,4 @@ +Get-Command Import-UnixCompleters -ErrorAction SilentlyContinue | ForEach-Object { + Import-UnixCompleters + Set-UnixCompleter -ShellType Zsh +} diff --git a/sys.Linux/sranger.ps1 b/sys.Linux/sranger.ps1 new file mode 100644 index 0000000..fa3e734 --- /dev/null +++ b/sys.Linux/sranger.ps1 @@ -0,0 +1,2 @@ + +Invoke-ExpressionEx -sudo "`$env:TERM='tmux-256color'; `$env:EDITOR='$env:EDITOR'; ranger"