Last active
August 29, 2015 14:25
-
-
Save techphoria414/c2dfb9653f9d75187101 to your computer and use it in GitHub Desktop.
Script to take a Sitecore Package XML definition and output a Sitecore Powershell Extensions script to replace it.
This file contains 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
Param ( | |
[Parameter(Mandatory=$True)] | |
[string]$PackageXml = $null, | |
[Parameter(Mandatory=$True)] | |
[string]$SitecoreUri = $null | |
) | |
Import-Module -Name SPE | |
set-alias out Write-Output | |
# | |
# Script to take a Sitecore Package XML definition and output a Sitecore Powershell Extensions script to replace it. | |
# | |
# Note 1: Requires SPE remoting module present in order to resolve item IDs to paths. We do this to make sources more self-documenting. | |
# Note 2: Does not account for packaging of specific item versions in package definition, or any filters in dynamic item sources. | |
# | |
Function ParseExplicitItem($explicitItem) | |
{ | |
#e.g. /master/sitecore/commerce/promos/{7939C467-8550-4829-B6DA-B877891A2560}/invariant/0 | |
$parts = $explicitItem.Split("/") | |
$path = $parts[1] + ":" | |
for ($i = 2; $i -lt $parts.Count; $i++) | |
{ | |
if ($parts[$i].StartsWith("{")) | |
{ | |
break | |
} | |
$path = "$path\$($parts[$i])" | |
} | |
return $path | |
} | |
Function GetItemPath($session, $database, $guid) | |
{ | |
$args = @{ | |
database = $database | |
guid = $guid | |
} | |
$path = Invoke-RemoteScript -Session $session -ArgumentList $args -ScriptBlock { | |
$database = [Sitecore.Data.Database]::GetDatabase($params.database) | |
$item = $database.GetItem($params.guid) | |
$item.Paths.FullPath | |
} | |
$path = $path.Replace("/", "\") | |
return "$($database):$path" | |
} | |
$session = New-ScriptSession -Username 'admin' -Password 'b' -ConnectionUri $SitecoreUri | |
[xml]$packageXml = gc $PackageXml | |
out @" | |
## | |
## ***Package Metadata*** | |
## | |
"@ | |
$metadata = $packageXml.project.Metadata.metadata | |
out "`$package = New-Package '$($metadata.PackageName)'" | |
out "`$package.Metadata.Author = '$($metadata.Author)'" | |
out "`$package.Metadata.Version = '$($metadata.Version)'" | |
out "`$package.Metadata.Revision = '$($metadata.Revision)'" | |
out "`$package.Metadata.License = '$($metadata.License)'" | |
out "`$package.Metadata.Comment = '$($metadata.Comment)'" | |
out "`$package.Metadata.Readme = '$($metadata.Readme)'" | |
out "`$package.Metadata.Publisher = '$($metadata.Publisher)'" | |
out "`$package.Metadata.Attributes = '$($metadata.Attributes)'" | |
out "`$package.Metadata.PostStep = '$($metadata.PostStep)'" | |
out '' | |
$items = @() | |
$files = @() | |
# Dynamic item sources | |
foreach ($source in $packageXml.project.Sources.items) | |
{ | |
$path = GetItemPath -session $session -database $source.Database -guid $source.Root | |
$options = $source.Converter.ItemToEntryConverter.Transforms.InstallerConfigurationTransform.Options.BehaviourOptions | |
$installMode = if ($options.ItemMode) { $options.ItemMode } else { "Merge" } | |
$mergeMode = if ($options.ItemMergeMode) { $options.ItemMergeMode } else { "Merge" } | |
$source = New-Object PSObject -Property @{ | |
Name = $source.Name | |
Script = "`$source = Get-Item '$path' | New-ItemSource -Name '$($source.Name)' -InstallMode $installMode -MergeMode $mergeMode" | |
} | |
$items += $source | |
} | |
# Explicit item sources | |
foreach ($source in $packageXml.project.Sources.xitems) | |
{ | |
$options = $source.Converter.ItemToEntryConverter.Transforms.InstallerConfigurationTransform.Options.BehaviourOptions | |
$installMode = if ($options.ItemMode) { $options.ItemMode } else { "Merge" } | |
$mergeMode = if ($options.ItemMergeMode) { $options.ItemMergeMode } else { "Merge" } | |
$xitems = @() | |
foreach ($entry in $source.Entries.'x-item') | |
{ | |
$path = ParseExplicitItem -explicitItem $entry | |
$xitems += "'$path'" | |
} | |
$script = "`$xitems = @(`n`t$([string]::Join(",`n`t",$xitems))`n)`n" | |
$script += "`$source = `$xitems | Get-Item | New-ExplicitItemSource -Name '$($source.Name)' -InstallMode $installMode -MergeMode $mergeMode" | |
$source = New-Object PSObject -Property @{ | |
Name = $source.Name | |
Script = $script | |
} | |
$items += $source | |
} | |
# Dynamic file sources | |
foreach ($source in $packageXml.project.Sources.files) | |
{ | |
$options = $source.Converter.FileToEntryConverter.Transforms.InstallerConfigurationTransform.Options.BehaviourOptions | |
$installMode = if ($options.ItemMode -and -not ($options.ItemMode = 'Merge')) { $options.ItemMode } else { "Overwrite" } | |
$path = '$AppPath' + $source.Root.Replace("/", "\") | |
$source = New-Object PSObject -Property @{ | |
Name = $source.Name | |
Script = "`$source = New-FileSource -Name '$($source.Name)' -Root `"$path`" -InstallMode $installMode" | |
} | |
$files += $source | |
} | |
# Explicit file sources | |
foreach ($source in $packageXml.project.Sources.xfiles) | |
{ | |
$options = $source.Converter.FileToEntryConverter.Transforms.InstallerConfigurationTransform.Options.BehaviourOptions | |
$installMode = if ($options.ItemMode -and -not ($options.ItemMode = 'Merge')) { $options.ItemMode } else { "Overwrite" } | |
$xitems = @() | |
foreach ($entry in $source.Entries.'x-item') | |
{ | |
$path = '$AppPath' + $entry.Replace("/", "\") | |
$xitems += "`"$path`"" | |
} | |
$script = "`$xitems = @(`n`t$([string]::Join(",`n`t",$xitems))`n)`n" | |
$script += "`$source = `$xitems | Get-Item | New-ExplicitFileSource -Name '$($source.Name)' -InstallMode $installMode" | |
$source = New-Object PSObject -Property @{ | |
Name = $source.Name | |
Script = $script | |
} | |
$files += $source | |
} | |
$items = $items | sort Name | |
$files = $files | sort Name | |
if ($items) | |
{ | |
out @" | |
## | |
## ***Item Sources*** | |
## | |
"@ | |
} | |
foreach ($source in $items) | |
{ | |
$commentName = $source.Name.ToUpper() | |
out @" | |
# | |
# $commentName | |
# | |
"@ | |
out $source.Script | |
out "`$package.Sources.Add(`$source)" | |
out '' | |
} | |
if ($files) | |
{ | |
out @" | |
## | |
## ***File Sources*** | |
## | |
"@ | |
} | |
foreach ($source in $files) | |
{ | |
$commentName = $source.Name.ToUpper() | |
out @" | |
# | |
# $commentName | |
# | |
"@ | |
out $source.Script | |
out "`$package.Sources.Add(`$source)" | |
out '' | |
} | |
out @" | |
## | |
## ***Generate Package*** | |
## | |
"@ | |
out '$packageName = "$($package.Name)-$($package.Metadata.Version).zip"' | |
out 'Export-Package -Project $package -Path $packageName -Zip' | |
out '"$SitecorePackageFolder\$packageName"' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment