2021-10-12 01:37:18 +00:00
|
|
|
[CmdletBinding(SupportsShouldProcess)]param(
|
|
|
|
[switch]$Update
|
|
|
|
)
|
2020-11-25 05:46:33 +00:00
|
|
|
|
2021-04-07 20:04:46 +00:00
|
|
|
function RemoveAlias {
|
|
|
|
param([string]$Name)
|
|
|
|
$local:a = $Name | Where-Object { $_ } | Get-Alias -ErrorAction SilentlyContinue
|
|
|
|
if ( -not $a ) { return }
|
2021-04-27 16:04:12 +00:00
|
|
|
if ( Get-Command -ListImported Remove-Alias -ErrorAction SilentlyContinue ) {
|
2021-04-07 20:04:46 +00:00
|
|
|
$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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 01:37:18 +00:00
|
|
|
if( $Update -and (Get-Command git -ErrorAction Ignore) ) {
|
|
|
|
Push-Location $MyPSScriptRoot
|
|
|
|
git pull
|
|
|
|
if( Get-Command Write-VcsStatus -ErrorAction Ignore ) { Write-Host "`n$(Write-VcsStatus)`n" }
|
|
|
|
Pop-Location
|
|
|
|
}
|
|
|
|
|
2021-04-07 20:04:46 +00:00
|
|
|
try { Get-Alias Get-MyAliases -ErrorAction Stop | ForEach-Object { . RemoveAlias $($_.Name) } } catch {}
|
2020-11-25 05:46:33 +00:00
|
|
|
function Get-MyAliases {
|
2021-04-07 20:04:46 +00:00
|
|
|
[CmdletBinding()]param([switch]$ScriptsOnly)
|
2020-11-25 05:46:33 +00:00
|
|
|
|
|
|
|
$local:allAliases = @()
|
|
|
|
|
|
|
|
$MyAliasScope = 0
|
|
|
|
$local:_scope = 0
|
|
|
|
$local:_done = $false
|
|
|
|
do {
|
|
|
|
try {
|
|
|
|
$local:newAliases += Get-Alias -Scope $_scope |
|
|
|
|
Where-Object {
|
2021-04-07 20:04:46 +00:00
|
|
|
($_.Definition -like "$MyPSScriptRoot*") -or (-not $ScriptsOnly -and ($_.Description -match '#MyAlias'))
|
2020-11-25 05:46:33 +00:00
|
|
|
}
|
2021-04-07 20:04:46 +00:00
|
|
|
if ( $newAliases ) {
|
2020-11-25 05:46:33 +00:00
|
|
|
$allAliases += $newAliases
|
2021-04-07 20:04:46 +00:00
|
|
|
$MyAliasScope = $_scope;
|
2020-11-25 05:46:33 +00:00
|
|
|
Write-Verbose "`$MyAliasScope is now set to $MyAliasScope"
|
|
|
|
}
|
2021-04-07 20:04:46 +00:00
|
|
|
}
|
|
|
|
catch {
|
2020-11-25 05:46:33 +00:00
|
|
|
Write-Verbose "catch: $($_.Exception.Message)"
|
2021-04-07 20:04:46 +00:00
|
|
|
$_done = $_.Exception.Message -match 'The scope .* exceeds'
|
2020-11-25 05:46:33 +00:00
|
|
|
}
|
|
|
|
$_scope += 1
|
|
|
|
} until ( $_done )
|
|
|
|
|
|
|
|
$allAliases
|
|
|
|
}
|
|
|
|
|
2021-04-07 20:04:46 +00:00
|
|
|
function getScriptName {
|
|
|
|
param([string]$FullPath)
|
|
|
|
$FullPath -replace '\.ps1$', '' -replace "^$([regex]::Escape($MyPSScriptRoot)).", ''
|
2020-11-25 05:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$local:IsVerbose = [bool]($PSBoundParameters['Verbose'])
|
|
|
|
|
|
|
|
# Loads mandatory Package code
|
|
|
|
try {
|
|
|
|
$null = [Packagesz]
|
2021-04-07 20:04:46 +00:00
|
|
|
}
|
|
|
|
catch {
|
2020-11-25 05:46:33 +00:00
|
|
|
$local:PackagePath = $(Join-Path $MyPSScriptRoot 'base')
|
|
|
|
Join-Path $(Join-Path $PackagePath profile.d) 'classes.ps1' |
|
|
|
|
Where-Object { Test-Path $_ } | ForEach-Object { . $_ }
|
|
|
|
}
|
2021-10-29 18:33:25 +00:00
|
|
|
|
|
|
|
# This is a hack that for some reason reduces (at least on one machine)
|
|
|
|
# this next line from running over 27 seconds, to under 5 seconds.
|
|
|
|
Invoke-Expression "function __GetMyPackages{$(Get-Content $(Join-Path $(Join-Path $MyPSScriptRoot "base") "Get-MyPackages.ps1") | Out-String )}"
|
|
|
|
$local:myPackages = __GetMyPackages -ReturnFullPath -IncludeRoot | Get-Item
|
|
|
|
Remove-Item function:__GetMyPackages
|
2020-11-25 05:46:33 +00:00
|
|
|
|
|
|
|
$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)
|
|
|
|
|
2021-04-07 20:04:46 +00:00
|
|
|
if ( $oldAliases -and $IsVerbose ) {
|
2020-11-25 05:46:33 +00:00
|
|
|
Write-Verbose "Removing: $($oldAliases.Name -join ', ')"
|
|
|
|
}
|
2021-04-07 20:04:46 +00:00
|
|
|
$oldAliases | ForEach-Object { . RemoveAlias $($_.Name) }
|
2020-11-25 05:46:33 +00:00
|
|
|
|
2021-04-07 20:04:46 +00:00
|
|
|
if ( $(. Get-ScopeDepth) -gt 0 ) {
|
2020-11-25 05:46:33 +00:00
|
|
|
Write-Host -ForegroundColor Red "Try sourcing Reload-MyScripts instead of just running it"
|
|
|
|
}
|
|
|
|
|
2021-04-07 20:04:46 +00:00
|
|
|
foreach ( $local:PackagePath in $myPackages ) {
|
|
|
|
#$local:PackageName = $PackagePath.Name
|
2020-11-25 05:46:33 +00:00
|
|
|
|
|
|
|
Join-Path $PackagePath 'profile.d' | Where-Object { Test-Path $_ } |
|
|
|
|
Get-ChildItem -Filter '*.ps1' |
|
|
|
|
ForEach-Object {
|
|
|
|
Write-Verbose "Loading $(getScriptName $_.FullName)...";
|
|
|
|
". '$($_.FullName)';"
|
|
|
|
} |
|
|
|
|
Invoke-Expression
|
2021-05-27 15:48:00 +00:00
|
|
|
|
|
|
|
Join-Path $PackagePath 'profile.d' | Where-Object { Test-Path $_ } |
|
|
|
|
Get-ChildItem -Filter '*.format.ps1xml' |
|
|
|
|
ForEach-Object {
|
|
|
|
Write-Verbose "Loading $(getScriptName $_.FullName)...";
|
|
|
|
"Update-FormatData '$($_.FullName)';"
|
|
|
|
} |
|
|
|
|
Invoke-Expression
|
|
|
|
|
2020-11-25 05:46:33 +00:00
|
|
|
$local:CommandsToAlias = (
|
|
|
|
@( $PackagePath ) + $(
|
2021-04-07 20:04:46 +00:00
|
|
|
[SystemName]::_GetValidValues("", $true, $true) |
|
2020-11-25 05:46:33 +00:00
|
|
|
ForEach-Object {
|
|
|
|
Join-Path $PackagePath "sys.$_"
|
|
|
|
}
|
2021-04-07 20:04:46 +00:00
|
|
|
)) |
|
|
|
|
Where-Object { Test-Path $_ } |
|
|
|
|
ForEach-Object {
|
|
|
|
Get-ChildItem (Join-Path $_ '*.ps1') | Where-Object Name -notmatch '\.inc\.ps1$'
|
|
|
|
}
|
2020-11-25 05:46:33 +00:00
|
|
|
|
2021-04-07 20:04:46 +00:00
|
|
|
foreach ( $local:newAlias in $CommandsToAlias ) {
|
|
|
|
Write-Verbose "Creating alias for $(getScriptName $newAlias.FullName) Script..."
|
|
|
|
. SafeSetAlias $($newAlias.BaseName) $($newAlias.FullName)
|
|
|
|
}
|
2020-11-25 05:46:33 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 20:04:46 +00:00
|
|
|
foreach ( $local:alias in $myAliases.Keys ) {
|
2020-11-25 05:46:33 +00:00
|
|
|
Write-Verbose "Adding $($alias) alias..."
|
2021-04-07 20:04:46 +00:00
|
|
|
. SafeSetAlias $alias $myAliases[$alias] '#MyAlias'
|
2020-11-25 05:46:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 15:48:00 +00:00
|
|
|
Remove-Item Function:\RemoveAlias, Function:\SafeSetAlias -ErrorAction SilentlyContinue
|