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:
lksz 2020-11-25 00:46:33 -05:00
parent 80a488484b
commit 42b39f4e25
83 changed files with 505 additions and 130 deletions

View file

@ -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
}
}

View file

@ -0,0 +1,38 @@
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 |
Where-Object { $_ } |
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)
}
}

View file

@ -0,0 +1,44 @@
{
"neovim": [
"~/.config/nvim/init.vim",
"~/.config/nvim/vim-plug/plugins.vim",
"#vim",
"#vi"
],
"sz-shrc": "~/.sz.shrc.sh",
"sz-rc-all": [
"#sz-aliases-sh",
"#sz-local-rc",
"#sz-shrc",
"#sz-zshrc",
"#zshrc"
],
"vi": "~/.virc",
"sz-local-rc": "~/.sz.local.sh",
"zshrc": "~/.zshrc",
"ansible": [
"/etc/ansible/ansible.cfg",
"/opt/ansible/ansible.cfg",
"/opt/ansible/hosts"
],
"sz-aliases-sh": "~/.sz.aliases.sh",
"vim": [
"~/.vimrc",
"~/.vim/vimrc"
],
"sz-zshrc": [
"~/.sz.zshrc.sh",
"#zshrc"
],
"tmux": [
"~/.tmux.conf",
"~/.byobu/.tmux.conf"
],
"ssh-pub": "~/.ssh/id_*.pub",
"ssh-id": ["~/.ssh/id_ed25519","~/.ssh/id_rsa"],
"ssh-auth": "~/.ssh/authorized_keys",
"ssh-known": "~/.ssh/known_hosts",
"ssh-conf": "~/.ssh/config",
"sshd-conf": "/etc/ssh/sshd_config",
"ssh" : [ "#ssh-auth", "#ssh-known", "#ssh-conf", "#sshd-conf", "#ssh-pub", "#ssh-id" ]
}

14
base/src/modules.json Normal file
View file

@ -0,0 +1,14 @@
{
"Always": [
"PowerShellCookbook",
"oh-my-posh",
"posh-git",
"z"
],
"Desktop": [
],
"Unix": [
"Microsoft.PowerShell.UnixCompleters"
]
}

View file

@ -0,0 +1,18 @@
function smartsudo() {
[CmdletBinding(SupportsShouldProcess)]param(
[switch]$sudo,
[switch]$Force,
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
[string[]]$expr
)
if( $sudo -and -not $(Test-IsAdmin) ) {
Write-Verbose "Perofrming the following command line via SUDO:`n$($expr -join ' ')"
$local:base64command = ConvertTo-Base64 "$expr"
/usr/bin/env sudo pwsh -EncodedCommand $base64command
return
}
Write-Verbose "Perofrming the following expression in-line:`n$($expr -join ' ')"
Invoke-Expression "$expr"
}