2020-11-25 05:46:33 +00:00
|
|
|
[CmdletBinding(SupportsShouldProcess)]param(
|
2021-01-24 02:25:39 +00:00
|
|
|
[Parameter(mandatory=$true, ValueFromPipeline=$true, ParameterSetName="Pipe")]
|
2020-11-25 05:46:33 +00:00
|
|
|
[szPackage]$InputObject,
|
2021-01-24 02:25:39 +00:00
|
|
|
[Parameter(mandatory=$true, ParameterSetName="LocalHost")]
|
|
|
|
[switch]$LocalHost,
|
|
|
|
[Parameter(mandatory=$true, ParameterSetName="LocalUser")]
|
|
|
|
[switch]$LocalUser,
|
2020-11-25 05:46:33 +00:00
|
|
|
[switch]$Force,
|
|
|
|
[switch]$PassThru,
|
2021-02-07 18:32:23 +00:00
|
|
|
[switch]$DryRun,
|
|
|
|
[ValidateSet("Default","None")]
|
|
|
|
[string]$Template="Default"
|
2020-11-25 05:46:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
process {
|
2021-01-24 02:25:39 +00:00
|
|
|
$local:iObj = $_
|
|
|
|
if( $LocalHost ) {
|
|
|
|
$iObj = New-MyPackage -Name "$(hostname).local" |
|
|
|
|
Add-PackageCondition -Hostname "$(hostname)"
|
|
|
|
} elseif( $LocalUser ) {
|
|
|
|
$iObj = New-MyPackage -Name "$($USER).local" |
|
|
|
|
Add-PackageCondition -Username $USER
|
|
|
|
}
|
|
|
|
Write-Verbose "Set-MyPackage called with $($iObj | Out-String)..."
|
|
|
|
$local:outObj = @{ package = @{ Name = $iObj.Name; Condition = @() } }
|
|
|
|
foreach( $local:c in $iObj.Condition ) {
|
2020-11-25 05:46:33 +00:00
|
|
|
$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
|
2021-02-07 18:32:23 +00:00
|
|
|
|
2020-11-25 05:46:33 +00:00
|
|
|
if( -not $DryRun ) {
|
2021-01-24 02:25:39 +00:00
|
|
|
$local:pkgPath = Join-Path $MyPSScriptRoot $iObj.name
|
|
|
|
Write-Verbose "pkgPath=$pkgPath"
|
2021-02-07 18:32:23 +00:00
|
|
|
if( -not (Test-Path $pkgPath) ) {
|
|
|
|
$null = New-Item -Type Directory $pkgPath -Force:$Force
|
|
|
|
} elseif ( -not $Force ) {
|
|
|
|
$Template = "None"
|
|
|
|
}
|
|
|
|
switch($Template){
|
|
|
|
"Default" {
|
|
|
|
New-Item -Type Directory (Join-Path $pkgPath 'profile.d')
|
|
|
|
New-Item -Type Directory (Join-Path $pkgPath 'Aliases')
|
|
|
|
New-Item -Type Directory (Join-Path $pkgPath 'src')
|
|
|
|
New-Item -Type File -Path (Join-Path (Join-Path $pkgPath 'profile.d') 'env.local.ps1') -Value ''
|
|
|
|
}
|
|
|
|
}
|
2021-01-24 02:25:39 +00:00
|
|
|
$jsonPath = Join-Path $pkgPath '_.package.json'
|
2020-11-25 05:46:33 +00:00
|
|
|
if( $Force -or -not (Test-Path $jsonPath) ) {
|
|
|
|
$jsonOut | Out-File $jsonPath -Force:$Force
|
2021-01-24 02:25:39 +00:00
|
|
|
|
|
|
|
$local:profilePath = $(Join-Path $pkgPath 'profile.d')
|
|
|
|
if( $LocalHost -or $LocalUser -and -not (Test-Path $profilePath) ) {
|
|
|
|
$null = New-Item -Type Directory $profilePath -Force:$Force
|
|
|
|
$null = New-Item -Type File $(Join-Path $profilePath env.ps1) -Value "# local environment"
|
|
|
|
}
|
2020-11-25 05:46:33 +00:00
|
|
|
} elseif ( Test-Path $jsonPath ) {
|
|
|
|
throw 'Package already exists!'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( $PassThru ) {
|
|
|
|
$jsonOut
|
|
|
|
}
|
|
|
|
}
|