Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(GH-616) Capture 7z's stdout in Get-ChocolateyUnzip #617

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 34 additions & 22 deletions src/chocolatey.resources/helpers/functions/Get-ChocolateyUnzip.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,32 +91,44 @@ param(
$destination32 = $destination
}

$exitCode = -1
$unzipOps = {
param($7zip, $destination, $fileFullPath, [ref]$exitCodeRef)
$params = "x -aoa -o`"$destination`" -y `"$fileFullPath`""
Write-Debug "Executing command ['$7zip' $params]"
$process = New-Object System.Diagnostics.Process
$process.StartInfo = new-object System.Diagnostics.ProcessStartInfo($7zip, $params)
$process.StartInfo.UseShellExecute = $false
$process.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden

# Make structures used by .NET for starting the 7z process

# 7z command line
$params = "x -aoa -o`"$destination`" -y `"$fileFullPath`""
$process = New-Object System.Diagnostics.Process
$process.StartInfo = new-object System.Diagnostics.ProcessStartInfo($7zip, $params)
# Required for stdout redirect
$process.StartInfo.UseShellExecute = $false
$process.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden

Write-Debug "Executing command ['$7zip' $params]"
if ($zipExtractLogFullPath) {
# Redirect stdout for processing by choco
$process.StartInfo.RedirectStandardOutput = $true
$process.Start() | Out-Null
$process.WaitForExit()
$processExitCode = $process.ExitCode
$process.Dispose()
Write-Debug "Command ['$7zip' $params] exited with `'$processExitCode`'."

$exitCodeRef.Value = $processExitCode
}

if ($zipExtractLogFullPath) {
Write-Debug "wrapping 7za invocation with Write-FileUpdateLog"
Write-FileUpdateLog -logFilePath $zipExtractLogFullPath -locationToMonitor $destination -scriptToRun $unzipOps -argumentList $7zip,$destination32,$fileFullPath32,([ref]$exitCode)
# Read each line from 7z's stdout synchroneously (ReadLine blocks).
# Since stderr is not redirected, it gets automatically printed to the console, avoiding deadlocks.
while(($process.StandardOutput -ne $null) -and (($line = $process.StandardOutput.ReadLine()) -ne $null)) {
if($line.StartsWith("Extracting")) {
# This is a line indicating an extracted file
$file = $destination + "\" + $line.Substring(12)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may fix #156 as well since we won't be using it anymore. 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this 12?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, the two spaces after "Extracting".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 7z's output in the form "Extracting path\of\file\in\zip", the file path begins at character 12.

# Save the filename
Add-Content $zipExtractLogFullPath $file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to hold the output in a variable and write the file once instead at the end.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be cases where the file doesn't exist as well. So it may be best not to write anything if the variable is not empty.

}
# Print the line, such that it looks as if stdout was not redirected
Write-Debug $line
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you move this to Write-Verbose? This will help with #476

}
} else {
Write-Debug "calling 7za directly"
Invoke-Command $unzipOps -ArgumentList $7zip,$destination32,$fileFullPath32,([ref]$exitCode)
# If we don't want to capture the file list, just execute 7z without stdout redirection
$process.Start() | Out-Null
}

# Wait for 7z to finish. Even if 7z has closed its stdout, and all lines have been read, the process might not have quit yet.
$process.WaitForExit()
$exitCode = $process.ExitCode
$process.Dispose()
Write-Debug "Command ['$7zip' $params] exited with `'$exitCode`'."

Write-Debug "7za exit code: $exitCode"
switch ($exitCode) {
Expand Down