Reload-MyAliases is now Reload-MyScripts
🔖 https://blog.lksz.me/editing-with-ease/ ✏️ Fixed $MyPSScriptRoot typo ✨ Get-ScopeDepth 🐛 Get-MyAliases didn't work correctly when sub-scoped, fixed the issue 🚚 Reload-MyAliases is now Reload-MyScripts 🐛 Reload-MyScripts loads scripts from $MyPSScriptRoot/profile.d subdir 📝 README updated to reflect change of script name and typo
This commit is contained in:
parent
bb2488e591
commit
06052cfa29
|
@ -0,0 +1 @@
|
|||
profile.d/env.ps1
|
|
@ -0,0 +1,6 @@
|
|||
[CmdletBinding(SupportsShouldProcess)]param(
|
||||
[switch]$sudo,
|
||||
[switch]$Force
|
||||
)
|
||||
|
||||
Edit-TextFile -sudo:$sudo $( Get-Profiles | Where-Object { $_.Exists -or $Force } | Select-Object -ExpandProperty Path )
|
|
@ -0,0 +1,47 @@
|
|||
[CmdletBinding(SupportsShouldProcess)]param(
|
||||
[ArgumentCompleter({ param (
|
||||
$commandName,
|
||||
$parameterName,
|
||||
$wordToComplete,
|
||||
$commandAst,
|
||||
$fakeBoundParameters
|
||||
)
|
||||
[FunctionName]::_GetValidValues($wordToComplete,$true)
|
||||
})]
|
||||
[string[]]$ImportFunction,
|
||||
[switch]$ForceImport,
|
||||
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
|
||||
[ArgumentCompleter({ param (
|
||||
$commandName,
|
||||
$parameterName,
|
||||
$wordToComplete,
|
||||
$commandAst,
|
||||
$fakeBoundParameters
|
||||
)
|
||||
[MyScript]::_GetValidValues($wordToComplete,$false)
|
||||
})]
|
||||
[string[]]$ScriptName
|
||||
)
|
||||
|
||||
$local:ScriptPaths = @()
|
||||
foreach( $local:p in $ImportFunction ) {
|
||||
$local:f = Get-Command $p -Type Function
|
||||
if( $f ) {
|
||||
$local:sp = Join-Path $MyPSScriptRoot "$p`.ps1"
|
||||
if( $ForceImport -or -not (Test-Path $sp) ) {
|
||||
Export-FunctionSource $p -NoHeader > $sp
|
||||
} elseif ( -not $ForceImport ) {
|
||||
throw "$p already exists in Script, to import the function again, use the -Force"
|
||||
}
|
||||
$ScriptPaths += $sp
|
||||
}
|
||||
}
|
||||
foreach( $local:p in $ScriptName ) {
|
||||
$local:sp = Join-Path $MyPSScriptRoot "$p`.ps1"
|
||||
$ScriptPaths += $sp
|
||||
}
|
||||
|
||||
Edit-TextFile $ScriptPaths
|
||||
|
||||
Get-Command Reload-MyScripts -ErrorAction SilentlyContinue |
|
||||
ForEach-Object { . $_.Definition }
|
|
@ -0,0 +1,25 @@
|
|||
[CmdletBinding(SupportsShouldProcess)]param(
|
||||
[switch]$sudo,
|
||||
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
|
||||
[string[]]$Path
|
||||
)
|
||||
|
||||
$local:editor = '/bin/nvim'
|
||||
if( (Test-Path env:EDITOR) -and (Test-Path $env:EDITOR) ) {
|
||||
$editor = $env:EDITOR;
|
||||
}
|
||||
|
||||
if( $editor -match 'vim?$' ) {
|
||||
$editor += ' -p'
|
||||
}
|
||||
|
||||
if( $sudo ) {
|
||||
$editor = "sudo $editor"
|
||||
}
|
||||
|
||||
$local:arguments = $Path -join "' '"
|
||||
if( $Path ) { $arguments = "'$arguments'" }
|
||||
|
||||
if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) {
|
||||
Invoke-Expression "$editor $arguments"
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
param(
|
||||
[switch]$NoHeader,
|
||||
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
|
||||
#[ValidateSet([Functions])]
|
||||
[string[]]$FunctionName
|
||||
|
||||
)
|
||||
|
||||
$local:src = ""
|
||||
foreach( $local:func in $FunctionName ) {
|
||||
if( -not $NoHeader ) { $src += "`nfunction $func {" }
|
||||
$src += "`n $((Get-Command -Type Function $func).Definition)"
|
||||
if( -not $NoHeader ) { $src += "`n}" }
|
||||
$src += "`n"
|
||||
}
|
||||
return $src.Trim()
|
|
@ -9,17 +9,18 @@ do {
|
|||
try {
|
||||
$local:newAliases += Get-Alias -Scope $_scope |
|
||||
Where-Object {
|
||||
($_.Definition -match "^$MyPSScrtipRoot") -or ($_.Description -match '#MyAlias')
|
||||
($_.Definition -match "^$MyPSScriptRoot") -or ($_.Description -match '#MyAlias')
|
||||
}
|
||||
if( $newAliases ) {
|
||||
$allAliases += $newAliases
|
||||
$MyAliasScope = $_scope;
|
||||
Write-Verbose "`$MyAliasScope is now set to $MyAliasScope"
|
||||
}
|
||||
$_scope += 1
|
||||
} catch {
|
||||
$done = $_.Exception.Message -match 'The scope .* exceeds'
|
||||
Write-Verbose "catch: $($_.Exception.Message)"
|
||||
$_done = $_.Exception.Message -match 'The scope .* exceeds'
|
||||
}
|
||||
} until ( $done )
|
||||
$_scope += 1
|
||||
} until ( $_done )
|
||||
|
||||
$allAliases
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
[CmdletBinding()]param(
|
||||
[string]$Filter,
|
||||
[switch]$NamesOnly
|
||||
)
|
||||
$local:rVal = [MyScript]::_GetValidValues($Filter,$true)
|
||||
if( $NamesOnly ) { return $rVal }
|
||||
return $rVal | ForEach-Object { Join-Path $MyPSScriptRoot "$_`.ps1" }
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
[CmdletBinding()]param(
|
||||
[string]$WordToComplete,
|
||||
[string[]]$FullValueSet,
|
||||
[switch]$Strict
|
||||
)
|
||||
$local:possibleValues = $fullValueSet
|
||||
if( $wordToComplete ) {
|
||||
$possibleValues = $possibleValues | Where-Object { $_ -match $wordToComplete }
|
||||
if( -not $strict -and ($wordToComplete -notin $possibleValues) ) {
|
||||
$possibleValues = $( $wordToComplete; $possibleValues )
|
||||
}
|
||||
}
|
||||
return $possibleValues
|
|
@ -0,0 +1,13 @@
|
|||
[CmdletBinding()]param(
|
||||
[switch]$AsDictionary
|
||||
)
|
||||
|
||||
filter ConvertTo-Hash { param([string]$Property="Name") begin { $hash = @{} } process{ $hash[$_."$Property"] = $_ } end {return $hash} }
|
||||
|
||||
$local:rVal = $PROFILE | fl * -Force | Out-String -Stream | Where-Object { $_ -match '( : /)|(:\\)' } |
|
||||
Select-Object @{
|
||||
N="Profile";E={($_ -split ": ")[0].Trim()}
|
||||
},@{N="Path";E={($_ -split ": ")[-1].Trim()}
|
||||
} | Select-Object *,@{N="Exists";E={Test-Path $_.Path}}
|
||||
if( $AsDictionary ) { return $rVal | ConvertTo-Hash -Property "Profile" }
|
||||
return $rVal
|
|
@ -0,0 +1,2 @@
|
|||
$local:_scope = 0;
|
||||
while ($true) { try { Get-Variable -Scope $_scope | Out-Null; $_scope += 1 } catch { return ($_scope - 1) } }
|
12
README.md
12
README.md
|
@ -12,7 +12,7 @@ The scripts all [assume](https://blog.lksz.me/functions-or-scripts/#myprofilecod
|
|||
The code establishes the following variables:
|
||||
* `$PathEnvDelimiter` - Linux/Unix it's `:`, while in Windows it's `;`
|
||||
* `$MyPSModulePath` - Location to place personal Modules
|
||||
* `$MyPSScrtipRoot` - Location of personal Scripts.
|
||||
* `$MyPSScriptRoot` - Location of personal Scripts.
|
||||
|
||||
The code also makes sure `$MyPSScriptRoot` is in the `$env:PATH` environment vairable, and finally it calls [`Reload-MyAliases`](https://blog.lksz.me/a-case-of-sensitivity/#myaliases) if it exists.
|
||||
|
||||
|
@ -34,13 +34,13 @@ if( -not $MyPSModulePath ) {
|
|||
}
|
||||
|
||||
$local:p = Split-PathEnv $env:PATH
|
||||
$global:MyPSScrtipRoot = Join-Path (Split-Path -Parent $MyPSModulePath) Scripts
|
||||
if( -not (Test-Path $MyPSScrtipRoot) ) {
|
||||
New-Item -ItemType Directory -Path $MyPSScrtipRoot -Force | Out-Null
|
||||
$global:MyPSScriptRoot = Join-Path (Split-Path -Parent $MyPSModulePath) Scripts
|
||||
if( -not (Test-Path $MyPSScriptRoot) ) {
|
||||
New-Item -ItemType Directory -Path $MyPSScriptRoot -Force | Out-Null
|
||||
}
|
||||
|
||||
$p = @($p[0], $MyPSScrtipRoot) + $($p | Select-Object -Skip 1)
|
||||
$p = @($p[0], $MyPSScriptRoot) + $($p | Select-Object -Skip 1)
|
||||
$env:PATH = $p -join $PathEnvDelimiter
|
||||
|
||||
Get-Command Reload-MyAliases -ErrorAction SilentlyContinue | ForEach-Object { Reload-MyAliases }
|
||||
Get-Command Reload-MyScripts -ErrorAction SilentlyContinue | ForEach-Object { . $_.Name }
|
||||
```
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
[CmdletBinding(SupportsShouldProcess)]param()
|
||||
|
||||
$local:myAliases = [ordered]@{}
|
||||
$myAliases.sudo = 'Invoke-MySudo'
|
||||
$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'
|
||||
|
||||
#######################################################################
|
||||
|
||||
|
@ -13,7 +15,7 @@ $local:IsVerbose = [bool]($PSBoundParameters['Verbose'])
|
|||
|
||||
$local:MyAliasScope = 1
|
||||
|
||||
$local:oldAliases = Get-MyAliases
|
||||
$local:oldAliases = . Get-MyAliases
|
||||
|
||||
$oldAliases = Get-Alias -Scope $MyAliasScope |
|
||||
Where-Object Name -in $($oldAliases.Name + $myAliases.Keys)
|
||||
|
@ -22,7 +24,7 @@ if( $oldAliases -and $IsVerbose ) {
|
|||
Write-Verbose "Removing: $($oldAliases.Name -join ', ')"
|
||||
}
|
||||
$oldAliases | Remove-Alias -Scope $MyAliasScope
|
||||
Get-ChildItem (Join-Path $MyPSScrtipRoot '*.ps1') |
|
||||
Get-ChildItem (Join-Path $MyPSScriptRoot '*.ps1') |
|
||||
ForEach-Object {
|
||||
Set-Alias $($_.BaseName) $_.FullName -Scope $MyAliasScope
|
||||
}
|
||||
|
@ -30,3 +32,6 @@ Get-ChildItem (Join-Path $MyPSScrtipRoot '*.ps1') |
|
|||
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
|
|
@ -0,0 +1,21 @@
|
|||
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 );
|
||||
}
|
||||
static [String[]] _GetValidValues([string]$wordToComplete,[switch]$Strict) {
|
||||
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $(Get-Command -Type Function | Select-Object -ExpandProperty Name) -Strict:$Strict)
|
||||
# $local:possibleValues =
|
||||
# if( $wordToComplete ) {
|
||||
# $possibleValues = $possibleValues | Where-Object { $_ -match $wordToComplete }
|
||||
# if( -not $strict -and ($wordToComplete -notin $possibleValues) ) {
|
||||
# $possibleValues = $( $wordToComplete; $possibleValues )
|
||||
# }
|
||||
# }
|
||||
# return $possibleValues
|
||||
}
|
||||
|
||||
[String[]] GetValidValues() {
|
||||
return [FunctionName]::_GetValidValues('',$true)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
class MyScript : System.Management.Automation.IValidateSetValuesGenerator {
|
||||
static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) {
|
||||
$local:possibleValues = $(
|
||||
Get-ChildItem $global:MyPSScriptRoot -Filter '*.ps1' -Recurse |
|
||||
Select-Object -ExpandProperty FullName | ForEach-Object {
|
||||
$_ -replace '\.ps1$','' -replace "$($MyPSScriptRoot -replace '\\',"\\")[/\\]",''
|
||||
}
|
||||
)
|
||||
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict );
|
||||
}
|
||||
|
||||
[String[]] GetValidValues() {
|
||||
return [MyScript]::_GetValidValues('',$true)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue