-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcompile-z.ps1
More file actions
124 lines (109 loc) · 3.28 KB
/
compile-z.ps1
File metadata and controls
124 lines (109 loc) · 3.28 KB
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
# Parse command-line arguments
param(
[switch]$use7z,
[switch]$useZip
)
# Set to stop on any error
$ErrorActionPreference = "Stop"
$manifestFile = "./src/manifest.json"
# Ensure at least one of the options is provided or detect available archiver
if (-not ($use7z -or $useZip)) {
if (Get-Command zip -ErrorAction SilentlyContinue) {
Write-Host "zip is installed."
$useZip = $true
}
elseif (Get-Command 7z -ErrorAction SilentlyContinue) {
Write-Host "7z is installed."
$use7z = $true
}
else {
Write-Host "No archiver found. Please install zip or 7z."
Exit 1
}
}
$currentDir = Get-Location
# Check if manifest.json exists and determine version
if (Test-Path "$manifestFile") {
if (Get-Command jq -ErrorAction SilentlyContinue) {
Write-Host "jq is installed."
$VERSION = & jq -r '.version' "$manifestFile"
}
else {
Write-Host "jq is not installed. Using regex to extract version."
$versionMatch = Select-String -Path "$manifestFile" -Pattern '"version"\s*:\s*"([^"]+)"'
if ($versionMatch) {
$VERSION = $versionMatch.Matches[0].Groups[1].Value
}
else {
Write-Host "Error: Could not extract version from $manifestFile."
Exit 1
}
}
Write-Host "Version: $VERSION"
}
else {
Write-Host "Error: manifest.json not found."
Exit 1
}
# Create build directory if it doesn't exist
$buildDir = Join-Path -Path $currentDir -ChildPath "build"
if (-not (Test-Path $buildDir)) {
New-Item -ItemType Directory -Path $buildDir | Out-Null # Avoid unnecessary output
Write-Host "Created build directory: $buildDir"
}
# Define variables
$fileName = "BetterUnsubscribe-$VERSION"
$xpiFileName = "$fileName.xpi"
$xpiFilePath = Join-Path -Path $buildDir -ChildPath $xpiFileName
# Check for the requested archiver
if ($useZip) {
if (Get-Command zip -ErrorAction SilentlyContinue) {
Write-Host "Using zip to create the archive."
# Temporarily change directory to src
Push-Location -Path "./src"
try {
# Add all files from ./src recursively
& zip -r -9 "$xpiFilePath" .
}
finally {
# Return to the original directory
Pop-Location
}
if ($LASTEXITCODE -ne 0) {
Write-Host "Error: Failed to create the archive using zip."
Exit 1
}
}
else {
Write-Host "Error: zip is not installed."
Exit 1
}
}
elseif ($use7z) {
if (Get-Command 7z -ErrorAction SilentlyContinue) {
Write-Host "Using 7z to create the archive."
# Temporarily change directory to src
Push-Location -Path "./src"
try {
# Add all files from ./src recursively
& 7z a -tzip -mx=9 $xpiFilePath *
}
finally {
# Return to the original directory
Pop-Location
}
if ($LASTEXITCODE -ne 0) {
Write-Host "Error: Failed to create the archive using 7z."
Exit 1
}
}
else {
Write-Host "Error: 7z is not installed."
Exit 1
}
}
else {
Write-Host "No archiver found. Please install zip or 7z."
Exit 1
}
Write-Host "Archive created successfully at $xpiFilePath"