Skip to content

Commit c2e16af

Browse files
committed
(maint) really fix up Chocolatey installer
- Fix filestream output redirection issues for good - Allow installing Chocolatey from local file - Allow installing the latest beta
1 parent 85613bf commit c2e16af

File tree

1 file changed

+101
-8
lines changed

1 file changed

+101
-8
lines changed

boxes/shared/shell/InstallChocolatey.ps1

+101-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
# PowerShell v2/3 caches the output stream. Then it throws errors due
2+
# to the FileStream not being what is expected. Fixes "The OS handle's
3+
# position is not what FileStream expected. Do not use a handle
4+
# simultaneously in one FileStream and in Win32 code or another
5+
# FileStream."
6+
# function Fix-PowerShellOutputRedirectionBug {
7+
# # http://www.leeholmes.com/blog/2008/07/30/workaround-the-os-handles-position-is-not-what-filestream-expected/
8+
# $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
9+
# $objectRef = $host.GetType().GetField("externalHostRef", $bindingFlags).GetValue($host)
10+
# $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetProperty"
11+
# $consoleHost = $objectRef.GetType().GetProperty("Value", $bindingFlags).GetValue($objectRef, @())
12+
# [void] $consoleHost.GetType().GetProperty("IsStandardOutputRedirected", $bindingFlags).GetValue($consoleHost, @())
13+
# $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
14+
# $field = $consoleHost.GetType().GetField("standardOutputWriter", $bindingFlags)
15+
# $field.SetValue($consoleHost, [Console]::Out)
16+
# [void] $consoleHost.GetType().GetProperty("IsStandardErrorRedirected", $bindingFlags).GetValue($consoleHost, @())
17+
# $field2 = $consoleHost.GetType().GetField("standardErrorWriter", $bindingFlags)
18+
# $field2.SetValue($consoleHost, [Console]::Error)
19+
# }
20+
21+
# Fix-PowerShellOutputRedirectionBug
22+
23+
# Write-Output "hi"
24+
# Write-Host "---"
25+
# Write-Warning "ruh roh"
26+
# Write-Host "---"
27+
# Write-Error "yep"
28+
29+
$installLocalFile = $true
30+
$localChocolateyPackageFilePath = 'c:\vagrantshared\resources\packages\chocolatey.0.9.10-beta-20160402.nupkg'
31+
$installLatestBeta = $false
32+
133
$ChocoInstallPathOld = "$env:SystemDrive\Chocolatey\bin"
234
$ChocoInstallPath = "$env:SystemDrive\ProgramData\Chocolatey\bin"
335

@@ -15,21 +47,82 @@ $ChocoInstallPath = "$env:SystemDrive\ProgramData\Chocolatey\bin"
1547
# [Environment]::SetEnvironmentVariable('Path', $ActualPath + $ChocoInstallPath, [System.EnvironmentVariableTarget]::Machine)
1648
# }
1749

18-
$env:ChocolateyInstall = "C:\ProgramData\chocolatey"
1950

20-
# $env:Path += ";$ChocoInstallPath"
51+
52+
$env:ChocolateyInstall = "$env:SystemDrive\ProgramData\Chocolatey"
53+
$env:Path += ";$ChocoInstallPath"
54+
55+
function Install-LocalChocolateyPackage {
56+
param (
57+
[string]$chocolateyPackageFilePath = ''
58+
)
59+
60+
if ($chocolateyPackageFilePath -eq $null -or $chocolateyPackageFilePath -eq '') {
61+
throw "You must specify a local package to run the local install."
62+
}
63+
64+
if (!(Test-Path($chocolateyPackageFilePath))) {
65+
throw "No file exists at $chocolateyPackageFilePath"
66+
}
67+
68+
if ($env:TEMP -eq $null) {
69+
$env:TEMP = Join-Path $env:SystemDrive 'temp'
70+
}
71+
$chocTempDir = Join-Path $env:TEMP "chocolatey"
72+
$tempDir = Join-Path $chocTempDir "chocInstall"
73+
if (![System.IO.Directory]::Exists($tempDir)) {[System.IO.Directory]::CreateDirectory($tempDir)}
74+
$file = Join-Path $tempDir "chocolatey.zip"
75+
Copy-Item $chocolateyPackageFilePath $file -Force
76+
77+
# unzip the package
78+
Write-Output "Extracting $file to $tempDir..."
79+
$shellApplication = new-object -com shell.application
80+
$zipPackage = $shellApplication.NameSpace($file)
81+
$destinationFolder = $shellApplication.NameSpace($tempDir)
82+
$destinationFolder.CopyHere($zipPackage.Items(),0x10)
83+
84+
# Call chocolatey install
85+
Write-Output "Installing chocolatey on this machine"
86+
$toolsFolder = Join-Path $tempDir "tools"
87+
$chocInstallPS1 = Join-Path $toolsFolder "chocolateyInstall.ps1"
88+
89+
& $chocInstallPS1
90+
91+
Write-Output 'Ensuring chocolatey commands are on the path'
92+
$chocInstallVariableName = "ChocolateyInstall"
93+
$chocoPath = [Environment]::GetEnvironmentVariable($chocInstallVariableName)
94+
if ($chocoPath -eq $null -or $chocoPath -eq '') {
95+
$chocoPath = 'C:\ProgramData\Chocolatey'
96+
}
97+
98+
$chocoExePath = Join-Path $chocoPath 'bin'
99+
100+
if ($($env:Path).ToLower().Contains($($chocoExePath).ToLower()) -eq $false) {
101+
$env:Path = [Environment]::GetEnvironmentVariable('Path',[System.EnvironmentVariableTarget]::Machine);
102+
}
103+
}
21104

22105
if (!(Test-Path $ChocoInstallPath)) {
23-
# Install chocolatey
24-
iex ((new-object net.webclient).DownloadString('http://chocolatey.org/install.ps1'))
106+
# Install Chocolatey
107+
if ($installLocalFile) {
108+
Install-LocalChocolateyPackage $localChocolateyPackageFilePath
109+
} else {
110+
if ($installLatestBeta) {
111+
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/installabsolutelatest.ps1'))
112+
} else {
113+
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
114+
}
115+
}
25116
}
26117

27-
#choco install chocolatey -pre
28-
# $resourcesPath = 'c:\vagrantshared\resources'
118+
# # choco upgrade chocolatey -pre
119+
# $resourcesPath = 'c:\vagrantshared\resources\packages'
29120
# $chocoPkgFile = get-childitem $resourcesPath -recurse -include 'chocolatey.*.nupkg' | select -First 1
30121

31122
# if ($chocoPkgFile -ne $null) {
32-
# cinst chocolatey -pre -force -source "$resourcesPath"
123+
# choco upgrade chocolatey -pre -force -source "$resourcesPath"
33124
# } else {
34-
# cinst chocolatey -pre
125+
# choco upgrade chocolatey -pre
35126
# }
127+
128+

0 commit comments

Comments
 (0)