Skip to content

Instantly share code, notes, and snippets.

@MartinMiles
Created February 16, 2023 20:26
Show Gist options
  • Save MartinMiles/f0b1ceaa00640a15765fceb41852ca16 to your computer and use it in GitHub Desktop.
Save MartinMiles/f0b1ceaa00640a15765fceb41852ca16 to your computer and use it in GitHub Desktop.
Converts Unicorn configurations into Sitecore CLI serialization (to be reworked to cover more configurations, use and an example)
$rootFolder = "C:\Projects\Upgrade\SC\src"
$files = Get-ChildItem -Filter *serialization.config -Path $rootFolder -Recurse
foreach($file in $files)
{
$newSerializationJson = New-Object -TypeName pscustomobject
Write-Host "Convert Started: " + $file.FullName
$Document = (Select-Xml -Path $file.FullName -XPath / ).Node
$configurationNode = $Document.configuration.sitecore.unicorn.configurations.SelectSingleNode('configuration')
$name = $configurationNode.name
$dependencies = $configurationNode.dependencies -split ","
$newSerializationJson | Add-Member -MemberType NoteProperty -Name namespace -Value $name
$newSerializationJson | Add-Member -MemberType NoteProperty -Name references -Value $dependencies
$newPredicateList = New-Object System.Collections.ArrayList
foreach($predicate in $Document.SelectNodes("//include"))
{
if([bool]$predicate.path)
{
$newPredicate = New-Object -TypeName pscustomobject
$newPredicate | Add-Member -MemberType NoteProperty -Name name -Value $predicate.name
$newPredicate | Add-Member -MemberType NoteProperty -Name path -Value $predicate.path
$newPredicate | Add-Member -MemberType NoteProperty -Name database -Value $predicate.database
$excludeNode = $predicate.SelectSingleNode('exclude')
if($excludeNode.children -eq "true")
{
$newPredicate | Add-Member -MemberType NoteProperty -Name scope -Value "SingleItem"
}
$newPredicateList.Add($newPredicate)
}
}
$items = New-Object -TypeName pscustomobject
$items | Add-Member -MemberType NoteProperty -Name includes -Value $newPredicateList
$newSerializationJson | Add-Member -MemberType NoteProperty -Name items -Value $items
$targatPath = Split-Path -Parent $file.FullName
$targetName = $targatPath + "\" + ($file.Name -replace "Serialization.config","module.json")
$newSerializationJson | ConvertTo-Json -Depth 5 | Out-File $targetName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment