-
Notifications
You must be signed in to change notification settings - Fork 0
/
moreSetup.ps1
364 lines (301 loc) · 11.7 KB
/
moreSetup.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
[CmdletBinding()]
param( [Parameter( Mandatory = $false )]
[switch] $NoGUI
)
try
{
Set-StrictMode -Version Latest
pushd ~
Write-Host "NoGUI? $NoGUI" -Fore Yellow
if( 0 -ne (id -u) )
{
Write-Host "(relaunching as superuser)" -Fore Yellow
# I would prefer to forward parameters here using @PSBoundParameters, but that
# bombs--because we are actually creating a new process, the serialized switch
# becomes "True", and then pwsh complains it can't convert the string into a bool.
# *sigh*
sudo pwsh -ExecutionPolicy Bypass -NoProfile -Command "$PSScriptRoot\$($MyInvocation.MyCommand)" -NoGUI:`$$NoGUI
return
}
elseif( !(Test-Path Variable:\ScriptRoot) )
{
$ScriptRoot = $PSScriptRoot
}
if( !$ScriptRoot )
{
throw "unexpected: don't know what my script root is"
}
if( !(Test-Path Env:\SUDO_USER) )
{
throw "unexpected: should have SUDO_USER env var"
}
if( !$NoGUI -and !(which dconf) )
{
Write-Host "(no dconf, so I'm going to assume -NoGUI)" -Fore DarkCyan
$NoGUI = $true
}
Write-Host "Continuing setup..." -Fore Cyan
# Fix up permissions, since we've first run pwsh as root:
# (irritating that chown can't handle ~)
$refDir = Resolve-Path '~'
$dstDir = Resolve-Path '~/.local/share/powershell'
chown -R --reference=$refDir $dstDir
$tmpFile = '/tmp/nixSetup_filesThatAreDifferent'
# Let's create these files as the normal user.
#
# Unfortunately when we use a ScriptBlock as a parameter to pwsh like this, we lose
# $PSScriptRoot, so we'll have to pass that in.
#
# We also pass in the $tmpFile path that we use to remember any pre-existing,
# conflicting files, so that we can show them again at the end, after all the other
# spew.
sudo -u $env:SUDO_USER pwsh -ExecutionPolicy Bypass -NoProfile -Command {
[CmdletBinding()]
param( [Parameter( Mandatory = $true, Position = 0 )]
[string] $ScriptRoot,
[Parameter( Mandatory = $true, Position = 1 )]
[string] $tmpFile
)
Set-StrictMode -Version Latest
Remove-Item -LiteralPath $tmpFile -Force -ErrorAction Ignore
# TODO: maybe we should make these files links, so we can easily commit any
# changes to them?
# FileName KnownDefaultFileHash
$stuff = @( @( '.vimrc' , '' ) ,
@( '.gvimrc' , '' ) ,
@( '.config/powershell' , '' ) ,
@( '.gitconfig' , '' ) ,
@( '.inputrc' , '' ) ,
@( '.Xresources' , '' ) ,
@( '.profile' , '28B4A453B68DDE64F814E94BAB14EE651F4F162E15DD9920490AA1D49F05D2A4' ) ,
@( '.bashrc' , '342099DA4DD28C394D3F8782D90D7465CB2EAA611193F8F378D6918261CB9BB8' )
) | ForEach-Object { [PSCustomObject] @{ FileName = $PSItem[ 0 ]
KnownDefaultFileHash = $PSItem[ 1 ] } }
foreach( $thing in $stuff )
{
$src = Join-Path $ScriptRoot 'home' $thing.FileName
$dst = $thing.FileName
if( (Test-Path $dst) )
{
if( (Test-Path $dst -PathType Container) )
{
$diff = diff -r $src $dst
}
else
{
$diff = cmp $src $dst
}
if( $diff )
{
# If the file is just the default file, let's just overwrite it.
if( $thing.KnownDefaultFileHash -and
((Get-FileHash -Algorithm SHA256 -LiteralPath $dst).Hash -eq $thing.KnownDefaultFileHash) )
{
Write-Host "(overwriting existing-but-default file: $dst)" -Fore DarkYellow
}
else
{
Write-Host "(diff) " -Fore Yello -NoNewline
Write-host "Already exists: $dst" -Fore DarkCyan
Write-Host " To compare: bcompare $src ~/$dst" -Fore DarkYellow
# Remember this for later, so that we can show the user at the
# very end, /after/ all the spew from other commands.
"$src ~/$dst" >> $tmpFile
continue
}
}
else
{
Write-Host "(same) " -Fore DarkGreen -NoNewline
Write-host "Already exists: $dst" -Fore DarkCyan
continue # no need to copy
}
}
Write-Host "Copying $($thing.FileName) ..." -Fore Cyan
if( (Test-Path $src -Type Container) )
{
$dst = Split-Path $dst
Copy-Item $src $dst -Recurse
}
else
{
Copy-Item $src $dst
}
}
$vimPs1Path = './.vim/pack/vim-ps1/start/vim-ps1'
if( !(Test-Path $vimPs1Path) )
{
Write-Host 'Cloning vim-ps1 (vim PowerShell stuff)' -Fore Cyan
$vimPs1Url = 'https://github.com/PProvost/vim-ps1.git'
git clone $vimPs1Url $vimPs1Path
}
} -args @( $ScriptRoot, $tmpFile )
if( !$NoGUI -and !(which git-cola) )
{
Write-Host "Installing git-cola..." -Fore cyan
# The version of the git-cola package in Ubuntu 18.04 is fairly old, so per the
# github page, we're using this PPA:
# https://launchpad.net/~pavreh/+archive/ubuntu/git-cola
add-apt-repository -y ppa:pavreh/git-cola
apt-get update
apt-get install -y --show-progress git-cola
}
else
{
Write-Host '(already have git-cola)' -Fore DarkCyan
}
if( !$NoGUI -and !(which bcompare) )
{
Write-Host "Installing Beyond Compare..." -Fore Cyan
apt-get install -y --show-progress gdebi-core
wget --show-progress http://www.scootersoftware.com/bcompare-4.2.9.23626_amd64.deb
gdebi -n bcompare-4.2.9.23626_amd64.deb
}
else
{
Write-Host '(already have bcompare)' -Fore DarkCyan
}
if( !$NoGUI -and !(which gitk) )
{
Write-Host "Installing gitk" -Fore Cyan
apt-get install -y --show-progress gitk
}
else
{
Write-Host '(already have gitk)' -Fore DarkCyan
}
if( !$NoGUI -and !(fc-list | grep '/hack/') )
{
Write-Host "Installing Hack font" -Fore Cyan
apt-get install -y fonts-hack-ttf
}
else
{
Write-Host '(already have hack font)' -Fore DarkCyan
}
if( !(which java) )
{
Write-Host "Installing java" -Fore Cyan
apt-get install -y --show-progress default-jre
}
else
{
Write-Host '(already have java)' -Fore DarkCyan
}
# I don't really need ruby, but brew does, and their script installer does not seem to
# be able to install it.
if( !(which ruby) )
{
Write-Host "Installing ruby" -Fore Cyan
apt-get install -y --show-progress ruby
}
else
{
Write-Host '(already have ruby)' -Fore DarkCyan
}
# brew may need gcc to compile things
if( !(which gcc) )
{
Write-Host "Installing gcc" -Fore Cyan
apt-get install -y --show-progress gcc
}
else
{
Write-Host '(already have gcc)' -Fore DarkCyan
}
# Can't just run "which brew" to detect brew, because root does not have it in the
# PATH (By Design).
if( !(Test-Path /home/linuxbrew/.linuxbrew/bin/brew) )
{
Write-Host "Installing brew" -Fore Cyan
# some pre-reqs
apt-get install -y --show-progress build-essential file
# Unfortunately when we use a ScriptBlock as a parameter to pwsh like this, we
# lose $PSScriptRoot, so we'll have to pass that in.
sudo -u $env:SUDO_USER pwsh -ExecutionPolicy Bypass -NoProfile -Command {
[CmdletBinding()]
param( [Parameter( Mandatory = $true, Position = 0 )]
[string] $ScriptRoot
)
Set-StrictMode -Version Latest
# I cannot for the life of me figure out why "sh -c $script" does not work,
# but "$script | sh" does (when called from pwsh; it works fine in bash).
$script = curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh
$script | sh
if( !$? )
{
Write-Error "Uh-oh; did the brew install shell script fail?"
}
else
{
$shEnvVars = /home/linuxbrew/.linuxbrew/bin/brew shellenv
# nevermind about the following line; I put this stuff, pre-canned into .profile
#$shEnvVars >> ~/.profile
# let's get the env vars locally, too
$shEnvVars.Replace( "export ", "`$env:" ).
Replace( "`$PATH", "`$env:PATH" ).
Replace( "`$MANPATH", "`$env:MANPATH" ).
Replace( "`$INFOPATH", "`$env:INFOPATH" ) | Invoke-Expression
}
} -args $ScriptRoot
}
else
{
Write-Host '(already have brew)' -Fore DarkCyan
}
# Brew-installed stuff.
if( (Test-Path /home/linuxbrew/.linuxbrew/bin/brew) )
{
# Unfortunately when we use a ScriptBlock as a parameter to pwsh like this, we
# lose $PSScriptRoot, so we'll have to pass that in.
sudo -u $env:SUDO_USER pwsh -ExecutionPolicy Bypass -NoProfile -Command {
[CmdletBinding()]
param( [Parameter( Mandatory = $true, Position = 0 )]
[string] $ScriptRoot
)
Set-StrictMode -Version Latest
# We probably need to load up the environment variables.
$shEnvVars = /home/linuxbrew/.linuxbrew/bin/brew shellenv
$shEnvVars.Replace( "export ", "`$env:" ).
Replace( "`$PATH", "`$env:PATH" ).
Replace( "`$MANPATH", "`$env:MANPATH" ).
Replace( "`$INFOPATH", "`$env:INFOPATH" ) | Invoke-Expression
if( !(which brew) )
{
Write-Error "Misconfiguration? Where's Brew?"
return
}
if( !(which git-credential-manager) )
{
Write-Host "Installing git-credential-manager" -Fore Cyan
Write-Host 'Updating brew... (this can take a while)' -Fore Cyan
brew update
Write-Host '[brew] installing git-credential-manager' -Fore Cyan
brew install git-credential-manager
git-credential-manager install
}
else
{
Write-Host '(already have git-credential-manager)' -Fore DarkCyan
}
} -args $ScriptRoot
}
if( (Test-Path $tmpFile) )
{
Write-Host ''
Write-Host "There were some existing files that are different from those included with this script:" -Fore Yellow
foreach( $line in (Get-Content $tmpFile) )
{
$src, $dst = $line.Split()
Write-Host "(diff) " -Fore Yellow -NoNewline
Write-host "Already exists: $dst" -Fore DarkCyan
Write-Host " To compare: bcompare $src $dst" -Fore DarkYellow
}
Remove-Item $tmpFile -Force -ErrorAction Ignore
}
Write-Host "Done." -Fore Green
}
finally
{
popd
}