Skip to content

Commit 3c02ee0

Browse files
committed
feat: add firefox_profile_path config, document changes and add error handling
1 parent 82d3a44 commit 3c02ee0

File tree

2 files changed

+152
-53
lines changed

2 files changed

+152
-53
lines changed

config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
"ublock-origin",
1111
"violentmonkey",
1212
"windscribe"
13-
]
13+
],
14+
"firefox_profile_path": ""
1415
}

setup.ps1

+150-52
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,178 @@
1-
# Read config file
2-
$jsonContent = Get-Content -Raw -Path "config.json" | ConvertFrom-Json
3-
Set-Variable addonsList -Option Constant -Value $jsonContent.addons_list -
4-
$addonsList = $jsonContent.addons_list -split ','
1+
# Read and parse configuration file
2+
try {
3+
$jsonContent = Get-Content -Raw -Path "config.json" | ConvertFrom-Json
4+
} catch {
5+
Write-Host "Error reading config.json. Please ensure the file exists and is valid JSON." -ForegroundColor Red
6+
exit 1
7+
}
8+
9+
# Initialize constants from config
10+
$ADDONS_LIST = $jsonContent.addons_list -split ',' | ForEach-Object { $_.Trim() }
11+
$FIREFOX_PROFILE_PATH = if ($jsonContent.firefox_profile_path) {
12+
$jsonContent.firefox_profile_path
13+
} else {
14+
"$env:APPDATA\Mozilla\Firefox"
15+
}
516

617
function Create-Profile {
18+
<#
19+
.SYNOPSIS
20+
Creates a new Firefox profile and returns the profile directory path.
21+
.OUTPUTS
22+
System.String. The path to the newly created profile directory.
23+
#>
724
Write-Host "Creating Profile..." -ForegroundColor Green
8-
9-
Set-Variable firefoxProfilePath -Option Constant -Value "$env:APPDATA\Mozilla\Firefox"
10-
Set-Variable profilesIniPath -Option Constant -Value "$firefoxProfilePath\profiles.ini"
25+
$profilesIniPath = "$FIREFOX_PROFILE_PATH\profiles.ini"
1126

1227
Write-Host "Enter name of the firefox profile: " -ForegroundColor Green -NoNewline
1328
$name = Read-Host
14-
Start-Process "firefox" -ArgumentList "-CreateProfile $name"
15-
Start-Sleep -Seconds 1 # Wait for Firefox to update profile.ini
29+
Start-Process "firefox" -ArgumentList "-CreateProfile $name" -Wait
1630

1731
# Find and cd into the new profile
18-
$folder = (Get-Content $profilesIniPath | Select-String -Pattern "Path=.*$name$" | ForEach-Object { $_ -replace 'Path=', '' })
19-
$profileDir = Join-Path -Path $firefoxProfilePath "$folder"
20-
Set-Location -Path $profileDir
32+
$profileContent = Get-Content $profilesIniPath -ErrorAction Stop
33+
$folder = $profileContent |
34+
Select-String -Pattern "Path=.*$name$" |
35+
ForEach-Object { $_ -replace 'Path=', '' } |
36+
Select-Object -First 1 # Added to handle multiple matches
37+
38+
if (-not $folder) {
39+
Write-Host "Error: Could not find newly created profile." -ForegroundColor Red
40+
exit 1
41+
}
2142

43+
$profileDir = Join-Path -Path $FIREFOX_PROFILE_PATH $folder
44+
Set-Location -Path $profileDir -ErrorAction Stop
2245
Write-Host "Profile Creation Finished" -ForegroundColor Green
2346
return $profileDir
2447
}
2548

