Skip to content

Commit

Permalink
(chocolatey-communityGH-30) Move code for extracting meta data to its…
Browse files Browse the repository at this point in the history
… own function
  • Loading branch information
AdmiringWorm committed Oct 13, 2020
1 parent 0655430 commit cebbf8b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Tests/Private/Get-PackageBinaryData.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#Requires -Modules chocolatey-diff

Describe "Get-PackageBinaryData tests" {
}
44 changes: 44 additions & 0 deletions chocolatey-diff/Private/Get-PackageBinaryData.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function Get-PackageBinaryData {
<#
.SYNOPSIS
The meta information about binaries included in package
.DESCRIPTION
The meta information about binaries included in the package, including
the relative path, the checksum of the file, the embedded file and product
version as well as the embedded company name.
.OUTPUTS
An array of hashtables with the basic information about each binary file.
.EXAMPLE
PS > Get-PackageBinaryData -packageDirectory path\to\package\dir
#>
param (
[Parameter(Mandatory = $true)]
[string]$packageDirectory,
[ValidateSet('sha1', 'sha256', 'sha512')]
[string]$hashAlgorithm = 'sha256'
)

Push-Location $packageDirectory | Out-Null

try {
Write-Verbose "Finding binary files in '$packageDirectory..."
$binaryFiles = Get-ChildItem -Path $packageDirectory -Recurse -File | ? { Test-IsBinary -Path $_.FullName }

foreach ($file in $binaryFiles) {
Write-Verbose "Aquiring binary data from $($file.Name)..."

$item = Get-Item $file.FullName
$result = @{
path = Resolve-Path $file.FullName -Relative
checksum = Get-FileHash $file.FullName -Algorithm $hashAlgorithm | % Hash
fileVersion = $item.VersionInfo.FileVersion
productVersion = $item.VersionInfo.ProductVersion
CompanyName = $item.VersionInfo.CompanyName
}
$result
}
} finally {

Pop-Location | Out-Null
}
}
18 changes: 3 additions & 15 deletions chocolatey-diff/Public/Get-ChocolateyPackageDiff.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,16 @@
Expand-Archive -Path $newFileName -DestinationPath $newExtractPath -Force

foreach ($path in @($oldExtractPath; $newExtractPath)) {
# Only because of unit tests
# Temporary workaround until mocking and unit tests
# are figured out.
if (!(Test-Path $path)) { continue; }

$metaDestination = Join-Path $path "binary-data.txt"
Push-Location -Path $path
if (Test-Path $metaDestination) {
Remove-Item $metaDestination
}

Get-ChildItem -Path $path -Recurse -File | ? { Test-IsBinary -Path $_.FullName } | % {
$item = Get-Item $_.FullName
$result = @{
path = Resolve-Path $_.FullName -Relative
checksum = Get-FileHash $_.FullName -Algorithm SHA256 | % Hash
fileVersion = $item.VersionInfo.FileVersion
productVersion = $item.VersionInfo.ProductVersion
}

$result
} | Out-File -Encoding utf8 -FilePath $metaDestination

Pop-Location
Get-PackageBinaryData -packageDirectory $path | Out-File -Encoding utf8 -FilePath $metaDestination
}

if ($compareFolder) {
Expand Down

0 comments on commit cebbf8b

Please sign in to comment.