-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
install.ps1
997 lines (856 loc) · 27.2 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
<#
.SYNOPSIS
Installer for Lepton
.DESCRIPTION
Installs Lepton onto selected Firefox profiles
.INPUTS
TODO: input directories for installation?
Would need to discuss a non-interactive install system
.OUTPUTS
TODO: output directories that Lepton has been installed to?
.PARAMETER u
Specifies whether to update a current installation
Defaults to false
.PARAMETER f
Specifies a custom path to look for Firefox profiles in
.PARAMETER p
Specifies a custom name to use when creating a profile
.PARAMETER h
Shows this help message
.PARAMETER ?
Shows this help message
.PARAMETER WhatIf
Runs the installer without actioning any file copies/moves
Equivalent to a dry-run
.EXAMPLE
PS> .\Install.ps1 -u -f C:\Users\someone\ff-profiles
Updates current installations in the profile directory 'C:\Users\someone\ff-profiles'
.LINK
https://github.com/black7375/Firefox-UI-Fix#readme
#>
[CmdletBinding(
SupportsShouldProcess = $true,
PositionalBinding = $false
)]
param(
[Alias("u")]
[switch]$updateMode,
[Alias("f")]
[string]$profileDir,
[Alias("p")]
[string]$profileName,
[Alias("h")]
[switch]$help = $false
)
#** Helper Utils ***************************************************************
#== Message ====================================================================
function Lepton-ErrorMessage() {
Write-Error "FAILED: ${args}"
exit -1
}
function Lepton-OKMessage() {
$local:SIZE = 50
$local:FILLED = ""
for ($i = 0; $i -le ($SIZE - 2); $i++) {
$FILLED += "."
}
$FILLED += "OK"
$local:message = "${args}"
Write-Host ${message}(${FILLED}.Substring(${message}.Length))
}
$PSMinSupportedVersion = 2
function Verify-PowerShellVersion {
Write-Host -NoNewline "Checking PowerShell version... "
$PSVersion = [int](Select-Object -Property Major -First 1 -ExpandProperty Major -InputObject $PSVersionTable.PSVersion)
Write-Host "[$PSVersion]"
if ($PSVersion -lt $PSMinSupportedVersion) {
Write-Error -Category NotInstalled "You need a minimum PowerShell version of [$PSMinSupportedVersion] to use this installer"
exit -1
}
}
#== Required Tools =============================================================
function Install-Choco() {
# https://chocolatey.org/install
# https://docs.chocolatey.org/en-us/choco/setup#non-administrative-install
$InstallDir='C:\ProgramData\chocoportable'
$env:ChocolateyInstall="$InstallDir"
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
$env:Path += ";C:\ProgramData\chocoportable" # Adimin: ";C:\ProgramData\chocolatey"
refreshenv
}
function Check-Git() {
if ( -Not (Get-Command git -errorAction SilentlyContinue) ) {
if ( -Not (Get-Command choco -errorAction SilentlyContinue) ) {
Install-Choco
}
choco install git.commandline -y
$env:Path += ";C:\tools\git\bin" # Adimin: ";C:\Program Files\Git\bin"
refreshenv
}
Lepton-OKMessage "Required - git"
}
#== PATH / File ================================================================
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
$currentDir = (Get-Location).path
function Filter-Path() {
Param (
[Parameter(Mandatory=$true, Position=0)]
[string[]] $pathList,
[Parameter(Position=1)]
[string] $option = "Any"
)
return $pathList.Where({ Test-Path -Path "$_" -PathType "${option}" })
}
function Copy-Auto() {
Param (
[Parameter(Mandatory=$true, Position=0)]
[string] $file,
[Parameter(Mandatory=$true, Position=1)]
[string] $target
)
if ( "${file}" -eq "${target}" ) {
Write-Host "'${file}' and ${target} are same file"
return 0
}
if ( Test-Path -Path "${target}" ) {
Write-Host "${target} already exists."
Write-Host "Now making a backup.."
Copy-Auto "${target}" "${target}.bak"
Remove-Item "${target}" -Recurse -Force
Write-Host ""
}
Copy-Item -Path "${file}" -Destination "${target}" -Force -Recurse
}
function Move-Auto() {
Param (
[Parameter(Mandatory=$true, Position=0)]
[string] $file,
[Parameter(Mandatory=$true, Position=1)]
[string] $target
)
if ( "${file}" -eq "${target}" ) {
Write-Host "'${file}' and ${target} are same file"
return 0
}
if ( Test-Path -Path "${target}" ) {
Write-Host "${target} already exists."
Write-Host "Now making a backup.."
Move-Auto "${target}" "${target}.bak"
Write-Host ""
}
Get-ChildItem -Path "${target}" -Recurse | Move-Item -Path "${file}" -Destination "${target}" -Force
}
function Restore-Auto() {
Param (
[Parameter(Mandatory=$true, Position=0)]
[string] $file
)
$local:target = "${file}.bak"
if ( Test-Path -Path "${file}" ) {
Remove-Item "${file}" -Recurse -Force
}
Get-ChildItem -Path "${target}" -Recurse | Move-Item -Destination "${file}" -Force
$local:lookupTarget = "${target}.bak"
if ( Test-Path -Path "${lookupTarget}" ) {
Restore-Auto "${target}"
}
}
function Write-File() {
Param (
[Parameter(Mandatory=$true, Position=0)]
[string] $filePath,
[Parameter(Position=1)]
[string] $fileContent = ""
)
if ( "${fileContent}" -eq "" ) {
New-Item -Path "${filePath}" -Force
}
else {
Out-File -FilePath "${filePath}" -InputObject "${fileContent}" -Force
}
}
#== INI File ================================================================
# https://devblogs.microsoft.com/scripting/use-powershell-to-work-with-any-ini-file/
function Get-IniContent () {
Param (
[Parameter(Mandatory=$true, Position=0)]
[string] $filePath
)
$ini = @{}
switch -regex -file $filePath {
"^\[(.+)\]" {
# Section
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
"^(;.*)$" {
# Comment
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = “Comment” + $CommentCount
$ini[$section][$name] = $value
}
"(.+?)\s*=(.*)" {
# For compatibility
if ( $section -eq $null ) {
$section = "Info"
$ini[$section] = @{}
}
# Key
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
return $ini
}
function Out-IniFile() {
Param (
[Parameter(Mandatory=$true, Position=0)]
[string] $filePath,
[Parameter(Position=1)]
[hashtable] $iniObject = @{}
)
# Create new file
$local:outFile = New-Item -ItemType file -Path "${filepath}" -Force
foreach ($i in $iniObject.keys) {
if (!($($iniObject[$i].GetType().Name) -eq "Hashtable")) {
#No Sections
Add-Content -Path $outFile -Value "$i=$($iniObject[$i])"
}
else {
#Sections
Add-Content -Path $outFile -Value "[$i]"
Foreach ($j in ($iniObject[$i].keys | Sort-Object)) {
if ($j -match "^Comment[\d]+") {
Add-Content -Path $outFile -Value "$($iniObject[$i][$j])"
}
else {
Add-Content -Path $outFile -Value "$j=$($iniObject[$i][$j])"
}
}
Add-Content -Path $outFile -Value ""
}
}
}
#** Select Menu ****************************************************************
# https://github.com/chrisseroka/ps-menu
function Check-PsMenu() {
if(-Not (Get-InstalledModule ps-menu -ErrorAction silentlycontinue)) {
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module -Name ps-menu -Confirm:$False -Force
}
}
function DrawMenu {
param ($menuItems, $menuPosition, $Multiselect, $selection)
$l = $menuItems.length
for ($i = 0; $i -le $l; $i++) {
if ($menuItems[$i] -ne $null){
$item = $menuItems[$i]
if ($Multiselect) {
if ($selection -contains $i){
$item = '[x] ' + $item
}
else {
$item = '[ ] ' + $item
}
}
if ($i -eq $menuPosition) {
Write-Host "> $($item)" -ForegroundColor Green
}
else {
Write-Host " $($item)"
}
}
}
}
function Toggle-Selection {
param ($pos, [array]$selection)
if ($selection -contains $pos){
$result = $selection | where {$_ -ne $pos}
}
else {
$selection += $pos
$result = $selection
}
$result
}
function Menu {
param ([array]$menuItems, [switch]$ReturnIndex=$false, [switch]$Multiselect)
$vkeycode = 0
$pos = 0
$selection = @()
if ($menuItems.Length -gt 0) {
try {
[console]::CursorVisible=$false #prevents cursor flickering
DrawMenu $menuItems $pos $Multiselect $selection
While ($vkeycode -ne 13 -and $vkeycode -ne 27) {
$press = $host.ui.rawui.readkey("NoEcho,IncludeKeyDown")
$vkeycode = $press.virtualkeycode
If ($vkeycode -eq 38 -or $press.Character -eq 'k') {$pos--}
If ($vkeycode -eq 40 -or $press.Character -eq 'j') {$pos++}
If ($vkeycode -eq 36) { $pos = 0 }
If ($vkeycode -eq 35) { $pos = $menuItems.length - 1 }
If ($press.Character -eq ' ') { $selection = Toggle-Selection $pos $selection }
if ($pos -lt 0) {$pos = 0}
If ($vkeycode -eq 27) {$pos = $null }
if ($pos -ge $menuItems.length) {$pos = $menuItems.length -1}
if ($vkeycode -ne 27) {
$startPos = [System.Console]::CursorTop - $menuItems.Length
[System.Console]::SetCursorPosition(0, $startPos)
DrawMenu $menuItems $pos $Multiselect $selection
}
}
}
finally {
[System.Console]::SetCursorPosition(0, $startPos + $menuItems.Length)
[console]::CursorVisible = $true
}
}
else {
$pos = $null
}
if ($ReturnIndex -eq $false -and $pos -ne $null) {
if ($Multiselect){
return $menuItems[$selection]
}
else {
return $menuItems[$pos]
}
}
else {
if ($Multiselect) {
return $selection
}
else {
return $pos
}
}
}
#** Profile ********************************************************************
#== Profile Dir ================================================================
# $HOME = (get-psprovider 'FileSystem').Home
$firefoxProfileDirPaths = @(
"${HOME}\AppData\Roaming\Mozilla\Firefox",
"${HOME}\AppData\Roaming\Waterfox",
"${HOME}\AppData\Roaming\librewolf",
"${HOME}\AppData\Roaming\Ghostery Browser",
"${HOME}\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser"
)
function Check-ProfileDir() {
Param (
[Parameter(Position=0)]
[string] $profileDir = ""
)
if ( "${profileDir}" -ne "" ) {
$global:firefoxProfileDirPaths = @("${profileDir}")
}
$global:firefoxProfileDirPaths = Filter-Path $global:firefoxProfileDirPaths "Container"
if ( $firefoxProfileDirPaths.Length -eq 0 ) {
Lepton-ErrorMessage "Unable to find firefox profile dir."
}
Lepton-OKMessage "Profiles dir found"
}
#== Profile Info ===============================================================
$PROFILEINFOFILE="profiles.ini"
function Check-ProfileIni() {
foreach ( $profileDir in $global:firefoxProfileDirPaths ) {
if ( -Not (Test-Path -Path "${profileDir}\${PROFILEINFOFILE}" -PathType "Leaf") ) {
Lepton-ErrorMessage "Unable to find ${PROFILEINFOFILE} at ${profileDir}"
}
}
Lepton-OKMessage "Profiles info file found"
}
#== Profile PATH ===============================================================
$firefoxProfilePaths = @()
function Update-ProfilePaths() {
foreach ( $profileDir in $global:firefoxProfileDirPaths ) {
$local:iniContent = Get-IniContent "${profileDir}\${PROFILEINFOFILE}"
$global:firefoxProfilePaths += $iniContent.Values.Path | ForEach-Object { "${profileDir}\${PSItem}" }
}
if ( $firefoxProfilePaths.Length -ne 0 ) {
Lepton-OkMessage "Profile paths updated"
}
else {
Lepton-ErrorMessage "Doesn't exist profiles"
}
}
# TODO: Multi select support
function Select-Profile() {
Param (
[Parameter(Position=0)]
[string] $profileName = ""
)
if ( "${profileName}" -ne "" ) {
$local:targetPath = ""
foreach ( $profilePath in $global:firefoxProfilePaths ) {
if ( "${profilePath}" -like "*${profileName}" ) {
$targetPath = "${profilePath}"
break
}
}
if ( "${targetPath}" -ne "" ) {
Lepton-OkMessage "Profile, `"${profileName}`" found"
$global:firefoxProfilePaths = @("${targetPath}")
}
else {
Lepton-ErrorMessage "Unable to find ${profileName}"
}
}
else {
if ( $firefoxProfilePaths.Length -eq 1 ) {
Lepton-OkMessage "Auto detected profile"
}
else {
$global:firefoxProfilePaths = Menu $firefoxProfilePaths
if ( $firefoxProfilePaths.Length -eq 0 ) {
Lepton-ErrorMessage "Please select profiles"
}
Lepton-OkMessage "Selected profile"
}
}
}
#** Lepton Info File ***********************************************************
# If you interst RFC, see install.sh
#== Lepton Info ================================================================
$LEPTONINFOFILE ="lepton.ini"
function Check-LeptonIni() {
foreach ( $profileDir in $global:firefoxProfileDirPaths ) {
if ( -Not (Test-Path -Path "${profileDir}\${LEPTONINFOFILE}") ) {
Lepton-ErrorMessage "Unable to find ${LEPTONINFOFILE} at ${profileDir}"
}
}
Lepton-OkMessage "Lepton info file found"
}
#== Create info file ===========================================================
# We should always create a new one, as it also takes into account the possibility of setting it manually.
# Updates happen infrequently, so the creation overhead is less significant.
function Get-ProfileDir() {
Param (
[Parameter(Mandatory=$true, Position=0)]
[string] $profilePath
)
foreach ( $profileDir in $firefoxProfileDirPaths ) {
if ( "$profilePath" -like "${profileDir}*" ) {
return "$profileDir"
}
}
}
$CHROMEINFOFILE="LEPTON"
function Write-LeptonInfo() {
# Init info
$local:output = @{}
$local:prevDir = $firefoxProfileDirPaths[0]
$local:latestPath = ( $firefoxProfilePaths | Select-Object -Last 1 )
foreach ( $profilePath in $global:firefoxProfilePaths ) {
$local:LEPTONINFOPATH = "${profilePath}\chrome\${CHROMEINFOFILE}"
$local:LEPTONGITPATH = "${profilePath}\chrome\.git"
# Profile info
$local:Type = ""
$local:Ver = ""
$local:Branch = ""
$local:Path = ""
if ( Test-Path -Path "${LEPTONINFOPATH}" ) {
if ( Test-Path -Path "${LEPTONGITPATH}" -PathType "Container" ) {
$Type = "Git"
$Ver = $(git --git-dir "${LEPTONGITPATH}" rev-parse HEAD)
$Branch = $(git --git-dir "${LEPTONGITPATH}" rev-parse --abbrev-ref HEAD)
}
else {
$local:iniContent = Get-IniContent "${LEPTONINFOPATH}"
$Type = $iniContent["Info"]["Type"]
$Ver = $iniContent["Info"]["Ver"]
$Branch = $iniContent["Info"]["Branch"]
if ( "${Type}" -eq "" ) {
$Type = "Local"
}
}
$Path = "${profilePath}"
}
# Flushing
$local:profileDir = Get-ProfileDir "${profilePath}"
$local:profileName = Split-Path "${profilePath}" -Leaf
if ( "${prevDir}" -ne "${profileDir}" ) {
Out-IniFile "${prevDir}\${LEPTONINFOFILE}" $output
$output = @{}
}
# Make output contents
foreach ( $key in @("Type", "Branch", "Ver", "Path") ) {
$local:value = (Get-Variable -Name "${key}").Value
if ( "$value" -ne "" ) {
$output["${profileName}"] += @{"${key}" = "${value}"}
}
}
# Latest element flushing
if ( "${profilePath}" -eq "${latestPath}" ) {
Out-IniFile "${profileDir}\${LEPTONINFOFILE}" $output
}
$prevDir = "${profileDir}"
}
# Verify
Check-LeptonIni
Lepton-OkMessage "Lepton info file created"
}
#** Install ********************************************************************
#== Install Types ==============================================================
$updateMode = $false
$leptonBranch = "master"
function Select-Distribution() {
while ( $true ) {
$local:selected = $false
$local:selectedDistribution = Menu @("Original(default)", "Photon-Style", "Proton-Style", "Update")
switch ( $selectedDistribution ) {
"Original(default)" { $global:leptonBranch = "master" ; $selected = $true }
"Photon-Style" { $global:leptonBranch = "photon-style"; $selected = $true }
"Proton-Style" { $global:leptonBranch = "proton-style"; $selected = $true }
"Update" { $global:updateMode = $true ; $selected = $true }
default { Write-Host "Invalid option, reselect please." }
}
if ( $selected -eq $true ) {
break
}
}
Lepton-OkMessage "Selected ${selectedDistribution}"
}
$leptonInstallType = "Network" # Other types: Local, Release
function Check-InstallType() {
Param (
[Parameter(Mandatory=$true, Position=0)]
[string[]] $targetList,
[Parameter(Mandatory=$true, Position=1)]
[string] $installType
)
$local:targetCount = $targetList.Length
$local:foundCount = (Filter-Path $targetList ).Length
if ( "${targetCount}" -eq "${foundCount}" ) {
$global:leptonInstallType="${installType}"
}
}
$checkLocalFiles = @(
"userChrome.css",
"userContent.css",
"icons"
)
$checkReleaseFiles = @(
"user.js"
"chrome\userChrome.css"
"chrome\userContent.css"
"chrome\icons"
)
function Check-InstallTypes() {
Check-InstallType $checkLocalFiles "Local"
Check-InstallType $checkReleaseFiles "Release"
Lepton-OkMessage "Checked install type: ${leptonInstallType}"
if ( "${leptonInstallType}" -eq "Network" ) {
Select-Distribution
}
if ( "${leptonInstallType}" -eq "Local" ) {
if ( Test-Path -Path ".git" -PathType "Container" ) {
Select-Distribution
git checkout "${leptonBranch}"
}
}
}
#== Custom Install =============================================================
$customFiles = @(
"user-overrides.js",
"userChrome-overrides.css",
"userContent-overrides.css"
)
$localCustomFiles = $customFiles.Clone()
$customFileExist = $false
function Check-CustomFiles() {
$global:localCustomFiles = Filter-Path $localCustomFiles
if ( $global:localCustomFiles.Length -gt 0 ) {
$global:customFileExist = $true
Lepton-OKMessage "Check custom file detected"
foreach ( $customFile in $global:localCustomFiles ) {
Write-Host "- ${customFile}"
}
}
}
function Copy-CustomFiles() {
if ( "${customFileExist}" -eq $true ) {
# If Release or Network mode, Local is passed (Already copied)
if ( "${leptonInstallType}" -ne "Local" ) {
foreach ( $profilePath in $global:firefoxProfilePaths ) {
foreach ( $customFile in $global:localCustomFiles ) {
if ( "${customFile}" -eq "user-overrides.js" ) {
Copy-Auto "${customFile}" "${profilePath}\${customFile}"
}
else {
Copy-Auto "${customFile}" "${profilePath}\chrome\${customFile}"
}
}
}
}
Lepton-OKMessage "End custom file copy"
}
}
$customMethod = ""
$customReset = $false
$customAppend = $false
function Set-CustomMethod() {
$local:menuAppend="Append - Maintain changes in existing files and apply custom"
$local:menuOverwrite="Overwrite - After initializing the change, apply only custom"
$local:menuNone="None - Maintain changes in existing files"
$local:menuReset="Reset- Reset to pure lepton theme without custom"
Write-Host "Select custom method"
while ( "${customMethod}" -eq "" ) {
$local:applyMethod = Menu @("${menuAppend}", "${menuOverwrite}", "${menuNone}", "${menuReset}")
switch ( $applyMethod ) {
"${menuAppend}" {
$global:customMethod = "Append"
$global:customAppend = $true
}
"${menuOverwrite}" {
$global:customMethod = "Overwrite"
$global:customReset = $true
$global:customAppend = $true
}
"${menuNone}" {
$global:customMethod = "None"
}
"${menuReset}" {
$global:customMethod = "Reset"
$global:customReset = $true
}
default { Write-Host "Invalid option, reselect please." }
}
}
Lepton-OKMessage "Selected ${customMethod}"
}
$customFileApplied = $false
function Apply-CustomFile() {
Param (
[Parameter(Mandatory=$true, Position=0)]
[string] $profilePath,
[Parameter(Mandatory=$true, Position=1)]
[string] $targetPath,
[Parameter(Mandatory=$true, Position=2)]
[string] $customPath,
[Parameter(Position=3)]
[string] $otherCustomPath = ""
)
$local:leptonDir = "${profilePath}\chrome"
$local:gitDir = "${leptonDir}\.git"
if ( Test-Path -Path "${customPath}" -PathType leaf ) {
$global:customFileApplied = $true
if ( "${customMethod}" -eq "" ) {
Set-CustomMethod
}
if ( "${customReset}" -eq $true ) {
if ( "${targetPath}" -like "*user.js" ) {
Copy-Item -Path "${leptonDir}\user.js" -Destination "${targetPath}" -Force
}
else {
git --git-dir "${gitDir}" --work-tree "${leptonDir}" checkout HEAD -- "${targetPath}"
}
}
if ( "${customAppend}" -eq $true ) {
# Apply without duplication
if ( -not (Write-Output "$(Write-Output $(Get-Content -Path "${targetPath}"))" | Select-String -Pattern "$(Write-Output $(Get-Content -Path "${customPath}"))" -SimpleMatch -Quiet) ) {
Get-Content -Path "${customPath}" | Out-File -FilePath "${targetPath}" -Append
}
}
}
elseif ( "${otherCustomPath}" -ne "" ) {
Apply-CustomFile "${profilePath}" "${targetPath}" "${otherCustomPath}"
}
}
function Apply-CustomFiles() {
foreach ( $profilePath in $global:firefoxProfilePaths ) {
foreach ( $customFile in $global:customFiles ) {
$local:targetFile = $customFile.Replace("-overrides", "")
if ( "${customFile}" -eq "user-overrides.js" ) {
$local:targetPath = "${profilePath}\${targetFile}"
$local:customPath = "${profilePath}\user-overrides.js"
$local:otherCustomPath = "${profilePath}\chrome\user-overrides.js"
Apply-CustomFile "${profilePath}" "${targetPath}" "${customPath}" "${otherCustomPath}"
}
else {
Apply-CustomFile "${profilePath}" "${profilePath}\chrome\${targetFile}" "${profilePath}\chrome\${customFile}"
}
}
}
if ( "${customFileApplied}" -eq $true ) {
Lepton-OKMessage "End custom file applied"
}
}
#== Install Helpers ============================================================
$chromeDuplicate = $false
function Check-ChromeExist() {
if ( (Test-Path -Path "chrome") -and (-Not (Test-Path -Path "chrome\${LEPTONINFOFILE}")) ) {
$global:chromeDuplicate = $true
Move-Auto "chrome" "chrome.bak"
Lepton-OkMessage "Backup files"
}
}
function Check-ChromeRestore() {
if ( "${chromeDuplicate}" -eq $true ) {
Restore-Auto "chrome"
Lepton-OkMessage "End restore files"
}
Lepton-OkMessage "End check restore files"
}
function Clean-Lepton() {
if ( ($chromeDuplicate -ne $true) -and (Test-Path -Path "chrome") ) {
Remove-Item -Path "chrome" -Recurse -Force
}
Lepton-OkMessage "End clean files"
}
function Clone-Lepton() {
Param (
[Parameter(Position=0)]
[string] $branch = ""
)
if ( "${branch}" -eq "" ) {
$branch = "${leptonBranch}"
}
git clone -b "${branch}" https://github.com/black7375/Firefox-UI-Fix.git chrome
if ( -Not (Test-Path -Path "chrome" -PathType "Container") ) {
Lepton-ErrorMessage "Unable to find downloaded files"
}
}
function Copy-Lepton() {
Param (
[Parameter(Position=0)]
[string] $chromeDir = "chrome",
[Parameter(Position=1)]
[string] $userJSPath = "${chromeDir}\user.js"
)
foreach ( $profilePath in $global:firefoxProfilePaths ) {
Copy-Auto "${userJSPath}" "${profilePath}\user.js"
Copy-Auto "${chromeDir}" "${profilePath}\chrome"
}
Lepton-OkMessage "End profile copy"
}
#== Each Install ===============================================================
function Install-Local() {
Copy-Lepton "${currentDir}" "user.js"
Copy-CustomFiles
Apply-CustomFiles
}
function Install-Release() {
Copy-Lepton "chrome" "user.js"
Copy-CustomFiles
Apply-CustomFiles
}
function Install-Network() {
Check-ChromeExist
Check-Git
Clone-Lepton
Copy-Lepton
Copy-CustomFiles
Clean-Lepton
Check-ChromeRestore
Apply-CustomFiles
}
function Install-Profile() {
Lepton-OkMessage "Started install"
switch ( "${leptonInstallType}" ) {
"Local" { Install-Local }
"Release" { Install-Release }
"Network" { Install-Network }
}
Lepton-OkMessage "End install"
}
#** Update *********************************************************************
function Stash-File() {
Param (
[Parameter(Mandatory=$true, Position=0)]
[string] $leptonDir,
[Parameter(Mandatory=$true, Position=1)]
[string] $gitDir
)
if ( "$(git --git-dir "${gitDir}" --work-tree "${leptonDir}" diff --stat)" -ne '' ) {
git --git-dir "${gitDir}" --work-tree "${leptonDir}" stash --quiet
return $true
}
return $false
}
function Restore-File() {
Param (
[Parameter(Mandatory=$true, Position=0)]
[string] $leptonDir,
[Parameter(Mandatory=$true, Position=1)]
[string] $gitDir,
[Parameter(Position=2)]
[string] $gitDirty = "$false"
)
if ( "${gitDirty}" -eq $true ) {
git --git-dir "${gitDir}" --work-tree "${leptonDir}" stash pop --quiet
}
}
function Update-Profile() {
Check-Git
foreach ( $profileDir in $global:firefoxProfileDirPaths ) {
$local:LEPTONINFOPATH = "${profileDir}\${LEPTONINFOFILE}"
$local:LEPTONINFO = Get-IniContent "${LEPTONINFOPATH}"
$local:sections = $LEPTONINFO.Keys
if ( $sections.Length -ne 0 ) {
foreach ( $section in $sections ) {
$local:Type = $LEPTONINFO["${section}"]["Type"]
$local:Branch = $LEPTONINFO["${section}"]["Branch"]
$local:Path = $LEPTONINFO["${section}"]["Path"]
$local:leptonDir = "${Path}\chrome"
$local:gitDir = "${leptonDir}\.git"
if ( "${Type}" -eq "Git" ) {
$local:gitDirty = $(Stash-File "${leptonDir}" "${gitDir}")
git --git-dir "${gitDir}" --work-tree "${leptonDir}" checkout "${Branch}"
git --git-dir "${gitDir}" --work-tree "${leptonDir}" pull --no-edit
Restore-File "${leptonDir}" "${gitDir}" "$gitDirty"
}
elseif ( "${Type}" -eq "Local" -or "${Type}" -eq "Release" ) {
Check-ChromeExist
Clone-Lepton
$firefoxProfilePaths = @("${Path}")
Copy-Lepton
if ( "${Branch}" -eq $null ) {
$Branch = "${leptonBranch}"
}
git --git-dir "${gitDir}" --work-tree "${leptonDir}" checkout "${Branch}"
if ( "${Type}" -eq "Release" ) {
$local:Ver=$(git --git-dir "${gitDir}" describe --tags --abbrev=0)
git --git-dir "${gitDir}" --work-tree "${leptonDir}" checkout "tags/${Ver}"
}
Clean-Lepton
Check-ChromeRestore
}
else {
Lepton-ErrorMessage "Unable to find update type, ${Type} at ${section}"
}
}
}
}
Apply-CustomFiles
}
#** Main ***********************************************************************
function Check-Help {
# Cheap and dirty way of getting the same output as '-?' for '-h' and '-Help'
if ($help) {
Get-Help "$PSCommandPath"
exit 0
}
}
function Install-Lepton {
Verify-PowerShellVersion # Check installed version meets minimum
Check-InstallTypes
Check-ProfileDir $profileDir
Check-ProfileIni
Update-ProfilePaths
Write-LeptonInfo
Check-CustomFiles
if ( $updateMode ) {
Update-Profile
}
else {
Select-Profile $profileName
Install-Profile
}
Write-LeptonInfo
}
Check-Help
Install-Lepton