ps/Modules/Cole.PowerShell.Developer/Public/Test-IsSourceFileVersionHigherThanTarget.ps1

95 lines
4.5 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
Function Test-IsSourceFileVersionHigherThanTarget {
<#
.SYNOPSIS
Given two file paths, and a common filename, determine which is the latter version
.EXAMPLE
$ProviderTargets = @('BankService','CoreService', 'NotificationService','SecurityManagementService','Radium','Nag','NagConfigurationService')
foreach($target in $ProviderTargets) {
$probePath = (Join-Path (Join-Path "C:\Orb\" $target) "bin")
if (!(Test-Path $probePath)) {
$probePath = Split-Path $probePath
}
$filesInProbe = @((Get-ChildItem -Path $probePath -Filter *.dll).Name)
foreach ($file in $filesInProbe) {
if (Test-IsSourceFileVersionHigherThanTarget $file $probePath "C:\Orb\Shared" $file) {
Write-Host "found $file in $probePath higher than shared"
}
}
}
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true,Position=0)]
[ValidateNotNullOrEmpty()]
[string]$filename,
[Parameter(Mandatory = $true,Position=1)]
[ValidateScript( {Test-Path (Resolve-Path $_)})]
[ValidateNotNullOrEmpty()]
[string]$sourceFolderPath,
[Parameter(Mandatory = $true,Position=2)]
[ValidateScript( {Test-Path (Resolve-Path $_)})]
[ValidateNotNullOrEmpty()]
[string]$targetFolderPath,
[Parameter(Mandatory = $true,Position=3)]
[ValidateNotNullOrEmpty()]
[string]$packageName
)
process {
$loglead = "File tester"
$sourceFile = (Join-Path $sourceFolderPath $filename);
$targetFile = (Join-Path $targetFolderPath $filename);
if ((Test-Path $sourceFile) -and (Test-Path $targetFile)) {
$stringVersionFromSourceFileVersion = (Get-Item $sourceFile).VersionInfo.FileVersion
$stringVersionFromTargetFileVersion = (Get-Item $targetFile).VersionInfo.FileVersion
## Both strings have values, so we can read something from that
if (![System.String]::IsNullOrWhiteSpace($stringVersionFromSourceFileVersion) -and ![System.String]::IsNullOrWhiteSpace($stringVersionFromTargetFileVersion)) {
## The version info FileVersion has a space and other content in it, so take what comes before the space.
$versionFromSourceFile = ([System.Version](((Get-Item $sourceFile).VersionInfo.FileVersion -split ' ')[0]))
$versionFromTargetFile = ([System.Version](((Get-Item $targetFile).VersionInfo.FileVersion -split ' ')[0]))
Write-Verbose "$loglead : $versionFromSourceFile : $sourceFile";
Write-Verbose "$loglead : $versionFromTargetFile : $targetFile";
## [System.Version] has .CompareTo so we can let it do the native comparison.
## -1 = source version is lower than target
## 0 = source version equals target
## 1 = source version is higher than the target
$result = $versionFromSourceFile.CompareTo($versionFromTargetFile) -gt 0;
## If we decided we are supposed to copy the file _and_ we match this condition of the target file matching a file that already exists
## We want to emit an error that the files are different and that we shouldn't be copying the files so we can investigate.
if (!$result -and ($targetFile -match $packageName) -and $versionFromSourceFile.CompareTo($versionFromTargetFile) -eq 0) {
if ((Get-FileHash $sourceFile).Hash -ne (Get-FileHash $targetFile).Hash) {
$message = "The files $sourceFile and $targetFile have the same version [$versionFromSourceFile] but the hashes don't match!";
Write-Error $message;
return $false
}
}
Write-Verbose "$loglead : Result was $result";
return $result;
} else {
Write-Warning "$loglead : Can not compare these two files because the version info is missing: $sourceFile [$stringVersionFromSourceFileVersion] - $targetFile [$stringVersionFromTargetFileVersion]"
}
} else {
if (($targetFile -match $packageName) -and (Test-Path $sourceFile) -and (Test-Path $targetFile)){
if ((Get-FileHash $sourceFile).Hash -ne (Get-FileHash $targetFile).Hash) {
$versionFromSourceFile = ([System.Version](((Get-Item $sourceFile).VersionInfo.FileVersion -split ' ')[0]))
$message = "The files $sourceFile and $targetFile have the same version [$versionFromSourceFile] but the hashes don't match!";
Write-Error $message;
}
}
$result = ((Test-Path $sourceFile) -and !(Test-Path $targetFile));
Write-Verbose "$loglead : Did the source exist and not the target? $result."
## Honestly, we don't care about files that only exist in the final folder and not shared
return $false;
}
}
}