-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository(forward slash) forward slash/
/
Copy pathNew-LeanSerpPackage.ps1
More file actions
More file actions
Latest commit
322 lines (261 loc) · 9.63 KB
/
Copy pathNew-LeanSerpPackage.ps1
File metadata and controls
322 lines (261 loc) · 9.63 KB
You must be signed in to make or propose changes
More edit options
Edit and raw actions
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$ApprovedBuild,
[Parameter(Mandatory = $false)]
[string]$HostSubdomainBlocks = "",
[Parameter(Mandatory = $false)]
[string]$PslHostCandidates = "",
[Parameter(Mandatory = $false)]
[string]$RejectedLines = "",
[Parameter(Mandatory = $false)]
[string]$OutputDirectory = "$env:USERPROFILE\Downloads\LeanSERP-Packages"
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$FormatName = "leanserp-package"
$FormatVersion = 1
$Encoding = [System.Text.UTF8Encoding]::new($false)
function Get-Sha256 {
param(
[Parameter(Mandatory = $true)]
[string]$Path
)
return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLowerInvariant()
}
function Get-LineCount {
param(
[Parameter(Mandatory = $true)]
[string]$Path
)
$reader = [System.IO.StreamReader]::new(
$Path,
[System.Text.UTF8Encoding]::new($false),
$true,
1048576
)
$count = [long]0
try {
while ($null -ne $reader.ReadLine()) {
$count++
}
}
finally {
$reader.Dispose()
}
return $count
}
function Test-SortedUniqueFile {
param(
[Parameter(Mandatory = $true)]
[string]$Path,
[Parameter(Mandatory = $true)]
[string]$Description
)
$reader = [System.IO.StreamReader]::new(
$Path,
[System.Text.UTF8Encoding]::new($false),
$true,
1048576
)
$lineNumber = [long]0
$previous = $null
try {
while ($null -ne ($line = $reader.ReadLine())) {
$lineNumber++
if ([string]::IsNullOrWhiteSpace($line)) {
throw "$Description contains an empty line at line $lineNumber."
}
if ($null -ne $previous) {
$comparison = [string]::CompareOrdinal($previous, $line)
if ($comparison -eq 0) {
throw "$Description contains a duplicate at line $lineNumber`: $line"
}
if ($comparison -gt 0) {
throw "$Description is not ordinally sorted at line $lineNumber`: $line"
}
}
$previous = $line
}
}
finally {
$reader.Dispose()
}
}
function Copy-VerifiedFile {
param(
[Parameter(Mandatory = $true)]
[string]$Source,
[Parameter(Mandatory = $true)]
[string]$Destination
)
if (-not (Test-Path -LiteralPath $Source -PathType Leaf)) {
throw "Source file was not found: $Source"
}
Copy-Item -LiteralPath $Source -Destination $Destination -Force
$sourceHash = Get-Sha256 -Path $Source
$destinationHash = Get-Sha256 -Path $Destination
if ($sourceHash -ne $destinationHash) {
throw "Copy verification failed: $Destination"
}
}
function New-EmptyUtf8File {
param(
[Parameter(Mandatory = $true)]
[string]$Path
)
[System.IO.File]::WriteAllText($Path, "", $Encoding)
}
if (-not (Test-Path -LiteralPath $ApprovedBuild -PathType Container)) {
throw "Approved build directory was not found: $ApprovedBuild"
}
$approvedLabels = Join-Path $ApprovedBuild "labels.txt"
$filesMetadata = [ordered]@{}
foreach ($name in $fileNames) {
$path = Join-Path $packageDirectory $name
$role = switch ($name) {
"labels.txt" { "active-label-blocks" }
"exact-host-blocks.txt" { "active-exact-host-blocks" }
"host-subdomain-blocks.txt" { "active-host-subdomain-blocks" }
"psl-label-overrides.txt" { "active-psl-label-overrides" }
"psl-host-candidates.txt" { "review-only-psl-host-candidates" }
"rejected-lines.tsv" { "diagnostic-rejections" }
}
$importByFilter = $name -in @(
"labels.txt",
"exact-host-blocks.txt",
"host-subdomain-blocks.txt",
"psl-label-overrides.txt"
)
$filesMetadata[$name] = [ordered]@{
role = $role
importByFilter = $importByFilter
count = Get-LineCount -Path $path
byteLength = (Get-Item -LiteralPath $path).Length
sha256 = Get-Sha256 -Path $path
}
}
$manifest = [ordered]@{
format = $FormatName
version = $FormatVersion
createdAt = [DateTimeOffset]::UtcNow.ToString("o")
encoding = "utf-8"
sorting = "ordinal"
packageDirectoryName = Split-Path $packageDirectory -Leaf
sourceApprovedBuild = (Resolve-Path -LiteralPath $ApprovedBuild).Path
activationPolicy = [ordered]@{
atomicImportRequired = $true
verifyEveryFileBeforeActivation = $true
candidateFilesAreReviewOnly = $true
retainPreviousGenerationUntilSuccess = $true
}
precedence = @(
"exact-host-allow",
"label-allow",
"exact-host-block",
"host-subdomain-block",
"psl-label-override",
"public-suffix-exclusion",
"blocked-label"
)
files = $filesMetadata
}
$manifestJson = $manifest | ConvertTo-Json -Depth 12
[System.IO.File]::WriteAllText(
$manifestDestination,
$manifestJson + [Environment]::NewLine,
$Encoding
)
$manifestHash = Get-Sha256 -Path $manifestDestination
Write-Host ""
Write-Host "Unified LeanSERP package created." -ForegroundColor Green
Write-Host "Package: $packageDirectory" -ForegroundColor Green
Write-Host "Labels: $labelsCount"
Write-Host "Exact-host blocks: $exactHostsCount"
Write-Host "Host-subdomain blocks: $($filesMetadata['host-subdomain-blocks.txt'].count)"
Write-Host "PSL label overrides: $overridesCount"
Write-Host "PSL host candidates: $($filesMetadata['psl-host-candidates.txt'].count)"
Write-Host "Manifest SHA-256: $manifestHash"