Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Zen Garden Release validator
$registered = [ordered]@{};
function Get-AttributeValue($attribute, $default){
@{$true="$default";$false=$attribute}[[string]::IsNullOrEmpty($attribute)];
}
function Is-Excluded($item, $excluded){
if($excluded.ContainsKey($item.Paths.Path)){
Write-Host "Excluded item: $($item.ProviderPath)" -ForegroundColor DarkCyan;
return $true;
} else {
return $false;
}
}
function IsItemRegistered($item){
return $registered.Contains($item.ProviderPath);
}
function GetRegisteredObject($itemPath, $preset, $step, $state, $Database, $Drive){
$registeredItem = New-Object PSObject;
Add-Member -InputObject $registeredItem -Name Path -Value ($itemPath) -MemberType NoteProperty;
Add-Member -InputObject $registeredItem -Name Preset -Value $preset -MemberType NoteProperty;
Add-Member -InputObject $registeredItem -Name Step -Value $step -MemberType NoteProperty;
Add-Member -InputObject $registeredItem -Name State -Value $state -MemberType NoteProperty;
Add-Member -InputObject $registeredItem -Name Duplicate -Value $false -MemberType NoteProperty;
Add-Member -InputObject $registeredItem -Name Database -Value $Database -MemberType NoteProperty;
Add-Member -InputObject $registeredItem -Name Drive -Value $Drive -MemberType NoteProperty;
$registeredItem;
}
function RegisterItem($item, $preset, $step){
if($registered.Contains($item.ProviderPath)){
RegisterDuplicate $item $preset $step, $true, $false;
} else {
#Write-Host "Registering Item $($item.ProviderPath)" -fore Gray
$registered.Add($item.ProviderPath,(GetRegisteredObject $item.ProviderPath $preset $step "Item" $true $false));
}
}
function RegisterItemTree($item, $preset, $step){
#Write-Host "Registering Tree $($item.ProviderPath)" -fore DarkGreen;
RegisterItem $item $preset $step;
$registration = $registered[$item.ProviderPath];
$registration.State = "Tree";
Get-ChildItem -recurse $item.ProviderPath | %{ RegisterItem $_ $preset $step };
}
function RegisterDuplicate($item, $preset, $step){
Write-Host "Duplicated item skipped $($item.ProviderPath) at step $step" -ForegroundColor Yellow;
$registeredEntry = $registered[$item.ProviderPath];
$registeredEntry.Preset += "$($registeredEntry.Preset), $preset";
$registeredEntry.Duplicate = $true;
}
function RegisterMissingItem($itemPath, $preset, $step){
$itemPath = $itemPath -replace "/", "\";
if(-not $registered.Contains($itemPath)){
$registered.Add($itemPath,(GetRegisteredObject $itemPath $preset $step "Missing" $false $false));
}
else
{
$registeredEntry = $registered[$itemPath];
$registeredEntry.Preset += "$($registeredEntry.Preset), $preset";
}
Write-Host "Cannot find item $itemPath at step $step" -ForegroundColor Red;
}
function GetItemOrNull($itemPath){
if(Test-Path $itemPath){
Get-Item $itemPath;
} else {
$null;
}
}
function ValidateSerializedFileHandled($filePath)
{
$streamReader = New-Object System.IO.StreamReader($filePath);
$tokenizer = New-Object Sitecore.Data.Serialization.ObjectModel.Tokenizer($streamReader);
$item = [Sitecore.Data.Serialization.ObjectModel.SyncItem]::ReadItem($tokenizer);
$sitecoreName = $item.DatabaseName + ":"+$item.ItemPath.Substring(9) -replace "/","\";
$streamReader.Close();
if($registered.Contains($sitecoreName)){
$preset = $registered[$sitecoreName];
Write-Host "$sitecoreName found in preset $($preset.Preset)" -ForegroundColor Green;
} else {
$DatabaseExists = Test-Path $sitecoreName;
$registered.Add($sitecoreName,(GetRegisteredObject $sitecoreName "" "File Analysis" "Item" $DatabaseExists $true));
Write-Host "$sitecoreName not found in any preset" -ForegroundColor Magenta;
}
}
$clock = New-Object -TypeName System.Diagnostics.Stopwatch;
$clock.Reset();
$clock.Start();
# Get all the presets from configuration (look for Cognifide.XXX.Serialization.config files)
$serializationPresets = [Sitecore.Configuration.Factory]::GetConfigNodes("serialization");
$presets = $serializationPresets.ChildNodes #| ?{ $_.Name -eq "Search" -or $_.Name -eq "ZenGarden"};
$currentPresetNumber = 0;
foreach($preset in $presets) {
Write-Host "Processing preset: [$($preset.Name)] $($preset.label)"
# Singles
$preset.single |? { $_.path } |% {
$releaseMode = Get-AttributeValue $_.releaseMode " ";
$itemPath = "$($_.database):$($_.path)";
$singleItem = GetItemOrNull $itemPath;
if($singleItem -ne $null -and -not (IsItemRegistered $singleItem) -and $releaseMode -ne "Skip"){
RegisterItem $singleItem $preset.Name "1";
} else {
if($singleItem -eq $null){
RegisterMissingItem $itemPath $preset.Name "2";
} else {
RegisterDuplicate $singleItem $preset.Name "3";
}
}
}
# Branches
$preset.include |? { $_.path } |% {
$releaseMode = Get-AttributeValue $_.releaseMode " ";
$exactItem = $_;
$presetDB = $_.database;
if ($_.exclude -or $_.skip) {
$excluded = @{};
$excludedPaths = $_.exclude |% {
if($_.path -ne $null){
$excluded.Add($_.path,$_.path);
}
}
$items = @();
$queue = New-Object -TypeName System.Collections.Queue;
$rootItemPath = "$presetDB`:$($exactItem.path)";
$rootItem = GetItemOrNull $rootItemPath;
if($rootItem -eq $null){
RegisterMissingItem $rootItemPath $preset.Name;
} else {
$queue.Enqueue($rootItem);
}
while($queue.Count -gt 0){
$current = $queue.Dequeue()
$rootItemPath = "$presetDB`:$($current.Paths.Path)"
if(-not (Test-Path $rootItemPath)){
RegisterMissingItem $rootItemPath $preset.Name "x2"
#continue;
}
$children = Get-ChildItem -Path $rootItemPath;
foreach($child in $children){
if(-not (Is-Excluded $child $excluded)){
if(-not (IsItemRegistered $child) -and $releaseMode -ne "Skip"){
if($_.skip.path -ne $child.Paths.Path){
RegisterItem $child $preset.Name "4";
} else {
Write-Host "Skipping $($child.Paths.Path) because $($_.skip.path)" -fore Magenta;
}
}
$queue.Enqueue($child);
}
}
}
} else {
$itemPath = "$($exactItem.database):$($exactItem.path)";
$item = GetItemOrNull $itemPath;
if($item -ne $null -and -not (IsItemRegistered $item) -and $releaseMode -ne "Skip"){
RegisterItemTree $item $preset.Name "8"
} else {
RegisterMissingItem $itemPath $preset.Name "9"
}
}
}
Write-Progress -Activity "Analysing presets." `
-Status "Analysed $currentPresetNumber out of $($presets.Count) - $([Math]::Round($currentPresetNumber / $presets.Count * 100))% completed" `
-PercentComplete ($currentPresetNumber / $presets.Count * 100) `
-CurrentOperation "Preset: $($preset.Name)";
$currentPresetNumber = $currentPresetNumber + 1;
Start-Sleep -m 256;
}
Write-Host "Time analysing presets: "$clock.ElapsedMilliseconds "ms";
$clock.Restart();
$serializedItems = Get-ChildItem $SitecoreSerializationFolder -Recurse -File #| Select-Object -Last 500
$currentFileNumber = 0;
$pathLength = $SitecoreSerializationFolder.Length + 1
$serializedItems.FullName | %{
$currentFileNumber++
ValidateSerializedFileHandled $_
Write-Progress -Activity "Analysing serialized items." `
-Status "Analysed $currentFileNumber out of $($serializedItems.Count) - $([Math]::Round($currentFileNumber * 100 / $serializedItems.Count))% completed" `
-PercentComplete ($currentFileNumber / $serializedItems.Count * 100) `
-CurrentOperation "File: $($_.Substring($pathLength))"
}
Write-Host "Time analysing serialized items: "$clock.ElapsedMilliseconds "ms"
$registered.Values | ft *
$registered.Values | Show-ListView -Title "Items" -Property Path, Preset, Step, State, Duplicate, Database, Drive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment