-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository(forward slash) forward slash/
/
Copy pathInvoke-LeanSERP-URL-Build.ps1
More file actions
More file actions
Latest commit
638 lines (558 loc) · 12.4 KB
/
Copy pathInvoke-LeanSERP-URL-Build.ps1
File metadata and controls
638 lines (558 loc) · 12.4 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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$NormalizerPath,
[Parameter(Mandatory = $false)]
[string[]]$InputFile = @(),
[Parameter(Mandatory = $false)]
[string]$InputListFile = "",
[Parameter(Mandatory = $true)]
[string]$OutputDirectory,
[Parameter(Mandatory = $true)]
[string]$LogPath,
[Parameter(Mandatory = $false)]
[string]$PublicSuffixList = "",
[Parameter(Mandatory = $false)]
[string]$ApprovedRulesDirectory = "",
[Parameter(Mandatory = $false)]
[ValidateRange(1000, 1000000)]
[int]$ChunkSize = 250000,
[Parameter(Mandatory = $false)]
[switch]$KeepUnderscores,
[Parameter(Mandatory = $false)]
[switch]$RemoveCommonWwwLabels,
[Parameter(Mandatory = $false)]
[switch]$KeepTemporaryFiles,
[Parameter(Mandatory = $false)]
[ValidateSet(
"None",
"Normal",
"Extended",
"Regex"
)]
[string]$ReplacementMode = "None",
[Parameter(Mandatory = $false)]
[string]$FindText = "",
[Parameter(Mandatory = $false)]
[string]$ReplaceText = "",
[Parameter(Mandatory = $false)]
[switch]$CaseSensitive,
[Parameter(Mandatory = $false)]
[switch]$PreviewOnly,
[Parameter(Mandatory = $false)]
[ValidateRange(1, 1000)]
[int]$PreviewLimit = 100
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
function New-Utf8Writer {
param(
[Parameter(Mandatory = $true)]
[string]$Path,
[Parameter(Mandatory = $false)]
[bool]$Append = $false
)
return [System.IO.StreamWriter]::new(
$Path,
$Append,
[System.Text.UTF8Encoding]::new($false),
4096
)
}
function Write-RunnerHeader {
param(
[Parameter(Mandatory = $true)]
[System.IO.StreamWriter]$Writer,
[Parameter(Mandatory = $true)]
[string]$ResolvedNormalizer,
[Parameter(Mandatory = $true)]
[string]$ResolvedOutput,
[Parameter(Mandatory = $true)]
[string[]]$ResolvedInputs,
[Parameter(Mandatory = $true)]
[DateTimeOffset]$StartedAt
)
$Writer.WriteLine(
"LeanSERP URL build runner started."
)
$Writer.WriteLine(
"Started UTC: " +
$StartedAt.ToString("o")
)
$Writer.WriteLine(
"Normalizer: " +
$ResolvedNormalizer
)
$Writer.WriteLine(
"Output directory: " +
$ResolvedOutput
)
$Writer.WriteLine(
"Input files: " +
$ResolvedInputs.Count
)
foreach ($path in $ResolvedInputs) {
$Writer.WriteLine(
" " + $path
)
}
$Writer.WriteLine(
"Chunk size: " +
$ChunkSize
)
$Writer.WriteLine(
"Keep underscores: " +
[string][bool]$KeepUnderscores
)
$Writer.WriteLine(
"Remove common www labels: " +
[string][bool]$RemoveCommonWwwLabels
)
$Writer.WriteLine(
"Keep temporary files: " +
[string][bool]$KeepTemporaryFiles
)
$Writer.WriteLine(
"Replacement mode: " +
$ReplacementMode
Write-Error $errorText
}
finally {
try {
Write-RunnerFooter `
-StartedAt $startedAt `
-ExitCode $exitCode
}
catch {
}
}
if ($exitCode -ne 0) {
exit $exitCode
}
exit 0
$path = "C:\Users\PC\Downloads\LeanSERP-Studio-URL-Mode\Invoke-LeanSERP-URL-Build.ps1"
if (-not (Test-Path -LiteralPath $path -PathType Leaf)) {
throw "Invoke-LeanSERP-URL-Build.ps1 does not exist."
}
$tokens = $null
$errors = $null
[void][System.Management.Automation.Language.Parser]::ParseFile(
$path,
[ref]$tokens,
[ref]$errors
)
if ($errors.Count -gt 0) {
$errors |
Select-Object @{
Name = "Line"
Expression = {
$_.Extent.StartLineNumber
}
}, ErrorId, Message |
Format-Table -Wrap
throw "The URL runner contains syntax errors."
}
$text = Get-Content -LiteralPath $path -Raw
$length = (Get-Item -LiteralPath $path).Length
if ($length -lt 8000) {
throw "The URL runner may be incomplete: $length bytes."
}
$required = @(
'$InputListFile = ""',
'function Write-RunnerHeader',
'function Write-RunnerError',
'function Write-RunnerFooter',
'$combinedInputs =',
'ProgressLogPath =',
'RemoveCommonWwwLabels',
'ReplacementMode',
'PreviewOnly',
'& $resolvedNormalizer @parameters',
'LeanSERP URL build runner started.',
'LeanSERP URL build runner finished.'
)
foreach ($item in $required) {
if (-not $text.Contains($item)) {
throw "Missing URL-runner component: $item"
}
}
if ($text -match '\bTee-Object\b') {
throw "The URL runner must not use Tee-Object."
}
if ($text -match '\[[^\]]+\]\(https?://') {
throw "Markdown-link contamination was detected."
}
if ($text -match '(?m)^\s*```') {
throw "A copied code-fence line was detected."
}
Write-Host "LeanSERP URL runner passed validation." -ForegroundColor Green
Write-Host "File size: $length bytes." -ForegroundColor Green