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
|
@ -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" }
|
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
{
|
||||||
|
"package": {
|
||||||
|
"Condition": [
|
||||||
|
{
|
||||||
|
"custom": null,
|
||||||
|
"System": [
|
||||||
|
"Linux"
|
||||||
|
],
|
||||||
|
"Hostname": null,
|
||||||
|
"Username": null,
|
||||||
|
"Logic": 0
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"Name": "base.linux"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
{
|
||||||
|
"package": {
|
||||||
|
"Name": "base.win",
|
||||||
|
"Condition": [
|
||||||
|
{
|
||||||
|
"custom": null,
|
||||||
|
"System": [
|
||||||
|
"Windows"
|
||||||
|
],
|
||||||
|
"Hostname": null,
|
||||||
|
"Username": null,
|
||||||
|
"Logic": 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
|
@ -16,10 +16,13 @@
|
||||||
$commandAst,
|
$commandAst,
|
||||||
$fakeBoundParameters
|
$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]$ForceImport,
|
||||||
|
[switch]$All,
|
||||||
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
|
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
|
||||||
[ArgumentCompleter({ param (
|
[ArgumentCompleter({ param (
|
||||||
$commandName,
|
$commandName,
|
||||||
|
@ -28,18 +31,25 @@
|
||||||
$commandAst,
|
$commandAst,
|
||||||
$fakeBoundParameters
|
$fakeBoundParameters
|
||||||
)
|
)
|
||||||
$local:possibleValues = [MyScript]::_GetValidValues($wordToComplete,$true)
|
$local:NotAll = $true
|
||||||
if( $fakeBoundParameters.ContainsKey("System") ) {
|
$local:Pkg = [string]::Empty
|
||||||
$possibleValues = $possibleValues | Where-Object { $_ -match "^sys\.$($fakeBoundParameters.System)" } | ForEach-Object { $_ -replace "^sys\.$($fakeBoundParameters.System).",'' }
|
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
|
$possibleValues
|
||||||
})]
|
})]
|
||||||
[string[]]$ScriptName
|
[string[]]$ScriptName
|
||||||
)
|
)
|
||||||
|
|
||||||
$local:EditRootPath=$MyPSScriptRoot
|
$local:EditRootPath=$MyPSScriptRoot
|
||||||
if( $System ) {
|
if( $Package ) {
|
||||||
$EditRootPath = Join-Path $EditRootPath "sys.$System"
|
$EditRootPath = Join-Path $EditRootPath $Package
|
||||||
}
|
}
|
||||||
$local:ScriptPaths = @()
|
$local:ScriptPaths = @()
|
||||||
foreach( $local:p in $ImportFunction ) {
|
foreach( $local:p in $ImportFunction ) {
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -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" }
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
[CmdletBinding(SupportsShouldProcess)]param(
|
||||||
|
[string]$Name
|
||||||
|
)
|
||||||
|
|
||||||
|
[szPackage]::Create($Name)
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
|
@ -424,11 +424,12 @@ $local:My_PSScriptRoot = Join-Path (Split-Path -Parent $MyPSModulePath) Scripts
|
||||||
Write-Verbose "MyPSScriptRoot = $My_PSScriptRoot"
|
Write-Verbose "MyPSScriptRoot = $My_PSScriptRoot"
|
||||||
if ( -not (Test-Path $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 -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
|
$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
|
$env:PATH = $p -join $PathEnvDelimiter
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"package": {
|
||||||
|
"name": "base"
|
||||||
|
}
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator {
|
class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator {
|
||||||
hidden static [hashtable]$_HardCodedConfigDict = @{
|
hidden static [hashtable]$_HardCodedConfigDict = @{
|
||||||
'myconfig' = @($(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'config.files.json'),
|
'myconfig' = @($(Join-Path $(Join-Path $PackagePath 'src') 'config.files.json'),
|
||||||
$(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'config.files.local.json'))
|
$(Join-Path $(Join-Path $PackagePath 'src') 'config.files.local.json'))
|
||||||
'mymodules' = @($(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'modules.json'),
|
'mymodules' = @($(Join-Path $(Join-Path $PackagePath 'src') 'modules.json'),
|
||||||
$(Join-Path $(Join-Path $MyPSScriptRoot 'src') 'modules.local.json'))
|
$(Join-Path $(Join-Path $PackagePath 'src') 'modules.local.json'))
|
||||||
}
|
}
|
||||||
|
|
||||||
static [hashtable]GetConfigDictionary() {
|
static [hashtable]GetConfigDictionary() {
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -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')"
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
5
dcc.ps1
5
dcc.ps1
|
@ -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 }
|
|
|
@ -1,4 +1,13 @@
|
||||||
[CmdletBinding(SupportsShouldProcess)]param(
|
[CmdletBinding(SupportsShouldProcess)]param(
|
||||||
|
[ArgumentCompleter({ param (
|
||||||
|
$commandName,
|
||||||
|
$parameterName,
|
||||||
|
$wordToComplete,
|
||||||
|
$commandAst,
|
||||||
|
$fakeBoundParameters
|
||||||
|
)
|
||||||
|
[DockerComposeDirs]::_GetValidValues($wordToComplete,$true)
|
||||||
|
})]
|
||||||
[string[]]$ProjectPath=@($PWD),
|
[string[]]$ProjectPath=@($PWD),
|
||||||
[switch]$Force,
|
[switch]$Force,
|
||||||
[string[]]$AdditionalFiles
|
[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 }
|
$editFiles | Select-Object -First 1 | ForEach-Object { docker-compose --file $(Resolve-Path $_) config -q }
|
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"package": {
|
||||||
|
"Condition": [
|
||||||
|
{
|
||||||
|
"custom": "Get-Command docker -Type Application",
|
||||||
|
"System": null,
|
||||||
|
"Hostname": null,
|
||||||
|
"Username": null,
|
||||||
|
"Logic": 0
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"Name": "docker"
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 }
|
|
@ -1,4 +1,13 @@
|
||||||
[CmdletBinding(SupportsShouldProcess)]param(
|
[CmdletBinding(SupportsShouldProcess)]param(
|
||||||
|
[ArgumentCompleter({ param (
|
||||||
|
$commandName,
|
||||||
|
$parameterName,
|
||||||
|
$wordToComplete,
|
||||||
|
$commandAst,
|
||||||
|
$fakeBoundParameters
|
||||||
|
)
|
||||||
|
[DockerComposeDirs]::_GetValidValues($wordToComplete,$true)
|
||||||
|
})]
|
||||||
[string[]]$ProjectPath=@($PWD),
|
[string[]]$ProjectPath=@($PWD),
|
||||||
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
|
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
|
||||||
[string[]]$CliParams
|
[string[]]$CliParams
|
|
@ -1,4 +1,13 @@
|
||||||
[CmdletBinding(SupportsShouldProcess)]param(
|
[CmdletBinding(SupportsShouldProcess)]param(
|
||||||
|
[ArgumentCompleter({ param (
|
||||||
|
$commandName,
|
||||||
|
$parameterName,
|
||||||
|
$wordToComplete,
|
||||||
|
$commandAst,
|
||||||
|
$fakeBoundParameters
|
||||||
|
)
|
||||||
|
[DockerComposeDirs]::_GetValidValues($wordToComplete,$true)
|
||||||
|
})]
|
||||||
[string[]]$ProjectPath=@($PWD),
|
[string[]]$ProjectPath=@($PWD),
|
||||||
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
|
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
|
||||||
[string[]]$CliParams
|
[string[]]$CliParams
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue