-
Notifications
You must be signed in to change notification settings - Fork 3
/
scoop-portable.cmd
866 lines (715 loc) · 30.9 KB
/
scoop-portable.cmd
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
@echo off
:: SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com) and contributors
:: SPDX-FileContributor: Sebastian Thomschke, Vegard IT GmbH
:: SPDX-License-Identifier: Apache-2.0
:: SPDX-ArtifactOfProjectHomePage: https://github.com/vegardit/scoop-portable
:: ABOUT
:: =====
:: This is a self-contained Windows batch file to install and launch a portable scoop (https://github.com/lukesampson/scoop) environment.
:: ############################################################################
:: act as wrapper for shims\scoop.cmd if this batch file is located at [scoop_install_root]\.portable\scoop.cmd
:: ############################################################################
call :ends_with "%~f0" ".portable\scoop.cmd" && (
call :intercept_scoop_command %*
goto :eof
)
:: ############################################################################
:: check if called with arguments, if so don't export variables to cmd process
:: ############################################################################
if not "%~1" == "" (
setlocal
)
:: ############################################################################
:: check if ANSI color output is supported
:: ############################################################################
for /F "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i
:: only Windows 10+ supports ANSI
if %VERSION% gtr 9 (
set ANSICON=1
)
:: ############################################################################
:: define env vars evaluated by scoop
:: ############################################################################
set SCOOP=%~dp0
set SCOOP=%SCOOP:~0,-1%
set SCOOP_CACHE=%SCOOP%\cache
set SCOOP_GLOBAL=%SCOOP%\globalApps
:: ############################################################################
:: install scoop if required
:: ############################################################################
if not exist "%SCOOP%\.portable" (
call :install_scoop || exit /B 1
) else (
if not exist "%SCOOP%\shims\scoop.cmd" (
call :install_scoop || exit /B 1
)
)
:: ##########################################################################
:: load the existing portable scoop installation
:: ##########################################################################
:: ==========================================================================
call :log_TASK Loading scoop-portable environment [%SCOOP%]
:: ==========================================================================
copy /Y "%~f0" "%SCOOP%\.portable\scoop.cmd" >NUL
:: ==========================================================================
call :log_TASK Checking file permissions
:: ==========================================================================
echo %USERDOMAIN%\%USERNAME%>"%SCOOP%\.portable\current.user"
fc "%SCOOP%\.portable\current.user" "%SCOOP%\.portable\last.user" >NUL 2>NUL
if errorlevel 1 (
call :log_WARN Granting user [%USERDOMAIN%\%USERNAME%] full access to [%SCOOP%]...
icacls "%SCOOP%" /Q /T /GRANT "%USERDOMAIN%\%USERNAME%:(CI)(OI)(F)"
)
del "%SCOOP%\.portable\current.user"
echo %USERDOMAIN%\%USERNAME%>"%SCOOP%\.portable\last.user"
:: ==========================================================================
:: check if installation location was moved
:: ==========================================================================
if exist "%SCOOP%\.portable\last.dir" (
echo %SCOOP%>"%SCOOP%\.portable\current.dir"
vol %SCOOP:~0,1%:>>"%SCOOP%\.portable\current.dir"
fc "%SCOOP%\.portable\current.dir" "%SCOOP%\.portable\last.dir" >NUL 2>NUL
if errorlevel 1 (
call :fix_paths || exit /B 1
)
del "%SCOOP%\.portable\current.dir"
)
echo %SCOOP%>"%SCOOP%\.portable\last.dir"
vol %SCOOP:~0,1%:>>"%SCOOP%\.portable\last.dir"
:: ==========================================================================
call :log_TASK Setting environment variables
:: ==========================================================================
call :extend_PATH "%SCOOP%\shims"
:: important to add .portable after shims so that out scoop wrapper is used
call :extend_PATH "%SCOOP%\.portable"
call :set_app_env_vars
call :log_SUCCESS The portable scoop environment is ready.
:: ==========================================================================
:: if scoop portable was launched with arguments, execute the arguments
:: ==========================================================================
if not "%~1" == "" (
%*
goto :eof
)
:: ==========================================================================
:: determine if a command window needs to be launched
:: ==========================================================================
:: check if launched via windows explorer
if /I "%CmdCmdLine:"=%" == "%ComSpec% /c %~dpf0 " (
title Command Prompt
if exist "%SCOOP%\apps\clink\current\clink.bat" (
call :log_TASK Loading clink
cmd /K %SCOOP%\apps\clink\current\clink.bat inject --quiet
) else (
cmd
)
goto :eof
)
:: launched via other batch file or manually from command window
goto :eof
:install_scoop
:: ##########################################################################
:: create a new portable scoop installation
:: ##########################################################################
call :log_HEADER Installing [scoop] at [%SCOOP%]...
where /Q scoop && (
call :exit_with_ERROR Cannot install scoop, 'scoop' command already on PATH
)
:: https://github.com/ScoopInstaller/Scoop/wiki/Quick-Start#installing-scoop
:: ==========================================================================
:: default config, can be overridden via scoop-portable-config.cmd
:: ==========================================================================
setlocal EnableDelayedExpansion
::set SCOOP_PROXY=<HOSTNAME>:<PORT>
::set SCOOP_PROXY=myproxy.local:8080
set SCOOP_PROXY=
:: if set to true the Windows credentials of the logged-in user are used for proxy authentication
set SCOOP_PROXY_USE_WINDOWS_CREDENTIALS=false
:: if PROXY_USE_WINDOWS_CREDENTIALS is set to false, then use these credentials for proxy authentication
set SCOOP_PROXY_USER=
set SCOOP_PROXY_PASSWORD=
:: additional scoop buckets to register by default
:: set SCOOP_BUCKETS=extras java
set SCOOP_BUCKETS=
:: packages to install by default
set SCOOP_PACKAGES=
:: ==========================================================================
:: load custom config from separate file if exists
:: ==========================================================================
set custom_config_file=%~dp0scoop-portable-config.cmd
if exist "%custom_config_file%" (
call :log_TASK Loading custom config from [%custom_config_file%]
call "%custom_config_file%" || exit /B 1
)
:: ==========================================================================
:: Setting PowerShell ExecutionPolicy [RemoteSigned] if required
:: ==========================================================================
powershell -noprofile -command ^
if ((Get-ExecutionPolicy).ToString() -notin @('Unrestricte', 'RemoteSigne', 'ByPass')) { ^
Write-Host "[$(Get-Date -Format 'HH:mm:ss,ff')] Setting PowerShell ExecutionPolicy [RemoteSigned]..."; ^
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser ^
}
:: ==========================================================================
call :log_TASK Downloading scoop installer
:: ==========================================================================
:: https://github.com/lukesampson/scoop/wiki/Using-Scoop-behind-a-proxy
if not "%SCOOP_PROXY%" == "" (
call :log_TASK Downloading scoop installer using proxy [%SCOOP_PROXY%]
set "scoopProxy=[net.webrequest]::defaultwebproxy = new-object net.webproxy 'http://%SCOOP_PROXY%';"
if "%SCOOP_PROXY_USE_WINDOWS_CREDENTIALS%" == "true" (
set "scoopProxy=!scoopProxy!; [net.webrequest]::defaultwebproxy.credentials = [net.credentialcache]::defaultcredentials;"
) else if not "%SCOOP_PROXY_USER%" == "" (
set "scoopProxy=!scoopProxy!; [net.webrequest]::defaultwebproxy.credentials = new-object net.networkcredential '%SCOOP_PROXY_USER%', '%SCOOP_PROXY_PASSWORD%';"
)
)
call :mkdirs "%SCOOP%\.portable"
:: 1) replacing '$env:USERPROFILE\.config' is a workaround for https://github.com/ScoopInstaller/Scoop/issues/4498
:: to make <USERPROFILE>\.config\scoop\config.json portable
:: 2) replacing ' Add-ShimsDirToPath' to prevent shim dir being permanently added to %PATH%
powershell -noprofile -command !scoopProxy! ^
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; ^
$installer_script = (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh'); ^
$installer_script = $installer_script.replace('$env:XDG_CONFIG_HOME', '\"$env:SCOOP\.portable\"'); ^
$installer_script = $installer_script -replace '\s\s+Add-ShimsDirToPath', ''; ^
Set-Content -Path "$env:TEMP\scoop_installer.ps1" -Value $installer_script || exit /B 1
powershell -noprofile -File "%TEMP%\scoop_installer.ps1" || exit /B 1
del "%TEMP%\scoop_installer.ps1"
call :patch_scoop
:: installing itself as scoop wrapper
copy /Y "%~f0" "%SCOOP%\.portable\scoop.cmd" >NUL
echo %USERDOMAIN%\%USERNAME%>"%SCOOP%\.portable\last.user"
if not "%SCOOP_PROXY%" == "" (
call "%SCOOP%\.portable\scoop.cmd" config proxy %SCOOP_PROXY% || exit /B 1
)
if not "%SCOOP_BUCKETS%" == "" (
REM install git if not present - required for adding buckets
where /Q git.exe
if errorlevel 1 (
call :has_substring "%SCOOP_PACKAGES%" "git-with-openssh"
if errorlevel 1 (
call :log_TASK Installing [git]
call "%SCOOP%\.portable\scoop.cmd" install git
call :extend_PATH "%SCOOP%\shims"
) else (
call :log_TASK Installing [git-with-openssh]
call "%SCOOP%\.portable\scoop.cmd" install git-with-openssh
call :extend_PATH "%SCOOP%\shims"
)
)
for %%b in (%SCOOP_BUCKETS%) do (
call :log_TASK Adding scoop bucket [%%b]
call "%SCOOP%\.portable\scoop.cmd" bucket add %%b
)
)
if not "%SCOOP_PACKAGES%" == "" (
call :log_TASK Installing packages [%SCOOP_PACKAGES%]
setlocal
for %%p in (%SCOOP_PACKAGES%) do (
call "%SCOOP%\.portable\scoop.cmd" install %%~p
)
endlocal
)
goto :eof
:fix_paths
:: ##########################################################################
:: function to fix paths after scoop dir was moved
:: ##########################################################################
setlocal
call :log_WARN Installation directory was moved. Fixing paths...
set fix_paths=^
Set-StrictMode -version latest; ^
$last_dir = (Get-Content -path '%SCOOP%\.portable\last.dir' -first 1).trim() + '\'; ^
^
function replaceScoopPaths($file_path) { ^
if (Test-Path -path $file_path) { ^
$old = Get-Content -path $file_path -raw; ^
if (-not [string]::IsNullOrEmpty($old)) { ^
$new = $old.replace($last_dir, '%SCOOP%\'); ^
if ($old -ne $new) { ^
Write-Host "[$(Get-Date -Format 'HH:mm:ss,ff')] --^> Path updated in: $file_path"; ^
Set-Content -noNewline -path $file_path -value $new; ^
} ^
} ^
} ^
} ^
^
replaceScoopPaths '%SCOOP%\.portable\scoop\config.json'; ^
replaceScoopPaths '%SCOOP%\shims\scoop'; ^
replaceScoopPaths '%SCOOP%\shims\scoop.cmd'; ^
replaceScoopPaths '%SCOOP%\shims\scoop.ps1'; ^
^
Get-ChildItem '%SCOOP%\.portable\active_versions' -file -filter *.env_set.cmd ^| Foreach-Object { replaceScoopPaths $_.FullName }; ^
Get-ChildItem '%SCOOP%\apps' -file -filter *.ini -recurse ^| Foreach-Object { replaceScoopPaths $_.FullName }; ^
Get-ChildItem '%SCOOP%\shims' -file -filter *.shim ^| Foreach-Object { replaceScoopPaths $_.FullName }; ^
^
function fixAppCurrentVersionSymlinks($app_curr_ver_path) { ^
$app_name = $app_curr_ver_path.Parent.Name; ^
if ($app_name -eq 'scoop') { return; } ^
$app_manifest = Get-Content -path "%SCOOP%\.portable\active_versions\$app_name.json" -raw ^| ConvertFrom-Json; ^
$app_curr_ver = $app_manifest.version; ^
if (Test-Path -Path $app_curr_ver_path) { ^
fsutil reparsepoint delete $app_curr_ver_path ^| out-null; ^
Remove-Item $app_curr_ver_path -recurse -force; ^
} ^
New-Item -itemType Junction -path $app_curr_ver_path -target "%SCOOP%\apps\$app_name\$app_curr_ver" ^| out-null; ^
Write-Host "[$(Get-Date -Format 'HH:mm:ss,ff')] --^> Junction updated: $app_curr_ver_path"; ^
^
if ('persist' -in $app_manifest.PSobject.Properties.Name) { ^
$app_manifest.persist ^| ForEach-Object { ^
$app_persist_path = $_; ^
if ((Get-Item "%SCOOP%\persist\$app_name\$app_persist_path") -is [System.IO.DirectoryInfo]) { ^
fsutil reparsepoint delete "$app_curr_ver_path\$app_persist_path" ^| out-null; ^
Remove-Item "$app_curr_ver_path\$app_persist_path" -recurse -force; ^
New-Item -itemType Junction -path "$app_curr_ver_path\$app_persist_path" -target "%SCOOP%\persist\$app_name\$app_persist_path" ^| out-null; ^
Write-Host "[$(Get-Date -Format 'HH:mm:ss,ff')] --^> Junction updated: $app_curr_ver_path\$app_persist_path"; ^
} else { ^
Remove-Item "$app_curr_ver_path\$app_persist_path" -force; ^
New-Item -itemType HardLink -path "$app_curr_ver_path\$app_persist_path" -target "%SCOOP%\persist\$app_name\$app_persist_path" ^| out-null; ^
Write-Host "[$(Get-Date -Format 'HH:mm:ss,ff')] --^> HardLink updated: $app_curr_ver_path\$app_persist_path"; ^
} ^
} ^
} ^
} ^
^
Get-ChildItem '%SCOOP%\apps\*\*' -directory -filter current ^| Foreach-Object { fixAppCurrentVersionSymlinks $_ }; ^
Get-ChildItem '%SCOOP%\apps\*' -directory ^| Where-Object { -not (Test-Path '$_\current') } ^| Foreach-Object { fixAppCurrentVersionSymlinks ([System.IO.DirectoryInfo](Join-Path $_.FullName 'current')) }; ^
#
powershell -noprofile -ex unrestricted -command "%fix_paths%" || exit /B 1
goto :eof
:intercept_scoop_command
:: ##########################################################################
:: wrapper for shims\scoop.cmd
:: ##########################################################################
:: ==========================================================================
:: check if scoop [command] --help is requested
:: ==========================================================================
call :has_arg --help %* && (
call "%SCOOP%\shims\scoop.cmd" %*
goto :eof
)
setlocal EnableDelayedExpansion
set scoop_command=%1
:: ==========================================================================
:: INTERCEPT scoop update
:: ==========================================================================
if "%scoop_command%" == "update" (
call :get_2nd_positional_arg app_name %*
if "!app_name!" == "" (
set app_name=scoop
)
if !app_name! == scoop (
call :unpatch_scoop
set "XDG_CONFIG_HOME=%SCOOP%\.portable"
)
call :getx_PATH PATH_BEFORE_UPDATE
call "%SCOOP%\shims\scoop.cmd" %*
set rc=!errorlevel!
if !app_name! == scoop (
call :patch_scoop
) else if "!app_name!" == "*" (
call :save_active_versions
) else (
REM /%* makes the first arg (the command) a flag so it is not treated as an app name
call :get_positional_args apps /%*
for %%a in (!apps!) do call :save_active_version %%a
)
call :getx_PATH PATH_AFTER_UPDATE
if not "!PATH_BEFORE_UPDATE!"=="!PATH_AFTER_UPDATE!" (
call :log_TASK Restoring PATH variable
setx PATH "!PATH_BEFORE_UPDATE!"
)
exit /B !rc!
)
:: ==========================================================================
:: INTERCEPT scoop install
:: ==========================================================================
if "%scoop_command%" == "install" (
call :has_arg --global %* && set global_install=true
call :has_arg -g %* && set global_install=true
if "!global_install!" == "true" (
call :exit_with_ERROR Installing applications globally is not supported by scoop-portable.
)
call "%SCOOP%\shims\scoop.cmd" %*
set rc=%errorlevel%
REM /%* makes the first arg (the command) a flag so it is not treated as an app name
call :get_positional_args apps /%*
for %%a in (!apps!) do call :save_active_version %%a
REM save app states of dependencies (if any)
call :save_active_versions_of_new_apps
endlocal
call :set_app_env_vars
exit /B !rc!
)
:: ==========================================================================
:: INTERCEPT scoop uninstall
:: ==========================================================================
if "%scoop_command%" == "uninstall" (
call :get_2nd_positional_arg app_name %*
call "%SCOOP%\shims\scoop.cmd" %*
set rc=%errorlevel%
call :cleanup_active_versions
exit /B !rc!
)
:: ==========================================================================
:: INTERCEPT scoop reset
:: ==========================================================================
if "%scoop_command%" == "reset" (
call "%SCOOP%\shims\scoop.cmd" %*
set rc=%errorlevel%
REM /%* makes the first arg (the command) a flag so it is not treated as an app name
call :get_positional_args apps /%*
for %%a in (!apps!) do call :save_active_version %%a
endlocal
call :set_app_env_vars
exit /B !rc!
)
:: ==========================================================================
:: EXECUTE other scoop command
:: ==========================================================================
call "%SCOOP%\shims\scoop.cmd" %*
exit /B %errorlevel%
goto :eof
:save_active_versions
setlocal
for /F %%f in ('dir /B "%SCOOP%\apps\*" 2^>NUL') do (
call :save_active_version %%~f
)
goto :eof
:save_active_versions_of_new_apps
setlocal
for /F %%f in ('dir /B "%SCOOP%\apps\*" 2^>NUL') do (
if not exist "%SCOOP%\.portable\active_versions\%%~f.json" (
call :save_active_version %%~f
)
)
goto :eof
:save_active_version <APP_NAME(@<APP_VERSION>)>
setlocal
call :mkdirs "%SCOOP%\.portable\active_versions"
set app=%~1
:: extract appname from app@version
call :substring_before %app% @ app_name
if not exist "%SCOOP%\apps\%app_name%\current\manifest.json" exit /B 0
copy /Y "%SCOOP%\apps\%app_name%\current\manifest.json" "%SCOOP%\.portable\active_versions\%app_name%.json" >NUL
:: check if expensive json parsing via powershell is necessary
findstr /C:env_set /C:env_add_path "%SCOOP%\.portable\active_versions\%app_name%.json" >NUL
if %errorlevel% == 1 exit /B 0
:: the "if ($env_key -eq 'JAVA_HOME')" branch is for switching between different java versions to ensure only one is on PATH
set save_env_additions= ^
Set-StrictMode -version latest; ^
$app_manifest = (Get-Content -path '%SCOOP%\.portable\active_versions\%app_name%.json' -raw ^| ConvertFrom-Json); ^
if ('env_set' -in $app_manifest.PSobject.Properties.Name) { ^
$app_manifest.env_set.PSObject.Properties ^| ForEach-Object { ^
$env_key = $_.Name; ^
$env_val = $_.Value.replace('$dir', '%SCOOP%\apps\%app_name%\current').replace('$persist_dir', '%SCOOP%\persist\%app_name%'); ^
if ($env_key -eq 'JAVA_HOME') { ^
Get-ChildItem -path '%SCOOP%\.portable\active_versions\*.JAVA_HOME.env_set.cmd' ^| ForEach-Object { ^
$jdk_name = $_.Name.split('.JAVA_HOME')[0]; ^
Remove-Item -path """%SCOOP%\.portable\active_versions\$jdk_name.*.env_add_path"""; ^
Remove-Item $_ -force; ^
} ^
} ^
$env_cmd = """@set $env_key=$env_val"""; ^
Set-Content -path """%SCOOP%\.portable\active_versions\%app_name%.$env_key.env_set.cmd""" -value $env_cmd; ^
} ^
} ^
if ('env_add_path' -in $app_manifest.PSobject.Properties.Name) { ^
$app_manifest.env_add_path ^| ForEach-Object -Begin {$i = 0} -Process { ^
$i++; ^
$env_add_path = $_; ^
Set-Content -path """%SCOOP%\.portable\active_versions\%app_name%.$i.env_add_path""" -value $env_add_path; ^
} ^
} ^
#
powershell -noprofile -command "%save_env_additions%"
goto :eof
:cleanup_active_versions
setlocal EnableDelayedExpansion
for /F %%f in ('dir /B "%SCOOP%\.portable\active_versions\*.json" 2^>NUL') do (
call :substring_before "%%~f" . app_name
if not exist "%SCOOP%\apps\!app_name!\current" (
del /F "%SCOOP%\.portable\active_versions\%%~f" >NUL
)
)
for /F %%f in ('dir /B "%SCOOP%\.portable\active_versions\*.env_add_path" 2^>NUL') do (
call :substring_before "%%~f" . app_name
if not exist "%SCOOP%\apps\!app_name!\current" (
del /F "%SCOOP%\.portable\active_versions\%%~f" >NUL
)
)
for /F %%f in ('dir /B "%SCOOP%\.portable\active_versions\*.env_set.cmd" 2^>NUL') do (
call :substring_before "%%~f" . app_name
if not exist "%SCOOP%\apps\!app_name!\current" (
del /F "%SCOOP%\.portable\active_versions\%%~f" >NUL
)
)
goto :eof
:patch_scoop
:: ##########################################################################
:: patch scoop to make it more portable
:: ##########################################################################
call :log_TASK Patching scoop
setlocal
:: replacing '$env:USERPROFILE\.config' is a workaround for https://github.com/ScoopInstaller/Scoop/issues/4498
:: to make <USERPROFILE>\.config\scoop\config.json portable
::
:: -replace '\n\s+env 'PATH'.*?\r?\n', '' to prevent persistent changes to global PATH variable in core.ps1
:: -replace '\n\s+env \$name.*?\r?\n', '' to prevent persistent changes to global PATH variable in install.ps1
set patch_scoop=^
Set-StrictMode -version latest; ^
$new = $old = Get-Content -path '%SCOOP%\apps\scoop\current\lib\core.ps1' -raw; ^
$new = $new.replace('$env:XDG_CONFIG_HOME', '\"$env:SCOOP\.portable\"'); ^
$new = $new -replace '\n\s+env ''PATH''.*?\r?\n', ''; ^
if ($old -ne $new) { Set-Content -noNewline -path '%SCOOP%\apps\scoop\current\lib\core.ps1' -value $new } ^
^
$new = $old = Get-Content -path '%SCOOP%\apps\scoop\current\lib\install.ps1' -raw; ^
$new = $new -replace '\n\s+env \$name.*?\r?\n', ''; ^
if ($old -ne $new) { Set-Content -noNewline -path '%SCOOP%\apps\scoop\current\lib\install.ps1' -value $new } ^
^
$new = $old = Get-Content -path '%SCOOP%\apps\scoop\current\lib\psmodules.ps1' -raw; ^
$new = $new -replace '\n\s+env ''psmodulepath''.*?\r?\n', ''; ^
if ($old -ne $new) { Set-Content -noNewline -path '%SCOOP%\apps\scoop\current\lib\psmodules.ps1' -value $new } ^
^
$new = $old = Get-Content -path '%SCOOP%\apps\scoop\current\lib\shortcuts.ps1' -raw; ^
if (-not $new.contains('function create_startmenu_shortcuts($manifest, $dir, $global, $arch) { }')) { ^
$new = $new + 'function create_startmenu_shortcuts($manifest, $dir, $global, $arch) { }' + """`n"""; ^
} ^
if (-not $new.contains('function startmenu_shortcut([System.IO.FileInfo] $target, $shortcutName, $arguments, [System.IO.FileInfo]$icon, $global) { }')) { ^
$new = $new + 'function startmenu_shortcut([System.IO.FileInfo] $target, $shortcutName, $arguments, [System.IO.FileInfo]$icon, $global) { }' + """`n"""; ^
} ^
if ($old -ne $new) { Set-Content -noNewline -path '%SCOOP%\apps\scoop\current\lib\shortcuts.ps1' -value $new } ^
#
powershell -noprofile -ex unrestricted -command "%patch_scoop%" || exit /B 1
goto :eof
:unpatch_scoop
:: ##########################################################################
:: revert scoop patches
:: ##########################################################################
if exist "%SCOOP%\apps\scoop\current\.git" (
call :log_TASK Reverting scoop patch
pushd "%SCOOP%\apps\scoop\current"
git checkout "lib\*.ps1"
popd
)
goto :eof
:set_app_env_vars
:: ##########################################################################
:: set app specific env variables
:: ##########################################################################
setlocal EnableDelayedExpansion
for /F %%f in ('dir /B "%SCOOP%\.portable\active_versions\*.env_add_path" 2^>NUL') do (
call :read_first_line_of_file "%SCOOP%\.portable\active_versions\%%~f" path_to_add
call :substring_before "%%~f" . app_name
if "!path_to_add!" == "." (
call :extend_PATH "%SCOOP%\apps\!app_name!\current"
) else (
call :extend_PATH "%SCOOP%\apps\!app_name!\current\!path_to_add!"
)
)
endlocal & set "PATH=%PATH%"
setlocal
for /F %%f in ('dir /B "%SCOOP%\.portable\active_versions\*.env_set.cmd" 2^>NUL') do (
endlocal & call "%SCOOP%\.portable\active_versions\%%f"
setlocal
)
endlocal
if exist "%SCOOP%\apps\clink\current\clink.bat" (
set "CLINK_PROFILE=%SCOOP%\persist\clink"
)
if exist "%SCOOP%\apps\nvm\current" (
set "NVM_HOME=%SCOOP%\apps\nvm\current\nvm.exe"
set "NVM_SYMLINK=%SCOOP%\persist\nvm\nodejs\nodejs"
call :extend_PATH "%SCOOP%\persist\nvm\nodejs"
)
if exist "%SCOOP%\apps\git-with-openssh\current\git-cmd.exe" (
call :append_PATH "%SCOOP%\apps\git-with-openssh\current\usr\bin"
where /Q vi.exe || doskey vi=vim
) else if exist "%SCOOP%\apps\git\current\git-cmd.exe" (
call :append_PATH "%SCOOP%\apps\git\current\usr\bin"
where /Q vi.exe || doskey vi=vim
)
goto :eof
:: ############################################################################
:: utility methods
:: ############################################################################
:append_PATH <PATH>
call :replace_substrings "%PATH%" "%~1;" "" PATH
call :ends_with "%PATH%" ";" && set "PATH=%PATH%%~1;" || set "PATH=%PATH%;%~1;"
goto :eof
:extend_PATH <PATH>
call :replace_substrings "%PATH%" "%~1;" "" PATH
set "PATH=%~1;%PATH%"
goto :eof
:exit_with_ERROR
:: only Windows 10+ supports ANSI
if "%ANSICON%" == "1" (
echo [91m[%time%] ERROR: %*[0m
) else (
echo [%time%] ERROR: %*
)
%SystemRoot%\System32\timeout.exe /T 30
exit /B 1
:getx_PATH <RESULT_VAR>
:: counterpart to "setx PATH" command
setlocal
set result_var=%~1
for /F "tokens=2* skip=2" %%a in ('reg query "HKEY_CURRENT_USER\Environment" /v PATH') do (
set "value=%%b"
)
endlocal & set "%result_var%=%value%"
goto :eof
:: ============================================================================
:: logging
:: ============================================================================
:log_HEADER <MSG,...>
if "%ANSICON%" == "1" (
echo [1m===========================================================[0m
echo [%time%] [1m%*[0m
echo [1m===========================================================[0m
) else (
echo ===========================================================
echo [%time%] %*
echo ===========================================================
)
echo.
goto :eof
:log_TASK <MSG,...>
:: only Windows 10+ supports ANSI
if "%ANSICON%" == "1" (
echo [%time%] [1m%*...[0m
) else (
echo [%time%] %*...
)
goto :eof
:log_WARN <MSG,...>
:: only Windows 10+ supports ANSI
if "%ANSICON%" == "1" (
echo [%time%] [93mWARNING: %*[0m
) else (
echo [%time%] WARNING: %*
)
goto :eof
:log_SUCCESS <MSG,...>
:: only Windows 10+ supports ANSI
if "%ANSICON%" == "1" (
echo [%time%] [92mSUCCESS: %*[0m
) else (
echo [%time%] SUCCESS: %*
)
goto :eof
:: ============================================================================
:: file system operations
:: ============================================================================
:read_first_line_of_file <FILE_PATH> <RESULT_VAR>
setlocal
set filePath=%~1
set result_var=%~2
set /P content=<"%filePath%"
endlocal & set "%result_var%=%content%"
goto :eof
:mkdirs <PATH>
:: like "mkdir -p" on Linux
setlocal enableextensions
if not exist %1 md %1
goto :eof
:: ============================================================================
:: string operations
:: ============================================================================
:ends_with <SEARCH_IN> <SEARCH_FOR>
echo %~1|findstr /E /L %2 >NUL
goto :eof
:has_substring <SEARCH_IN> <SEARCH_FOR>
setlocal
set searchIn=%~1
set searchFor=%~2
call :replace_substrings "%searchIn%" "%searchFor%" "" "result"
if "%searchIn%" == "%result%" (
REM substring not found
exit /B 1
)
goto :eof
:replace_substrings <SEARCH_IN> <SEARCH_FOR> <REPLACE_WITH> <RESULT_VAR>
setlocal
set searchIn=%~1
set searchFor=%~2
set replaceWith=%~3
set result_var=%~4
call set result=%%searchIn:%searchFor%=%replaceWith%%%
endlocal & set "%result_var%=%result%"
goto :eof
:substring_before <SEARCH_IN> <SEARCH_FOR> <RESULT_VAR>
setlocal
set searchIn=%~1
set separator=%~2
set result_var=%~3
for /F "delims=%separator%" %%a in ("%searchIn%") do (
endlocal & set "%result_var%=%%a"
exit /B 0
)
goto :eof
:: ============================================================================
:: arg parsing
:: ============================================================================
:has_arg <SEARCH_FOR> <ARG,...>
setlocal
set "search_for=%~1" & shift /1
set empty_args=0
REM not using "for %%a in (%*)" which automatically expands wildcard arguments
:has_arg___CHECK_NEXT_ARG
set "arg=%~1"
if "%arg%" == "%search_for%" exit /B 0
if "%arg%" == "" (
REM stop looping if more than 6 empty args in a row were found. this is a workaround for the fact that one cannot
REM distinguish between an empty "" argument and the end of the argument list
if %empty_args% == 6 (
exit /B 1
) else (
set /a empty_args+=1
)
) else (
set empty_args=0
)
shift /1
goto :has_arg___CHECK_NEXT_ARG
:get_positional_args <RESULT_VAR>
setlocal EnableDelayedExpansion
set result_var=%~1
set args=
:: /%* makes the first arg (containing the result var name) a flag so it is ignored in the loop
for %%a in (/%*) do (
set a=%%~a
set first_char=!a:~0,1!
if not "!first_char!" == "-" (
if not "!first_char!" == "/" (
set args=!args! "!a!"
)
)
)
endlocal & set %result_var%=%args%
goto :eof
:get_1st_positional_arg <RESULT_VAR> <ARG,...>
call :get_nth_positional_arg 1 %*
goto :eof
:get_2nd_positional_arg <RESULT_VAR> <ARG,...>
call :get_nth_positional_arg 2 %*
goto :eof
:get_nth_positional_arg <ARG_INDEX> <RESULT_VAR> <ARG,...>
setlocal EnableDelayedExpansion
set "wanted_pos_arg_index=%~1" & shift /1
set "result_var=%~1" & shift /1
set current_pos_arg_index=0
REM not using "for %%a in (%*)" which automatically expands wildcard arguments
:get_nth_positional_arg___CHECK_NEXT_ARG
set "arg=%~1"
set first_char=%arg:~0,1%
if not "%first_char%" == "-" (
if not "%first_char%" == "/" (
set /A current_pos_arg_index=%current_pos_arg_index%+1
if !current_pos_arg_index! equ %wanted_pos_arg_index% (
endlocal & set "%result_var%=%arg%"
exit /B 0
)
)
)
shift /1
goto :get_nth_positional_arg___CHECK_NEXT_ARG