基于WebView2实现的Win11桌面框架,利用插件自定义
chunhualin1
2026-08-12 da97410f48a1e94d7e1fe019c69a7c847fe67ddf
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
param(
    [string]$Configuration = "Release",
    [string]$Runtime = "win-x64",
    [string]$TargetRoot = "D:\lin\tools\Moli.Desktop",
    [string]$AppName = "Moli.Desktop",
    [switch]$SelfContained,
    [switch]$SkipTests,
    [switch]$NoZip,
    [switch]$KeepStaging
)
 
$ErrorActionPreference = "Stop"
 
function Invoke-CheckedRobocopy {
    param(
        [Parameter(Mandatory = $true)]
        [string]$Source,
        [Parameter(Mandatory = $true)]
        [string]$Destination
    )
 
    robocopy $Source $Destination /MIR /FFT /R:2 /W:2 /XD ".git" ".vs" /XF "*.pdb"
    $exitCode = $LASTEXITCODE
    if ($exitCode -gt 7) {
        throw "robocopy failed with exit code $exitCode"
    }
}
 
function Stop-PublishedApplication {
    param(
        [Parameter(Mandatory = $true)]
        [string]$ProcessName,
        [Parameter(Mandatory = $true)]
        [string]$ExecutablePath
    )
 
    $normalizedExecutablePath = [System.IO.Path]::GetFullPath($ExecutablePath)
    $processes = Get-Process -Name $ProcessName -ErrorAction SilentlyContinue
    foreach ($process in $processes) {
        $matchesTarget = $false
        try {
            $processPath = [System.IO.Path]::GetFullPath($process.MainModule.FileName)
            $matchesTarget = [string]::Equals($processPath, $normalizedExecutablePath, [System.StringComparison]::OrdinalIgnoreCase)
        }
        catch {
            Write-Warning "Cannot inspect process path for $ProcessName (PID $($process.Id)); skipping it."
        }
 
        if ($matchesTarget) {
            Write-Host "Stopping running application: $ProcessName (PID $($process.Id))"
            Stop-Process -Id $process.Id -Force
            $process.WaitForExit(10000)
        }
    }
}
 
