Skip to content

Commit

Permalink
Merge branch 'stable'
Browse files Browse the repository at this point in the history
* stable:
  (GH-526) Fix: The handle is invalid
  (GH-532) Fix - Temp uses 8.3 Path
  (maint) update binroot deprecation
  • Loading branch information
ferventcoder committed Jan 11, 2016
2 parents ed8a2a6 + 4cea3e4 commit 0328603
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/chocolatey.resources/helpers/functions/Get-BinRoot.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
function Get-BinRoot {

Write-Debug "Running 'Get-BinRoot'";
Write-Host "Get-BinRoot is going to be deprecated by v1. Many packages no longer require it since the folders no longer have versions on them."
Write-Host "Get-BinRoot is going to be deprecated in v1.0.0 and removed in v2.0.0 (replacing with a new function that will be announced nearing that time). However many packages no longer require Get-BinRoot since the folders no longer have versions on them. Some do though and should continue to use it."

# Since CamelCase was decided upon when $env:ChocolateyInstall was first invented, whe should stick to this convention and use $env:ChocolateyBinRoot.
# I propose:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# limitations under the License.

function Start-ChocolateyProcessAsAdmin {
param(
[string] $statements,
Expand All @@ -23,6 +23,8 @@ param(
Write-Debug "Running 'Start-ChocolateyProcessAsAdmin' with exeToRun:`'$exeToRun`', statements: `'$statements`' ";

$wrappedStatements = $statements
if ($wrappedStatements -eq $null) { $wrappedStatements = ''}

if ($exeToRun -eq 'powershell') {
$exeToRun = "$($env:windir)\System32\WindowsPowerShell\v1.0\powershell.exe"
$importChocolateyHelpers = ""
Expand All @@ -48,49 +50,83 @@ $block
This may take a while, depending on the statements.
"@
}
else {
else
{
$dbgMessage = @"
Elevating Permissions and running $exeToRun $wrappedStatements. This may take a while, depending on the statements.
Elevating Permissions and running [`"$exeToRun`" $wrappedStatements]. This may take a while, depending on the statements.
"@
}
$dbgMessage | Write-Debug

Write-Debug $dbgMessage

# Redirecting output slows things down a bit.
$writeOutput = {
if ($EventArgs.Data -ne $null) {
Write-Host "$($EventArgs.Data)"
}

#foreach ($line in $EventArgs.Data) {
#Write-Host "$line"
#}
}

$writeError = {
if ($EventArgs.Data -ne $null) {
Write-Host "[ERROR] $($EventArgs.Data)" -ForegroundColor $ErrorColor -BackgroundColor Black
}
#foreach ($line in $EventArgs.Data) {
#if (!$line.IsNullOrEmpty) {
# do not stop execution, but pass the output back to the user.
# Write-Host "[ERROR] $line" -ForegroundColor $ErrorColor -BackgroundColor Black
#}
#}
}

$psi = new-object System.Diagnostics.ProcessStartInfo
$psi.RedirectStandardError = $true
$psi.UseShellExecute = $false
$process = New-Object System.Diagnostics.Process
$process.EnableRaisingEvents = $true
Register-ObjectEvent -InputObject $process -SourceIdentifier "LogOutput_ChocolateyProc" -EventName OutputDataReceived -Action $writeOutput | Out-Null
Register-ObjectEvent -InputObject $process -SourceIdentifier "LogErrors_ChocolateyProc" -EventName ErrorDataReceived -Action $writeError | Out-Null

#$process.StartInfo = New-Object System.Diagnostics.ProcessStartInfo($exeToRun, $wrappedStatements)
# in case empty args makes a difference, try to be compatible with the older
# version
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = $exeToRun
if ($wrappedStatements -ne '') {
$psi.Arguments = "$wrappedStatements"
}
$process.StartInfo = $psi

if ([Environment]::OSVersion.Version -ge (new-object 'Version' 6,0)){
$psi.Verb = "runas"
# process start info
$process.StartInfo.RedirectStandardOutput = $true
$process.StartInfo.RedirectStandardError = $true
$process.StartInfo.UseShellExecute = $false
$process.StartInfo.WorkingDirectory = Get-Location
if ([Environment]::OSVersion.Version -ge (New-Object 'Version' 6,0)){
Write-Debug "Setting RunAs for elevation"
$process.StartInfo.Verb = "RunAs"
}

$psi.WorkingDirectory = get-location

if ($minimized) {
$psi.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Minimized
$process.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Minimized
}

$s = [System.Diagnostics.Process]::Start($psi)
$process.Start() | Out-Null
if ($process.StartInfo.RedirectStandardOutput) { $process.BeginOutputReadLine() }
if ($process.StartInfo.RedirectStandardError) { $process.BeginErrorReadLine() }
$process.WaitForExit()

$chocTempDir = Join-Path $env:TEMP "chocolatey"
if (![System.IO.Directory]::Exists($chocTempDir)) { [System.IO.Directory]::CreateDirectory($chocTempDir) | Out-Null }
$errorFile = Join-Path $chocTempDir "$($s.Id)-error.stream"
$s.StandardError.ReadToEnd() | Out-File $errorFile
$s.WaitForExit()
if ($validExitCodes -notcontains $s.ExitCode) {
try {
$innerError = Import-CLIXML $errorFile | ? { $_.GetType() -eq [String] } | Out-String
}
catch{
$innerError = Get-Content $errorFile | Out-String
}
$errorMessage = "[ERROR] Running $exeToRun with $statements was not successful. Exit code was `'$($s.ExitCode)`' Error Message: $innerError."
Remove-Item $errorFile -Force -ErrorAction SilentlyContinue
throw $errorMessage
}
# For some reason this forces the jobs to finish and waits for
# them to do so. Without this it never finishes.
Unregister-Event -SourceIdentifier "LogOutput_ChocolateyProc"
Unregister-Event -SourceIdentifier "LogErrors_ChocolateyProc"

$exitCode = $process.ExitCode
$process.Dispose()

Write-Debug "Command [`"$exeToRun`" $wrappedStatements] exited with `'$exitCode`'."
if ($validExitCodes -notcontains $exitCode) {
throw "Running [`"$exeToRun`" $statements] was not successful. Exit code was '$exitCode'. See log for possible error messages."
}

Write-Debug "Finishing 'Start-ChocolateyProcessAsAdmin'"
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private static void set_machine_sources(ChocolateyConfiguration config, ConfigFi
private static void set_config_items(ChocolateyConfiguration config, ConfigFileSettings configFileSettings, IFileSystem fileSystem)
{
var cacheLocation = set_config_item(ApplicationParameters.ConfigSettings.CacheLocation, configFileSettings, string.IsNullOrWhiteSpace(configFileSettings.CacheLocation) ? string.Empty : configFileSettings.CacheLocation, "Cache location if not TEMP folder.");
config.CacheLocation = !string.IsNullOrWhiteSpace(cacheLocation) ? cacheLocation : System.Environment.GetEnvironmentVariable("TEMP");
config.CacheLocation = !string.IsNullOrWhiteSpace(cacheLocation) ? cacheLocation : fileSystem.get_temp_path(); // System.Environment.GetEnvironmentVariable("TEMP");
if (string.IsNullOrWhiteSpace(config.CacheLocation))
{
config.CacheLocation = fileSystem.combine_paths(ApplicationParameters.InstallLocation, "temp");
Expand Down

0 comments on commit 0328603

Please sign in to comment.