gaelcolas / DscBuildHelpers

10 stars 7 forks source link

Compress-DscResourceModule adding errant newline in checksum file #6

Open liamkirwan opened 5 years ago

liamkirwan commented 5 years ago

Hello, just including some more colour on this as I think it explains the issue I've been seeing. The file encoding of the checksum file is always UTF-8, so that doesn't seem to matter. But, checksum files with this extra newline refuse to match the SHA-256 of the zip file even when it's correct.

I think the underlying cause is the Get-FileHash | Set-Content - is there a reason not to use New-DscChecksum here? If not, I also found that -NoNewLine switch on Set-Content removed the issue.

PS C:\dev\test\mw.dsc.rootconfig> New-DscChecksum .\BuildOutput\MOF\09aa98b1-c417-4171-b096-da342a0d99dc.mof
PS C:\dev\test\mw.dsc.rootconfig> (gc .\BuildOutput\MOF\09aa98b1-c417-4171-b096-da342a0d99dc.mof.checksum -raw) -like "*`n"
False
PS C:\dev\test\mw.dsc.rootconfig> rm .\BuildOutput\MOF\09aa98b1-c417-4171-b096-da342a0d99dc.mof.checksum
PS C:\dev\test\mw.dsc.rootconfig> (Get-FileHash .\BuildOutput\MOF\09aa98b1-c417-4171-b096-da342a0d99dc.mof).Hash | Set-Content .\BuildOutput\MOF\09aa98b1-c417-4171-b096-da342a0d99dc.mof.checksum
PS C:\dev\test\mw.dsc.rootconfig> (gc .\BuildOutput\MOF\09aa98b1-c417-4171-b096-da342a0d99dc.mof.checksum -raw) -like "*`n"
True
PS C:\dev\test\mw.dsc.rootconfig> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17763.592
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.592
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

PS C:\dev\test\mw.dsc.rootconfig> gcm Compress-DscResourceModule

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Compress-DscResourceModule                         0.0.40     DscBuildHelpers

PS C:\dev\test\mw.dsc.rootconfig> gcm Set-Content

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Set-Content                                        3.1.0.0    Microsoft.PowerShell.Management

PS C:\dev\test\mw.dsc.rootconfig> gcm Get-FileHash

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Get-FileHash                                       3.1.0.0    Microsoft.PowerShell.Utility
raandree commented 5 years ago

This is why I skipped using the DscBuildHelpers and used the following code. In my case the function 'Compress-DscResourceModule' never worked.

task CompressModulesWithChecksum {
    if (-not (Test-Path -Path $BuildOutput\CompressedModules)) {
        mkdir -Path $BuildOutput\CompressedModules | Out-Null
    }
    $modules = Get-ModuleFromFolder -ModuleFolder "$ProjectPath\DscResources\"
    $compressedModulesPath = "$BuildOutput\CompressedModules"

    foreach ($module in $modules) {
        $destinationPath = Join-Path -Path $compressedModulesPath -ChildPath "$($module.Name)_$($module.Version).zip"
        Compress-Archive -Path "$($module.ModuleBase)\*" -DestinationPath $destinationPath
        $hash = (Get-FileHash -Path $destinationPath).Hash

        try {
            $stream = New-Object -TypeName System.IO.StreamWriter("$destinationPath.checksum", $false)
            [void] $stream.Write($hash)
        }
        finally {
            if ($stream) {
                $stream.Close()
            }
        }
    }
}
gaelcolas commented 5 years ago

And you couldn't do a PR :)

raandree commented 5 years ago

Will do later. The quick way was just to replace the function call :)