-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.ps1
More file actions
115 lines (100 loc) · 3.5 KB
/
compile.ps1
File metadata and controls
115 lines (100 loc) · 3.5 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
# 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 = "BulkDelete-$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)
# List of files to include in the ZIP
$files = @(
"./src/manifest.json",
"./src/_locales",
"./src/icons",
"./src/popup.html",
"./src/popup.js",
"./src/i18n.js",
"./src/styles.css"
)
# Iterate through each file and add it to the ZIP
foreach ($file in $files)
{
$fileObject = Get-Item $file
# Calculate the relative path inside the ZIP archive
$relativePath = $fileObject.Name #$fileObject.FullName.Substring($workingDir.FullName.Length + 1) -replace '\\', '/'
Write-Host "Adding: $relativePath"
if ($fileObject.PSIsContainer)
{
# Add all files inside the directory to the ZIP
$directoryFiles = Get-ChildItem -Path $fileObject.FullName -Recurse -File
foreach ($dirFile in $directoryFiles)
{
$relativeDirFilePath = $dirFile.FullName.Substring($workingDir.FullName.Length + 1) -replace '\\', '/'
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $dirFile.FullName, $relativeDirFilePath)
}
}
else
{
# Add the single file to the ZIP
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $fileObject.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"