2649
function Init-GitRepo {
50+
<#
51+
.SYNOPSIS
52+
Initializes a Git repository with Firefox user preferences and configurations.
53+
#>
2754
Write-Host "Initializing Git Repository..." -ForegroundColor Green
2855

29-
# Initialize the Git repository and fetch updates
30-
git init
31-
git remote add origin https://github.com/ghoulboii/foxdots
32-
git fetch
33-
git checkout --force --track origin/master
34-
git submodule update --init --recursive --remote
35-
36-
robocopy userjs . user.js prefsCleaner.bat updater.bat
37-
38-
.\updater.bat -unattended
39-
40-
Write-Host "Git Repository Initialized" -ForegroundColor Green
56+
try {
57+
# Check if git is installed
58+
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
59+
throw "Git is not installed or not in PATH"
60+
}
61+
62+
# Initialize repository and configure
63+
git init
64+
git remote add origin https://github.com/ghoulboii/foxdots
65+
66+
# Fetch and checkout master branch
67+
git fetch --quiet
68+
git checkout --force --track origin/master
69+
70+
# Update submodules
71+
git submodule update --init --recursive --remote
72+
73+
# Copy necessary files
74+
$filesToCopy = @("user.js", "prefsCleaner.bat", "updater.bat")
75+
foreach ($file in $filesToCopy) {
76+
if (Test-Path "userjs\$file") {
77+
Copy-Item "userjs\$file" -Destination "." -Force
78+
}
79+
}
80+
81+
# Run updater if it exists
82+
if (Test-Path ".\updater.bat") {
83+
.\updater.bat -unattended
84+
}
85+
86+
Write-Host "Git Repository Initialized" -ForegroundColor Green
87+
} catch {
88+
Write-Host "Error during Git initialization: $_" -ForegroundColor Red
89+
exit 1
90+
}
4191
}
4292

