-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGitSetup.ps1
More file actions
313 lines (256 loc) · 8.28 KB
/
GitSetup.ps1
File metadata and controls
313 lines (256 loc) · 8.28 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#Run the following command with quotations:
# ./GitSetup "Your name" "Your email"
param(
[Parameter(Mandatory=$true)] [string]$name,
[Parameter(Mandatory=$true)] [string]$mail
)
$ErrorActionPreference = "Stop"
$global:x86 = $true
$global:PowerProfile = $PROFILE
$global:ConfigFile = ".gitconfig"
$global:P4MergeFile = "P4Merge.zip"
$global:gitPath = "${env:ProgramFiles(x86)}" + "\Git"
$global:mergePath = $gitPath + "\libexec\git-core\mergetools"
$global:installationFolder = $pwd;
function CheckAdminMode
{
$IsInAdminMode = ([Security.Principal.WindowsPrincipal] `
[Security.Principal.WindowsIdentity]::GetCurrent() `
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if($IsInAdminMode -eq $False) {
Write-Error "You are not in admin mode! Please switch to run this installtion script."
}
}
function CheckPowerShellVersion
{
if($PSVersionTable.PSVersion.Major -lt 5) {
Write-Host "You are not running PowerShell version 5+, and cannot make a full installation (posh-git requires it)." -Foreground Red
Write-Host "To upgrade, you can install Chocolatey through the following command, followed by a restart:" -Foreground Cyan
Write-Host "Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" -Foreground Gray
Write-Host "After that, run the following code to install the new verison of PowerShell:"
Write-Host "choco install powershell -y" -Foreground Gray
Write-Error "You are not running PowerShell version 5+! Please upgrade."
}
Get-PackageProvider -Name "NuGet" -ForceBootstrap
Import-PackageProvider NuGet -Force
}
function CheckIfMsysIsInstallad
{
# Check if MSysGit has been installed
$ValidPath = Test-Path $gitPath
if($ValidPath -eq $false)
{
$global:x86 = $false
$global:gitPath = "${env:ProgramFiles}" + "\Git"
$global:mergePath = $gitPath + "\mingw64\libexec\git-core\mergetools"
}
$ValidPath = Test-Path $global:gitPath
# It has not:
if ($ValidPath -eq $false)
{
Write-Host "You have not install MSysGit yet. Please do so." -Foreground Green
Write-Host "Please hit a key to download it..." -Foreground Green
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
# Open browser to GitHub to insert key
cmd.exe /C start https://git-for-windows.github.io/
$host.exit
}
}
# Test if .$PROFILE has been created.
# If not, do so
function CreateProfile
{
# Check if the profile has been created
$GotPath = Test-path $global:PowerProfile
# Create profile
if(-not $GotPath)
{
New-item -type file -force $global:PowerProfile
}
}
# Download prefix for the profile as either x64 or x86
function DownloadGit
{
#Paste to $PROFILE
if($global:x86 -eq $true)
{
$text = @"
`$env:path += ";" + (Get-Item "Env:ProgramFiles").Value + "\Git\bin"
`$env:home = `$env:userprofile
#Alias
Set-Alias g git # Use Git commands but just typing 'g'
Set-Alias ex explorer # Open a File Explorer with 'ex .'
Import-Module posh-git
Import-Module posh-sshell
Start-Service ssh-agent
"@
$text | out-file $global:PowerProfile
}
else #x64
{
$text = @"
`$env:path += ";" + (Get-Item "Env:ProgramFiles").Value + "\Git\bin"
`$env:path += ";" + (Get-Item "Env:ProgramFiles").Value + "\Git\usr\bin"
`$env:home = `$env:userprofile
#Alias
Set-Alias g git # Use Git commands but just typing 'g'
Set-Alias ex explorer # Open a File Explorer with 'ex .'
"@
$text | out-file $global:PowerProfile
}
}
function SetupSSH
{
try
{
# Generate an ssh-key to use
Write-Host "If you want a password enter one. Else leave blank" -Foreground Green
$sshPath = (Get-Item "Env:userprofile").Value + "\.ssh"
$sshKey = $sshPath + "\id_rsa"
if((Test-Path $sshPath) -eq $false)
{
New-item $sshPath -itemType directory
Set-itemProperty -Path $sshPath -Name Attributes -Value ([System.IO.FileAttributes]::Hidden)
}
ssh-keygen.exe -t rsa -b 4096 -f $sshKey
ssh-add -k $sshKey
}
catch
{
Write-Host "Something went wrong when you tried to setup your SSH-key"
}
}
#Download content for the ~/.gitconfig file
function DownloadGitconfigContent
{
try
{
# Download content and paste to .gitconfig
Write-Host "Overriding the .gitconfig file. It can be found in $env:userprofile\$global:ConfigFile"
cp "$global:installationFolder\gitconfig.txt" "$env:userprofile\$global:ConfigFile"
# Insert your name and email into .gitconfig
git config --global user.name "$name"
git config --global user.email "$mail"
}
catch {
Write-Host "Can't use Git - No clue to why this is happening"
}
}
function SetupMergeTool
{
try
{
# Extract it
Write-Host "Installing P4Merge as mergetool"
$shell = new-object -com shell.application
$zip = $shell.NameSpace("$pwd\$global:P4MergeFile")
foreach($item in $zip.items())
{
$shell.Namespace($mergePath).copyhere($item, 0x14)
}
# Insert start menu link
$objShell = New-Object -ComObject ("WScript.Shell")
$objShortCut = $objShell.CreateShortcut($env:ALLUSERSPROFILE + "\Microsoft\Windows\Start Menu\Programs" + "\P4Merge.lnk")
$objShortCut.TargetPath = "C:\Program Files\Git\mingw64\libexec\git-core\mergetools\p4merge.exe"
$objShortCut.Save()
}
catch {
Write-Host "Are you sure you are in Admin mode?" -ForegroundColor Green
}
}
#If needed - Posh-git cannot be downloaded without
function SetupProxy
{
# Set proxy, if any
$proxies = (Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').proxyServer
if ($proxies)
{
if ($proxies -ilike "*=*")
{
$proxies -replace "=","://" -split(';') | Select-Object -First 1
}
else
{
"http://" + $proxies
}
}
if($proxies)
{
git config --global http.proxy $proxies
}
}
function SetupPoshGit
{
try
{
# Download and install Posh-Git
Write-Host "Download and install posh-git..."
$currentFolder = $pwd
PowerShellGet\Install-Module posh-git -Scope CurrentUser -Force
cd $currentFolder
# Run when starting PowerShell
$text = @"
# Load Posh-git
Import-Module posh-git
Import-Module posh-sshell
# Load SSH-agent with password
Start-SshAgent -Quiet
"@
$text | out-file $global:PowerProfile -append
Write-Host "Install posh-sshell"
PowerShellGet\Install-Module posh-sshell -Scope CurrentUser -Force
# Due to a bug (https://github.com/PowerShell/Win32-OpenSSH/issues/1234), run this command:
sc.exe create sshd binPath=C:\Windows\System32\OpenSSH\ssh.exe
}
catch {
Write-Host "While downloading and installing posh-git, something went wrong!" -Foreground Red
}
}
function DownloadGitconfigContent2
{
#Colors for PowerShell
$text = @"
#Set readable colors
`$Global:GitPromptSettings.BeforeIndex.ForegroundColor = [ConsoleColor]::Green
`$Global:GitPromptSettings.IndexColor.ForegroundColor = [ConsoleColor]::Green
`$Global:GitPromptSettings.WorkingColor.ForegroundColor = [ConsoleColor]::Red
`$Global:GitPromptSettings.LocalWorkingStatusSymbol.ForegroundColor = [ConsoleColor]::Red
`$Global:GitPromptSettings.LocalDefaultStatusSymbol.ForegroundColor = [ConsoleColor]::Red
# Set start location
#Set-Location `$env:home
"@
$text | out-file $global:PowerProfile -append
}
function GetSSHKey
{
# Copy your ssh-key
$keyPath = (Get-Item "Env:userprofile").Value + "\.ssh\id_rsa.pub"
Get-Content $keyPath | clip
Write-Host "" #Empty line
Write-Host "Your SSH-key is in you paste-bin. Use 'Ctrl+V' to paste it into the browser." -Foreground Green
Write-Host "Go register on https://github.com/settings/ssh if you use that :)" -Foreground Green
}
CheckAdminMode
CheckPowerShellVersion
CheckIfMsysIsInstallad
CreateProfile
DownloadGit
# Reload profile to get the Git commands
.$PROFILE
DownloadGitconfigContent
SetupSSH
SetupMergeTool
SetupProxy
SetupPoshGit
# Go to the home folder
cd $env:userprofile
# Reload the profile
.$PROFILE
DownloadGitconfigContent2
GetSSHKey
.$PROFILE
Write-Host "" #Empty line
Write-Host "If you missed the paste-bin's SSH-key, get it by writing the follwing command:"
Write-Host "cat ~/.ssh/id_rsa.pub | clip"
Write-Host "" #Empty line
cd $global:installationFolder