Introducing Packages
Major Overhaul with Breaking Changes split into packaged, default behavior moved into 'base' package each package has a json package description file with criteria for loading Modified Setup-Profile to refer to 'base' package path for auto loading moved Linux aliases and command to 'base.linux' package created 'docker' package to address docker supported systems modified Get-MyScripts, Edit-MyScripts and Reload-MyScripts accordingly. Dropped -System and sys.*, package json conditions will take care of it. Supplied command to create/edit package json files: - New-MyPackage - Add-PackageCondition - Set-MyPackage
This commit is contained in:
parent
80a488484b
commit
42b39f4e25
83 changed files with 505 additions and 130 deletions
111
base/Reload-MyScripts.ps1
Normal file
111
base/Reload-MyScripts.ps1
Normal file
|
@ -0,0 +1,111 @@
|
|||
[CmdletBinding(SupportsShouldProcess)]param()
|
||||
|
||||
try { Get-Alias Get-MyAliases -ErrorAction Stop | ForEach-Object { Remove-Item "Alias:$($_.Name)" } } catch {}
|
||||
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
|
||||
}
|
||||
|
||||
function getScriptName{param([string]$FullPath)
|
||||
$FullPath -replace '\.ps1$','' -replace "^$([regex]::Escape($MyPSScriptRoot)).",''
|
||||
}
|
||||
|
||||
$local:IsVerbose = [bool]($PSBoundParameters['Verbose'])
|
||||
|
||||
# Loads mandatory Package code
|
||||
try {
|
||||
$null = [Packagesz]
|
||||
} catch {
|
||||
$local:PackagePath = $(Join-Path $MyPSScriptRoot 'base')
|
||||
Join-Path $(Join-Path $PackagePath profile.d) 'classes.ps1' |
|
||||
Where-Object { Test-Path $_ } | ForEach-Object { . $_ }
|
||||
}
|
||||
$local:myPackages = Get-MyPackages -ReturnFullPath -IncludeRoot | Get-Item
|
||||
|
||||
$local:myAliases = [ordered]@{}
|
||||
|
||||
$myPackages |
|
||||
ForEach-Object { Join-Path $_.FullName Aliases } |
|
||||
Where-Object { Test-Path $_ } |
|
||||
Get-ChildItem | ForEach-Object {
|
||||
$myAliases[$_.BaseName] = Get-Content $_.FullName
|
||||
}
|
||||
|
||||
$script:MyAliasScope = 0
|
||||
|
||||
$local:oldAliases = Get-MyAliases
|
||||
|
||||
$oldAliases = Get-Alias -Scope $MyAliasScope |
|
||||
Where-Object Name -in $($oldAliases.Name + $myAliases.Keys)
|
||||
|
||||
if( $oldAliases -and $IsVerbose ) {
|
||||
Write-Verbose "Removing: $($oldAliases.Name -join ', ')"
|
||||
}
|
||||
if( Get-Command Remove-Alias -ErrorAction SilentlyContinue ) {
|
||||
$oldAliases | Remove-Alias -Scope $MyAliasScope
|
||||
} else {
|
||||
$oldAliases | ForEach-Object { Remove-Item "Alias:$($_.Name)" }
|
||||
}
|
||||
|
||||
if( $(. Get-ScopeDepth) -gt 0 ) {
|
||||
Write-Host -ForegroundColor Red "Try sourcing Reload-MyScripts instead of just running it"
|
||||
}
|
||||
|
||||
foreach( $local:PackagePath in $myPackages ) {
|
||||
$local:PackageName = $PackagePath.Name
|
||||
|
||||
Join-Path $PackagePath 'profile.d' | Where-Object { Test-Path $_ } |
|
||||
Get-ChildItem -Filter '*.ps1' |
|
||||
ForEach-Object {
|
||||
Write-Verbose "Loading $(getScriptName $_.FullName)...";
|
||||
". '$($_.FullName)';"
|
||||
} |
|
||||
Invoke-Expression
|
||||
|
||||
$local:CommandsToAlias = (
|
||||
@( $PackagePath ) + $(
|
||||
[SystemName]::_GetValidValues("",$true,$true) |
|
||||
ForEach-Object {
|
||||
Join-Path $PackagePath "sys.$_"
|
||||
}
|
||||
)) |
|
||||
Where-Object { Test-Path $_ } |
|
||||
ForEach-Object {
|
||||
Get-ChildItem (Join-Path $_ '*.ps1') | Where-Object Name -notmatch '\.inc\.ps1$'
|
||||
}
|
||||
|
||||
$CommandsToAlias | ForEach-Object {
|
||||
Write-Verbose "Creating alias for $(getScriptName $_.FullName) Script..."
|
||||
Set-Alias $($_.BaseName) $_.FullName -Scope $MyAliasScope
|
||||
}
|
||||
}
|
||||
|
||||
foreach( $local:alias in $myAliases.Keys ) {
|
||||
Write-Verbose "Adding $($alias) alias..."
|
||||
Set-Alias -Name $alias -Value $myAliases[$alias] -Description '#MyAlias' -Scope $MyAliasScope
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue