比較提交

...

4 次程式碼提交

作者 SHA1 備註 日期
lksz 1792bb23a1 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
2020-11-25 01:20:52 -05:00
lksz@work 80a488484b Set-EnvVariable + minor fix to SystemName 2020-11-24 13:15:47 -05:00
Gal Szkolnik 25ec753675 Renamed dl into dlogs.
scoop relies on an internal command of it's named dl, and having a dl
command breaks it. Renaming the dl command resolved the issue.
2020-11-16 08:51:21 -05:00
lksz 1bef8ca119 Improved Export-FunctionSource + some Docker fixes
Export-FunctionSource to handle Filters and scripts + following Aliases.
Edit-DockerCompose will allow editing of additional files next to the
default docker-compose files.
dcc, dcdown and dcup to work with ProjectPath parameter.
Added ssh.conf to the config.files.json dictionary.
2020-11-11 16:38:56 -05:00
共有 86 個檔案被更改,包括 580 行新增147 行删除

查看文件

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

查看文件

@ -13,14 +13,14 @@ iwr https://lksz.me/pwsz | iex
```
This sets up the `$PROFILE` file, and initilizes the default dir `$MyPSScriptRoot` with a git clone of this repo.
The `https://lksz.me/pwsz` actually points to the [`Setup-Profile.ps1`](Setup-Profile.ps1) script, which means, that after you have the `Scripts` directory setup, you can call `Setup-Profile` to update the `$PROFILE` after the `Setup-Profile.ps1` has been modified (or updated via `git pull`).
The `https://lksz.me/pwsz` actually points to the [`Setup-Profile.ps1`](base/Setup-Profile.ps1) script, which means, that after you have the `Scripts` directory setup, you can call `Setup-Profile` to update the `$PROFILE` after the `Setup-Profile.ps1` has been modified (or updated via `git pull`).
### The Short, but with control option
### The somewhat Short, but with control option
```PowerShell
$sfw=1; iwr https://lksz.me/pwsz | iex; _setup [-sudo] [-Force] [-NoGitClone] [-GitURL <alternative git URL>] [-WhatIf] [-Confirm] [<CommonParameters>]
```
The key here is `$sfw=1`, which can be substitued with the more verbose `$SetupFromWeb=1`, this let's the [`Setup-Profile`](Setup-Profile.ps1) script know to just load it's content, but allow you to manually call it's internal function via `_setup`
The key here is `$sfw=1`, which can be substitued with the more verbose `$SetupFromWeb=1`, this let's the [`Setup-Profile`](base/Setup-Profile.ps1) script know to just load it's content, but allow you to manually call it's internal function via `_setup`
### The 'I don't trust one liners, what's going on ?!?' option
You can always clone the git repo first, and only then run the [`Setup-Profile.ps1`](Setup-Profile.ps1) script.
You can always clone the git repo first, and only then run the [`Setup-Profile.ps1`](base/Setup-Profile.ps1) script.

查看文件

@ -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,6 @@
[CmdletBinding()]param(
[string]$Name,
[object]$Value
)
Set-Item -Path (Join-Path "env:" $Name) -Value $Value

16
base.linux/_.package.json Normal file
查看文件

@ -0,0 +1,16 @@
{
"package": {
"Condition": [
{
"custom": null,
"System": [
"Linux"
],
"Hostname": null,
"Username": null,
"Logic": 0
}
],
"Name": "base.linux"
}
}

查看文件

@ -0,0 +1,11 @@
[CmdletBinding()]param(
[string]$Name,
[object]$Value,
[ValidateSet('User','Machine')]
[string]$Persist
)
Set-Item -Path (Join-Path "env:" $Name) -Value $Value
if( $Persist ) {
[environment]::setEnvironmentVariable($Name, $Value, $Persist)
}

16
base.win/_.package.json Normal file
查看文件

@ -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,
$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 ) {

21
base/Get-MyPackages.ps1 Normal file
查看文件

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

44
base/Get-MyScript.ps1 Normal file
查看文件

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

5
base/New-MyPackage.ps1 Normal file
查看文件

@ -0,0 +1,5 @@
[CmdletBinding(SupportsShouldProcess)]param(
[string]$Name
)
[szPackage]::Create($Name)

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
}

38
base/Set-MyPackage.ps1 Normal file
查看文件

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

查看文件

@ -371,9 +371,23 @@ param(
$local:src = ""
foreach ( $local:func in $FunctionName ) {
$local:funcDef = $null
foreach( $cmd in (Get-Command -All $func) ) {
if( $cmd.CommandType -in @('Function','Filter') ) {
$funcDef = $cmd.Definition
break
} elseif( -$funcDef -and ($cmd.CommandType -in @('Script','ExternalScript')) ) {
$funcDef = Get-Content $cmd.Definition
}
}
if( -not $funcDef ) {
default { throw "Don't know how to handle $func" }
}
if ( -not $NoHeader ) { $src += "`nfunction $func {" }
$src += "`n"
$src += $((Get-Command -Type Function $func).Definition)
$src += $funcDef
if ( -not $NoHeader ) { $src += "`n}" }
$src += "`n"
}
@ -410,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
}

5
base/_.package.json Normal file
查看文件

@ -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 {
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() {

查看文件

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

查看文件

@ -18,6 +18,7 @@ class SystemName { #: System.Management.Automation.IValidateSetValuesGenerator {
}
$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" }

查看文件

@ -37,6 +37,8 @@
"ssh-pub": "~/.ssh/id_*.pub",
"ssh-id": ["~/.ssh/id_ed25519","~/.ssh/id_rsa"],
"ssh-auth": "~/.ssh/authorized_keys",
"ssh-conf": "/etc/ssh/sshd_config",
"ssh" : [ "#ssh-auth", "#ssh-conf", "#ssh-pub", "#ssh-id" ]
"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" ]
}

查看文件

@ -1,3 +0,0 @@
[CmdletBinding(SupportsShouldProcess)]param([string[]]$ProjectPath=@($PWD))
$ProjectPath | ForEach-Object { docker-compose --file $(Resolve-Path $(Join-Path $_ docker-compose.yml)) config | less }

查看文件

@ -1 +0,0 @@
docker-compose down --timeout=3 --volumes --remove-orphans "$args"

查看文件

@ -1,10 +0,0 @@
#[CmdletBinding(SupportsShouldProcess)]param()
$local:dcParams = [string]::Empty
if( $args ) {
$dcParams = $args | ForEach-Object {
if( $_ -match "(?:^'[^']+'$)|(?:^`"[^`"]+`"$)|(?:^[^\s]+$)" ) { $_ }
else { "'$($_.Replace( "'", "''" ))'" }
}
}
docker-compose up -d --remove-orphans $dcParams && docker-compose logs --follow --tail 10 $dcParams

查看文件

@ -1,4 +1,18 @@
[CmdletBinding(SupportsShouldProcess)]param([string[]]$ProjectPath=@($PWD),[switch]$Force)
[CmdletBinding(SupportsShouldProcess)]param(
[ArgumentCompleter({ param (
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
[DockerComposeDirs]::_GetValidValues($wordToComplete,$true)
})]
[string[]]$ProjectPath=@($PWD),
[switch]$Force,
[string[]]$AdditionalFiles
)
$local:editCandidates = @('docker-compose.yml','.base.docker-compose.yml')
$local:editFiles = @()
@ -9,6 +23,6 @@ foreach( $local:fileName in $EditCandidates ) {
}
}
Edit-TextFile $editFiles
Edit-TextFile ($($editFiles | Sort-Object) + $AdditionalFiles)
$editFiles | Select-Object -First 1 | ForEach-Object { docker-compose --file $(Resolve-Path $_) config -q }

14
docker/_.package.json Normal file
查看文件

@ -0,0 +1,14 @@
{
"package": {
"Condition": [
{
"custom": "Get-Command docker -Type Application",
"System": null,
"Hostname": null,
"Username": null,
"Logic": 0
}
],
"Name": "docker"
}
}

14
docker/dcc.ps1 Normal file
查看文件

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

18
docker/dcdown.ps1 Normal file
查看文件

@ -0,0 +1,18 @@
[CmdletBinding(SupportsShouldProcess)]param(
[ArgumentCompleter({ param (
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
[DockerComposeDirs]::_GetValidValues($wordToComplete,$true)
})]
[string[]]$ProjectPath=@($PWD),
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
[string[]]$CliParams
)
$ProjectPath | ForEach-Object {
docker-compose --file $(Resolve-Path $(Join-Path $_ docker-compose.yml)) down --timeout=3 --volumes --remove-orphans "$CliParams"
}

28
docker/dcup.ps1 Normal file
查看文件

@ -0,0 +1,28 @@
[CmdletBinding(SupportsShouldProcess)]param(
[ArgumentCompleter({ param (
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
[DockerComposeDirs]::_GetValidValues($wordToComplete,$true)
})]
[string[]]$ProjectPath=@($PWD),
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
[string[]]$CliParams
)
$local:dcParams = [string]::Empty
if( $CliParams ) {
$dcParams = $CliParams | ForEach-Object {
if( $_ -match "(?:^'[^']+'$)|(?:^`"[^`"]+`"$)|(?:^[^\s]+$)" ) { $_ }
else { "'$($_.Replace( "'", "''" ))'" }
}
}
$ProjectPath | ForEach-Object {
$local:dcPath = $(Resolve-Path $(Join-Path $_ docker-compose.yml))
Write-Verbose "docker-compose --file $dcPath up -d --remove-orphans $dcParams && docker-compose --file $dcPath logs --follow --tail 10 $dcParams"
docker-compose --file $dcPath up -d --remove-orphans $dcParams && docker-compose --file $dcPath logs --follow --tail 10 $dcParams
}

查看文件

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