-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfigure_win.ps1
1081 lines (947 loc) · 35.5 KB
/
configure_win.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
function New-Hardlink() { New-Item -ItemType HardLink -Force @args; }
function New-Softlink() { New-Item -ItemType SymbolicLink -Force @args; }
function New-Dir() { New-Item -ItemType Directory -Force @args; }
function New-File() { New-Item -ItemType File -Force @args; }
class App {
#region Instance properties
$_ver = "1.0.25";
$_isTest = $false;
$_isFull = $false;
$_isUpdateEnv = $false;
$_isPublic = $false;
$_POST_INSTALL_MSG = "";
$_keepassdb = $null;
$_pass = $null;
$_cfgDirLinux = $null;
$_cfgDir = $null;
$_psDir = $null;
$_pathIntrinsics = $null;
$_github = @{
user = "foo";
pass = "bar";
token = "baz";
};
$_mqtt = @{
url = $null;
user = $null;
pass = $null;
};
$_vk = @{
cc_token = $null;
};
#endregion
App($argList, $pathIntrinsics) {
$this._pathIntrinsics = $pathIntrinsics;
$this._isTest = ($argList.Contains("--test"));
$this._isFull = ($argList.Contains("--full"));
$this._isUpdateEnv = ($argList.Contains("--update-env"));
# Do not touch private info like passwords, personal kb etc.
$this._isPublic = ($argList.Contains("--public"));
# Version-controlled dir with scripts, powershell config, passwords etc.
$this._cfgDirLinux = "~/dotfiles";
$this._cfgDir = $this._path(@("~", "dotfiles"));
$this._psDir = $this._path(@("~", "Documents", "PowerShell"));
$this._POST_INSTALL_MSG = @"
Config complete. Manual things to do
- Reboot and make --full configuration
- Load X-Mouse Button Control settings
* Disable 'Settings/Pointer/Change cursor when move to scroll'
* Map Mouse4 => 'change movement to scroll' with setings:
* Sensitivity 1
* Invert axis
- Disable adaptive contrast for the built-in Intel GPU, if any
- "Change Proxy Settings", Turn off "Automatically Detect Settings"
- Add C-S-4-5-6 as en-ru-js hotkeys in Time/Lang/Typing/Advanced/Keys
- Unassign switch between languages in Time/Lang/Typing/Advanced/Keys
- copy lang settings via Time/Lang/Administrative"
- Disable spam in System/Notification/Additional
- Disable autostart in Task Manager
- Disable autostart in Task Scheduler
- Disable snap assist
- Disable touchpad click and set maximum speed
- Pin term, vscode, web, double, pass, telegram, wino, notionc, trello
- Uninstall 'OneDrive' and other software
- Login and sync browser
- Switch nVidia display mode to "optimus" and drag gpu activity icon
- Set Settings/Accounts/Sign-in/Sign-in to "Every time"
- Set Settings/System/Power/Saver/Auto to "Never"
- Uncheck "Sniping tool" in Settings/Accessibility/Keyboard
- Disable ASUS "lightingservice", if any
- Disable G-Sync in the nVidia settings
- Disable the "SSDP Discovery" service
- Disable Battle.net launcher "gpu acceleration"
- Configure Win11 for top taskbar with small icons
"@;
}
configure() {
Write-Host "Running configuration script v$($this._ver)";
# For 'Install-Module'
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted;
if (-not $this._isTest) {
if (-not (Test-Path -Path "$($this._cfgDir)")) {
New-Dir -Path "$($this._cfgDir)";
}
}
# Auto-created by PowerShell 5.x until 6.x+ is a system default.
# Create and set hidden attribute to exclude from 'ls'.
if (-not $this._isTest) {
$oldPsDir = $this._path(@("~", "Documents", "WindowsPowerShell"));
if (-not (Test-Path -Path "$oldPsDir")) {
Write-Host "Creating dir $oldPsDir";
$ret = & New-Dir -Path "$oldPsDir";
$ret.Attributes = 'Hidden';
}
else {
Write-Host "$oldPsDir already exists";
}
}
# PowerShell config is loaded from this dir.
# Create and set hidden attribute to exclude from 'ls'.
if (-not $this._isTest) {
if (-not (Test-Path -Path "$($this._psDir)")) {
Write-Host "Creating dir $($this._psDir)";
$ret = & New-Dir -Path "$($this._psDir)";
$ret.Attributes = 'Hidden';
}
else {
Write-Host "$($this._psDir) already exists";
}
}
Write-Host "Downloading let's encrypt root certificate..."
$url = "https://letsencrypt.org/certs/isrgrootx1.pem";
$certFile = $this._path(@("~", "isrgrootx1.pem"));
Invoke-WebRequest $url -OutFile $certFile
$this._setEnv("MQTT_CERT", $certFile);
# Game compatibility
$this._setEnv("OPENSSL_ia32cap", "~0x20000000");
# uv tool install target
$this._addToPath($this._path(@("~", ".local", "bin")));
if (-not $this._isTest) {
# Ensure at least 1.9 version for "add to path" manifest flag
& winget upgrade winget;
# Enable install from manifests
& winget settings --enable LocalManifestFiles
}
$this._installApp("Microsoft.VCRedist.2015+.x64");
# Requires reboot for a second stage install
$this._installWsl();
$this._installPowershellModule("posh-git");
$this._installPowershellModule("WindowsCompatibility");
$this._generateSshKey();
$this._setPowerOptions();
$this._setDebounceOptions();
$this._setTouchpadOptions();
$this._setInputMethodOptions();
$this._installBinApp("Git.Git", $this._path(
@(${env:ProgramFiles}, "Git", "cmd")));
# Clone without keys via HTTPS
$this._getFilesFromGit();
$this._installLocationApp("AutoHotkey.AutoHotkey", "");
$this._uninstallApp("Microsoft.OneDrive");
$this._uninstallApp("Microsoft.Teams");
$this._uninstallApp("Copilot");
# TODO: Need version 2.20.4, in 2.20.5 "{WAITMS:100}{LMB}" does not work.
# https://dvps.highrez.co.uk/downloads/XMouseButtonControlSetup.2.20.4.exe
# Auto registers to run on startup
$this._installApp("Highresolution.X-MouseButtonControl");
$this._installBinApp("KeePassXCTeam.KeePassXC", $this._path(
@($env:ProgramFiles, "KeePassXC")));
$this._installBinApp("Microsoft.VisualStudioCode", $this._path(
@($env:LOCALAPPDATA, "Programs", "Microsoft VS Code", "bin")));
$this._configureVscode();
# Better ls
$this._installApp("lsd-rs.lsd");
$this._configureLsd();
# TODO: install batteryinfoview via winget like "NirSoft.WifiInfoView"
# this._installLocationApp("NirSoft.BatteryInfoView", "")
# $this._configureBatteryInfoView();
$this._installApp("strayge.tray-monitor");
$this._installApp("Stardock.Start11");
if (-not $this._isPublic) {
$markerPath = $this._path(@("~", ".ssh", ".uploaded_to_github"));
$sshUploaded = (Test-Path -Path "$markerPath");
# Interactive.
if (-not $sshUploaded -or $this._isUpdateEnv) {
$this._askForCredentials();
$this._setEnv("MQTT_URL", $this._mqtt.url);
$this._setEnv("MQTT_USER", $this._mqtt.user);
$this._setEnv("MQTT_PASS", $this._mqtt.pass);
$this._setEnv("VK_CC_TOKEN", $this._vk.cc_token);
}
if (-not $sshUploaded) {
$this._uploadSshKey();
}
# Re-clone with SSH keys
$this._getFilesFromGit();
}
# After additional files are received
# TODO: wait for https://github.com/microsoft/winget-pkgs/pull/178129,
$this._installAppFromManifest("EFLFE.PingoMeter");
$this._configurePingoMeter();
# Register startup after all additional files are received since
# starting apps like autohotkey blocks config files
$this._registerPingometerStartup();
$this._registerAutohotkeyStartup();
# TODO: wait for BatteryInfoView install
# $this._registerBatteryInfoViewStartup();
# Interactive
$this._mapKeyboard();
# Interactive.
$this._installFonts();
$this._getXiWindows();
# Symlink PowerShel config file into PowerShell config dir.
if (-not $this._isTest) {
$src = $this._path(@($this._cfgDir, "profile.ps1"));
$dst = $this._path(@($this._psDir, "profile.ps1"));
if (Test-Path -Path "$dst") {
Remove-Item "$dst" -Recurse -Force;
}
Write-Host "Creating hardlink $src => $dst";
New-Hardlink -Path "$($this._psDir)" -Name "profile.ps1" -Value "$src";
}
# Create git config with link to the git-cfg.toml
if (-not $this._isTest) {
$src = $this._path(@($this._cfgDir, ".gitconfig"));
$dst = $this._path(@("~", ".gitconfig"));
if (Test-Path -Path "$dst") {
Remove-Item "$dst" -Recurse -Force;
}
$content = "[include]`npath = `"$($this._cfgDirLinux)/git-cfg.toml`"`n";
New-File -Path "~" -Name ".gitconfig" -Value "$content";
}
if (-not (Test-Path -Path ".editorconfig")) {
$src = $this._path(@($this._cfgDir, ".editorconfig"));
Copy-Item -Path "$src" -Destination . -Force;
}
# Hide search in the taskbar
$path = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Search"
Set-ItemProperty -Path $path -Name SearchboxTaskbarMode -Value 0
# Optional installs after reboot
if ($this._isFull) {
# for diff-so-fancy
$this._installBinApp("StrawberryPerl.StrawberryPerl",
"C:\Strawberry\perl\bin\");
# nvm command
$this._installBinAppWithVer(
"CoreyButler.NVMforWindows",
$this._path(@($env:APPDATA, "nvm")),
# version 1.1.12 fails "non-terminal" execution
"1.1.11");
# For nvm command to work in an existing terminal
$env:NVM_HOME = $this._path(@($env:APPDATA, "nvm"));
# Node.js
Write-Host "Installing latest nodejs";
& nvm on
& nvm install latest
& nvm use latest
$nodePath = $this._path(@($env:ProgramFiles, "nodejs"));
if (-not (Test-Path -Path $nodePath)) {
throw "run 'nvm use latest' manually";
}
if (-not $env:PATH.Contains($nodePath)) {
$env:PATH = "${env:PATH};$nodePath";
}
Write-Host "Updating npm"
& npm install -g npm@latest
# Better diff
& npm install -g diff-so-fancy
# USB camera control
& npm install -g uvcc
# General-purpose messaging.
$this._installApp("Telegram.TelegramDesktop");
# "Offline" google apps support and no telemetry delays line in "Edge".
$this._installApp("Google.Chrome");
# file management
$this._installApp("alexx2000.DoubleCommander");
# PDF view.
$this._installApp("Foxit.FoxitReader");
# Better process maangement
$this._installApp("Microsoft.Sysinternals.ProcessExplorer");
# Desktop recording.
$this._installApp("OBSProject.OBSStudio");
# TODO: configure to save position on exit
$this._installApp("clsid2.mpc-hc");
# ag command, "the silver searcher"
$this._installApp("JFLarvoire.Ag");
# screenshot tool, "sniping tool" corrupts colors
$this._installApp("Flameshot.Flameshot");
# for g-helper
$this._installApp("Microsoft.DotNet.DesktopRuntime.7");
# for mosquitto_pub
$this._installBinApp("EclipseFoundation.Mosquitto", $this._path(
@($env:ProgramFiles, "mosquitto")));
# for keylight control
$this._installApp("Elgato.ControlCenter");
# ChatGPT
$this._installApp("9NT1R1C2HH7J");
# Notion Calendar
$this._installApp("Notion.NotionCalendar");
# Torrent client
$this._installApp("qBittorrent.qBittorrent");
# Video player
$this._installApp("VideoLAN.VLC");
# Wino Mail
$this._installApp("9NCRCVJC50WL");
# Trello client
$this._installApp("9NBLGGH4XXVW");
}
if ($this._isTest) {
Write-Host "Test complete";
}
else {
Write-Host $this._POST_INSTALL_MSG;
}
}
[Boolean] _hasCli($name) {
if ($this._isTest) { return $false; }
Get-Command $name -ErrorAction SilentlyContinue;
return $?;
}
[Boolean] _isAppStatusInstalled($appName) {
if ($this._isTest) { return $false; }
$res = & winget list
if ($LASTEXITCODE -ne 0) { return $false; }
return ($res | Out-String).Contains($appName);
}
[String] _path([array] $pathList) {
return $this._pathIntrinsics.GetUnresolvedProviderPathFromPSPath(
[io.path]::combine([string[]]$pathList));
}
_installApp($appName) {
if ($this._isTest) { return; }
if ($this._isAppStatusInstalled($appName)) {
Write-Host "$appName is already installed";
return;
}
Write-Host "Installing $appName"
& winget install --silent $appName;
if ($LASTEXITCODE -ne 0) { throw "Failed to install $appName" }
}
_installAppFromManifest($appName) {
if ($this._isTest) { return; }
if ($this._isAppStatusInstalled($appName)) {
Write-Host "$appName is already installed";
return;
}
$manifestPath = $this._path(@(
$this._cfgDir,
"winget",
"manifests",
$appName
));
Write-Host "Installing $appName from $manifestPath"
& winget install --silent --manifest $manifestPath;
if ($LASTEXITCODE -ne 0) { throw "Failed to install $appName" }
}
_uninstallApp($appName) {
if ($this._isTest) { return; }
if (-not $this._isAppStatusInstalled($appName)) {
Write-Host "$appName is already uninstalled";
return;
}
Write-Host "Uninstalling $appName"
& winget uninstall --silent $appName;
# There is no reason to check error code since uninstallers tend to
# show error codes upon successfull uninstall
}
# For installers that add something to PATH (requires terminal restart)
_installBinApp($appName, $binPath) {
$this._installBinAppWithVer($appName, $binPath, $null);
}
_installBinAppWithVer($appName, $binPath, $ver) {
if ($this._isTest) { return; }
if ($this._isAppStatusInstalled($appName)) {
Write-Host "$appName is already installed";
if (-not $env:PATH.Contains($binPath)) {
$env:PATH = "${env:PATH};$binPath";
}
return;
}
if ($ver) {
Write-Host "Installing $appName with binary in path, version $ver"
& winget install --silent $appName --version $ver --no-upgrade --exact;
}
else {
Write-Host "Installing $appName with binary in path"
& winget install --silent $appName;
}
if ($LASTEXITCODE -ne 0) { throw "Failed to install $appName" }
if (-not $env:PATH.Contains($binPath)) {
$env:PATH = "${env:PATH};$binPath";
}
$this._addToPath($binPath);
}
# For installers that require install location to be specified
_installLocationApp($appName, $binSubpath) {
$location = $this._path(@("~", "apps", $appName));
$binPath = $this._path(@($location, $binSubpath));
if ($this._isTest) { return; }
if ($this._isAppStatusInstalled($appName)) {
Write-Host "$appName is already installed";
if (-not $env:PATH.Contains($binPath)) {
$env:PATH = "${env:PATH};$binPath";
}
return;
}
Write-Host "Installing $appName into $location"
& winget install --silent --location $location $appName;
if ($LASTEXITCODE -ne 0) { throw "Failed to install $appName" }
if (-not $env:PATH.Contains($binPath)) {
$env:PATH = "${env:PATH};$binPath";
}
$this._addToPath($binPath);
}
_installPowershellModule($moduleName) {
Write-Host "Installing $moduleName";
if ($this._isTest) { return; }
if (Get-InstalledModule | Where-Object Name -eq $moduleName) { return; }
Install-Module $moduleName -Scope CurrentUser;
if (-not $?) { throw "Failed" }
}
_installWsl() {
Write-Host "Installing WSL";
if ($this._isTest) { return; }
& wsl --status;
if ($LASTEXITCODE -eq 0) {
if (Test-Path -Path "\\wsl$\Ubuntu") {
Write-Host "WSL is already installed";
$wslSshDir = "\\wsl$\Ubuntu\home\user\.ssh"
if (Test-Path -Path "$wslSshDir" ) {
Write-Host "Keys are already copied to WSL"
return;
}
else {
Write-Host "Creating wsl://~/.ssh";
New-Dir -Path "$wslSshDir";
Write-Host "Copying keys to wsl://~/.ssh";
$srcPath = $this._path(@("~", ".ssh", "id_rsa"))
Copy-Item -Path "$srcPath" -Destination "$wslSshDir" -Force;
$srcPath = $this._path(@("~", ".ssh", "id_rsa.pub"))
Copy-Item -Path "$srcPath" -Destination "$wslSshDir" -Force;
& wsl chmod 600 ~/.ssh/id_rsa
return;
}
}
else {
# Need to be install two times: before and after reboot
Write-Host "Installing WSL. Create a 'user' user with a password";
Start-Process wsl -ArgumentList "--install" -Wait;
return;
}
}
else {
Start-Process wsl -ArgumentList "--install" -Wait;
return;
}
}
_getFilesFromGit() {
if ($this._isTest) { return; }
$gitCfgFile = $this._path(@($this._cfgDir, ".git", "config"));
if (Test-Path -Path "$gitCfgFile") {
$gitCfg = Get-Content "$gitCfgFile" | Out-String;
# Already cloned with SSH?
if ($gitCfg.Contains("[email protected]")) {
Write-Host "dotfiles already cloned via ssh";
return;
}
}
# Have keys to clone with SSH?
$markerPath = $this._path(@("~", ".ssh", ".uploaded_to_github"));
if (Test-Path -Path "$markerPath") {
$uri = "[email protected]:grigoryvp/dotfiles.git";
}
else {
# Already cloned without keys?
if (Test-Path -Path "$gitCfgFile") {
Write-Host "dotfiles already cloned via https";
return;
}
# Clone with HTTPS
$uri = "https://github.com/grigoryvp/dotfiles.git";
}
$tmpDirName = $this._path(@("~", "dotfiles-tmp"));
if (Test-Path -Path "$tmpDirName") {
Write-Host "Removing existing temp dir $tmpDirName"
Remove-Item "$tmpDirName" -Recurse -Force;
}
# May hang: https://gitlab.com/gitlab-org/gitlab/-/issues/499350
Write-Host "git clone $uri $tmpDirName";
& git clone --quiet "$uri" "$tmpDirName";
# Replace HTTP git config with SSH one, if any.
Write-Host "Removing current dir $($this._cfgDir)"
Remove-Item "$($this._cfgDir)" -Recurse -Force;
Write-Host "Recreating config dir $($this._cfgDir)"
New-Dir -Path $this._cfgDir;
Write-Host "Moving files $tmpDirName => $($this._cfgDir)";
Move-Item -Force "$tmpDirName/*" "$($this._cfgDir)";
Write-Host "Removing temp dir $tmpDirName";
Remove-Item "$tmpDirName" -Recurse -Force;
}
_generateSshKey() {
if ($this._isTest) { return; }
if (Test-Path -Path $this._path(@("~", ".ssh", "id_rsa"))) {
Write-Host "ssh key already generated";
return;
}
$sshDir = $this._path(@("~", ".ssh"));
if (-not (Test-Path -Path "$sshDir" )) {
Write-Host "Creating ~/.ssh";
New-Dir -Path "$sshDir";
}
Write-Host "Generating ssh key";
Start-Process ssh-keygen -ArgumentList '-N "" -f .ssh/id_rsa' -Wait;
}
_setPowerOptions() {
if ($this._isTest) { return; }
Write-Host "Setting power policy";
powercfg -change -monitor-timeout-ac 120;
powercfg -change -monitor-timeout-dc 120;
powercfg -change -disk-timeout-ac 0;
powercfg -change -disk-timeout-dc 0;
powercfg -change -standby-timeout-ac 0;
powercfg -change -standby-timeout-dc 0;
powercfg -change -hibernate-timeout-ac 0;
powercfg -change -hibernate-timeout-dc 0;
# Set 'plugged in' cooling policy to 'active'
powercfg -setacvalueindex scheme_current 54533251-82be-4824-96c1-47b60b740d00 94d3a615-a899-4ac5-ae2b-e4d8f634367f 1
# Set 'on battery' cooling policy to 'active'
#! If set to 'passive' will downclock cpu to minimum, unusable
powercfg -setdcvalueindex scheme_current 54533251-82be-4824-96c1-47b60b740d00 94d3a615-a899-4ac5-ae2b-e4d8f634367f 1
}
_setDebounceOptions() {
if ($this._isTest) { return; }
Write-Host "Setting touchpad debounce options";
$argmap = @{
Path = "HKCU:\Control Panel\Accessibility\Keyboard Response"
PropertyType = "String"
Force = $true
}
# Ms before key is repeated
$argmap.Name = 'AutoRepeatDelay';
$argmap.Value = '300';
New-ItemProperty @argmap;
# Less is faster
$argmap.Name = 'AutoRepeatRate';
$argmap.Value = '30';
New-ItemProperty @argmap;
# Milliseconds to supres bounce (with 30ms it RARELY bounces).
#! On some laptops like Dell 5490 setting this value will result in fast
# double presses not handled.
$argmap.Name = 'BounceTime';
$argmap.Value = '35';
New-ItemProperty @argmap;
# Milliseconds to wait before accepting a keystroke
$argmap.Name = 'DelayBeforeAcceptance';
$argmap.Value = '0';
New-ItemProperty @argmap;
# Bit Flags:
# 00000001 On
# 00000010 Available
# 00000100 Use shortcut
# 00001000 Confirm activation
# 00010000 Activation sound
# 00100000 Show status
# 01000000 Key click
$argmap.Name = 'Flags';
$argmap.Value = '1';
New-ItemProperty @argmap;
}
[Boolean] _needMapCapsToF24() {
if ($this._isTest) { return $false; }
$val = Get-ItemProperty `
-Path "HKLM:\SYSTEM\CurrentControlSet\Control\Keyboard Layout" `
-Name "Scancode Map" `
-ErrorAction SilentlyContinue;
if ($val) {
$len = $val.'Scancode Map'.Length;
# Already set?
if ($len -eq 20) { return $false; }
}
return $true;
}
# Remapped via RandyRants.SharpKeys
_mapKeyboard() {
if (-not $this._needMapCapsToF24()) {
Write-Host "caps already mapped to F24";
return;
}
Write-Host "remapping keyboard";
# caps to F24 (m1)
# esc to alt (alt availability for left-hand keypad gaming)
# left alt to esc (m2 and left-hand keypad gaming)
# right alt for enter (m3, enter key for login screen and w/o ahk)
# tab to lctrl (ahk changes single key back to tab)
# enter to rctrl (autohotkey change single key back to enter)
# left ctrl to left win for left-hand keypad gaming (no win key)
$val = ([byte[]](
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x3A, 0x00,
0x1D, 0xE0, 0x1C, 0x00, 0x38, 0x00, 0x01, 0x00,
0x01, 0x00, 0x38, 0x00, 0x5B, 0xE0, 0x1D, 0x00,
0x1C, 0x00, 0x38, 0xE0, 0x1D, 0x00, 0x0F, 0x00,
0x00, 0x00, 0x00, 0x00
));
New-ItemProperty `
-Path "HKLM:\SYSTEM\CurrentControlSet\Control\Keyboard Layout" `
-Name "Scancode Map" `
-PropertyType "Binary" `
-Value $val `
-Force
}
_setTouchpadOptions() {
if ($this._isTest) { return; }
$root = "HKCU:\Software\Microsoft\Windows\CurrentVersion";
$uri = "$root\PrecisionTouchPad";
$propName = "AAPThreshold";
$prop = Get-ItemProperty $uri -Name $propName;
if ($prop) {
$val = $prop.AAPThreshold;
if ($val -eq 0) { return; }
}
# Requires reboot.
Set-ItemProperty $uri -Name $propName -Type Dword -Value 0;
}
_setInputMethodOptions() {
if ($this._isTest) { return; }
$current = & pwsh -Command Get-WinUserLanguageList | Out-String;
if (-not $current.Contains("Russian")) {
Write-Host "Adding Russian language";
$cmd = '' +
'$list = Get-WinUserLanguageList;' +
'$list.Add("ru");' +
'Set-WinUserLanguageList -Force $list;';
& pwsh -Command $cmd;
}
if (-not $current.Contains("Japanese")) {
Write-Host "Adding Japanese language";
$cmd = '' +
'$list = Get-WinUserLanguageList;' +
'$list.Add("ja");' +
'Set-WinUserLanguageList -Force $list;';
& pwsh -Command $cmd;
}
}
[String] _attrFromKeepass($record, $attr) {
#! keepassxc-cli displays the "enter password" promt into stderr and
# powershell throws the NativeCommandError exception if program output
# to stderr (redirect does not help).
$ErrorActionPreference = "SilentlyContinue";
$ret = $null;
# -s to show protected attribute (password) as clear text.
$ret = $(
Write-Output $this._pass |
keepassxc-cli show -s $this._keepassdb $record --attributes $attr
2>$null
);
$ErrorActionPreference = "Stop";
if (-not $?) { throw "keepassxc-cli failed" }
return $ret;
}
_askForCredentials() {
$pass = Read-Host -AsSecureString -Prompt "Enter password"
$ptr = [Security.SecureStringMarshal]::SecureStringToCoTaskMemAnsi($pass);
$pass = [Runtime.InteropServices.Marshal]::PtrToStringAnsi($ptr);
$this._pass = $pass;
$this._keepassdb = $this._path(@($this._cfgDir, "auth/passwords.kdbx"));
$this._github.user = $this._attrFromKeepass("github", "username");
$this._github.pass = $this._attrFromKeepass("github", "password");
$this._github.token = $this._attrFromKeepass("github", "auto-cfg-token");
$this._mqtt.url = $this._attrFromKeepass("hivemq", "login_url");
$this._mqtt.user = $this._attrFromKeepass("hivemq", "login_user");
$this._mqtt.pass = $this._attrFromKeepass("hivemq", "login_pass");
$record = "vk.gvp-url-shortener";
$this._vk.cc_token = $this._attrFromKeepass($record, "token");
}
_uploadSshKey() {
$marker = ".uploaded_to_github";
if (Test-Path -Path $this._path(@("~", ".ssh", "$marker"))) { return; }
$pair = "$($this._github.user):$($this._github.token)";
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair);
$creds = [System.Convert]::ToBase64String($bytes)
$headers = @{Authorization = "Basic $creds";}
$body = ConvertTo-Json @{
title = "box key $(Get-Date)";
key = (Get-Content "~/.ssh/id_rsa.pub" | Out-String);
}
$url = "https://api.github.com/user/keys"
if (-not $this._isTest) {
try {
Invoke-WebRequest -Method 'POST' -Headers $headers -Body $body $url;
}
catch {
if ($_.Exception.Response.StatusCode -eq 422) {
Write-Host "ssh key already added to GitHub";
New-File -Path .ssh -Name $marker;
}
elseif ($_.Exception.Response.StatusCode -eq 401) {
# TODO: try to upload via auth token.
Write-Host "Failed to add key to GitHub";
Write-Host "Upload manually and touch .ssh/${marker}";
Write-Host "Login: '$($this._github.user)'";
Write-Host "Pass: '$($this._github.pass)'";
Write-Host "REBOOT IF FIRST INSTALL (to correctly install WSL)";
throw "Failed";
}
else {
throw "Failed $($_.Exception)";
}
}
New-File -Path "~/.ssh" -Name $marker;
}
}
_prompt($msg) {
if ($this._isTest) { return; }
Write-Host -NoNewLine $msg;
[System.Console]::ReadKey("NoEcho,IncludeKeyDown");
Write-Host "";
}
_registerAutohotkeyStartup() {
if ($this._isTest) { return; }
$name = "autohotkey";
$argmap = @{
Execute = $this._path(@(
"~", "apps", "AutoHotkey.AutoHotkey", "v2", "autohotkey.exe"))
Argument = $this._path(@($this._cfgDir, "keyboard.ahk"))
}
$action = New-ScheduledTaskAction @argmap;
$trigger = New-ScheduledTaskTrigger -AtLogOn;
# Delay for a few seconds, otherwise Windows will not display tray icon
$trigger.Delay = 'PT10S'
$task = Get-ScheduledTask -TaskName $name -ErrorAction SilentlyContinue;
if (-not $task) {
Write-Host "Creating new AutoHotkey scheduled task";
Register-ScheduledTask -TaskName $name `
-Action $action -Trigger $trigger -RunLevel Highest;
}
else {
Write-Host "Modifying existing AutoHotkey scheduled task";
}
Set-ScheduledTask -TaskName $name -Action $action;
Set-ScheduledTask -TaskName $name -Trigger $trigger;
$settings = New-ScheduledTaskSettingsSet `
-ExecutionTimeLimit 0 `
-AllowStartIfOnBatteries `
-DontStopIfGoingOnBatteries;
Set-ScheduledTask -TaskName $name -Settings $settings;
}
[Boolean] _needInstallFonst() {
$path = $this._path(@("~", "apps", "nerd-fonts"));
if (Test-Path -Path $path) { return $false; }
return $true;
}
_installFonts() {
if (-not $this._needInstallFonst()) {
Write-Host "Fonts are already installed";
return;
}
$path = $this._path(@("~", "apps", "nerd-fonts"));
$uri = "https://github.com/ryanoasis/nerd-fonts.git";
$fontName = "JetBrainsMono";
Write-Host "Cloning nerd-fonts into $path";
& git clone --quiet --depth 1 --filter=blob:none --sparse $uri $path;
Set-Location $path;
Write-Host "Checking out files for $fontName";
& git sparse-checkout add "patched-fonts/$fontName";
Write-Host "Installing $fontName";
& "./install.ps1" $fontName;
}
_getXiWindows() {
if ($this._isTest) { return; }
$uri = "[email protected]:grigoryvp/xi.git";
$dstDir = $this._path(@("~", ".xi"));
if (Test-Path -Path "$dstDir") {
Write-Host "xi already cloned";
return;
}
Write-Host "cloning xi into $dstDir";
& git clone $uri $dstDir;
return;
}
# Not used since VSCode always opens host dir even if remoting WSL.
_getXiWSL() {
if ($this._isTest) { return; }
$dstDir = "\\wsl$\Ubuntu\home\user\.xi"
if (Test-Path -Path "$dstDir") {
Write-Host "xi already cloned";
return;
}
Write-Host "cloning xi into $dstDir";
$uri = "[email protected]:grigoryvp/xi.git";
& wsl git clone $uri $dstDir;
if ($LASTEXITCODE -ne 0) { throw "Failed" }
}
_installVscodeExt($extId) {
$extList = @(& code --list-extensions);
if (-not $extList.Contains($extId)) {
& code --install-extension "$extId";
if ($LASTEXITCODE -ne 0) { throw "Failed to install $extId" }
}
}
_configureVscode() {
if ($this._isTest) { return; }
$dstDir = $this._path(@($env:APPDATA, "Code", "User"));
if (-not (Test-Path -Path "$dstDir")) {
# Not created during install, only on first UI start.
New-Dir -Path "$dstDir";
}
$srcPath = $this._path(@($this._cfgDir, "vscode_settings.json"));
New-Hardlink -Path "$dstDir" -Name "settings.json" -Value "$srcPath";
$srcPath = $this._path(@($this._cfgDir, "vscode_keybindings.json"));
New-Hardlink -Path "$dstDir" -Name "keybindings.json" -Value "$srcPath";
$srcPath = $this._path(@($this._cfgDir, "vscode_tasks.json"));
New-Hardlink -Path "$dstDir" -Name "tasks.json" -Value "$srcPath";
$srcPath = $this._path(@($this._cfgDir, "vscode_snippets/"));
$dstPath = $this._path(@($dstDir, "vscode_snippets/"));
if (Test-Path -Path "$dstPath") {
Remove-Item "$dstPath" -Recurse -Force;
}
New-Softlink -Path "$dstDir" -Name "vscode_snippets/" -Value "$srcPath";
$this._installVscodeExt("grigoryvp.language-xi");
$this._installVscodeExt("grigoryvp.memory-theme");
$this._installVscodeExt("grigoryvp.goto-link-provider");
$this._installVscodeExt("markdown-inline-fence");
$this._installVscodeExt("grigoryvp.markdown-python-repl-syntax");
$this._installVscodeExt("grigoryvp.markdown-pandoc-rawattr");
$this._installVscodeExt("vscodevim.vim");
$this._installVscodeExt("EditorConfig.EditorConfig");
$this._installVscodeExt("esbenp.prettier-vscode");
$this._installVscodeExt("formulahendry.auto-close-tag");
$this._installVscodeExt("dnut.rewrap-revived");
$this._installVscodeExt("streetsidesoftware.code-spell-checker");
$this._installVscodeExt("streetsidesoftware.code-spell-checker-russian");
$this._installVscodeExt("mark-wiemer.vscode-autohotkey-plus-plus");
$docCfgDir = $this._path(@("~", "Documents", ".vscode"));
if (-not (Test-Path -Path "$docCfgDir")) {
New-Dir -Path "$docCfgDir";
}
$content = @'
{
"files.exclude": {
"My Music/": true,
"My Pictures/": true,
"My Videos/": true,
"My Games/": true,
"Sound Recordings/": true,
"Diablo IV/": true,
"PowerShell": true,
"WindowsPowerShell": true,
"desktop.ini": true,
".vscode/": true
}
}
'@;
New-File -Path $docCfgDir -Name "settings.json" -Value "$content";
## Exclude from 'ls'.
$(Get-Item -Force $docCfgDir).Attributes = 'Hidden';
}
_configureLsd() {
if ($this._isTest) { return; }
$dstDir = $this._path(@($env:APPDATA, "lsd"));
if (-not (Test-Path -Path "$dstDir")) {
New-Dir -Path "$dstDir";
}
$srcPath = $this._path(@($this._cfgDir, "lsd.config.yaml"));
New-Hardlink -Path "$dstDir" -Name "config.yaml" -Value "$srcPath";
}
_configureBatteryInfoView() {
if ($this._isTest) { return; }
$name = "BatteryInfoView.cfg";
$srcPath = $this._path(@($this._cfgDir, $name));
$dstDir = $this._path(@("~", "apps", "NirSoft.BatteryInfoView"));
New-Hardlink -Path "$dstDir" -Name $name -Value "$srcPath";
}
_configurePingoMeter() {
if ($this._isTest) { return; }
$srcPath = $this._path(@($this._cfgDir, "pingometer-cfg.txt"));
$dstDir = $this._path(@(
$env:LOCALAPPDATA,
"Microsoft",
"WinGet",
"Packages",
"EFLFE.PingoMeter__DefaultSource",
"PingoMeter"
));
$dstFileName = "config.txt";
$dstPath = $this._path(@($dstDir, $dstFileName));
if (Test-Path -Path "$dstFileName") {
Remove-Item "$dstFileName" -Recurse -Force;
}
Write-Host "Creating hardlink $srcPath => $dstPath";
New-Hardlink -Path "$dstDir" -Name "$dstFileName" -Value "$srcPath";
}
_registerBatteryInfoViewStartup() {
if ($this._isTest) { return; }
$startDir = "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\Startup"
if (Test-Path -Path "$startDir\battery-info-view.bat") {