-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcompile.ps1
More file actions
88 lines (75 loc) · 2.73 KB
/
compile.ps1
File metadata and controls
88 lines (75 loc) · 2.73 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
# Set to stop on any error
$ErrorActionPreference = "Stop"
$currentDir = Get-Location
$workingDir = Get-Item -Path (Join-Path -Path $currentDir -ChildPath "src")
Write-Host "The working directory path is:" $workingDir.Path
$manifestFile = "./src/manifest.json"
# 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."
$VERSION = Select-String -Path "$manifestFile" -Pattern '"version"\s*:\s*"([^"]+)"' | ForEach-Object {
$_.Matches[0].Groups[1].Value
}
}
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
Write-Host "Created build directory: $buildDir"
}
# Define variables
$fileName = "BetterUnsubscribe-$VERSION"
$zipFileName = "$fileName.zip"
$xpiFileName = "$fileName.xpi"
$zipFilePath = Join-Path -Path "$buildDir" -ChildPath $zipFileName
$xpiFilePath = Join-Path -Path "$buildDir" -ChildPath $xpiFileName
# Add required .NET assemblies for compression
Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.FileSystem
# Create the ZIP archive
$zip = [System.IO.Compression.ZipFile]::Open($zipFilePath, [System.IO.Compression.ZipArchiveMode]::Create)
# Add all files from ./src into the root of the ZIP (no "src/" prefix)
$allFiles = Get-ChildItem -Path $workingDir.FullName -Recurse -File
foreach ($file in $allFiles)
{
$relativePath = $file.FullName.Substring($workingDir.FullName.Length + 1) -replace '\\', '/'
Write-Host "Adding: $relativePath"
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $file.FullName, $relativePath)
}
# Release the ZIP file
$zip.Dispose()
# Output the location of the ZIP file
Write-Host "Directory compressed into: $zipFilePath"
# Check if the .xpi file already exists
if (Test-Path $xpiFilePath)
{
# Ask the user if they want to overwrite the existing file
$response = Read-Host "The file $xpiFilePath already exists. Do you want to replace it? (y/n)"
if ($response.ToLower() -ne "y")
{
Write-Host "Skipping the renaming. Exiting script."
Exit 0
}
Write-Host "Overwriting the existing file..."
# Remove the existing .xpi file
Remove-Item -Path $xpiFilePath -Force
}
# Rename the .zip file to .xpi
Rename-Item -Path $zipFilePath -NewName $xpiFileName
Write-Host "Archive renamed to: $xpiFileName"