-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBuild-CRX.ps1
More file actions
154 lines (127 loc) · 5.23 KB
/
Copy pathBuild-CRX.ps1
File metadata and controls
154 lines (127 loc) · 5.23 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
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
# Build-CRX.ps1
#
# Packages the MV3 extension folder into a signed Chromium `.crx`.
# A repository with a pinned extension ID must be packed with its matching
# private key. Refuse to invent a replacement identity when that key is absent.
[CmdletBinding()]
param(
[string]$RepoRoot,
[string]$ExtensionDir = 'extension',
[string]$OutputDir = 'dist',
[string]$KeyPath,
[string]$BrowserPath,
[switch]$SkipExtensionBuild
)
if ([string]::IsNullOrEmpty($RepoRoot)) {
if ($PSScriptRoot) {
$RepoRoot = $PSScriptRoot
} else {
$RepoRoot = (Get-Location).Path
}
}
$ErrorActionPreference = 'Stop'
function Resolve-AbsolutePath([string]$Base, [string]$Value) {
if ([string]::IsNullOrWhiteSpace($Value)) { return $null }
if ([System.IO.Path]::IsPathRooted($Value)) { return $Value }
return (Join-Path $Base $Value)
}
function Resolve-BrowserBinary([string]$PreferredPath) {
if ($PreferredPath) {
$resolved = Resolve-AbsolutePath $RepoRoot $PreferredPath
if (-not (Test-Path -LiteralPath $resolved)) {
throw "BrowserPath not found: $resolved"
}
return $resolved
}
$cmdCandidates = @('chrome', 'msedge')
foreach ($cmd in $cmdCandidates) {
$found = Get-Command -Name $cmd -ErrorAction SilentlyContinue
if ($found) { return $found.Source }
}
$pathCandidates = @(
'C:\Program Files\Google\Chrome\Application\chrome.exe',
'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe',
'C:\Program Files\Microsoft\Edge\Application\msedge.exe',
'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe'
)
foreach ($candidate in $pathCandidates) {
if (Test-Path -LiteralPath $candidate) { return $candidate }
}
throw 'Could not locate Chrome or Edge. Set -BrowserPath to a Chromium-family browser binary.'
}
function Remove-DirectoryIfPresent([string]$TargetPath) {
if (-not (Test-Path -LiteralPath $TargetPath)) { return }
Remove-Item -LiteralPath $TargetPath -Recurse -Force
}
$repoRootAbs = (Resolve-Path -LiteralPath $RepoRoot).Path
$extensionDirAbs = Resolve-AbsolutePath $repoRootAbs $ExtensionDir
$outputDirAbs = Resolve-AbsolutePath $repoRootAbs $OutputDir
if (-not (Test-Path -LiteralPath $extensionDirAbs)) {
throw "Extension directory not found: $extensionDirAbs"
}
if (-not $SkipExtensionBuild) {
& (Join-Path $repoRootAbs 'Build-Extension.ps1') -RepoRoot $repoRootAbs
if ($LASTEXITCODE -ne 0) {
throw 'Build-Extension.ps1 failed before CRX packaging.'
}
}
if (-not (Test-Path -LiteralPath $outputDirAbs)) {
New-Item -ItemType Directory -Path $outputDirAbs | Out-Null
}
$manifestPath = Join-Path $extensionDirAbs 'manifest.json'
$manifest = Get-Content -LiteralPath $manifestPath -Raw | ConvertFrom-Json
if (-not $manifest.version) {
throw "Manifest version missing in $manifestPath"
}
if ([string]::IsNullOrWhiteSpace($KeyPath)) {
$KeyPath = Join-Path $outputDirAbs 'YoutubeAdblock-extension.pem'
} else {
$KeyPath = Resolve-AbsolutePath $repoRootAbs $KeyPath
}
$expectedIdPath = Join-Path $extensionDirAbs 'extension-id.txt'
if ((Test-Path -LiteralPath $expectedIdPath) -and -not (Test-Path -LiteralPath $KeyPath)) {
throw "Stable CRX key not found at $KeyPath. Supply the private key matching $expectedIdPath with -KeyPath. Refusing to generate a new extension identity."
}
$browserBinary = Resolve-BrowserBinary $BrowserPath
$version = [string]$manifest.version
$artifactName = "YoutubeAdblock-extension-v$version.crx"
$artifactPath = Join-Path $outputDirAbs $artifactName
$packRoot = Join-Path $outputDirAbs 'crx-pack'
$packDir = Join-Path $packRoot 'YoutubeAdblock-extension'
$generatedCrx = Join-Path $packRoot 'YoutubeAdblock-extension.crx'
$generatedPem = Join-Path $packRoot 'YoutubeAdblock-extension.pem'
Remove-DirectoryIfPresent $packRoot
New-Item -ItemType Directory -Path $packRoot | Out-Null
Copy-Item -LiteralPath $extensionDirAbs -Destination $packDir -Recurse
$devReadme = Join-Path $packDir 'README.md'
if (Test-Path -LiteralPath $devReadme) {
Remove-Item -LiteralPath $devReadme -Force
}
if (Test-Path -LiteralPath $artifactPath) {
Remove-Item -LiteralPath $artifactPath -Force
}
$args = @(
"--pack-extension=$packDir",
'--no-message-box'
)
if (Test-Path -LiteralPath $KeyPath) {
$args += "--pack-extension-key=$KeyPath"
}
$process = Start-Process -FilePath $browserBinary -ArgumentList $args -Wait -PassThru
if (-not (Test-Path -LiteralPath $generatedCrx)) {
throw "Chromium pack step failed (exit code $($process.ExitCode)); no CRX was generated."
}
$verifyScriptPath = Join-Path $repoRootAbs 'tools\verify-release-artifacts.mjs'
& node $verifyScriptPath --repo-root $repoRootAbs --crx-path $generatedCrx
if ($LASTEXITCODE -ne 0) {
throw 'Generated CRX failed identity or signature verification.'
}
if (-not (Test-Path -LiteralPath $KeyPath) -and (Test-Path -LiteralPath $generatedPem)) {
Move-Item -LiteralPath $generatedPem -Destination $KeyPath -Force
} elseif (Test-Path -LiteralPath $generatedPem) {
Remove-Item -LiteralPath $generatedPem -Force
}
Move-Item -LiteralPath $generatedCrx -Destination $artifactPath -Force
Remove-DirectoryIfPresent $packRoot
Write-Host "Wrote $artifactPath"
Write-Host "Key $KeyPath"