Skip to content

Instantly share code, notes, and snippets.

@fluxdigital
Last active October 29, 2023 19:40
Show Gist options
  • Save fluxdigital/5e70a7694e2e01863692df045d1bbf2c to your computer and use it in GitHub Desktop.
Save fluxdigital/5e70a7694e2e01863692df045d1bbf2c to your computer and use it in GitHub Desktop.
Creates an Sitecore CLI JSON file from an Sitecore Package File
#*** converts an Publish.xml file to the new Sitecore CLI Json format ***
#helper functions
function CheckNamePart($namePart){
$namePart = $namePart.Replace(' ','')
$namePart = $namePart.Replace('{','')
$namePart = $namePart.Replace('}','')
return $namePart
}
function TruncatePath($path){
$path = $path.Replace('//master','')
$path = $path.Replace('//core','')
$path = $path.Replace('//web','')
return $path
}
function SplitPath($path){
$itemPath = $path.split("/")
return $itemPath
}
function GetNameFromPath($path){
$itemPath = $path.split("/")
return $itemPath[$itemPath.Count-1]
}
function GetItemPath($item){
$paths = SplitPath($item)
$pathCount = 0
$pathString = ""
foreach ($path in $paths){
$pathCount +=1
#find where the path ends and return it
if($path -like "{*"){
#write-host "path: $($pathString)" -ForegroundColor Green
return $pathString
}
$pathString = $pathString + "/" + "$($path)"
}
}
#variables - UPDATE THESE 4 AS REQUIRED
$inputPackageFilePath = "C:\Projects\SCScheduledPublishing\Packages\Sitecore Scheduled Publish.xml"
$outputJsonFilePath = "C:\Projects\SCScheduledPublishing\Sitecore.Schedule.Publish-module.json"
$namespace = "Sitecore.Schedule.Publish"
$description = "Sitecore.Schedule.Publish Module Items"
$uniqueNames = @()
$count = 0
$processedCount = 0
#create Sitecore CLI Json file structure
$json = [pscustomobject]@{
namespace = "$($namespace)"
description = "$($description)"
items = [PSCustomObject]@{ includes = [System.Collections.ArrayList]@() }
}
## cast publish file to an XML object
[xml]$xmlAttr = Get-Content -Path "$($inputPackageFilePath)"
write-host "loading xml file: $($inputPackageFilePath)" -ForegroundColor cyan
write-host "processing items..." -ForegroundColor Yellow
## looping through all items in package
$xmlAttr."project"."Sources"."xitems"."Entries"."x-item" | ForEach-Object {
$count +=1
$truncatedPath = TruncatePath(GetItemPath($_))
$itemPath = SplitPath($_)
$itemName = GetNameFromPath -path $truncatedPath
#create item in format required for Json file
$item = [pscustomobject]@{
path = $truncatedPath
name = $itemName
scope = "SingleItem"
database = $itemPath[1]
allowedpushoperations = "CreateUpdateAndDelete"
}
write-host "item name: $($item.Name) -> item path: $($item.Path)" -ForegroundColor White
$json.items.includes.Add($item) | Out-Null
}
write-host $("processed: $count items") -ForegroundColor yellow
$jsonFile = ConvertTo-Json -InputObject $json -Depth 5
$jsonFile | Set-Content -Path "$($outputJsonFilePath)"
write-host "Json file saved: $($outputJsonFilePath)" -ForegroundColor cyan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment