diff --git a/Get-MyScript.ps1 b/Get-MyScript.ps1 deleted file mode 100644 index f438b76..0000000 --- a/Get-MyScript.ps1 +++ /dev/null @@ -1,8 +0,0 @@ -[CmdletBinding()]param( - [string]$Filter, - [switch]$NamesOnly -) -$local:rVal = [MyScript]::_GetValidValues($Filter,$true) -if( $NamesOnly ) { return $rVal } -return $rVal | ForEach-Object { Join-Path $MyPSScriptRoot "$_`.ps1" } - diff --git a/Reload-MyScripts.ps1 b/Reload-MyScripts.ps1 deleted file mode 100644 index c421da8..0000000 --- a/Reload-MyScripts.ps1 +++ /dev/null @@ -1,88 +0,0 @@ -[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:myAliases = [ordered]@{} -if( Test-Path $(Join-Path $MyPSScriptRoot Aliases) ) { - Get-ChildItem $(Join-Path $MyPSScriptRoot Aliases) | ForEach-Object { - $myAliases[$_.BaseName] = Get-Content $_.FullName - } -} - -$local:IsVerbose = [bool]($PSBoundParameters['Verbose']) - -$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" } -Get-ChildItem $(Join-Path $MyPSScriptRoot profile.d) -Filter '*.ps1' | ForEach-Object { - Write-Verbose "Loading $(getScriptName $_.FullName)..."; - ". '$($_.FullName)';" - } | Invoke-Expression - -$local:CommandsToAlias = ( - @( $MyPSScriptRoot ) + $( - [SystemName]::_GetValidValues("",$true,$true) | ForEach-Object { - Join-Path $MyPSScriptRoot "sys.$_" - } - )) | ForEach-Object { - if( Test-Path $_ ) { - 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 -} - diff --git a/sys.Linux/Get-PlexInfo.ps1 b/base.linux/Get-PlexInfo.ps1 similarity index 100% rename from sys.Linux/Get-PlexInfo.ps1 rename to base.linux/Get-PlexInfo.ps1 diff --git a/sys.Linux/Invoke-ViaAnsible.ps1 b/base.linux/Invoke-ViaAnsible.ps1 similarity index 100% rename from sys.Linux/Invoke-ViaAnsible.ps1 rename to base.linux/Invoke-ViaAnsible.ps1 diff --git a/sys.Linux/Pull-ViaAnsible.ps1 b/base.linux/Pull-ViaAnsible.ps1 similarity index 100% rename from sys.Linux/Pull-ViaAnsible.ps1 rename to base.linux/Pull-ViaAnsible.ps1 diff --git a/sys.Linux/Set-EnvVariable.ps1 b/base.linux/Set-EnvVariable.ps1 similarity index 100% rename from sys.Linux/Set-EnvVariable.ps1 rename to base.linux/Set-EnvVariable.ps1 diff --git a/sys.Linux/Update-ArchOSz.ps1 b/base.linux/Update-ArchOSz.ps1 similarity index 100% rename from sys.Linux/Update-ArchOSz.ps1 rename to base.linux/Update-ArchOSz.ps1 diff --git a/sys.Linux/Update-OSz.ps1 b/base.linux/Update-OSz.ps1 similarity index 100% rename from sys.Linux/Update-OSz.ps1 rename to base.linux/Update-OSz.ps1 diff --git a/sys.Linux/Update-UbuntuOSz.ps1 b/base.linux/Update-UbuntuOSz.ps1 similarity index 100% rename from sys.Linux/Update-UbuntuOSz.ps1 rename to base.linux/Update-UbuntuOSz.ps1 diff --git a/sys.Linux/Update-ViaAnsible.ps1 b/base.linux/Update-ViaAnsible.ps1 similarity index 100% rename from sys.Linux/Update-ViaAnsible.ps1 rename to base.linux/Update-ViaAnsible.ps1 diff --git a/base.linux/_.package.json b/base.linux/_.package.json new file mode 100644 index 0000000..816ed22 --- /dev/null +++ b/base.linux/_.package.json @@ -0,0 +1,16 @@ +{ + "package": { + "Condition": [ + { + "custom": null, + "System": [ + "Linux" + ], + "Hostname": null, + "Username": null, + "Logic": 0 + } + ], + "Name": "base.linux" + } +} diff --git a/sys.Linux/ls.ps1 b/base.linux/ls.ps1 similarity index 100% rename from sys.Linux/ls.ps1 rename to base.linux/ls.ps1 diff --git a/sys.Linux/sranger.ps1 b/base.linux/sranger.ps1 similarity index 100% rename from sys.Linux/sranger.ps1 rename to base.linux/sranger.ps1 diff --git a/sz-df.ps1 b/base.linux/sz-df.ps1 similarity index 100% rename from sz-df.ps1 rename to base.linux/sz-df.ps1 diff --git a/sz-du.ps1 b/base.linux/sz-du.ps1 similarity index 100% rename from sz-du.ps1 rename to base.linux/sz-du.ps1 diff --git a/sys.Linux/sz-rsync.ps1 b/base.linux/sz-rsync.ps1 similarity index 100% rename from sys.Linux/sz-rsync.ps1 rename to base.linux/sz-rsync.ps1 diff --git a/sys.Linux/txm.ps1 b/base.linux/txm.ps1 similarity index 100% rename from sys.Linux/txm.ps1 rename to base.linux/txm.ps1 diff --git a/sys.Windows/Set-EnvVariable.ps1 b/base.win/Set-EnvVariable.ps1 similarity index 100% rename from sys.Windows/Set-EnvVariable.ps1 rename to base.win/Set-EnvVariable.ps1 diff --git a/base.win/_.package.json b/base.win/_.package.json new file mode 100644 index 0000000..3a5346f --- /dev/null +++ b/base.win/_.package.json @@ -0,0 +1,16 @@ +{ + "package": { + "Name": "base.win", + "Condition": [ + { + "custom": null, + "System": [ + "Windows" + ], + "Hostname": null, + "Username": null, + "Logic": 0 + } + ] + } +} diff --git a/base/Add-PackageCondition.ps1 b/base/Add-PackageCondition.ps1 new file mode 100644 index 0000000..34a57b0 --- /dev/null +++ b/base/Add-PackageCondition.ps1 @@ -0,0 +1,21 @@ +[CmdletBinding(SupportsShouldProcess)]param( + [Parameter(mandatory=$true, ValueFromPipeline=$true)] + [szPackage]$InputObject, + [szLogic]$logic = "and", + [ScriptBlock]$CustomCondition, + [string[]]$System = $null, + [string[]]$Hostname = $null, + [string[]]$Username = $null +) + +process { + $local:newCond = New-Object -Type 'szCondition' + $newCond.custom = $CustomCondition + $newCond.System = $System + $newCond.Hostname = $Hostname + $newCond.Username = $null + $newCond.Logic = [szLogic]::and + $InputObject.Condition += $newCond + + $InputObject +} diff --git a/Aliases/dcedit.ps1 b/base/Aliases/dcedit.ps1 similarity index 100% rename from Aliases/dcedit.ps1 rename to base/Aliases/dcedit.ps1 diff --git a/Aliases/dco.ps1 b/base/Aliases/dco.ps1 similarity index 100% rename from Aliases/dco.ps1 rename to base/Aliases/dco.ps1 diff --git a/Aliases/dps.ps1 b/base/Aliases/dps.ps1 similarity index 100% rename from Aliases/dps.ps1 rename to base/Aliases/dps.ps1 diff --git a/Aliases/edit b/base/Aliases/edit similarity index 100% rename from Aliases/edit rename to base/Aliases/edit diff --git a/Aliases/l.ps1 b/base/Aliases/l.ps1 similarity index 100% rename from Aliases/l.ps1 rename to base/Aliases/l.ps1 diff --git a/Aliases/ll.ps1 b/base/Aliases/ll.ps1 similarity index 100% rename from Aliases/ll.ps1 rename to base/Aliases/ll.ps1 diff --git a/Aliases/sz-update.ps1 b/base/Aliases/sz-update.ps1 similarity index 100% rename from Aliases/sz-update.ps1 rename to base/Aliases/sz-update.ps1 diff --git a/Aliases/vi b/base/Aliases/vi similarity index 100% rename from Aliases/vi rename to base/Aliases/vi diff --git a/Aliases/vidc.ps1 b/base/Aliases/vidc.ps1 similarity index 100% rename from Aliases/vidc.ps1 rename to base/Aliases/vidc.ps1 diff --git a/Aliases/vim b/base/Aliases/vim similarity index 100% rename from Aliases/vim rename to base/Aliases/vim diff --git a/ConvertFrom-Base64.ps1 b/base/ConvertFrom-Base64.ps1 similarity index 100% rename from ConvertFrom-Base64.ps1 rename to base/ConvertFrom-Base64.ps1 diff --git a/Edit-MyConfig.ps1 b/base/Edit-MyConfig.ps1 similarity index 100% rename from Edit-MyConfig.ps1 rename to base/Edit-MyConfig.ps1 diff --git a/Edit-MyProfiles.ps1 b/base/Edit-MyProfiles.ps1 similarity index 100% rename from Edit-MyProfiles.ps1 rename to base/Edit-MyProfiles.ps1 diff --git a/Edit-MyScript.ps1 b/base/Edit-MyScript.ps1 similarity index 73% rename from Edit-MyScript.ps1 rename to base/Edit-MyScript.ps1 index 50cf3b0..8095b79 100644 --- a/Edit-MyScript.ps1 +++ b/base/Edit-MyScript.ps1 @@ -16,10 +16,13 @@ $commandAst, $fakeBoundParameters ) - [SystemName]::_GetValidValues($wordToComplete,$false,$true) + $local:NotAll = $true + if( $fakeBoundParameters.ContainsKey("All") ) { $NotAll = -not $fakeBoundParameters.All } + [Packagesz]::_GetValidValues($wordToComplete,$NotAll,$true) })] - [string]$System, + [string]$Package, [switch]$ForceImport, + [switch]$All, [Parameter(Position = 0, ValueFromRemainingArguments = $true)] [ArgumentCompleter({ param ( $commandName, @@ -28,18 +31,25 @@ $commandAst, $fakeBoundParameters ) - $local:possibleValues = [MyScript]::_GetValidValues($wordToComplete,$true) - if( $fakeBoundParameters.ContainsKey("System") ) { - $possibleValues = $possibleValues | Where-Object { $_ -match "^sys\.$($fakeBoundParameters.System)" } | ForEach-Object { $_ -replace "^sys\.$($fakeBoundParameters.System).",'' } + $local:NotAll = $true + $local:Pkg = [string]::Empty + if( $fakeBoundParameters.ContainsKey("All") ) { $NotAll = -not $fakeBoundParameters.All } + if( $fakeBoundParameters.ContainsKey("Package") ) { $Pkg = Join-Path $fakeBoundParameters.Package '' } + $local:possibleValues = [MyScript]::_GetValidValues($wordToComplete,$NotAll,$true) + if( $Pkg ) { + $possibleValues = $possibleValues | + Where-Object { $_ -match "^$Pkg" } | + ForEach-Object { $_.Replace($Pkg,'') } } + $possibleValues })] [string[]]$ScriptName ) $local:EditRootPath=$MyPSScriptRoot -if( $System ) { - $EditRootPath = Join-Path $EditRootPath "sys.$System" +if( $Package ) { + $EditRootPath = Join-Path $EditRootPath $Package } $local:ScriptPaths = @() foreach( $local:p in $ImportFunction ) { diff --git a/Edit-TextFile.ps1 b/base/Edit-TextFile.ps1 similarity index 100% rename from Edit-TextFile.ps1 rename to base/Edit-TextFile.ps1 diff --git a/base/Get-MyPackages.ps1 b/base/Get-MyPackages.ps1 new file mode 100644 index 0000000..0f638dc --- /dev/null +++ b/base/Get-MyPackages.ps1 @@ -0,0 +1,21 @@ +[CmdletBinding()]param( + [Parameter(ParameterSetName="OptionA")] + [Parameter(ParameterSetName="Path")] + [switch]$Force, + [Parameter(ParameterSetName="Path",Mandatory)] + [switch]$ReturnFullPath, + [Parameter(ParameterSetName="Path")] + [switch]$IncludeRoot +) + +$local:packages = [Packagesz]::_GetValidValues("", -not $Force, $true) | Sort-Object +if( $IncludeRoot -and $ReturnFullPath ) { $MyPSScriptRoot } +foreach ( $local:pkg in $packages ) { + if( $ReturnFullPath ) { + Join-Path $MyPSScriptRoot $pkg + } else { + $pkg + } +} + + diff --git a/base/Get-MyScript.ps1 b/base/Get-MyScript.ps1 new file mode 100644 index 0000000..e780a33 --- /dev/null +++ b/base/Get-MyScript.ps1 @@ -0,0 +1,44 @@ +[CmdletBinding()]param( + [Parameter(ParameterSetName="Normal")] + [Parameter(ParameterSetName="DropNamespace")] + [string]$Filter, + [Parameter(ParameterSetName="ByPackage",Mandatory)] + [Parameter(ParameterSetName="DropNamespace",Mandatory)] + [ArgumentCompleter({ param ( + $commandName, + $parameterName, + $wordToComplete, + $commandAst, + $fakeBoundParameters + ) + $local:NotAll = $true + if( $fakeBoundParameters.ContainsKey("Force") ) { $NotAll = -not $fakeBoundParameters.Force } + [Packagesz]::_GetValidValues($wordToComplete,$NotAll,$true) + })] + [string]$Package, + [Parameter(ParameterSetName="Normal")] + [Parameter(ParameterSetName="ByPackage")] + [Parameter(ParameterSetName="DropNamespace",Mandatory)] + [switch]$NamesOnly, + [Parameter(ParameterSetName="Normal")] + [Parameter(ParameterSetName="ByPackage")] + [Parameter(ParameterSetName="DropNamespace")] + [switch]$Force, + [Parameter(ParameterSetName="DropNamespace",Mandatory)] + [switch]$DropNamespace +) +$local:rVal = [MyScript]::_GetValidValues($Filter,-not $Force,$true) + +if( $Package ) { + $Package = Join-Path $Package '' + $rVal = $rVal | + Where-Object { $_ -match "^$Package" } + if( $DropNamespace ) { + $rVal = $rVal | + ForEach-Object { $_.Replace($Package,'') } + } +} + +if( $NamesOnly ) { return $rVal } +return $rVal | ForEach-Object { Join-Path $MyPSScriptRoot "$_`.ps1" } + diff --git a/Get-Path.ps1 b/base/Get-Path.ps1 similarity index 100% rename from Get-Path.ps1 rename to base/Get-Path.ps1 diff --git a/Get-PossibleArguments.ps1 b/base/Get-PossibleArguments.ps1 similarity index 100% rename from Get-PossibleArguments.ps1 rename to base/Get-PossibleArguments.ps1 diff --git a/Get-Profiles.ps1 b/base/Get-Profiles.ps1 similarity index 100% rename from Get-Profiles.ps1 rename to base/Get-Profiles.ps1 diff --git a/Get-ScopeDepth.ps1 b/base/Get-ScopeDepth.ps1 similarity index 100% rename from Get-ScopeDepth.ps1 rename to base/Get-ScopeDepth.ps1 diff --git a/Invoke-Sudo.ps1 b/base/Invoke-Sudo.ps1 similarity index 100% rename from Invoke-Sudo.ps1 rename to base/Invoke-Sudo.ps1 diff --git a/base/New-MyPackage.ps1 b/base/New-MyPackage.ps1 new file mode 100644 index 0000000..f960247 --- /dev/null +++ b/base/New-MyPackage.ps1 @@ -0,0 +1,5 @@ +[CmdletBinding(SupportsShouldProcess)]param( + [string]$Name +) + +[szPackage]::Create($Name) diff --git a/base/Reload-MyScripts.ps1 b/base/Reload-MyScripts.ps1 new file mode 100644 index 0000000..162cf65 --- /dev/null +++ b/base/Reload-MyScripts.ps1 @@ -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 +} + diff --git a/base/Set-MyPackage.ps1 b/base/Set-MyPackage.ps1 new file mode 100644 index 0000000..655ae09 --- /dev/null +++ b/base/Set-MyPackage.ps1 @@ -0,0 +1,38 @@ +[CmdletBinding(SupportsShouldProcess)]param( + [Parameter(mandatory=$true, ValueFromPipeline=$true)] + [szPackage]$InputObject, + [switch]$Force, + [switch]$PassThru, + [switch]$DryRun +) + +process { + $local:outObj = @{ package = @{ Name = $InputObject.Name; Condition = @() } } + foreach( $local:c in $InputObject.Condition ) { + $local:newC = [ordered]@{} + foreach( $local:p in $c.PSObject.Properties ) { + if( $p.TypeNameOfValue -match 'ScriptBlock' -and $p.Value ) { + $newC[$p.Name] = $p.Value.ToString().Trim() + } else { + $newC[$p.Name] = $p.Value + } + } + $outObj.package.Condition += $newC; + } + $local:jsonOut = $outObj | ConvertTo-Json -Depth 4 + + if( -not $DryRun ) { + $local:jsonPath = Join-Path $MyPSScriptRoot $_.name + if( -not (Test-Path $jsonPath) ) { $null = New-Item -Type Directory $jsonPath -Force:$Force } + $jsonPath = Join-Path $jsonPath '_.package.json' + if( $Force -or -not (Test-Path $jsonPath) ) { + $jsonOut | Out-File $jsonPath -Force:$Force + } elseif ( Test-Path $jsonPath ) { + throw 'Package already exists!' + } + } + + if( $PassThru ) { + $jsonOut + } +} diff --git a/Setup-Profile.ps1 b/base/Setup-Profile.ps1 similarity index 98% rename from Setup-Profile.ps1 rename to base/Setup-Profile.ps1 index 74b0c73..8acf000 100644 --- a/Setup-Profile.ps1 +++ b/base/Setup-Profile.ps1 @@ -424,11 +424,12 @@ $local:My_PSScriptRoot = Join-Path (Split-Path -Parent $MyPSModulePath) Scripts Write-Verbose "MyPSScriptRoot = $My_PSScriptRoot" if ( -not (Test-Path $My_PSScriptRoot) ) { New-Item -ItemType Directory -Path $My_PSScriptRoot -Force | Out-Null - New-Item -ItemType Directory -Path $My_PSScriptRoot/profile.d -Force | Out-Null + New-Item -ItemType Directory -Path $(Join-Path $My_PSScriptRoot base) -Force | Out-Null + New-Item -ItemType Directory -Path $(Join-Path $(Join-Path $My_PSScriptRoot base) profile.d) -Force | Out-Null } $global:MyPSScriptRoot = $My_PSScriptRoot -$p = @($p[0], $MyPSScriptRoot) + $($p | Select-Object -Skip 1) +$p = @($p[0], $(Join-Path $My_PSScriptRoot base)) + $($p | Select-Object -Skip 1) $env:PATH = $p -join $PathEnvDelimiter } diff --git a/Stop-ProcessTree.ps1 b/base/Stop-ProcessTree.ps1 similarity index 100% rename from Stop-ProcessTree.ps1 rename to base/Stop-ProcessTree.ps1 diff --git a/base/_.package.json b/base/_.package.json new file mode 100644 index 0000000..f7b2ff5 --- /dev/null +++ b/base/_.package.json @@ -0,0 +1,5 @@ +{ + "package": { + "name": "base" + } +} diff --git a/base/profile.d/Docker.class.ps1 b/base/profile.d/Docker.class.ps1 new file mode 100644 index 0000000..11d3132 --- /dev/null +++ b/base/profile.d/Docker.class.ps1 @@ -0,0 +1,32 @@ +class DockerContainer { #: System.Management.Automation.IValidateSetValuesGenerator { + static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) { + $local:possibleValues = $( + docker ps -a --no-trunc --format "{{.Names}}" + ) + return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict ); + } + + [String[]] GetValidValues() { + return [DockerContainer]::_GetValidValues('',$true) + } +} + +class DockerComposeDirs { #: System.Management.Automation.IValidateSetValuesGenerator { + static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) { + $local:possibleValues = $( + Get-ChildItem -Directory -Depth 3 | + Where-Object { $_ | + Get-ChildItem -Include 'docker-compose.y*l' | + Where-Object Extension -in '.yml','.yaml' + } | ForEach-Object { + $_.FullName.Replace($(Join-Path $PWD ""),'') + } + ) + return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict ); + } + + [String[]] GetValidValues() { + return [DockerComposeDirs]::_GetValidValues('',$true) + } +} + diff --git a/profile.d/FromPowerShellCookbook.ps1 b/base/profile.d/FromPowerShellCookbook.ps1 similarity index 100% rename from profile.d/FromPowerShellCookbook.ps1 rename to base/profile.d/FromPowerShellCookbook.ps1 diff --git a/profile.d/FunctionName.class.ps1 b/base/profile.d/FunctionName.class.ps1 similarity index 100% rename from profile.d/FunctionName.class.ps1 rename to base/profile.d/FunctionName.class.ps1 diff --git a/profile.d/MyConfig.class.ps1 b/base/profile.d/MyConfig.class.ps1 similarity index 85% rename from profile.d/MyConfig.class.ps1 rename to base/profile.d/MyConfig.class.ps1 index 4e7c457..8d2d42b 100644 --- a/profile.d/MyConfig.class.ps1 +++ b/base/profile.d/MyConfig.class.ps1 @@ -1,9 +1,9 @@ class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator { hidden static [hashtable]$_HardCodedConfigDict = @{ - 'myconfig' = @($(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'config.files.json'), - $(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'config.files.local.json')) - 'mymodules' = @($(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'modules.json'), - $(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'modules.local.json')) + 'myconfig' = @($(Join-Path $(Join-Path $PackagePath 'src') 'config.files.json'), + $(Join-Path $(Join-Path $PackagePath 'src') 'config.files.local.json')) + 'mymodules' = @($(Join-Path $(Join-Path $PackagePath 'src') 'modules.json'), + $(Join-Path $(Join-Path $PackagePath 'src') 'modules.local.json')) } static [hashtable]GetConfigDictionary() { diff --git a/base/profile.d/MyScript.class.ps1 b/base/profile.d/MyScript.class.ps1 new file mode 100644 index 0000000..ed219be --- /dev/null +++ b/base/profile.d/MyScript.class.ps1 @@ -0,0 +1,16 @@ +class MyScript { #: System.Management.Automation.IValidateSetValuesGenerator { + static [string[]] _GetValidValues([string]$wordToComplete,[bool]$CurrentOnly,[bool]$Strict) { + $local:possibleValues = $( + Get-MyPackages -ReturnFullPath -IncludeRoot -Force:$(-not $CurrentOnly) | + Get-ChildItem -Filter '*.ps1' | + 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('',$false,$true) + } +} diff --git a/profile.d/Style.ps1 b/base/profile.d/Style.ps1 similarity index 100% rename from profile.d/Style.ps1 rename to base/profile.d/Style.ps1 diff --git a/profile.d/Test-MyModules.ps1 b/base/profile.d/Test-MyModules.ps1 similarity index 100% rename from profile.d/Test-MyModules.ps1 rename to base/profile.d/Test-MyModules.ps1 diff --git a/profile.d/UnixCompleters.ps1 b/base/profile.d/UnixCompleters.ps1 similarity index 100% rename from profile.d/UnixCompleters.ps1 rename to base/profile.d/UnixCompleters.ps1 diff --git a/base/profile.d/classes.ps1 b/base/profile.d/classes.ps1 new file mode 100644 index 0000000..44c05a7 --- /dev/null +++ b/base/profile.d/classes.ps1 @@ -0,0 +1,3 @@ +. Invoke-Expression ". $(Join-Path $(Join-Path $PackagePath 'src') 'SystemName.class.inc.ps1')" +. Invoke-Expression ". $(Join-Path $(Join-Path $PackagePath 'src') 'Packagesz.class.inc.ps1')" + diff --git a/profile.d/env.local.ps1.template b/base/profile.d/env.local.ps1.template similarity index 100% rename from profile.d/env.local.ps1.template rename to base/profile.d/env.local.ps1.template diff --git a/profile.d/env.ps1 b/base/profile.d/env.ps1 similarity index 100% rename from profile.d/env.ps1 rename to base/profile.d/env.ps1 diff --git a/profile.d/load-z.ps1 b/base/profile.d/load-z.ps1 similarity index 100% rename from profile.d/load-z.ps1 rename to base/profile.d/load-z.ps1 diff --git a/base/src/Packagesz.class.inc.ps1 b/base/src/Packagesz.class.inc.ps1 new file mode 100644 index 0000000..047d498 --- /dev/null +++ b/base/src/Packagesz.class.inc.ps1 @@ -0,0 +1,97 @@ +class Packagesz { #: System.Management.Automation.IValidateSetValuesGenerator { + static [hashtable]GetPackageDictionary() { + $local:packages = @{} + + Get-ChildItem $global:MyPSScriptRoot | + ForEach-Object { Join-Path $_.FullName "_.package.json" } | + Where-Object { Test-Path $_ } | + Get-Item | + ForEach-Object { + $local:testPkg = Get-Content $_ -Raw | + ConvertFrom-Json | + Select-Object -ExpandProperty "package" -ErrorAction SilentlyContinue + if( $testPkg ) { + $local:k = $_.Directory.FullName.Replace($MyPSScriptRoot,'') -replace '^/','' + $packages[$k] = $testPkg + } + } + + return $packages; + } + static [bool] ValidatePackageConditions($Package) { + if( -not $Package.Condition ) { return $true } + $local:valid = $Package.Condition[0].Logic -notin ([szLogic]::or, [szLogic]::ornot) + $local:currentSys = [SystemName]::_GetValidValues('',$true,$true); + $local:hostname = $(hostname) + $local:username = $env:USER ?? $env:USERNAME + foreach( $local:c in $Package.Condition ) { + if( $valid -eq ($c.Logic -in ([szLogic]::or, [szLogic]::ornot )) ) { continue } + + $local:v = $true; + if( $v -and $c.System ) { + $v = $v -and $($c.System | Where-Object { $currentSys -contains $_ }) + } + if( $v -and $c.Hostname ) { + $v = $v -and $($c.Hostname | Where-Object { $hostname -match $_ }) + } + if( $v -and $c.Username ) { + $v = $v -and $($c.Username | Where-Object { $username -match $_ }) + } + if( $v -and $c.custom ) { + $v = $v -and ([bool]$(Invoke-ScriptBlock ([ScriptBlock]::Create($c.custom)))) + } + switch( $c.Logic ) { + [szLogic]::not { $valid = $valid -and -not $v } + [szLogic]::or { $valid = $valid -or $v } + [szLogic]::ornot { $valid = $valid -or -not $v } + default { $valid = $valid -and $v } + } + } + return $valid + } + static [string[]] _GetValidValues([string]$wordToComplete,[bool]$CurrentOnly,[bool]$Strict) { + $local:pkgz = [Packagesz]::GetPackageDictionary() + $local:possibleValues = $pkgz.Keys | + Where-Object { -not $CurrentOnly -or $( + [Packagesz]::ValidatePackageConditions($pkgz[$_]) + ) } + + return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict ); + } + + [String[]] GetValidValues() { + return [Packagesz]::_GetValidValues('',$false,$true) + } +} + +enum szLogic { + and = 0 + not = 1 # nand + or = 2 + ornot = 3 +} + +class szCondition { + [ScriptBlock]$custom = { $true } + [string[]]$System = $null + [string[]]$Hostname = $null + [string[]]$Username = $null + [szLogic]$Logic = [szLogic]::and + + [string] ToString() { + return "Logic = $($this.Logic); System = $($this.System); Hostname = $($this.Hostname); Username = $($this.Username); Custom = { $($this.Custom.ToString().Trim()) }" + } +} + +class szPackage { + [string]$Name + [szCondition[]]$Condition + + static [szPackage]Create($Name) { + $local:newPkg = New-Object -Type 'szPackage' + $newPkg.Name = $Name + $newPkg.Condition = @() + + return $newPkg + } +} diff --git a/profile.d/SystemName.class.ps1 b/base/src/SystemName.class.inc.ps1 similarity index 100% rename from profile.d/SystemName.class.ps1 rename to base/src/SystemName.class.inc.ps1 diff --git a/src/config.files.json b/base/src/config.files.json similarity index 100% rename from src/config.files.json rename to base/src/config.files.json diff --git a/src/modules.json b/base/src/modules.json similarity index 100% rename from src/modules.json rename to base/src/modules.json diff --git a/src/smartsudo.inc.ps1 b/base/src/smartsudo.inc.ps1 similarity index 100% rename from src/smartsudo.inc.ps1 rename to base/src/smartsudo.inc.ps1 diff --git a/dcc.ps1 b/dcc.ps1 deleted file mode 100644 index e28210f..0000000 --- a/dcc.ps1 +++ /dev/null @@ -1,5 +0,0 @@ -[CmdletBinding(SupportsShouldProcess)]param( - [string[]]$ProjectPath=@($PWD) -) - -$ProjectPath | ForEach-Object { docker-compose --file $(Resolve-Path $(Join-Path $_ docker-compose.yml)) config | less } diff --git a/Edit-DockerCompose.ps1 b/docker/Edit-DockerCompose.ps1 similarity index 65% rename from Edit-DockerCompose.ps1 rename to docker/Edit-DockerCompose.ps1 index 8190795..cb715a9 100644 --- a/Edit-DockerCompose.ps1 +++ b/docker/Edit-DockerCompose.ps1 @@ -1,4 +1,13 @@ [CmdletBinding(SupportsShouldProcess)]param( + [ArgumentCompleter({ param ( + $commandName, + $parameterName, + $wordToComplete, + $commandAst, + $fakeBoundParameters + ) + [DockerComposeDirs]::_GetValidValues($wordToComplete,$true) + })] [string[]]$ProjectPath=@($PWD), [switch]$Force, [string[]]$AdditionalFiles @@ -14,6 +23,6 @@ foreach( $local:fileName in $EditCandidates ) { } } -Edit-TextFile ($editFiles + $AdditionalFiles) +Edit-TextFile ($($editFiles | Sort-Object) + $AdditionalFiles) $editFiles | Select-Object -First 1 | ForEach-Object { docker-compose --file $(Resolve-Path $_) config -q } diff --git a/Get-DockerProcess.ps1 b/docker/Get-DockerProcess.ps1 similarity index 100% rename from Get-DockerProcess.ps1 rename to docker/Get-DockerProcess.ps1 diff --git a/docker/_.package.json b/docker/_.package.json new file mode 100644 index 0000000..d790eb8 --- /dev/null +++ b/docker/_.package.json @@ -0,0 +1,14 @@ +{ + "package": { + "Condition": [ + { + "custom": "Get-Command docker -Type Application", + "System": null, + "Hostname": null, + "Username": null, + "Logic": 0 + } + ], + "Name": "docker" + } +} diff --git a/docker/dcc.ps1 b/docker/dcc.ps1 new file mode 100644 index 0000000..9b65de9 --- /dev/null +++ b/docker/dcc.ps1 @@ -0,0 +1,14 @@ +[CmdletBinding(SupportsShouldProcess)]param( + [ArgumentCompleter({ param ( + $commandName, + $parameterName, + $wordToComplete, + $commandAst, + $fakeBoundParameters + ) + [DockerComposeDirs]::_GetValidValues($wordToComplete,$true) + })] + [string[]]$ProjectPath=@($PWD) +) + +$ProjectPath | ForEach-Object { docker-compose --file $(Resolve-Path $(Join-Path $_ docker-compose.yml)) config | less } diff --git a/dcdown.ps1 b/docker/dcdown.ps1 similarity index 59% rename from dcdown.ps1 rename to docker/dcdown.ps1 index 666fc2b..8c83258 100644 --- a/dcdown.ps1 +++ b/docker/dcdown.ps1 @@ -1,4 +1,13 @@ [CmdletBinding(SupportsShouldProcess)]param( + [ArgumentCompleter({ param ( + $commandName, + $parameterName, + $wordToComplete, + $commandAst, + $fakeBoundParameters + ) + [DockerComposeDirs]::_GetValidValues($wordToComplete,$true) + })] [string[]]$ProjectPath=@($PWD), [Parameter(Position = 0, ValueFromRemainingArguments = $true)] [string[]]$CliParams diff --git a/dcl.ps1 b/docker/dcl.ps1 similarity index 100% rename from dcl.ps1 rename to docker/dcl.ps1 diff --git a/dcll.ps1 b/docker/dcll.ps1 similarity index 100% rename from dcll.ps1 rename to docker/dcll.ps1 diff --git a/dcr.ps1 b/docker/dcr.ps1 similarity index 100% rename from dcr.ps1 rename to docker/dcr.ps1 diff --git a/dcre.ps1 b/docker/dcre.ps1 similarity index 100% rename from dcre.ps1 rename to docker/dcre.ps1 diff --git a/dcreup.ps1 b/docker/dcreup.ps1 similarity index 100% rename from dcreup.ps1 rename to docker/dcreup.ps1 diff --git a/dcup.ps1 b/docker/dcup.ps1 similarity index 77% rename from dcup.ps1 rename to docker/dcup.ps1 index 8600f47..47cc001 100644 --- a/dcup.ps1 +++ b/docker/dcup.ps1 @@ -1,4 +1,13 @@ [CmdletBinding(SupportsShouldProcess)]param( + [ArgumentCompleter({ param ( + $commandName, + $parameterName, + $wordToComplete, + $commandAst, + $fakeBoundParameters + ) + [DockerComposeDirs]::_GetValidValues($wordToComplete,$true) + })] [string[]]$ProjectPath=@($PWD), [Parameter(Position = 0, ValueFromRemainingArguments = $true)] [string[]]$CliParams diff --git a/dcx.ps1 b/docker/dcx.ps1 similarity index 100% rename from dcx.ps1 rename to docker/dcx.ps1 diff --git a/di.ps1 b/docker/di.ps1 similarity index 100% rename from di.ps1 rename to docker/di.ps1 diff --git a/dll.ps1 b/docker/dll.ps1 similarity index 100% rename from dll.ps1 rename to docker/dll.ps1 diff --git a/dlogs.ps1 b/docker/dlogs.ps1 similarity index 100% rename from dlogs.ps1 rename to docker/dlogs.ps1 diff --git a/dx.ps1 b/docker/dx.ps1 similarity index 100% rename from dx.ps1 rename to docker/dx.ps1 diff --git a/profile.d/MyScript.class.ps1 b/profile.d/MyScript.class.ps1 deleted file mode 100644 index 794ee8b..0000000 --- a/profile.d/MyScript.class.ps1 +++ /dev/null @@ -1,15 +0,0 @@ -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) - } -}