-
Notifications
You must be signed in to change notification settings - Fork 13
/
install-helper.ps1
270 lines (237 loc) · 10.2 KB
/
install-helper.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#
# Windows Install Helper
#
# PowerShell v2/3 caches the output stream. Then it throws errors due
# to the FileStream not being what is expected. Fixes "The OS handle's
# position is not what FileStream expected. Do not use a handle
# simultaneously in one FileStream and in Win32 code or another
# FileStream."
function Fix-PowerShellOutputRedirectionBug {
if ($PSVersionTable.PSVersion.Major -lt 4) {
try {
# http://www.leeholmes.com/blog/2008/07/30/workaround-the-os-handles-position-is-not-what-filestream-expected/ plus comments
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
$objectRef = $host.GetType().GetField("externalHostRef", $bindingFlags).GetValue($host)
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetProperty"
$consoleHost = $objectRef.GetType().GetProperty("Value", $bindingFlags).GetValue($objectRef, @())
[void] $consoleHost.GetType().GetProperty("IsStandardOutputRedirected", $bindingFlags).GetValue($consoleHost, @())
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"
$field = $consoleHost.GetType().GetField("standardOutputWriter", $bindingFlags)
$field.SetValue($consoleHost, [Console]::Out)
[void] $consoleHost.GetType().GetProperty("IsStandardErrorRedirected", $bindingFlags).GetValue($consoleHost, @())
$field2 = $consoleHost.GetType().GetField("standardErrorWriter", $bindingFlags)
$field2.SetValue($consoleHost, [Console]::Error)
} catch {
Write-Output "Unable to apply redirection fix."
}
}
}
# Download a file with progress indicator
# c.f. https://blogs.msdn.microsoft.com/jasonn/2008/06/13/downloading-files-from-the-internet-in-powershell-with-progress/
function Download-File {
param (
[string]$url,
[string]$file,
[string]$name
)
Write-Host "Downloading $($name) ..."
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$uri = New-Object System.Uri $url
$request = [System.Net.HttpWebRequest]::Create($uri)
$request.set_Timeout(15000) #15 second timeout
$response = $request.GetResponse()
$totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
$responseStream = $response.GetResponseStream()
$targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $file, Create
$buffer = new-object byte[] 10KB
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $count
while ($count -gt 0)
{
$progressMsg = "Downloaded {0}K of {1}K" -f [System.Math]::Floor($downloadedBytes/1024),$totalLength
try {
# Jump to the beginning of the line in cmd.exe
[System.Console]::CursorLeft = 0
} catch {
# Jump to the beginning of the line in git-bash.exe
[System.Console]::Write("`r")
}
[System.Console]::Write($progressMsg)
$targetStream.Write($buffer, 0, $count)
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $downloadedBytes + $count
}
$targetStream.Flush()
$targetStream.Close()
$targetStream.Dispose()
$responseStream.Dispose()
$downloader = new-object System.Net.WebClient
$defaultCreds = [System.Net.CredentialCache]::DefaultCredentials
if ($defaultCreds -ne $null) {
$downloader.Credentials = $defaultCreds
}
$downloader.DownloadFile($url, $file)
Write-Host ""
Write-Host ""
}
function Check-SHA256-Hash {
param (
[string]$filepath,
[string]$sha256hash,
[string]$errormsg
)
if ($PSVersionTable.PSVersion.Major -ge 4) {
$hash = Get-FileHash $filepath -Algorithm SHA256
if ($hash.Hash -ne $sha256hash) {
Write-Output "ERROR: SHA256 hash of the $($errormsg) does not match."
exit
}
} else {
Write-Output "WARNING: SHA256 hash of the $($errormsg) cannot be checked as your Powershell version is outdated (expected on Windows 7 and below)."
}
}
function Run {
param (
[string]$permissions,
[string]$filepath,
[string]$arguments,
[string]$errormsg
)
if ($PSVersionTable.PSVersion.Major -ge 3) {
if ($permissions -eq "admin") {
$result = Start-Process -verb RunAs -PassThru -Wait $filepath -ArgumentList $arguments
} else {
$result = Start-Process -NoNewWindow -PassThru -Wait $filepath -ArgumentList $arguments
}
if ($result.ExitCode -ne 0) {
Write-Output "ERROR: $($errormsg) with exit code $($result.ExitCode)."
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit
}
} else {
if ($permissions -eq "admin") {
$result = $(Start-Process -verb RunAs -PassThru $filepath $arguments)
} else {
$result = $(Start-Process -NoNewWindow -PassThru $filepath $arguments)
}
$result.WaitForExit()
if ($result.ExitCode) {
Write-Output "ERROR: $($errormsg) with exit code $($result.ExitCode)."
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit
}
}
}
function Detect-Previous-Installations {
if ([System.IO.Directory]::Exists("$env:programfiles\Git LFS")) {
Write-Output "ERROR: 'Git LFS' installation detected."
Write-Output "Git LFS is now part of Git for Windows and your"
Write-Output "installation might conflict with the official install."
Write-Output "Please do the following:"
Write-Output " 1. Open 'Programs and Features'"
Write-Output " 2. Uninstall 'Git LFS version x.y.z'"
Write-Output " 3. Delete the directory '$env:programfiles\Git LFS'"
Write-Output " if it still exists"
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit
}
}
# The following options are recognized in the -options array parameter as
# of git for windows 2.14.1.windows.1. They are reverse engineered from
# this code:
# https://github.com/git-for-windows/build-extra/blob/9e59621fc536037fe913ef08af0242572a0e5c08/installer/install.iss#L2088-L2158
#
# PathOption = BashOnly|Cmd|CmdTools
# SSHOption = OpenSSH|Plink
# PlinkPath = <path>
# CURLOption= OpenSSL|WinSSL
# CRLFOption = LFOnly|CRLFAlways|CRLFCommitAsIs
# BashTerminal = MinTTY|ConHost
# PerformanceTweaksFSCache = Disabled|Enabled
# UsecredentialManager = Disabled|Enabled
# EnableSymLinks = Diabled|Enabled
#
# TODO: Maybe print out this table via a function parameter?
function Install-Git-For-Windows {
param(
[string]$username,
[string]$password,
[string]$auth,
[string]$server,
[string]$repo,
[string]$branch,
[string]$kitID,
[string]$gitVersion='2.17.0.windows.1',
[string]$sha64bit='39b3da8be4f1cf396663dc892cbf818cb4cfddb5bf08c13f13f5b784f6654496',
[string]$sha32bit='65b710e39db3d83b04a8a4bd56f54e929fb0abbab728c0a9abbc0dace8e361d2',
[switch]$prompt,
[string[]]$options=@()
)
Fix-PowerShellOutputRedirectionBug
$bitness = (Get-WmiObject Win32_OperatingSystem).OSArchitecture
if ($bitness -eq "32-bit") {
# This check works only for Windows 7/8. AFAIK there is no 32-bit Windows 10
$gitInstallerHash = $sha32bit
} else {
# Set the bitness string explicitly as Windows 10 returns "64 bits"
$bitness = "64-bit"
$gitInstallerHash = $sha64bit
}
$gitBaseVersion = $gitVersion.SubString(0, $gitVersion.IndexOf('windows')-1)
$gitInstallerURL = "https://github.com/git-for-windows/git/releases/download/v$($gitVersion)/Git-$($gitBaseVersion)-$($bitness).exe"
$gitInstallerEXE = Join-Path $([System.IO.Path]::GetTempPath()) "git-installer.exe"
Detect-Previous-Installations
Download-File "$gitInstallerURL" "$gitInstallerEXE" "Git for Windows (Version $($gitVersion))"
Check-SHA256-Hash "$gitInstallerEXE" "$gitInstallerHash" "Git for Windows installer"
# Kill all existing shells to make the update possible
Stop-Process -erroraction 'silentlycontinue' -processname mintty
Stop-Process -erroraction 'silentlycontinue' -processname bash
#
# Read install options from the command line
#
$silent_arg="/SILENT"
if ($prompt.IsPresent) {
# If the user wants prompting, then
# remove the "silent" switch
$silent_arg = ""
}
$gfw_options = @{
"UseCredentialManager" = "Disabled"
}
# Set/override options from the input parameters
$options | % { $o = $_ -split '='; $gfw_options.Set_Item($o[0], $o[1]) }
# combine options in to a string to pass to the invocation
$gfw_str_opts = ($gfw_options.GetEnumerator() | % { "/o:$($_.Name)=$($_.Value)" }) -join ' '
#
# Install "Git for Windows"
#
Run "admin" $gitInstallerEXE "$silent_arg /COMPONENTS='icons,icons\desktop,ext,ext\shellhere,ext\guihere,gitlfs,assoc,assoc_sh' $gfw_str_opts" "Git for Windows installation failed"
#
# Setup "Enterprise Config for Git"
#
$script = Join-Path $([System.IO.Path]::GetTempPath()) "setup-enterprise-config-for-git.sh"
$content = @"
#!/usr/bin/env bash
set -e
# Remove an existing Enterprise Config for Git installation
rm -rf '$home\.$kitID-git'
# Clone Enterprise Config for Git
printf -v HELPER "!f() { cat >/dev/null; echo 'username=%s'; echo 'password=%s'; }; f" "`$1" "`$2"
git -c credential.helper="`$HELPER" \
clone --branch $branch \
https://$server/$repo.git \
'$home\.$kitID-git'
git config --global include.path '$home\.$kitID-git\config.include'
# Configure Enterprise Config for Git with the given username and password
CREDENTIALS_BASE64=`$3 BRANCH=$branch git $kitID
"@
Set-Content $script $content -Encoding ASCII
$params = @"
-c "/'$($script.Replace(':', '').Replace('\','/'))' '$username' '$password' '$auth'"
"@
$env:Path = "$env:programfiles\Git\bin;C:\Program Files\Git\bin;" + $env:Path
Run "user" sh.exe $params "'git $kitID' setup failed"
}