Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test-SitecoreAssemblyList.ps1
<#
.SYNOPSIS
Validates a Sitecore "Assembly list" (from dev.sitecore.net) against a folder
.PARAMETER AssemblyList
The assembly list text file downloaded from dev.sitecore.net
.PARAMETER AssemblyFolder
The folder containing the Sitecore assemblies to validate
.PARAMETER CheckFileVersion
If set, also verifies the file version. Requires loading the assembly, so should be invoked in an isolated powershell environment
.NOTES
Author : Richard Szalay (richard@richardszalay.com)
Copyright 2017 - Richard Szalay
.EXAMPLE
Test-SitecoreAssemblyList.ps1 -AssemblyList ".\Sitecore.Platform.Assemblies 9.0.0 rev. 171002.txt" -AssemblyFolder ".\bin"
#>
param(
[string]$AssemblyList,
[string]$AssemblyFolder,
[switch]$CheckFileVersion
)
function ParseAssemblyList($file)
{
$lines = Get-Content $file
$lines | Select-Object -Skip 1 | Foreach-Object {
$parts = $_ -split ","
@{
Assembly = ($parts[0]);
FileVersion = $parts[1];
AssemblyVersion = $parts[2]
}
}
}
function NormalizeVersion([string]$versionString) {
$normalizedVerson = ($versionString.Split('.') | Select -First 4) -join "."
while (([version]$normalizedVerson).Revision -eq -1) { # yes, this is dubious
$normalizedVerson = "$normalizedVerson.0"
}
$normalizedVerson
}
function GetAsemblyInfo([string]$assemblyFile) {
if ($CheckFileVersion)
{
$assembly = [System.Reflection.Assembly]::ReflectionOnlyLoadFrom($assemblyFile)
$assemblyVersion = $assembly.GetName().Version.ToString()
$fileVersion = $assembly.CustomAttributes | `
Where-Object { $_.AttributeType.FullName -eq "System.Reflection.AssemblyFileVersionAttribute" } | `
ForEach-Object {
$rawVersion = $_.ConstructorArguments[0].Value
$normalizedVerson = NormalizeVersion $rawVersion
$v = ([version]$normalizedVerson)
"$($v.Major).$($v.Minor).$($v.Build).$($v.Revision)"
}
@{
Assembly = $assemblyFile;
AssemblyVersion = $assemblyVersion;
FileVersion = $fileVersion
}
}
else
{
$assemblyVersion = [System.Reflection.AssemblyName]::GetAssemblyName($assemblyFile).Version.ToString()
@{
Assembly = $assemblyFile;
AssemblyVersion = $assemblyVersion;
}
}
}
function TestAssembly($definition, $folder)
{
$assemblyFile = Join-Path $folder $definition.Assembly
if (-not (Test-Path $assemblyFile))
{
Write-Warning "Assembly $($definition.Assembly) not found"
return $false
}
$assembly = GetAsemblyInfo $assemblyFile
if ($definition.AssemblyVersion -ne $assembly.AssemblyVersion)
{
Write-Warning "Assembly $($definition.Assembly) version mismatch. Expected $($definition.AssemblyVersion) but found $($assembly.AssemblyVersion)"
return $false
}
if ($CheckFileVersion -and $assembly.FileVersion -and $assembly.FileVersion -ne $definition.FileVersion)
{
Write-Warning "Assembly $($definition.Assembly) file version mismatch. Expected $($definition.FileVersion) but found $($assembly.FileVersion)"
return $false
}
return $true
}
$assemblyDefinitions = ParseAssemblyList $AssemblyList
$passedValidations = ($assemblyDefinitions | Where-Object { TestAssembly $_ $AssemblyFolder })
Write-Information "Total $($assemblyDefinitions.Length). Failed $($assemblyDefinitions.Length - $passedValidations.Length) " -InformationAction Continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment