|
| 1 | +# ===================================================================== |
| 2 | +# Copyright 2011 - Present RealDimensions Software, LLC, and the |
| 3 | +# original authors/contributors from ChocolateyGallery |
| 4 | +# at https://github.com/chocolatey/chocolatey.org |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# ===================================================================== |
| 18 | + |
| 19 | +$gitInstallerURL = 'https://github.com/git-for-windows/git/releases/download/v2.11.0.windows.3/Git-2.11.0.3-64-bit.exe' |
| 20 | +$gitInstallerHash = 'c3897e078cd7f7f496b0e4ab736ce144c64696d3dbee1e5db417ae047ca3e27f' |
| 21 | + |
| 22 | +if ((Get-WmiObject Win32_OperatingSystem).OSArchitecture -eq "32-bit") { |
| 23 | + $gitInstallerURL = 'https://github.com/git-for-windows/git/releases/download/v2.11.0.windows.3/Git-2.11.0.3-32-bit.exe' |
| 24 | + $gitInstallerHash = 'dff9bec9c4e21eaba5556fe4a7b1071d1f18e3a8b9645bffb48fda9eaee37e62' |
| 25 | +} |
| 26 | + |
| 27 | +if ($env:TEMP -eq $null) { |
| 28 | + $env:TEMP = Join-Path $env:SystemDrive 'temp' |
| 29 | +} |
| 30 | + |
| 31 | +$tempDir = Join-Path $env:TEMP "adsk-bundle" |
| 32 | +if (![System.IO.Directory]::Exists($tempDir)) {[System.IO.Directory]::CreateDirectory($tempDir)} |
| 33 | +$gitInstallerEXE = Join-Path $tempDir "Git-64-bit-installer.exe" |
| 34 | +$bootstrap = Join-Path $tempDir "install-Git-and-GitADSK.bat" |
| 35 | + |
| 36 | +# PowerShell v2/3 caches the output stream. Then it throws errors due |
| 37 | +# to the FileStream not being what is expected. Fixes "The OS handle's |
| 38 | +# position is not what FileStream expected. Do not use a handle |
| 39 | +# simultaneously in one FileStream and in Win32 code or another |
| 40 | +# FileStream." |
| 41 | +function Fix-PowerShellOutputRedirectionBug { |
| 42 | + $poshMajorVerion = $PSVersionTable.PSVersion.Major |
| 43 | + |
| 44 | + if ($poshMajorVerion -lt 4) { |
| 45 | + try{ |
| 46 | + # http://www.leeholmes.com/blog/2008/07/30/workaround-the-os-handles-position-is-not-what-filestream-expected/ plus comments |
| 47 | + $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField" |
| 48 | + $objectRef = $host.GetType().GetField("externalHostRef", $bindingFlags).GetValue($host) |
| 49 | + $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetProperty" |
| 50 | + $consoleHost = $objectRef.GetType().GetProperty("Value", $bindingFlags).GetValue($objectRef, @()) |
| 51 | + [void] $consoleHost.GetType().GetProperty("IsStandardOutputRedirected", $bindingFlags).GetValue($consoleHost, @()) |
| 52 | + $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField" |
| 53 | + $field = $consoleHost.GetType().GetField("standardOutputWriter", $bindingFlags) |
| 54 | + $field.SetValue($consoleHost, [Console]::Out) |
| 55 | + [void] $consoleHost.GetType().GetProperty("IsStandardErrorRedirected", $bindingFlags).GetValue($consoleHost, @()) |
| 56 | + $field2 = $consoleHost.GetType().GetField("standardErrorWriter", $bindingFlags) |
| 57 | + $field2.SetValue($consoleHost, [Console]::Error) |
| 58 | + } catch { |
| 59 | + Write-Output "Unable to apply redirection fix." |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +Fix-PowerShellOutputRedirectionBug |
| 65 | + |
| 66 | +function Download-File { |
| 67 | +param ( |
| 68 | + [string]$url, |
| 69 | + [string]$file |
| 70 | + ) |
| 71 | + $downloader = new-object System.Net.WebClient |
| 72 | + $defaultCreds = [System.Net.CredentialCache]::DefaultCredentials |
| 73 | + if ($defaultCreds -ne $null) { |
| 74 | + $downloader.Credentials = $defaultCreds |
| 75 | + } |
| 76 | + $downloader.DownloadFile($url, $file) |
| 77 | +} |
| 78 | + |
| 79 | +Download-File $gitInstallerURL $gitInstallerEXE |
| 80 | + |
| 81 | +if ($PSVersionTable.PSVersion.Major -ge 4) { |
| 82 | + $hash = Get-FileHash $gitInstallerEXE -Algorithm SHA256 |
| 83 | + if ($hash.Hash -ne $gitInstallerHash) { |
| 84 | + Write-Output "ERROR: SHA256 hash of the Git for Windows installer does not match." |
| 85 | + exit |
| 86 | + } |
| 87 | +} else { |
| 88 | + Write-Output "WARNING: SHA256 hash of the Git for Windows installer cannot be checked as your Powershell version is outdated (expected on Windows 7 and below)." |
| 89 | +} |
| 90 | + |
| 91 | +Set-Content $bootstrap "@echo off" -Encoding ASCII |
| 92 | +Add-Content $bootstrap "`"$gitInstallerEXE`" /SILENT /COMPONENTS=`"icons,icons\desktop,ext,ext\shellhere,ext\guihere,assoc,assoc_sh`"" -Encoding ASCII |
| 93 | +Add-Content $bootstrap "SET PATH=%PATH%;`"C:\Program Files\Git\bin`"" |
| 94 | +Add-Content $bootstrap "git config --system --unset credential.helper" -Encoding ASCII |
| 95 | +Add-Content $bootstrap "git clone -c credential.helper=`"!f() { cat >/dev/null; echo `"username=%1`"; echo `"password=%2`"; }; f`" --branch production https://git.autodesk.com/github-solutions/adsk-git.git `"%HOMEDRIVE%%HOMEPATH%\.adsk-git`"" -Encoding ASCII |
| 96 | +Add-Content $bootstrap "git config --global include.path `"%HOMEDRIVE%%HOMEPATH%/.adsk-git/config.include`"" -Encoding ASCII |
| 97 | + |
| 98 | +if ("<<USERNAME>>" -eq "token") { |
| 99 | + Add-Content $bootstrap "cmd /V /C `"set GITHUB_TOKEN=%2 && git adsk`"" -Encoding ASCII |
| 100 | +} else { |
| 101 | + Add-Content $bootstrap "cmd /V /C `"git adsk`"" -Encoding ASCII |
| 102 | +} |
| 103 | + |
| 104 | +Start-Process -verb RunAs $bootstrap '<<USERNAME>> <<PASSWORD>>' |
0 commit comments