function Get-RelativePathCompat {
    param(
        [Parameter(Mandatory = $true)]
        [string]$BasePath,
        [Parameter(Mandatory = $true)]
        [string]$TargetPath
    )
 
    $baseFullPath = [System.IO.Path]::GetFullPath($BasePath).TrimEnd([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar)
    $targetFullPath = [System.IO.Path]::GetFullPath($TargetPath).TrimEnd([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar)
    $baseUri = New-Object System.Uri(($baseFullPath + [System.IO.Path]::DirectorySeparatorChar))
    $targetUri = New-Object System.Uri($targetFullPath)
    $relativeUri = $baseUri.MakeRelativeUri($targetUri)
    return [System.Uri]::UnescapeDataString($relativeUri.ToString()).Replace('/', [System.IO.Path]::DirectorySeparatorChar)
}
 
function Backup-DeployedPluginConfigs {
    param(
        [Parameter(Mandatory = $true)]
        [string]$ApplicationDirectory,
        [Parameter(Mandatory = $true)]
        [string]$BackupDirectory
    )
 
    $pluginsRoot = Join-Path $ApplicationDirectory "samples\plugins"
    if (-not (Test-Path -LiteralPath $pluginsRoot)) {
        return
    }
 
    Get-ChildItem -LiteralPath $pluginsRoot -Directory | ForEach-Object {
        $pluginJson = Join-Path $_.FullName "plugin.json"
        if (-not (Test-Path -LiteralPath $pluginJson)) {
            return
        }
 
        $relativeDirectory = Get-RelativePathCompat -BasePath $pluginsRoot -TargetPath $_.FullName
        $backupPluginDirectory = Join-Path $BackupDirectory $relativeDirectory
        New-Item -ItemType Directory -Force -Path $backupPluginDirectory | Out-Null
        Copy-Item -LiteralPath $pluginJson -Destination (Join-Path $backupPluginDirectory "plugin.json") -Force
    }
}
 
function Restore-DeployedPluginConfigs {
    param(
        [Parameter(Mandatory = $true)]
        [string]$ApplicationDirectory,
        [Parameter(Mandatory = $true)]
        [string]$BackupDirectory
    )
 
    if (-not (Test-Path -LiteralPath $BackupDirectory)) {
        return
    }
 
    $pluginsRoot = Join-Path $ApplicationDirectory "samples\plugins"
    Get-ChildItem -LiteralPath $BackupDirectory -Directory | ForEach-Object {
        $relativeDirectory = Get-RelativePathCompat -BasePath $BackupDirectory -TargetPath $_.FullName
        $targetPluginDirectory = Join-Path $pluginsRoot $relativeDirectory
        $newPluginJson = Join-Path $targetPluginDirectory "plugin.json"
        $templateJson = Join-Path $targetPluginDirectory "plugin.template.json"
        $backupPluginJson = Join-Path $_.FullName "plugin.json"
 
        if ((Test-Path -LiteralPath $newPluginJson) -and -not (Test-Path -LiteralPath $templateJson)) {
            Copy-Item -LiteralPath $newPluginJson -Destination $templateJson -Force
        }
 
        if (Test-Path -LiteralPath $backupPluginJson) {
            Copy-Item -LiteralPath $backupPluginJson -Destination $newPluginJson -Force
        }
    }
}
 
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot "..")
$solutionPath = Join-Path $repoRoot "Moli.Desktop.slnx"
$projectPath = Join-Path $repoRoot "src\Moli.Desktop\Moli.Desktop.csproj"
$stagingRoot = Join-Path $repoRoot "artifacts\release"
$publishDir = Join-Path $stagingRoot "publish\$AppName"
$configBackupDir = Join-Path $stagingRoot "preserved-plugin-configs"
$targetRootPath = Join-Path $TargetRoot ""
$targetDir = Join-Path $targetRootPath $AppName
$targetExePath = Join-Path $targetDir "$AppName.exe"
$zipPath = Join-Path $targetRootPath "$AppName-$Runtime.zip"
 
if (-not (Test-Path -LiteralPath $projectPath)) {
    throw "Project file not found: $projectPath"
}
 
if (-not (Test-Path -LiteralPath $TargetRoot)) {
    New-Item -ItemType Directory -Force -Path $TargetRoot | Out-Null
}
 
$resolvedTargetRoot = (Resolve-Path -LiteralPath $TargetRoot).Path
New-Item -ItemType Directory -Force -Path $targetDir | Out-Null
$resolvedTargetDir = (Resolve-Path -LiteralPath $targetDir).Path
 
if ($resolvedTargetDir -eq $resolvedTargetRoot) {
    throw "Refusing to publish directly into target root: $resolvedTargetRoot"
}
 
if (-not $resolvedTargetDir.StartsWith($resolvedTargetRoot, [System.StringComparison]::OrdinalIgnoreCase)) {
    throw "Resolved target is outside target root: $resolvedTargetDir"
}
 
New-Item -ItemType Directory -Force -Path $stagingRoot | Out-Null
if (Test-Path -LiteralPath $publishDir) {
    Remove-Item -LiteralPath $publishDir -Recurse -Force
}
 
if (-not $SkipTests) {
    dotnet test $solutionPath --configuration $Configuration
}
 
$selfContainedValue = if ($SelfContained) { "true" } else { "false" }
dotnet publish $projectPath `
    --configuration $Configuration `
    --runtime $Runtime `
    --self-contained $selfContainedValue `
    -p:PublishSingleFile=false `
    --output $publishDir
 
Stop-PublishedApplication -ProcessName $AppName -ExecutablePath $targetExePath
 
if (Test-Path -LiteralPath $configBackupDir) {
    Remove-Item -LiteralPath $configBackupDir -Recurse -Force
}
New-Item -ItemType Directory -Force -Path $configBackupDir | Out-Null
Backup-DeployedPluginConfigs -ApplicationDirectory $targetDir -BackupDirectory $configBackupDir
 
if (Test-Path -LiteralPath $targetDir) {
    Write-Host "Removing old published files:"
    Write-Host $targetDir
    Remove-Item -LiteralPath $targetDir -Recurse -Force
}
New-Item -ItemType Directory -Force -Path $targetDir | Out-Null
 
Invoke-CheckedRobocopy -Source $publishDir -Destination $targetDir
Restore-DeployedPluginConfigs -ApplicationDirectory $targetDir -BackupDirectory $configBackupDir
 
if (-not $NoZip) {
    if (Test-Path -LiteralPath $zipPath) {
        Remove-Item -LiteralPath $zipPath -Force
    }
    Compress-Archive -Path (Join-Path $publishDir "*") -DestinationPath $zipPath -Force
}
 
if (-not $KeepStaging) {
    Remove-Item -LiteralPath $stagingRoot -Recurse -Force
}
 
Write-Host "Starting application:"
Write-Host $targetExePath
Start-Process -FilePath $targetExePath -WorkingDirectory $targetDir
 
Write-Host "Published application:"
Write-Host $targetDir
if (-not $NoZip) {
    Write-Host "Portable package:"
    Write-Host $zipPath
}