forked from ScoopInstaller/Scoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ps1
1224 lines (1045 loc) · 40.1 KB
/
install.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
. "$psscriptroot/autoupdate.ps1"
. "$psscriptroot/buckets.ps1"
function nightly_version($date, $quiet = $false) {
$date_str = $date.tostring("yyyyMMdd")
if (!$quiet) {
warn "This is a nightly version. Downloaded files won't be verified."
}
"nightly-$date_str"
}
function install_app($app, $architecture, $global, $suggested, $use_cache = $true, $check_hash = $true) {
$app, $bucket, $null = parse_app $app
$app, $manifest, $bucket, $url = Find-Manifest $app $bucket
if(!$manifest) {
abort "Couldn't find manifest for '$app'$(if($url) { " at the URL $url" })."
}
$version = $manifest.version
if(!$version) { abort "Manifest doesn't specify a version." }
if($version -match '[^\w\.\-\+_]') {
abort "Manifest version has unsupported character '$($matches[0])'."
}
$is_nightly = $version -eq 'nightly'
if ($is_nightly) {
$version = nightly_version $(get-date)
$check_hash = $false
}
if(!(supports_architecture $manifest $architecture)) {
write-host -f DarkRed "'$app' doesn't support $architecture architecture!"
return
}
write-output "Installing '$app' ($version) [$architecture]"
$dir = ensure (versiondir $app $version $global)
$original_dir = $dir # keep reference to real (not linked) directory
$persist_dir = persistdir $app $global
$fname = dl_urls $app $version $manifest $bucket $architecture $dir $use_cache $check_hash
pre_install $manifest $architecture
run_installer $fname $manifest $architecture $dir $global
ensure_install_dir_not_in_path $dir $global
$dir = link_current $dir
create_shims $manifest $dir $global $architecture
create_startmenu_shortcuts $manifest $dir $global $architecture
install_psmodule $manifest $dir $global
if($global) { ensure_scoop_in_path $global } # can assume local scoop is in path
env_add_path $manifest $dir $global
env_set $manifest $dir $global
# persist data
persist_data $manifest $original_dir $persist_dir
persist_permission $manifest $global
post_install $manifest $architecture
# save info for uninstall
save_installed_manifest $app $bucket $dir $url
save_install_info @{ 'architecture' = $architecture; 'url' = $url; 'bucket' = $bucket } $dir
if($manifest.suggest) {
$suggested[$app] = $manifest.suggest
}
success "'$app' ($version) was installed successfully!"
show_notes $manifest $dir $original_dir $persist_dir
}
function locate($app, $bucket) {
Show-DeprecatedWarning $MyInvocation 'Find-Manifest'
return Find-Manifest $app $bucket
}
function Find-Manifest($app, $bucket) {
$manifest, $url = $null, $null
# check if app is a URL or UNC path
if($app -match '^(ht|f)tps?://|\\\\') {
$url = $app
$app = appname_from_url $url
$manifest = url_manifest $url
} else {
# check buckets
$manifest, $bucket = find_manifest $app $bucket
if(!$manifest) {
# couldn't find app in buckets: check if it's a local path
$path = $app
if(!$path.endswith('.json')) { $path += '.json' }
if(test-path $path) {
$url = "$(resolve-path $path)"
$app = appname_from_url $url
$manifest, $bucket = url_manifest $url
}
}
}
return $app, $manifest, $bucket, $url
}
function dl_with_cache($app, $version, $url, $to, $cookies = $null, $use_cache = $true) {
$cached = fullpath (cache_path $app $version $url)
if(!(test-path $cached) -or !$use_cache) {
ensure $cachedir | Out-Null
do_dl $url "$cached.download" $cookies
Move-Item "$cached.download" $cached -force
} else { write-host "Loading $(url_remote_filename $url) from cache"}
if (!($null -eq $to)) {
Copy-Item $cached $to
}
}
function do_dl($url, $to, $cookies) {
$progress = [console]::isoutputredirected -eq $false -and
$host.name -ne 'Windows PowerShell ISE Host'
try {
$url = handle_special_urls $url
dl $url $to $cookies $progress
} catch {
$e = $_.exception
if($e.innerexception) { $e = $e.innerexception }
throw $e
}
}
function aria_exit_code($exitcode) {
$codes = @{
0='All downloads were successful'
1='An unknown error occurred'
2='Timeout'
3='Resource was not found'
4='Aria2 saw the specified number of "resource not found" error. See --max-file-not-found option'
5='Download aborted because download speed was too slow. See --lowest-speed-limit option'
6='Network problem occurred.'
7='There were unfinished downloads. This error is only reported if all finished downloads were successful and there were unfinished downloads in a queue when aria2 exited by pressing Ctrl-C by an user or sending TERM or INT signal'
8='Remote server did not support resume when resume was required to complete download'
9='There was not enough disk space available'
10='Piece length was different from one in .aria2 control file. See --allow-piece-length-change option'
11='Aria2 was downloading same file at that moment'
12='Aria2 was downloading same info hash torrent at that moment'
13='File already existed. See --allow-overwrite option'
14='Renaming file failed. See --auto-file-renaming option'
15='Aria2 could not open existing file'
16='Aria2 could not create new file or truncate existing file'
17='File I/O error occurred'
18='Aria2 could not create directory'
19='Name resolution failed'
20='Aria2 could not parse Metalink document'
21='FTP command failed'
22='HTTP response header was bad or unexpected'
23='Too many redirects occurred'
24='HTTP authorization failed'
25='Aria2 could not parse bencoded file (usually ".torrent" file)'
26='".torrent" file was corrupted or missing information that aria2 needed'
27='Magnet URI was bad'
28='Bad/unrecognized option was given or unexpected option argument was given'
29='The remote server was unable to handle the request due to a temporary overloading or maintenance'
30='Aria2 could not parse JSON-RPC request'
31='Reserved. Not used'
32='Checksum validation failed'
}
if($null -eq $codes[$exitcode]) {
return 'An unknown error occurred'
}
return $codes[$exitcode]
}
function get_filename_from_metalink($file) {
$bytes = get_magic_bytes_pretty $file ''
# check if file starts with '<?xml'
if(!($bytes.StartsWith('3c3f786d6c'))) {
return $null
}
# Add System.Xml for reading metalink files
Add-Type -AssemblyName 'System.Xml'
$xr = [System.Xml.XmlReader]::Create($file)
$filename = $null
try {
$xr.ReadStartElement('metalink')
if($xr.ReadToFollowing('file') -and $xr.MoveToFirstAttribute()) {
$filename = $xr.Value
}
} catch [System.Xml.XmlException] {
return $null
} finally {
$xr.Close()
}
return $filename
}
function dl_with_cache_aria2($app, $version, $manifest, $architecture, $dir, $cookies = $null, $use_cache = $true, $check_hash = $true) {
$data = @{}
$urls = @(url $manifest $architecture)
# aria2 input file
$urlstxt = "$cachedir\$app.txt"
$urlstxt_content = ''
$has_downloads = $false
# aria2 options
$options = @(
"--input-file='$urlstxt'"
"--user-agent='$(Get-UserAgent)'"
"--allow-overwrite=true"
"--auto-file-renaming=false"
"--retry-wait=$(get_config 'aria2-retry-wait' 2)"
"--split=$(get_config 'aria2-split' 5)"
"--max-connection-per-server=$(get_config 'aria2-max-connection-per-server' 5)"
"--min-split-size=$(get_config 'aria2-min-split-size' '5M')"
"--console-log-level=warn"
"--enable-color=false"
"--no-conf=true"
"--follow-metalink=true"
"--metalink-preferred-protocol=https"
"--min-tls-version=TLSv1.2"
"--stop-with-process=$PID"
"--continue"
)
if($cookies) {
$options += "--header='Cookie: $(cookie_header $cookies)'"
}
$proxy = get_config 'proxy'
if($proxy -ne 'none') {
if([Net.Webrequest]::DefaultWebProxy.Address) {
$options += "--all-proxy='$([Net.Webrequest]::DefaultWebProxy.Address.Authority)'"
}
if([Net.Webrequest]::DefaultWebProxy.Credentials.UserName) {
$options += "--all-proxy-user='$([Net.Webrequest]::DefaultWebProxy.Credentials.UserName)'"
}
if([Net.Webrequest]::DefaultWebProxy.Credentials.Password) {
$options += "--all-proxy-passwd='$([Net.Webrequest]::DefaultWebProxy.Credentials.Password)'"
}
}
$more_options = get_config 'aria2-options'
if($more_options) {
$options += $more_options
}
foreach($url in $urls) {
$data.$url = @{
'filename' = url_filename $url
'target' = "$dir\$(url_filename $url)"
'cachename' = fname (cache_path $app $version $url)
'source' = fullpath (cache_path $app $version $url)
}
if(!(test-path $data.$url.source)) {
$has_downloads = $true
# create aria2 input file content
$urlstxt_content += "$(handle_special_urls $url)`n"
if(!$url.Contains('sourceforge.net')) {
$urlstxt_content += " referer=$(strip_filename $url)`n"
}
$urlstxt_content += " dir=$cachedir`n"
$urlstxt_content += " out=$($data.$url.cachename)`n"
} else {
Write-Host "Loading " -NoNewline
Write-Host $(url_remote_filename $url) -f Cyan -NoNewline
Write-Host " from cache."
}
}
if($has_downloads) {
# write aria2 input file
Set-Content -Path $urlstxt $urlstxt_content
# build aria2 command
$aria2 = "& '$(aria2_path)' $($options -join ' ')"
# handle aria2 console output
Write-Host "Starting download with aria2 ..."
$prefix = "Download: "
Invoke-Expression $aria2 | ForEach-Object {
if([String]::IsNullOrWhiteSpace($_)) {
# skip blank lines
return
}
Write-Host $prefix -NoNewline
if($_.StartsWith('(OK):')) {
Write-Host $_ -f Green
} elseif($_.StartsWith('[') -and $_.EndsWith(']')) {
Write-Host $_ -f Cyan
} else {
Write-Host $_ -f Gray
}
}
if($lastexitcode -gt 0) {
error "Download failed! (Error $lastexitcode) $(aria_exit_code $lastexitcode)"
error $urlstxt_content
error $aria2
abort $(new_issue_msg $app $bucket "download via aria2 failed")
}
# remove aria2 input file when done
if(test-path($urlstxt)) {
Remove-Item $urlstxt
}
}
foreach($url in $urls) {
$metalink_filename = get_filename_from_metalink $data.$url.source
if($metalink_filename) {
Remove-Item $data.$url.source -Force
Rename-Item -Force (Join-Path -Path $cachedir -ChildPath $metalink_filename) $data.$url.source
}
# run hash checks
if($check_hash) {
$manifest_hash = hash_for_url $manifest $url $architecture
$ok, $err = check_hash $data.$url.source $manifest_hash $(show_app $app $bucket)
if(!$ok) {
error $err
if(test-path $data.$url.source) {
# rm cached file
Remove-Item -force $data.$url.source
}
if($url.Contains('sourceforge.net')) {
Write-Host -f yellow 'SourceForge.net is known for causing hash validation fails. Please try again before opening a ticket.'
}
abort $(new_issue_msg $app $bucket "hash check failed")
}
}
# copy or move file to target location
if(!(test-path $data.$url.source) ) {
abort $(new_issue_msg $app $bucket "cached file not found")
}
if(!($dir -eq $cachedir)) {
if($use_cache) {
Copy-Item $data.$url.source $data.$url.target
} else {
Move-Item $data.$url.source $data.$url.target -force
}
}
}
}
# download with filesize and progress indicator
function dl($url, $to, $cookies, $progress) {
$reqUrl = ($url -split "#")[0]
$wreq = [net.webrequest]::create($reqUrl)
if($wreq -is [net.httpwebrequest]) {
$wreq.useragent = Get-UserAgent
if (-not ($url -imatch "sourceforge\.net")) {
$wreq.referer = strip_filename $url
}
if($cookies) {
$wreq.headers.add('Cookie', (cookie_header $cookies))
}
}
$wres = $wreq.getresponse()
$total = $wres.ContentLength
if($total -eq -1 -and $wreq -is [net.ftpwebrequest]) {
$total = ftp_file_size($url)
}
if ($progress -and ($total -gt 0)) {
[console]::CursorVisible = $false
function dl_onProgress($read) {
dl_progress $read $total $url
}
} else {
write-host "Downloading $url ($(filesize $total))..."
function dl_onProgress {
#no op
}
}
try {
$s = $wres.getresponsestream()
$fs = [io.file]::openwrite($to)
$buffer = new-object byte[] 2048
$totalRead = 0
$sw = [diagnostics.stopwatch]::StartNew()
dl_onProgress $totalRead
while(($read = $s.read($buffer, 0, $buffer.length)) -gt 0) {
$fs.write($buffer, 0, $read)
$totalRead += $read
if ($sw.elapsedmilliseconds -gt 100) {
$sw.restart()
dl_onProgress $totalRead
}
}
$sw.stop()
dl_onProgress $totalRead
} finally {
if ($progress) {
[console]::CursorVisible = $true
write-host
}
if ($fs) {
$fs.close()
}
if ($s) {
$s.close();
}
$wres.close()
}
}
function dl_progress_output($url, $read, $total, $console) {
$filename = url_remote_filename $url
# calculate current percentage done
$p = [math]::Round($read / $total * 100, 0)
# pre-generate LHS and RHS of progress string
# so we know how much space we have
$left = "$filename ($(filesize $total))"
$right = [string]::Format("{0,3}%", $p)
# calculate remaining width for progress bar
$midwidth = $console.BufferSize.Width - ($left.Length + $right.Length + 8)
# calculate how many characters are completed
$completed = [math]::Abs([math]::Round(($p / 100) * $midwidth, 0) - 1)
# generate dashes to symbolise completed
if ($completed -gt 1) {
$dashes = [string]::Join("", ((1..$completed) | ForEach-Object {"="}))
}
# this is why we calculate $completed - 1 above
$dashes += switch($p) {
100 {"="}
default {">"}
}
# the remaining characters are filled with spaces
$spaces = switch($dashes.Length) {
$midwidth {[string]::Empty}
default {
[string]::Join("", ((1..($midwidth - $dashes.Length)) | ForEach-Object {" "}))
}
}
"$left [$dashes$spaces] $right"
}
function dl_progress($read, $total, $url) {
$console = $host.UI.RawUI;
$left = $console.CursorPosition.X;
$top = $console.CursorPosition.Y;
$width = $console.BufferSize.Width;
if($read -eq 0) {
$maxOutputLength = $(dl_progress_output $url 100 $total $console).length
if (($left + $maxOutputLength) -gt $width) {
# not enough room to print progress on this line
# print on new line
write-host
$left = 0
$top = $top + 1
}
}
write-host $(dl_progress_output $url $read $total $console) -nonewline
[console]::SetCursorPosition($left, $top)
}
function dl_urls($app, $version, $manifest, $bucket, $architecture, $dir, $use_cache = $true, $check_hash = $true) {
# we only want to show this warning once
if(!$use_cache) { warn "Cache is being ignored." }
# can be multiple urls: if there are, then msi or installer should go last,
# so that $fname is set properly
$urls = @(url $manifest $architecture)
# can be multiple cookies: they will be used for all HTTP requests.
$cookies = $manifest.cookie
$fname = $null
# extract_dir and extract_to in manifest are like queues: for each url that
# needs to be extracted, will get the next dir from the queue
$extract_dirs = @(extract_dir $manifest $architecture)
$extract_tos = @(extract_to $manifest $architecture)
$extracted = 0;
# download first
if(aria2_enabled) {
dl_with_cache_aria2 $app $version $manifest $architecture $dir $cookies $use_cache $check_hash
} else {
foreach($url in $urls) {
$fname = url_filename $url
try {
dl_with_cache $app $version $url "$dir\$fname" $cookies $use_cache
} catch {
write-host -f darkred $_
abort "URL $url is not valid"
}
if($check_hash) {
$manifest_hash = hash_for_url $manifest $url $architecture
$ok, $err = check_hash "$dir\$fname" $manifest_hash $(show_app $app $bucket)
if(!$ok) {
error $err
$cached = cache_path $app $version $url
if(test-path $cached) {
# rm cached file
Remove-Item -force $cached
}
if($url.Contains('sourceforge.net')) {
Write-Host -f yellow 'SourceForge.net is known for causing hash validation fails. Please try again before opening a ticket.'
}
abort $(new_issue_msg $app $bucket "hash check failed")
}
}
}
}
foreach($url in $urls) {
$fname = url_filename $url
$extract_dir = $extract_dirs[$extracted]
$extract_to = $extract_tos[$extracted]
# work out extraction method, if applicable
$extract_fn = $null
if ($manifest.innosetup) {
$extract_fn = 'Expand-InnoArchive'
} elseif($fname -match '\.zip$') {
$extract_fn = 'Expand-ZipArchive'
} elseif($fname -match '\.msi$') {
# check manifest doesn't use deprecated install method
if(msi $manifest $architecture) {
warn "MSI install is deprecated. If you maintain this manifest, please refer to the manifest reference docs."
} else {
$extract_fn = 'Expand-MSIArchive'
}
} elseif(Test-7ZipRequirement -File $fname) { # 7zip
$extract_fn = 'Expand-7ZipArchive'
}
if($extract_fn) {
Write-Host "Extracting " -NoNewline
Write-Host $fname -f Cyan -NoNewline
Write-Host " ... " -NoNewline
ensure "$dir\_tmp" | Out-Null
& $extract_fn "$dir\$fname" "$dir\_tmp" -Removal
if ($extract_to) {
ensure "$dir\$extract_to" | Out-Null
}
# fails if zip contains long paths (e.g. atom.json)
#cp "$dir\_tmp\$extract_dir\*" "$dir\$extract_to" -r -force -ea stop
try {
movedir "$dir\_tmp\$extract_dir" "$dir\$extract_to"
}
catch {
error $_
abort $(new_issue_msg $app $bucket "extract_dir error")
}
if(Test-Path "$dir\_tmp") { # might have been moved by movedir
try {
Remove-Item -r -force "$dir\_tmp" -ea stop
} catch [system.io.pathtoolongexception] {
& "$env:COMSPEC" /c "rmdir /s /q $dir\_tmp"
} catch [system.unauthorizedaccessexception] {
warn "Couldn't remove $dir\_tmp: unauthorized access."
}
}
Write-Host "done." -f Green
$extracted++
}
}
$fname # returns the last downloaded file
}
function cookie_header($cookies) {
if(!$cookies) { return }
$vals = $cookies.psobject.properties | ForEach-Object {
"$($_.name)=$($_.value)"
}
[string]::join(';', $vals)
}
function is_in_dir($dir, $check) {
$check = "$(fullpath $check)"
$dir = "$(fullpath $dir)"
$check -match "^$([regex]::escape("$dir"))(\\|`$)"
}
function ftp_file_size($url) {
$request = [net.ftpwebrequest]::create($url)
$request.method = [net.webrequestmethods+ftp]::getfilesize
$request.getresponse().contentlength
}
# hashes
function hash_for_url($manifest, $url, $arch) {
$hashes = @(hash $manifest $arch) | Where-Object { $_ -ne $null };
if($hashes.length -eq 0) { return $null }
$urls = @(url $manifest $arch)
$index = [array]::indexof($urls, $url)
if($index -eq -1) { abort "Couldn't find hash in manifest for '$url'." }
@($hashes)[$index]
}
# returns (ok, err)
function check_hash($file, $hash, $app_name) {
$file = fullpath $file
if(!$hash) {
warn "Warning: No hash in manifest. SHA256 for '$(fname $file)' is:`n $(compute_hash $file 'sha256')"
return $true, $null
}
Write-Host "Checking hash of " -NoNewline
Write-Host $(url_remote_filename $url) -f Cyan -NoNewline
Write-Host " ... " -nonewline
$algorithm, $expected = get_hash $hash
if ($null -eq $algorithm) {
return $false, "Hash type '$algorithm' isn't supported."
}
$actual = compute_hash $file $algorithm
$expected = $expected.ToLower()
if($actual -ne $expected) {
$msg = "Hash check failed!`n"
$msg += "App: $app_name`n"
$msg += "URL: $url`n"
if(Test-Path $file) {
$msg += "First bytes: $((get_magic_bytes_pretty $file ' ').ToUpper())`n"
}
if($expected -or $actual) {
$msg += "Expected: $expected`n"
$msg += "Actual: $actual"
}
return $false, $msg
}
Write-Host "ok." -f Green
return $true, $null
}
function compute_hash($file, $algname) {
try {
if([bool](Get-Command -Name Get-FileHash -ErrorAction SilentlyContinue) -eq $true) {
return (Get-FileHash -Path $file -Algorithm $algname).Hash.ToLower()
} else {
$fs = [system.io.file]::openread($file)
$alg = [system.security.cryptography.hashalgorithm]::create($algname)
$hexbytes = $alg.computehash($fs) | ForEach-Object { $_.tostring('x2') }
return [string]::join('', $hexbytes)
}
} catch {
error $_.exception.message
} finally {
if($fs) { $fs.dispose() }
if($alg) { $alg.dispose() }
}
return ''
}
function cmd_available($cmd) {
try { Get-Command $cmd -ea stop | out-null } catch { return $false }
$true
}
# for dealing with installers
function args($config, $dir, $global) {
if($config) { return $config | ForEach-Object { (format $_ @{'dir'=$dir;'global'=$global}) } }
@()
}
function run($exe, $arg, $msg, $continue_exit_codes) {
if($msg) { write-host "$msg " -nonewline }
try {
#Allow null/no arguments to be passed
$parameters = @{ }
if ($arg)
{
$parameters.arg = $arg;
}
$proc = start-process $exe -wait -ea stop -passthru @parameters
if($proc.exitcode -ne 0) {
if($continue_exit_codes -and ($continue_exit_codes.containskey($proc.exitcode))) {
warn $continue_exit_codes[$proc.exitcode]
return $true
}
write-host "Exit code was $($proc.exitcode)."; return $false
}
} catch {
write-host -f darkred $_.exception.tostring()
return $false
}
if($msg) { Write-Host "done." -f Green }
return $true
}
function run_installer($fname, $manifest, $architecture, $dir, $global) {
# MSI or other installer
$msi = msi $manifest $architecture
$installer = installer $manifest $architecture
if($installer.script) {
write-output "Running installer script..."
Invoke-Expression (@($installer.script) -join "`r`n")
return
}
if($msi) {
install_msi $fname $dir $msi
} elseif($installer) {
install_prog $fname $dir $installer $global
}
}
# deprecated (see also msi_installed)
function install_msi($fname, $dir, $msi) {
$msifile = "$dir\$(coalesce $msi.file "$fname")"
if(!(is_in_dir $dir $msifile)) {
abort "Error in manifest: MSI file $msifile is outside the app directory."
}
if(!($msi.code)) { abort "Error in manifest: Couldn't find MSI code."}
if(msi_installed $msi.code) { abort "The MSI package is already installed on this system." }
$logfile = "$dir\install.log"
$arg = @("/i `"$msifile`"", '/norestart', "/lvp `"$logfile`"", "TARGETDIR=`"$dir`"",
"INSTALLDIR=`"$dir`"") + @(args $msi.args $dir)
if($msi.silent) { $arg += '/qn', 'ALLUSERS=2', 'MSIINSTALLPERUSER=1' }
else { $arg += '/qb-!' }
$continue_exit_codes = @{ 3010 = "a restart is required to complete installation" }
$installed = run 'msiexec' $arg "Running installer..." $continue_exit_codes
if(!$installed) {
abort "Installation aborted. You might need to run 'scoop uninstall $app' before trying again."
}
Remove-Item $logfile
Remove-Item $msifile
}
# deprecated
# get-wmiobject win32_product is slow and checks integrity of each installed program,
# so this uses the [wmi] type accelerator instead
# http://blogs.technet.com/b/heyscriptingguy/archive/2011/12/14/use-powershell-to-find-and-uninstall-software.aspx
function msi_installed($code) {
$path = "hklm:\software\microsoft\windows\currentversion\uninstall\$code"
if(!(test-path $path)) { return $false }
$key = Get-Item $path
$name = $key.getvalue('displayname')
$version = $key.getvalue('displayversion')
$classkey = "IdentifyingNumber=`"$code`",Name=`"$name`",Version=`"$version`""
try { $wmi = [wmi]"Win32_Product.$classkey"; $true } catch { $false }
}
function install_prog($fname, $dir, $installer, $global) {
$prog = "$dir\$(coalesce $installer.file "$fname")"
if(!(is_in_dir $dir $prog)) {
abort "Error in manifest: Installer $prog is outside the app directory."
}
$arg = @(args $installer.args $dir $global)
if($prog.endswith('.ps1')) {
& $prog @arg
} else {
$installed = run $prog $arg "Running installer..."
if(!$installed) {
abort "Installation aborted. You might need to run 'scoop uninstall $app' before trying again."
}
# Don't remove installer if "keep" flag is set to true
if(!($installer.keep -eq "true")) {
Remove-Item $prog
}
}
}
function run_uninstaller($manifest, $architecture, $dir) {
$msi = msi $manifest $architecture
$uninstaller = uninstaller $manifest $architecture
if($uninstaller.script) {
write-output "Running uninstaller script..."
Invoke-Expression (@($uninstaller.script) -join "`r`n")
return
}
if($msi -or $uninstaller) {
$exe = $null; $arg = $null; $continue_exit_codes = @{}
if($msi) {
$code = $msi.code
$exe = "msiexec";
$arg = @("/norestart", "/x $code")
if($msi.silent) {
$arg += '/qn', 'ALLUSERS=2', 'MSIINSTALLPERUSER=1'
} else {
$arg += '/qb-!'
}
$continue_exit_codes.1605 = 'not installed, skipping'
$continue_exit_codes.3010 = 'restart required'
} elseif($uninstaller) {
$exe = "$dir\$($uninstaller.file)"
$arg = args $uninstaller.args
if(!(is_in_dir $dir $exe)) {
warn "Error in manifest: Installer $exe is outside the app directory, skipping."
$exe = $null;
} elseif(!(test-path $exe)) {
warn "Uninstaller $exe is missing, skipping."
$exe = $null;
}
}
if($exe) {
if($exe.endswith('.ps1')) {
& $exe @arg
} else {
$uninstalled = run $exe $arg "Running uninstaller..." $continue_exit_codes
if(!$uninstalled) { abort "Uninstallation aborted." }
}
}
}
}
# get target, name, arguments for shim
function shim_def($item) {
if($item -is [array]) { return $item }
return $item, (strip_ext (fname $item)), $null
}
function create_shims($manifest, $dir, $global, $arch) {
$shims = @(arch_specific 'bin' $manifest $arch)
$shims | Where-Object { $_ -ne $null } | ForEach-Object {
$target, $name, $arg = shim_def $_
write-output "Creating shim for '$name'."
if(test-path "$dir\$target" -pathType leaf) {
$bin = "$dir\$target"
} elseif(test-path $target -pathType leaf) {
$bin = $target
} else {
$bin = search_in_path $target
}
if(!$bin) { abort "Can't shim '$target': File doesn't exist."}
shim $bin $global $name (substitute $arg @{ '$dir' = $dir; '$original_dir' = $original_dir; '$persist_dir' = $persist_dir})
}
}
function rm_shim($name, $shimdir) {
$shim = "$shimdir\$name.ps1"
if(!(test-path $shim)) { # handle no shim from failed install
warn "Shim for '$name' is missing. Skipping."
} else {
write-output "Removing shim for '$name'."
Remove-Item $shim
}
# other shim types might be present
'', '.exe', '.shim', '.cmd' | ForEach-Object {
if(test-path -Path "$shimdir\$name$_" -PathType leaf) {
Remove-Item "$shimdir\$name$_"
}
}
}
function rm_shims($manifest, $global, $arch) {
$shims = @(arch_specific 'bin' $manifest $arch)
$shims | Where-Object { $_ -ne $null } | ForEach-Object {
$target, $name, $null = shim_def $_
$shimdir = shimdir $global
rm_shim $name $shimdir
}
}
# Gets the path for the 'current' directory junction for
# the specified version directory.
function current_dir($versiondir) {
$parent = split-path $versiondir
return "$parent\current"
}
# Creates or updates the directory junction for [app]/current,
# pointing to the specified version directory for the app.
#
# Returns the 'current' junction directory if in use, otherwise
# the version directory.
function link_current($versiondir) {
if(get_config NO_JUNCTIONS) { return $versiondir }
$currentdir = current_dir $versiondir
write-host "Linking $(friendly_path $currentdir) => $(friendly_path $versiondir)"
if($currentdir -eq $versiondir) {
abort "Error: Version 'current' is not allowed!"
}
if(test-path $currentdir) {
# remove the junction
attrib -R /L $currentdir
& "$env:COMSPEC" /c rmdir $currentdir
}
& "$env:COMSPEC" /c mklink /j $currentdir $versiondir | out-null
attrib $currentdir +R /L
return $currentdir
}
# Removes the directory junction for [app]/current which
# points to the current version directory for the app.
#
# Returns the 'current' junction directory (if it exists),
# otherwise the normal version directory.
function unlink_current($versiondir) {
if(get_config NO_JUNCTIONS) { return $versiondir }
$currentdir = current_dir $versiondir
if(test-path $currentdir) {
write-host "Unlinking $(friendly_path $currentdir)"
# remove read-only attribute on link
attrib $currentdir -R /L
# remove the junction
& "$env:COMSPEC" /c "rmdir $currentdir"
return $currentdir
}
return $versiondir
}
# to undo after installers add to path so that scoop manifest can keep track of this instead
function ensure_install_dir_not_in_path($dir, $global) {
$path = (env 'path' $global)
$fixed, $removed = find_dir_or_subdir $path "$dir"
if($removed) {
$removed | ForEach-Object { "Installer added '$(friendly_path $_)' to path. Removing."}
env 'path' $global $fixed
}
if(!$global) {
$fixed, $removed = find_dir_or_subdir (env 'path' $true) "$dir"
if($removed) {
$removed | ForEach-Object { warn "Installer added '$_' to system path. You might want to remove this manually (requires admin permission)."}
}
}
}
function find_dir_or_subdir($path, $dir) {
$dir = $dir.trimend('\')
$fixed = @()
$removed = @()
$path.split(';') | ForEach-Object {
if($_) {
if(($_ -eq $dir) -or ($_ -like "$dir\*")) { $removed += $_ }
else { $fixed += $_ }
}
}
return [string]::join(';', $fixed), $removed
}
function env_add_path($manifest, $dir, $global) {
$manifest.env_add_path | Where-Object { $_ } | ForEach-Object {
$path_dir = join-path $dir $_
if(!(is_in_dir $dir $path_dir)) {
abort "Error in manifest: env_add_path '$_' is outside the app directory."
}
add_first_in_path $path_dir $global
}
}