Skip to content

Commit

Permalink
(GH-666) Setup messages as Output with color
Browse files Browse the repository at this point in the history
PowerShell v2 when being run outputting to a filestream cannot send
output to multiple streams. This will cause the install to fail when
being run as part of a filestream.
  • Loading branch information
ferventcoder committed Mar 22, 2016
1 parent 7fc7af3 commit ea161cc
Showing 1 changed file with 45 additions and 19 deletions.
64 changes: 45 additions & 19 deletions nuget/chocolatey/tools/chocolateysetup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@ $sysDrive = $env:SystemDrive
$tempDir = $env:TEMP
$defaultChocolateyPathOld = "$sysDrive\Chocolatey"

$originalForegroundColor = $host.ui.RawUI.ForegroundColor

function Write-ChocolateyWarning {
param (
[string]$message = ''
)

try {
$host.ui.RawUI.ForegroundColor = "Yellow"
Write-Output "WARNING: $message"
$host.ui.RawUI.ForegroundColor = $originalForegroundColor
} catch {
Write-Output "WARNING: $message"
}
}

function Write-ChocolateyError {
try {
$host.ui.RawUI.ForegroundColor = "Red"
Write-Output "ERROR: $message"
$host.ui.RawUI.ForegroundColor = $originalForegroundColor
} catch {
Write-Output "ERROR: $message"
}
}

function Initialize-Chocolatey {
<#
.DESCRIPTION
Expand Down Expand Up @@ -76,7 +102,7 @@ Creating Chocolatey folders if they do not already exist.
"@ | Write-Output

Write-Warning "You can safely ignore errors related to missing log files when `n upgrading from a version of Chocolatey less than 0.9.9. `n 'Batch file could not be found' is also safe to ignore. `n 'The system cannot find the file specified' - also safe."
Write-ChocolateyWarning "You can safely ignore errors related to missing log files when `n upgrading from a version of Chocolatey less than 0.9.9. `n 'Batch file could not be found' is also safe to ignore. `n 'The system cannot find the file specified' - also safe."

#create the base structure if it doesn't exist
Create-DirectoryIfNotExists $chocolateyExePath
Expand Down Expand Up @@ -129,11 +155,11 @@ param(
# removing old variable
Install-ChocolateyEnvironmentVariable -variableName "$chocInstallVariableName" -variableValue $null -variableType $environmentTarget
} else {
Write-Warning "Setting ChocolateyInstall Environment Variable on USER and not SYSTEM variables.`n This is due to either non-administrator install OR the process you are running is not being run as an Administrator."
Write-ChocolateyWarning "Setting ChocolateyInstall Environment Variable on USER and not SYSTEM variables.`n This is due to either non-administrator install OR the process you are running is not being run as an Administrator."
}

Write-Output "Creating $chocInstallVariableName as an environment variable (targeting `'$environmentTarget`') `n Setting $chocInstallVariableName to `'$folder`'"
Write-Warning "It's very likely you will need to close and reopen your shell `n before you can use choco."
Write-ChocolateyWarning "It's very likely you will need to close and reopen your shell `n before you can use choco."
Install-ChocolateyEnvironmentVariable -variableName "$chocInstallVariableName" -variableValue "$folder" -variableType $environmentTarget
}

Expand All @@ -154,7 +180,7 @@ param(
Write-Debug "Ensure-UserPermissions"

if (!(Test-ProcessAdminRights)) {
Write-Warning "User is not running elevated, cannot set user permissions."
Write-ChocolateyWarning "User is not running elevated, cannot set user permissions."
return
}

Expand All @@ -176,7 +202,7 @@ param(
$acl.SetAccessRule($userAccessRule)
Set-Acl $folder $acl
} catch {
Write-Warning "Not able to set permissions for user."
Write-ChocolateyWarning "Not able to set permissions for user."
}
$ErrorActionPreference = $currentEA
}
Expand All @@ -191,9 +217,9 @@ param(

if (Test-Path $chocolateyPathOld) {
Write-Output "Attempting to upgrade `'$chocolateyPathOld`' to `'$chocolateyPath`'."
Write-Warning "Copying the contents of `'$chocolateyPathOld`' to `'$chocolateyPath`'. `n This step may fail if you have anything in this folder running or locked."
Write-ChocolateyWarning "Copying the contents of `'$chocolateyPathOld`' to `'$chocolateyPath`'. `n This step may fail if you have anything in this folder running or locked."
Write-Output 'If it fails, just manually copy the rest of the items out and then delete the folder.'
Write-Warning "!!!! ATTN: YOU WILL NEED TO CLOSE AND REOPEN YOUR SHELL !!!!"
Write-ChocolateyWarning "!!!! ATTN: YOU WILL NEED TO CLOSE AND REOPEN YOUR SHELL !!!!"
#-ForegroundColor Magenta -BackgroundColor Black

$chocolateyExePathOld = Join-Path $chocolateyPathOld 'bin'
Expand All @@ -206,7 +232,7 @@ param(
try {
Set-EnvironmentVariable -Name 'Path' -Value $updatedPath -Scope $_ -ErrorAction Stop
} catch {
Write-Warning "Was not able to remove the old environment variable from PATH. You will need to do this manually"
Write-ChocolateyWarning "Was not able to remove the old environment variable from PATH. You will need to do this manually"
}

}
Expand All @@ -228,7 +254,7 @@ param(
Copy-Item $_ -Destination $fileToMove -Exclude $exclude -Force -ErrorAction Stop
}
catch {
Write-Warning "Was not able to move `'$fileToMove`'. You may need to reinstall the shim"
Write-ChocolateyWarning "Was not able to move `'$fileToMove`'. You may need to reinstall the shim"
}
}
}
Expand All @@ -242,7 +268,7 @@ param(
Write-Debug "Remove-OldChocolateyInstall"

if (Test-Path $chocolateyPathOld) {
Write-Warning "This action will result in Log Errors, you can safely ignore those. `n You may need to finish removing '$chocolateyPathOld' manually."
Write-ChocolateyWarning "This action will result in Log Errors, you can safely ignore those. `n You may need to finish removing '$chocolateyPathOld' manually."
try {
Get-ChildItem -Path "$chocolateyPathOld" | % {
if (Test-Path $_.FullName) {
Expand All @@ -255,7 +281,7 @@ param(
Remove-Item "$($chocolateyPathOld)" -force -recurse -ErrorAction Stop
}
catch {
Write-Warning "Was not able to remove `'$chocolateyPathOld`'. You will need to manually remove it."
Write-ChocolateyWarning "Was not able to remove `'$chocolateyPathOld`'. You will need to manually remove it."
}
}
}
Expand Down Expand Up @@ -291,7 +317,7 @@ param(
Move-Item $chocoExe "$chocoExe.old" -force -ErrorAction SilentlyContinue
}
catch {
Write-Warning "Was not able to rename `'$chocoExe`' to `'$chocoExe.old`'."
Write-ChocolateyWarning "Was not able to rename `'$chocoExe`' to `'$chocoExe.old`'."
}
}

Expand Down Expand Up @@ -350,7 +376,7 @@ param(
Write-Debug "Installing the bin file redirects"
$redirectsPath = Join-Path $chocolateyPath 'redirects'
if (!(Test-Path "$redirectsPath")) {
Write-Warning "$redirectsPath does not exist"
Write-ChocolateyWarning "$redirectsPath does not exist"
return
}

Expand All @@ -370,7 +396,7 @@ param(
Remove-Item $binFilePathRename -force -ErrorAction Stop
}
catch {
Write-Warning "Was not able to remove `'$binFilePathRename`'. This may cause errors."
Write-ChocolateyWarning "Was not able to remove `'$binFilePathRename`'. This may cause errors."
}
}
if (Test-Path ($binFilePath)) {
Expand All @@ -379,7 +405,7 @@ param(
Move-Item -path $binFilePath -destination $binFilePathRename -force -ErrorAction Stop
}
catch {
Write-Warning "Was not able to rename `'$binFilePath`' to `'$binFilePathRename`'."
Write-ChocolateyWarning "Was not able to rename `'$binFilePath`' to `'$binFilePathRename`'."
}
}

Expand All @@ -388,7 +414,7 @@ param(
Copy-Item -path $exeFilePath -destination $binFilePath -force -ErrorAction Stop
}
catch {
Write-Warning "Was not able to replace `'$binFilePath`' with `'$exeFilePath`'. You may need to do this manually."
Write-ChocolateyWarning "Was not able to replace `'$binFilePath`' with `'$exeFilePath`'. You may need to do this manually."
}

$commandShortcut = [System.IO.Path]::GetFileNameWithoutExtension("$exeFilePath")
Expand All @@ -408,7 +434,7 @@ param(
Write-Debug "Administrator installing so using Machine environment variable target instead of User."
$environmentTarget = [System.EnvironmentVariableTarget]::Machine
} else {
Write-Warning "Setting ChocolateyInstall Path on USER PATH and not SYSTEM Path.`n This is due to either non-administrator install OR the process you are running is not being run as an Administrator."
Write-ChocolateyWarning "Setting ChocolateyInstall Path on USER PATH and not SYSTEM Path.`n This is due to either non-administrator install OR the process you are running is not being run as an Administrator."
}

Install-ChocolateyPath -pathToInstall "$chocolateyExePath" -pathType $environmentTarget
Expand Down Expand Up @@ -488,10 +514,10 @@ param(
$s.WaitForExit();
if ($s.ExitCode -ne 0 -and $s.ExitCode -ne 3010) {
if ($netFx4InstallTries -eq 2) {
Write-Error ".NET Framework install failed with exit code `'$($s.ExitCode)`'. `n This will cause the rest of the install to fail."
Write-ChocolateyError ".NET Framework install failed with exit code `'$($s.ExitCode)`'. `n This will cause the rest of the install to fail."
throw "Error installing .NET Framework 4.0 (exit code $($s.ExitCode)). `n Please install the .NET Framework 4.0 manually and then try to install Chocolatey again. `n Download at `'$NetFx4Url`'"
} else {
Write-Warning "First try of .NET framework install failed with exit code `'$($s.ExitCode)`'. Trying again."
Write-ChocolateyWarning "First try of .NET framework install failed with exit code `'$($s.ExitCode)`'. Trying again."
Install-DotNet4IfMissing $true
}
}
Expand Down

0 comments on commit ea161cc

Please sign in to comment.