-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository(forward slash) forward slash/
/
Copy pathLeanSERP-Compiled-GUI.ps1
More file actions
More file actions
Latest commit
1734 lines (1499 loc) · 52.7 KB
/
Copy pathLeanSERP-Compiled-GUI.ps1
File metadata and controls
1734 lines (1499 loc) · 52.7 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
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
[CmdletBinding()]
param()
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$scriptPath = $MyInvocation.MyCommand.Path
$scriptDirectory = Split-Path -Parent $scriptPath
$normalizerPath = Join-Path $scriptDirectory "bin\Release\net8.0\LeanSerpUrlNormalizer.exe"
$postprocessorPath = Join-Path $scriptDirectory "bin\Release\net8.0\ApplyApprovedOverrides.exe"
$packageGeneratorPath = Join-Path $scriptDirectory "New-LeanSerpPackage.ps1"
$pslPath = Join-Path $scriptDirectory "public_suffix_list.dat"
$approvedRulesDirectory = Join-Path $scriptDirectory "Approved-Rules"
$hostSubdomainRulesPath = Join-Path $scriptDirectory "PSL-Host-Review\Approved-Exact-Hosts\approved-psl-host-subdomains.txt"
$defaultCompiledOutput = Join-Path $env:USERPROFILE "Downloads\LeanSERP-Compiled-Output"
$defaultApprovedOutput = Join-Path $env:USERPROFILE "Downloads\LeanSERP-Compiled-Approved-Output"
$defaultPackageOutput = Join-Path $env:USERPROFILE "Downloads\LeanSERP-Packages"
$defaultLogDirectory = Join-Path $scriptDirectory "GUI-Logs"
$selectedFiles = [System.Collections.Generic.List[string]]::new()
$script:activeProcess = $null
$script:activeStage = "Idle"
$script:stopRequested = $false
$script:activeInputListPath = ""
$script:activeLogPath = ""
$script:displayedLogLineCount = 0
$script:compiledBuildPath = ""
$script:approvedBuildPath = ""
$script:packagePath = ""
$script:buildStartedAt = $null
function New-Button {
param(
[Parameter(Mandatory = $true)]
[string]$Text,
[Parameter(Mandatory = $false)]
[int]$Width = 145
)
$button = [System.Windows.Forms.Button]::new()
$button.Text = $Text
$button.Width = $Width
$button.Height = 34
$button.Margin = [System.Windows.Forms.Padding]::new(0, 0, 8, 8)
return $button
}
function Set-ClipboardText {
param(
[AllowEmptyString()]
[string]$Text,
[Parameter(Mandatory = $true)]
[string]$Description
)
if ([string]::IsNullOrWhiteSpace($Text)) {
[void][System.Windows.Forms.MessageBox]::Show(
"There is no $Description to copy.",
"LeanSERP Studio",
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Information
)
return
}
try {
[System.Windows.Forms.Clipboard]::SetText($Text)
}
catch {
[void][System.Windows.Forms.MessageBox]::Show(
"Could not copy the ${Description}:`r`n$($_.Exception.Message)",
"LeanSERP Studio",
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Error
)
}
}
function Quote-NativeArgument {
param(
[AllowEmptyString()]
[string]$Value
)
if ($null -eq $Value) {
return '""'
}
if ($Value.Length -gt 0 -and $Value -notmatch '[\s"]') {
return $Value
}
$escaped = $Value -replace '(\\*)"', '$1$1\"'
$escaped = $escaped -replace '(\\+)$', '$1$1'
return '"' + $escaped + '"'
}
function Test-ProcessRunning {
if ($null -eq $script:activeProcess) {
return $false
}
try {
return -not $script:activeProcess.HasExited
}
catch {
return $false
}
}
function Stop-ProcessTree {
param(
[Parameter(Mandatory = $true)]
[int]$ProcessId
)
$children = @(
Get-CimInstance -ClassName Win32_Process -Filter "ParentProcessId=$ProcessId" -ErrorAction SilentlyContinue
)
foreach ($child in $children) {
Stop-ProcessTree -ProcessId $child.ProcessId
}
try {
Stop-Process -Id $ProcessId -Force -ErrorAction Stop
}
catch {
}
}
function Get-NewestDirectory {
param(
[Parameter(Mandatory = $true)]
[string]$Root,
[Parameter(Mandatory = $true)]
[string]$Pattern,
[Parameter(Mandatory = $true)]
[datetime]$CreatedAfter
)
if (-not (Test-Path -LiteralPath $Root -PathType Container)) {
return $null
}
return @(
Get-ChildItem -LiteralPath $Root -Directory -ErrorAction SilentlyContinue |
Where-Object {
$_.Name -like $Pattern -and
$_.CreationTime -ge $CreatedAfter.AddSeconds(-5)
} |
Sort-Object LastWriteTime -Descending
) | Select-Object -First 1
}
function Get-LineCount {
param(
[Parameter(Mandatory = $true)]
[string]$Path
)
$reader = [System.IO.StreamReader]::new(
$Path,
[System.Text.UTF8Encoding]::new($false),
$true,
1048576
$form.Add_FormClosing({
param(
[object]$Sender,
[System.Windows.Forms.FormClosingEventArgs]$Event
)
if (-not (Test-ProcessRunning)) {
return
}
$choice = [System.Windows.Forms.MessageBox]::Show(
"The compiled pipeline is still running. Stop it and close Studio?",
"LeanSERP Studio",
[System.Windows.Forms.MessageBoxButtons]::YesNo,
[System.Windows.Forms.MessageBoxIcon]::Warning
)
if ($choice -ne [System.Windows.Forms.DialogResult]::Yes) {
$Event.Cancel = $true
return
}
$script:stopRequested = $true
try {
Stop-ProcessTree -ProcessId $script:activeProcess.Id
}
catch {
}
})
$form.Add_FormClosed({
$timer.Stop()
$timer.Dispose()
if ($null -ne $script:activeProcess) {
try {
if (-not $script:activeProcess.HasExited) {
Stop-ProcessTree -ProcessId $script:activeProcess.Id
}
}
catch {
}
try {
$script:activeProcess.Dispose()
}
catch {
}
$script:activeProcess = $null
}
if (
-not [string]::IsNullOrWhiteSpace($script:activeInputListPath) -and
(Test-Path -LiteralPath $script:activeInputListPath -PathType Leaf)
) {
Remove-Item `
-LiteralPath $script:activeInputListPath `
-Force `
-ErrorAction SilentlyContinue
}
$script:activeInputListPath = ""
})
Test-RequiredFiles
Update-FileList
Set-InterfaceRunning -Running $false
$null = $form.ShowDialog()
$form.Dispose()