Last active
July 11, 2023 14:40
-
-
Save peplau/6fc0f839330e90e65b987d32b26b2ea4 to your computer and use it in GitHub Desktop.
SPE: Advanced Packaging Features
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /sitecore/system/Modules/PowerShell/Script Library/SPE/Tools/Package Generator/Content Editor/Context Menu/Packaging/Add Item to Package Advanced | |
$selectedItem = Get-Item -Path . | |
$includeLinkedItems = 0 | |
$parameters = @( | |
@{ Name = "Mode"; Title="Installation Options"; Value = "Merge-Merge"; Options = $installOptions; OptionTooltips = $installOptionsTooltips; Tooltip = "Hover over each option to view a short description."; Hint = "How should the installer behave if the package contains items that already exist?<br/><br/>Item : $($selectedItem.ProviderPath)"; Editor="combo"}) | |
#if($selectedItem.ItemPath.StartsWith("/sitecore/content")) { | |
#$parameters += @{ Name = "IncludeLinkedItems"; Title = "Include Linked Items"; Hint = "Choose one or more options below to include items linked. This relies on the Link Database to determine references."; Options = $linkedItemOptions; OptionTooltips = $linkedItemOptionTooltips; Editor = "checkbox" } | |
$parameters += @{ Name = "IncludeLinkOptions"; Title = "Include linked Items"; Value=0; Tooltip = "Define how linked items will be included in the package"; Options = $linkOptions; OptionTooltips = $linkOptionsTooltips; Editor = "radio" } | |
#} | |
$result = Read-Variable @defaultProps -Parameters $parameters -Description "Set installation options for this package source." | |
Resolve-Error | |
if($result -ne "ok") { | |
Close-Window | |
Exit | |
} | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Undefined | |
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Undefined | |
switch ($Mode) { | |
"Overwrite" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Overwrite | |
} | |
"Merge-Merge" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Merge | |
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Merge | |
} | |
"Merge-Clear" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Merge | |
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Clear | |
} | |
"Merge-Append" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Merge | |
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Append | |
} | |
"Skip" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Skip | |
} | |
"SideBySide" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::SideBySide | |
} | |
"AskUser" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Undefined | |
} | |
} | |
# Linked items dialog | |
$itemsToPack = @() | |
$itemsToPack += Get-ChildrenToInclude $selectedItem $IncludeItems | |
$linkedItems = @() | |
$linkedItems += Get-LinkedItems $selectedItem $itemsToPack $IncludeLinkOptions | |
if ($linkedItems.Count -gt 0){ | |
$selectedLinks = Get-SelectedLinks $linkedItems | |
} | |
# Add selected linked items to package | |
if ($selectedLinks -AND $selectedLinks.Count -gt 0){ | |
foreach ($linkId in $selectedLinks) { | |
$itemsToPack += $linkedItems | Where-Object -FilterScript { $_.ID.ToString() -like $linkId } | |
} | |
} | |
# Adding items to package | |
foreach ($itemToPack in $itemsToPack) { | |
$package = Add-ItemToPackage -Package $package -Item $itemToPack -IncludeDescendants $false | |
} | |
Close-Window |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /sitecore/system/Modules/PowerShell/Script Library/SPE/Tools/Package Generator/Content Editor/Context Menu/Packaging/Add Tree to Package Advanced | |
$selectedItem = Get-Item -Path . | |
$includeLinkedItems = 0 | |
$parameters = @( | |
@{ Name = "IncludeItems"; Title="Items to include in package"; Value="RootAndDescendants"; Options=$rootOptions; OptionTooltips = $rootOptionTooltips; Tooltip = "Hover over each option to view a short description."; Hint = "The package will dynamically include the items based on your selection below. `n`nItem Root : '$($selectedItem.ProviderPath)'"; Editor="combo" }, | |
@{ Name = "Mode"; Title="Installation Options"; Value= "Merge-Merge"; Options = $installOptions; OptionTooltips = $installOptionsTooltips; Tooltip = "Hover over each option to view a short description."; Hint = "How should the installer behave if the package contains items that already exist?"; Editor="combo"} | |
) | |
#if($selectedItem.ItemPath.StartsWith("/sitecore/content")) { | |
#$parameters += @{ Name = "IncludeLinkedItems"; Title = "Include Linked Items"; Hint = "Choose one or more options below to include items linked. This relies on the Link Database to determine references."; Options = $linkedItemOptions; OptionTooltips = $linkedItemOptionTooltips; Editor = "checkbox" } | |
$parameters += @{ Name = "IncludeLinkOptions"; Title = "Include linked Items"; Value=0; Tooltip = "Define how linked items will be included in the package"; Options = $linkOptions; OptionTooltips = $linkOptionsTooltips; Editor = "radio" } | |
#} | |
$result = Read-Variable @defaultProps -Parameters $parameters -Description "Set installation options for this package source." | |
Resolve-Error | |
if($result -ne "ok") { | |
Close-Window | |
Exit | |
} | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Undefined | |
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Undefined | |
switch ($Mode) { | |
"Overwrite" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Overwrite | |
} | |
"Merge-Merge" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Merge | |
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Merge | |
} | |
"Merge-Clear" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Merge | |
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Clear | |
} | |
"Merge-Append" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Merge | |
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Append | |
} | |
"Skip" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Skip | |
} | |
"SideBySide" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::SideBySide | |
} | |
"AskUser" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Undefined | |
} | |
} | |
# Linked items dialog | |
$itemsToPack = @() | |
$itemsToPack += Get-ChildrenToInclude $selectedItem $IncludeItems | |
$linkedItems = @() | |
$linkedItems += Get-LinkedItems $selectedItem $itemsToPack $IncludeLinkOptions | |
if ($linkedItems.Count -gt 0){ | |
$selectedLinks = Get-SelectedLinks $linkedItems | |
} | |
# Add selected linked items to package | |
if ($selectedLinks -AND $selectedLinks.Count -gt 0){ | |
foreach ($linkId in $selectedLinks) { | |
$itemsToPack += $linkedItems | Where-Object -FilterScript { $_.ID.ToString() -like $linkId } | |
} | |
} | |
# Adding items to package | |
foreach ($itemToPack in $itemsToPack) { | |
$package = Add-ItemToPackage -Package $package -Item $itemToPack -IncludeDescendants $false | |
} | |
Close-Window |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /sitecore/system/Modules/PowerShell/Script Library/SPE/Tools/Package Generator/Content Editor/Context Menu/Packaging/Quick Download Tree as Package Advanced | |
Import-Function -Name Setup-PackageGeneratorAdvanced | |
$timestamp = Get-Date -Format "yyyyMMdd.HHss" | |
$selectedItem = Get-Item -Path . | |
$examplePackageName = "$($timestamp).$($selectedItem.Name)" | |
$includeLinkedItems = 0 | |
$parameters = @( | |
@{ Name = "packageName"; Title="Package Name"; Placeholder = $examplePackageName; Tab="Package Metadata"}, | |
@{ Name = "Author"; Value = [Sitecore.Context]::User.Profile.FullName; Tab="Package Metadata"}, | |
@{ Name = "Publisher"; Value = [Sitecore.SecurityModel.License.License]::Licensee; Tab="Package Metadata"}, | |
@{ Name = "Version"; Value = $selectedItem.Version; Tab="Package Metadata"}, | |
@{ Name = "Readme"; Title="Readme"; Lines=10; Tab="Package Metadata"}, | |
@{ Name = "AsXml"; Title="Download Package as XML"; Value=[bool]$False; Editor="bool"; Tab="Package Metadata" }, | |
@{ Name = "IncludeItems"; Title="Items to include in package"; Value="RootAndDescendants"; Options=$rootOptions; OptionTooltips = $rootOptionTooltips; Tooltip = "Hover over each option to view a short description."; Hint = "The package will dynamically include the items based on your selection below. <br /><br />Root : '$($selectedItem.ProviderPath)'"; Editor="combo"; Tab="Installation Options" }, | |
@{ Name = "Mode"; Title="Installation Options"; Value = "Merge-Merge"; Options = $installOptions; OptionTooltips = $installOptionsTooltips; Tooltip = "Hover over each option to view a short description."; Hint = "How should the installer behave if the package contains items that already exist?"; Editor="combo"; Tab="Installation Options"} | |
) | |
#if($selectedItem.ItemPath.StartsWith("/sitecore/content")) { | |
$parameters += @{ Name = "IncludeLinkOptions"; Title = "Include linked Items"; Value=0; Tooltip = "Define how linked items will be included in the package"; Options = $linkOptions; OptionTooltips = $linkOptionsTooltips; Editor = "radio" } | |
#} | |
$props = @{} + $defaultProps | |
$props["Title"] = "Download Tree as Package" | |
$props["Description"] = "This Tool allows you to download the item tree as a package quickly." | |
$props["Parameters"] = $parameters | |
$props["Width"] = 600 | |
$props["Height"] = 750 | |
$result = Read-Variable @props | |
Resolve-Error | |
if($result -ne "ok") { | |
Close-Window | |
Exit | |
} | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Undefined | |
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Undefined | |
switch ($Mode) { | |
"Overwrite" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Overwrite | |
} | |
"Merge-Merge" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Merge | |
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Merge | |
} | |
"Merge-Clear" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Merge | |
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Clear | |
} | |
"Merge-Append" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Merge | |
$MergeMode = [Sitecore.Install.Utils.MergeMode]::Append | |
} | |
"Skip" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Skip | |
} | |
"SideBySide" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::SideBySide | |
} | |
"AskUser" { | |
$InstallMode = [Sitecore.Install.Utils.InstallMode]::Undefined | |
} | |
} | |
# Linked items dialog | |
$itemsToPack = @() | |
$itemsToPack += Get-ChildrenToInclude $selectedItem $IncludeItems | |
$linkedItems = @() | |
$linkedItems += Get-LinkedItems $selectedItem $itemsToPack $IncludeLinkOptions | |
if ($linkedItems.Count -gt 0){ | |
$selectedLinks = Get-SelectedLinks $linkedItems | |
} | |
# Add selected linked items to package | |
if ($selectedLinks -AND $selectedLinks.Count -gt 0){ | |
foreach ($linkId in $selectedLinks) { | |
$itemsToPack += $linkedItems | Where-Object -FilterScript { $_.ID.ToString() -like $linkId } | |
} | |
} | |
# Adding items to package | |
foreach ($itemToPack in $itemsToPack) { | |
$package = Add-ItemToPackage -Package $package -Item $itemToPack -IncludeDescendants $false | |
} | |
if([string]::IsNullOrEmpty($packageName)) { | |
$packageName = $examplePackageName | |
} | |
$package.Name = $packageName | |
$package.Metadata.Author = $Author | |
$package.Metadata.Publisher = $Publisher | |
$package.Metadata.Version = $Version | |
$package.Metadata.Readme = $Readme | |
[string]$packageName = "$($package.Name)-$($package.Metadata.Version)".Trim('-') | |
if ($AsXml) { | |
$packageFileName = "$($packageName).xml" | |
} | |
else { | |
$packageFileName = "$($packageName).zip" | |
} | |
Export-Package -Project $package -Path $packageFileName -Zip:$(!$AsXml) | |
Download-File "$($SitecorePackageFolder)\$($packageFileName)" | |
Remove-Item "$($SitecorePackageFolder)\$($packageFileName)" | |
Close-Window |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /sitecore/system/Modules/PowerShell/Script Library/SPE/Tools/Package Generator/Functions/Setup-PackageGeneratorAdvanced | |
Import-Function -Name Setup-PackageGenerator | |
function Get-UniqueLinks { | |
param( | |
[item]$Item | |
) | |
$combined = @() | |
$combined += Get-UniqueReferrers $Item | |
$combined += Get-UniqueReferences $Item | |
return $combined | Sort-Object -Property @{Expression={$_.ID}} -Unique | |
} | |
function Get-UniqueReferrers { | |
param( | |
[item]$Item | |
) | |
$items = Get-ItemReferrer -Item $Item | |
return $items | Sort-Object -Property @{Expression={$_.ID}} -Unique | |
} | |
function Get-UniqueReferences { | |
param( | |
[item]$Item | |
) | |
$links = $Item.Links.GetAllLinks($true,$true) | |
$items = @() | |
foreach ($link in $links) { | |
$path = $link.TargetDatabaseName | |
$path += ":" + $link.TargetPath | |
try { | |
$item = Get-Item -Database $link.TargetDatabaseName -Path $path -ErrorAction SilentlyContinue | |
if ($item -ne $null){ | |
$items += $item | |
} | |
} | |
catch {} | |
} | |
return $items | Sort-Object -Property @{Expression={$_.ID}} -Unique | |
} | |
function Get-OptionsForItems { | |
param( | |
[item[]]$Items, | |
[Item]$baseItem | |
) | |
$options = [ordered]@{} | |
foreach ($item in $Items) { | |
$display = $item.Paths.Path.ToString() | |
if ($baseItem.Database.Name -ne $item.Database.Name){ | |
$display = $item.Database.Name + ":" + $display | |
} | |
if ($options.Contains($display) -eq $false){ | |
$options.Add($display, $item.ID.ToString()) | |
} | |
} | |
return $options | |
} | |
function Filter-ByPathContains { | |
param( | |
[item[]]$Items, | |
[string]$path | |
) | |
$filtered = @() | |
$filtered += $Items | Where-Object -FilterScript { $_.Paths.Path -like $path } | |
return $filtered | |
} | |
function Get-OtherItems { | |
param( | |
[item[]]$coveredItems, | |
[item[]]$allItems | |
) | |
$others = @() | |
foreach ($i in $allItems) { | |
$filtered = $coveredItems | Where-Object -FilterScript { $_.ID -eq $i.ID } | |
if ($filtered -eq $null -or $filtered.Count -eq 0){ | |
$others += $i | |
} | |
} | |
return $others | |
} | |
function Get-ChildrenToInclude { | |
param( | |
[Item]$baseItem, | |
[string]$rootOption | |
) | |
$items = @() | |
if($rootOption -eq "RootAndDescendants"){ | |
$items += $baseItem | |
$items += $baseItem.Axes.GetDescendants() | |
} | |
elseif($rootOption -eq "RootAndChildren"){ | |
$items += $baseItem | |
$items += $baseItem.Children | |
} | |
elseif($rootOption -eq "DescendantsOnly"){ | |
$items += $baseItem.Axes.GetDescendants() | |
} | |
elseif($rootOption -eq "ChildrenOnly"){ | |
$items += $baseItem.Children | |
} | |
return $items | |
} | |
function Get-LinkedItems { | |
param( | |
[Item]$selectedItem, | |
[Item[]]$itemsToPack, | |
[int]$IncludeLinkOptions | |
) | |
$linkedItems = @() | |
# 1 - Only parent references are added to the package | |
if ($IncludeLinkOptions -eq 1){ | |
$parentItem = $itemsToPack | Where-Object -FilterScript { $_.ID -like $selectedItem.ID } | |
if ($parentItem){ | |
$linkedItems = Get-UniqueLinks $parentItem | |
} | |
} | |
# 2 - All references are added to the package | |
elseif ($IncludeLinkOptions -eq 2){ | |
foreach ($item in $itemsToPack) { | |
$linkedItems += Get-UniqueLinks $item | |
} | |
} | |
return $linkedItems | |
} | |
function Get-SelectedLinks { | |
param( | |
[Item[]]$linkedItems | |
) | |
$selectedLinks = @() | |
$parameters = @() | |
$linkedItems = $linkedItems|Sort-Object {$_.Paths.Path} | |
# Separate items by type | |
$covered = @() | |
$content = Filter-ByPathContains $linkedItems -Path "/sitecore/content/*" | |
if ($content -ne $null){ | |
$covered += $content | |
} | |
$media = Filter-ByPathContains $linkedItems -Path "/sitecore/media library/*" | |
if ($media -ne $null){ | |
$covered += $media | |
} | |
$layouts = Filter-ByPathContains $linkedItems -Path "/sitecore/layout/*" | |
if ($layouts -ne $null){ | |
$covered += $layouts | |
} | |
$templates = Filter-ByPathContains $linkedItems -Path "/sitecore/templates/*" | |
if ($templates -ne $null){ | |
$covered += $templates | |
} | |
$system = Filter-ByPathContains $linkedItems -Path "/sitecore/system/*" | |
if ($system -ne $null){ | |
$covered += $system | |
} | |
$others = Get-OtherItems $covered $linkedItems | |
foreach ($template in $templates) { | |
$templateChildren = $template.Axes.GetDescendants() | |
$templates += $templateChildren | |
$linkedItems += $templateChildren | |
} | |
$templates = $templates|Sort-Object {$_.Paths.Path} | |
$linkedItems = $linkedItems|Sort-Object {$_.Paths.Path} | |
# Build dialogs for each type | |
if ($content){ | |
$options = Get-OptionsForItems $content $selectedItem | |
$parameters += @{ Name = "IncludedContent"; Title = "Content"; Options = $options; Editor = "checkbox"; Tab="Content ("+$options.Count+")" } | |
} | |
if ($media){ | |
$options = Get-OptionsForItems $media $selectedItem | |
$parameters += @{ Name = "IncludedMedia"; Title = "Media"; Options = $options; Editor = "checkbox"; Tab="Media ("+$options.Count+")" } | |
} | |
if ($layouts){ | |
$options = Get-OptionsForItems $layouts $selectedItem | |
$parameters += @{ Name = "IncludedLayouts"; Title = "Layouts"; Options = $options; Editor = "checkbox"; Tab="Layouts ("+$options.Count+")" } | |
} | |
if ($system){ | |
$options = Get-OptionsForItems $system $selectedItem | |
$parameters += @{ Name = "IncludedSystem"; Title = "System"; Options = $options; Editor = "checkbox"; Tab="System ("+$options.Count+")" } | |
} | |
if ($templates){ | |
$options = Get-OptionsForItems $templates $selectedItem | |
$parameters += @{ Name = "IncludedTemplates"; Title = "Templates"; Options = $options; Editor = "checkbox"; Tab="Templates ("+$options.Count+")" } | |
} | |
if ($others){ | |
$options = Get-OptionsForItems $others $selectedItem | |
$parameters += @{ Name = "IncludedOthers"; Title = "Others"; Options = $options; Editor = "checkbox"; Tab="Others ("+$options.Count+")" } | |
} | |
$props = @{} + $defaultProps | |
$props["Title"] = "Download Tree as Package - Select Linked Items" | |
$props["Description"] = "Choose the Linked Items that you want to include in the package." | |
$props["Parameters"] = $parameters | |
$props["Width"] = 800 | |
$props["Height"] = 750 | |
$result = Read-Variable @props | |
Resolve-Error | |
if($result -ne "ok") { | |
Close-Window | |
Exit | |
} | |
$selectedLinks = $IncludedContent + $IncludedMedia + $IncludedLayouts + $IncludedSystem + $IncludedTemplates + $IncludedOthers | |
return $selectedLinks | |
} | |
$linkOptions = [ordered]@{ | |
"Do not include linked items" = 0 | |
"Include parent links only" = 1 | |
"Include all linked items" = 2 | |
} | |
$linkOptionsTooltips = [ordered]@{ | |
0 = "Linked items are not included in the package" | |
1 = "Only items linked to the parent will be included in the package" | |
2 = "All linked items will be included in the package" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /sitecore/system/Modules/PowerShell/Script Library/SPE/Tools/Package Generator/Content Editor/Context Menu/Packaging/Start New Package Advanced | |
Import-Function -Name Setup-PackageGeneratorAdvanced | |
$props = @{} + $defaultProps | |
$props["Title"] = "Starting New Package" | |
$props["Description"] = "A new package has been prepared for you." | |
$props["OkButtonName"] = "Add" | |
$props["CancelButtonName"] = "Skip" | |
$selectionOptions = [ordered]@{"Add Item to Package"=1;"Add Tree to Package"=2;} | |
$selectionOptionsTooltips = [ordered]@{ | |
"1" = "Choosing this option will prompt you with a dialog for adding a single item." | |
"2" = "Choosing this option will prompt you with a dialog for adding a tree of items." | |
} | |
$selectedOption = 1 | |
$parameters = @( | |
@{ Name = "selectedOption"; Title="Selected Item"; Hint = "If you would like to add the selected item or tree to the new package, choose an option below."; Options=$selectionOptions; OptionTooltips = $selectionOptionsTooltips; Editor="radio" } | |
) | |
$result = Read-Variable @props -Parameters $parameters | |
Resolve-Error | |
if($result -ne "ok") { | |
Close-Window | |
Exit | |
} | |
switch($selectedOption) { | |
1 { | |
Get-Item -Path "master:" -ID "{039A7DC6-F4A5-47FF-9E70-8603188A7AD5}" | Invoke-Script | |
} | |
2 { | |
Get-Item -Path "master:" -ID "{1DF1F3A5-BDAD-46BD-852F-1A078FC850CC}" | Invoke-Script | |
} | |
} | |
Close-Window |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment