-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository(forward slash) forward slash/
/
Copy pathStart-Compiled-LeanSERP-Pipeline.ps1
More file actions
More file actions
Latest commit
349 lines (289 loc) · 8.16 KB
/
Copy pathStart-Compiled-LeanSERP-Pipeline.ps1
File metadata and controls
349 lines (289 loc) · 8.16 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$InputFile,
[Parameter(Mandatory = $false)]
[string]$CompiledOutputDirectory =
"$env:USERPROFILE\Downloads\LeanSERP-Compiled-Output",
[Parameter(Mandatory = $false)]
[string]$ApprovedOutputDirectory =
"$env:USERPROFILE\Downloads\LeanSERP-Compiled-Approved-Output",
[Parameter(Mandatory = $false)]
[ValidateRange(1000, 1000000)]
[int]$ChunkSize = 500000,
[Parameter(Mandatory = $false)]
[switch]$KeepUnderscores,
[Parameter(Mandatory = $false)]
[switch]$RemoveCommonWwwLabels
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$studioDirectory =
"C:\Users\PC\Downloads\LeanSERP-Studio-URL-Mode"
$normalizerExe = Join-Path `
$studioDirectory `
"bin\Release\net8.0\LeanSerpUrlNormalizer.exe"
$postprocessorExe = Join-Path `
$studioDirectory `
"bin\Release\net8.0\ApplyApprovedOverrides.exe"
$publicSuffixList = Join-Path `
$studioDirectory `
"public_suffix_list.dat"
$approvedRulesDirectory = Join-Path `
$studioDirectory `
"Approved-Rules"
function Get-Sha256 {
param(
[Parameter(Mandatory = $true)]
[string]$Path
)
return (
Get-FileHash `
-LiteralPath $Path `
-Algorithm SHA256
).Hash.ToLowerInvariant()
}
foreach ($requiredFile in @(
$InputFile,
$normalizerExe,
$postprocessorExe,
$publicSuffixList
)) {
if (-not (
Test-Path `
-LiteralPath $requiredFile `
-PathType Leaf
)) {
throw "Required file was not found: $requiredFile"
}
}
if (-not (
Test-Path `
-LiteralPath $approvedRulesDirectory `
-PathType Container
)) {
throw "Approved-rules directory was not found: $approvedRulesDirectory"
}
New-Item `
-ItemType Directory `
-Path $CompiledOutputDirectory `
-Force |
Out-Null
New-Item `
-ItemType Directory `
-Path $ApprovedOutputDirectory `
-Force |
Out-Null
$pipelineStarted = Get-Date
$existingCompiledBuilds = @{}
Get-ChildItem `
-LiteralPath $CompiledOutputDirectory `
-Directory `
-ErrorAction SilentlyContinue |
Where-Object {
$_.Name -like "compiled-build-*"
} |
ForEach-Object {
$existingCompiledBuilds[$_.FullName] = $true
}
Write-Host ""
Write-Host "Starting compiled LeanSERP normalization." `
-ForegroundColor Cyan
Write-Host "Input: $InputFile"
Write-Host "Chunk size: $ChunkSize"
$normalizerArguments = @(
"--input",
$InputFile,
"--output",
$CompiledOutputDirectory,
"--psl",
$publicSuffixList,
"--chunk-size",
[string]$ChunkSize
)
if ($KeepUnderscores) {
$normalizerArguments += "--keep-underscores"
}
if ($RemoveCommonWwwLabels) {
$normalizerArguments += "--remove-www"
}
& $normalizerExe @normalizerArguments
if ($LASTEXITCODE -ne 0) {
throw "Compiled normalizer failed with exit code $LASTEXITCODE."
}
$newCompiledBuilds = @(
Get-ChildItem `
-LiteralPath $CompiledOutputDirectory `
-Directory |
Where-Object {
"approved-overrides-report.json"
foreach ($requiredOutput in @(
$approvedLabelsPath,
$approvedOverridesPath,
$approvedExactHostsPath,
$approvedReportPath
)) {
if (-not (
Test-Path `
-LiteralPath $requiredOutput `
-PathType Leaf
)) {
throw "Approved build is incomplete: $requiredOutput"
}
}
$approvedReport = Get-Content `
-LiteralPath $approvedReportPath `
-Raw |
ConvertFrom-Json
$approvedLabelsHash = Get-Sha256 `
-Path $approvedLabelsPath
$approvedOverridesHash = Get-Sha256 `
-Path $approvedOverridesPath
$approvedExactHostsHash = Get-Sha256 `
-Path $approvedExactHostsPath
if ($approvedLabelsHash -ne (
[string]$approvedReport.LabelsSha256
).ToLowerInvariant()) {
throw "Approved labels failed SHA-256 verification."
}
if ($approvedOverridesHash -ne (
[string]$approvedReport.OverridesSha256
).ToLowerInvariant()) {
throw "Approved PSL overrides failed SHA-256 verification."
}
if ($approvedExactHostsHash -ne (
[string]$approvedReport.ExactHostsSha256
).ToLowerInvariant()) {
throw "Approved exact hosts failed SHA-256 verification."
}
$pipelineFinished = Get-Date
$durationSeconds = [math]::Round(
($pipelineFinished - $pipelineStarted).TotalSeconds,
3
)
Write-Host ""
Write-Host "Compiled LeanSERP pipeline completed." `
-ForegroundColor Green
[pscustomobject]@{
InputFile = (Resolve-Path -LiteralPath $InputFile).Path
CompiledBuild = $compiledBuild.FullName
ApprovedBuild = $approvedBuild.FullName
InputLines = $compiledMetadata.InputLines
CompiledLabels = $compiledMetadata.UniqueLabels
FinalLabels = $approvedReport.FinalLabelCount
LabelsAddedByOverrides = $approvedReport.AddedLabelCount
UsedPslOverrides = $approvedReport.UsedOverrideCount
UsedExactHosts = $approvedReport.UsedExactHostCount
LabelsSha256 = $approvedLabelsHash
DurationSeconds = $durationSeconds
Verified = $true
} |
Format-List