4393
function Download-Addons {
94+
<#
95+
.SYNOPSIS
96+
Downloads and installs Firefox addons from Mozilla's addon store.
97+
.PARAMETER profileDir
98+
The Firefox profile directory where addons should be installed.
99+
#>
44100
param (
101+
[Parameter(Mandatory=$true)]
45102
[string]$profileDir
46103
)
47104
Write-Host "Downloading Addons..." -ForegroundColor Green
48105

49-
Set-Variable mozillaAddonURL -Option Constant -Value "https://addons.mozilla.org"
50-
$addonTmpDir = New-Item -ItemType Directory -Path "$env:TEMP\addon" -Force
51-
$extensionsDir = New-Item -ItemType Directory -Path "$profileDir\extensions" -ErrorAction SilentlyContinue
52-
53-
foreach ($addon in $addonsList) {
54-
$addon = $addon.Trim()
55-
56-
Write-Host "Installing addon: $addon" -ForegroundColor Green
57-
58-
# Get addon download URL
59-
$addonPageContent = Invoke-WebRequest -Uri "$mozillaAddonURL/en-US/firefox/addon/$addon/" -UseBasicParsing
60-
$addonDownloadURL = $addonPageContent.Content | Select-String -Pattern "$mozillaAddonURL/firefox/downloads/file/[^`"]*" | Select-Object -ExpandProperty Matches | ForEach-Object { $_.Value }
61-
62-
# Download and extract the add-on
63-
$addonFileName = $addonDownloadURL -split '/' | Select-Object -Last 1
64-
$addonDownloadPath = Join-Path -Path $addonTmpDir.FullName -ChildPath $addonFileName
65-
66-
Invoke-WebRequest -Uri $addonDownloadURL -OutFile $addonDownloadPath
67-
$addonExtractDir = Join-Path -Path $addonTmpDir.FullName -ChildPath "${addonFileName}folder"
68-
Expand-Archive -Path $addonDownloadPath -DestinationPath $addonExtractDir -Force
69-
70-
# Extract addon ID and move to profile extensions folder
71-
$addonId = (Select-String -Path "$addonExtractDir/manifest.json" -Pattern '"id"' -Raw | ForEach-Object { $_ -replace '\s*"id":\s*"([^"]*)"', '$1' }).Trim(',')
72-
Move-Item -Path "$addonTmpDir\$addonFileName" -Destination "$extensionsDir\$addonId.xpi" -Force
106+
$MOZILLA_ADDON_URL = "https://addons.mozilla.org"
107+
$addonTmpDir = New-Item -ItemType Directory -Path "$env:TEMP\firefox_addons" -Force
108+
$extensionsDir = New-Item -ItemType Directory -Path "$profileDir\extensions"
109+
110+
foreach ($addon in $ADDONS_LIST) {
111+
try {
112+
Write-Host "Installing addon: $addon" -ForegroundColor Green
113+
114+
# Get addon download URL
115+
$addonPageUrl = "$MOZILLA_ADDON_URL/en-US/firefox/addon/$addon/"
116+
$addonPageContent = Invoke-WebRequest -Uri $addonPageUrl -UseBasicParsing
117+
$addonDownloadURL = $addonPageContent.Links |
118+
Where-Object { $_.href -match "/firefox/downloads/file/" } |
119+
Select-Object -First 1 -ExpandProperty href
120+
121+
if (-not $addonDownloadURL) {
122+
throw "Could not find download URL for addon: $addon"
123+
}
124+
125+
# Download and extract addon
126+
$addonFileName = Split-Path $addonDownloadURL -Leaf
127+
$addonDownloadPath = Join-Path -Path $addonTmpDir.FullName -ChildPath $addonFileName
128+
129+
Invoke-WebRequest -Uri $addonDownloadURL -OutFile $addonDownloadPath
130+
$addonExtractDir = Join-Path -Path $addonTmpDir.FullName -ChildPath "${addon}_extracted"
131+
Expand-Archive -Path $addonDownloadPath -DestinationPath $addonExtractDir -Force
132+
133+
# Get addon ID and install
134+
$manifestPath = Join-Path $addonExtractDir "manifest.json"
135+
if (-not (Test-Path $manifestPath)) {
136+
throw "manifest.json not found in addon package"
137+
}
138+
139+
$manifestContent = Get-Content $manifestPath -Raw | ConvertFrom-Json
140+
141+
# Handle both Manifest V2 and V3 addon ID locations
142+
$addonId = if ($manifestContent.applications -and $manifestContent.applications.gecko.id) {
143+
# Manifest V2
144+
$manifestContent.applications.gecko.id
145+
} elseif ($manifestContent.browser_specific_settings -and $manifestContent.browser_specific_settings.gecko.id) {
146+
# Manifest V3
147+
$manifestContent.browser_specific_settings.gecko.id
148+
} else {
149+
throw "Could not find addon ID in manifest (neither V2 nor V3 format)"
150+
}
151+
152+
if (-not $addonId) {
153+
throw "Could not find addon ID in manifest"
154+
}
155+
156+
Move-Item -Path $addonDownloadPath -Destination "$extensionsDir\$addonId.xpi" -Force
157+
158+
} catch {
159+
Write-Host "Error installing addon $addon`: $_" -ForegroundColor Red
160+
continue
161+
}
73162
}
74163

75-
Write-Host "Addons Installed" -ForegroundColor Green
164+
# Cleanup
165+
Remove-Item -Path $addonTmpDir -Recurse -Force -ErrorAction SilentlyContinue
166+
Write-Host "Addons Installation Completed" -ForegroundColor Green
76167
}
77168

78-
$profileDir = Create-Profile
79-
Init-GitRepo
80-
Download-Addons -profileDir $profileDir
169+
# Main
170+
try {
171+
$profileDir = Create-Profile
172+
Init-GitRepo
173+
Download-Addons -profileDir $profileDir
174+
Write-Host "Firefox profile setup completed successfully!" -ForegroundColor Green
175+
} catch {
176+
Write-Host "An error occurred during profile setup: $_" -ForegroundColor Red
177+
exit 1
178+
}

0 commit comments

Comments
 (0)