33
44param (
55 [Parameter (Mandatory = $false )]
6- [string ]$OutputDir = " C:\Users\14438\Desktop\wuji_things"
6+ [string ]$OutputDir = " C:\Users\14438\Desktop\wuji_things" ,
7+ [string ]$Notes = " " ,
8+ [switch ]$SkipBuild
79)
810
911# 定义Android目标平台
@@ -34,58 +36,62 @@ Write-Host "输出目录: $OutputDir" -ForegroundColor Cyan
3436Write-Host " ======================================" - ForegroundColor Cyan
3537
3638# 首先打包通用版本(不加target参数)
37- Write-Host " `n [1/5] 正在打包通用版本..." - ForegroundColor Yellow
38- try {
39- pnpm tauri android build
40- if ($LASTEXITCODE -eq 0 ) {
41- Write-Host " ✓ 通用版本打包成功" - ForegroundColor Green
42-
43- # 复制APK到输出目录
44- $apkFiles = Get-ChildItem - Path $apkSourcePath - Filter " *.apk"
45- foreach ($apk in $apkFiles ) {
46- $newName = " wuji-$version -universal.apk"
47- $destPath = Join-Path $OutputDir $newName
48- Copy-Item $apk.FullName $destPath - Force
49- Write-Host " 已保存: $newName " - ForegroundColor Gray
39+ if (-not $SkipBuild ) {
40+ Write-Host " `n [1/5] 正在打包通用版本..." - ForegroundColor Yellow
41+ try {
42+ pnpm tauri android build
43+ if ($LASTEXITCODE -eq 0 ) {
44+ Write-Host " ✓ 通用版本打包成功" - ForegroundColor Green
45+
46+ # 复制APK到输出目录
47+ $apkFiles = Get-ChildItem - Path $apkSourcePath - Filter " *.apk"
48+ foreach ($apk in $apkFiles ) {
49+ $newName = " wuji-$version -universal.apk"
50+ $destPath = Join-Path $OutputDir $newName
51+ Copy-Item $apk.FullName $destPath - Force
52+ Write-Host " 已保存: $newName " - ForegroundColor Gray
53+ }
54+ } else {
55+ Write-Host " ✗ 通用版本打包失败" - ForegroundColor Red
5056 }
51- } else {
52- Write-Host " ✗ 通用版本打包失败 " - ForegroundColor Red
57+ } catch {
58+ Write-Host " ✗ 通用版本打包出错: $_ " - ForegroundColor Red
5359 }
54- } catch {
55- Write-Host " ✗ 通用版本打包出错: $_ " - ForegroundColor Red
56- }
5760
58- # 打包每个平台
59- $index = 2
60- foreach ($target in $targets ) {
61- Write-Host " `n [$index /5] 正在打包 $target 平台..." - ForegroundColor Yellow
62-
63- try {
64- pnpm tauri android build -- target $target
61+ # 打包每个平台
62+ $index = 2
63+ foreach ($target in $targets ) {
64+ Write-Host " `n [$index /5] 正在打包 $target 平台..." - ForegroundColor Yellow
6565
66- if ( $LASTEXITCODE -eq 0 ) {
67- Write-Host " ✓ $target 平台打包成功 " - ForegroundColor Green
66+ try {
67+ pnpm tauri android build -- target $target
6868
69- # 复制APK到输出目录
70- if (Test-Path $apkSourcePath ) {
71- $apkFiles = Get-ChildItem - Path $apkSourcePath - Filter " *.apk"
72- foreach ($apk in $apkFiles ) {
73- $newName = " wuji-$version -${target} .apk"
74- $destPath = Join-Path $OutputDir $newName
75- Copy-Item $apk.FullName $destPath - Force
76- Write-Host " 已保存: $newName " - ForegroundColor Gray
69+ if ($LASTEXITCODE -eq 0 ) {
70+ Write-Host " ✓ $target 平台打包成功" - ForegroundColor Green
71+
72+ # 复制APK到输出目录
73+ if (Test-Path $apkSourcePath ) {
74+ $apkFiles = Get-ChildItem - Path $apkSourcePath - Filter " *.apk"
75+ foreach ($apk in $apkFiles ) {
76+ $newName = " wuji-$version -${target} .apk"
77+ $destPath = Join-Path $OutputDir $newName
78+ Copy-Item $apk.FullName $destPath - Force
79+ Write-Host " 已保存: $newName " - ForegroundColor Gray
80+ }
81+ } else {
82+ Write-Host " 警告: 找不到APK文件路径" - ForegroundColor Yellow
7783 }
7884 } else {
79- Write-Host " 警告: 找不到APK文件路径 " - ForegroundColor Yellow
85+ Write-Host " ✗ $target 平台打包失败 " - ForegroundColor Red
8086 }
81- } else {
82- Write-Host " ✗ $target 平台打包失败 " - ForegroundColor Red
87+ } catch {
88+ Write-Host " ✗ $target 平台打包出错: $_ " - ForegroundColor Red
8389 }
84- } catch {
85- Write-Host " ✗ $target 平台打包出错: $_ " - ForegroundColor Red
90+
91+ $index ++
8692 }
87-
88- $index ++
93+ } else {
94+ Write-Host " `n 跳过 Android 打包步骤,直接处理现有文件... " - ForegroundColor Yellow
8995}
9096
9197# 打包完成统计
@@ -95,9 +101,88 @@ Write-Host "======================================" -ForegroundColor Cyan
95101
96102$allApks = Get-ChildItem - Path $OutputDir - Filter " wuji-$version -*.apk"
97103Write-Host " 共生成 $ ( $allApks.Count ) 个APK文件:" - ForegroundColor Green
104+
105+ # 准备更新 updater_android.json
106+ $updaterJsonPath = " C:\Users\14438\Documents\GitHub\wuji-tauri\scripts\update\updater_android.json"
107+ $androidPlatforms = @ {}
108+
98109foreach ($apk in $allApks ) {
99110 $size = [math ]::Round($apk.Length / 1 MB , 2 )
100111 Write-Host " - $ ( $apk.Name ) ($size MB)" - ForegroundColor Gray
112+
113+ # 构建下载 URL (缤纷云地址)
114+ $encodedName = [Uri ]::EscapeDataString($apk.Name )
115+ $downloadUrl = " https://wuji.moshangwangluo.com/$encodedName "
116+
117+ # 提取平台名称
118+ if ($apk.Name -match " wuji-$version -(.*).apk" ) {
119+ $platformKey = " android-" + $Matches [1 ]
120+ $androidPlatforms [$platformKey ] = @ { url = $downloadUrl }
121+ }
122+ }
123+
124+ # 更新 updater_android.json
125+ if (Test-Path $updaterJsonPath ) {
126+ try {
127+ $jsonContent = Get-Content $updaterJsonPath - Raw - Encoding UTF8 | ConvertFrom-Json
128+
129+ # 更新基础信息
130+ $jsonContent.version = $version
131+ $jsonContent.pub_date = (Get-Date ).ToString(" yyyy-MM-ddTHH:mm:ss" ) + " +08:00"
132+
133+ if ([string ]::IsNullOrWhiteSpace($Notes )) {
134+ $jsonContent.notes = " 更新版本 $version "
135+ } else {
136+ $jsonContent.notes = $Notes
137+ }
138+
139+ # 保留原有的 cloudpan,更新或添加 android 平台
140+ if (-not $jsonContent.platforms ) {
141+ $jsonContent.platforms = @ {}
142+ }
143+
144+ foreach ($key in $androidPlatforms.Keys ) {
145+ $jsonContent.platforms .$key = $androidPlatforms [$key ]
146+ }
147+
148+ # 保存 JSON (2 空格缩进)
149+ $jsonRaw = $jsonContent | ConvertTo-Json - Depth 10 - Compress
150+
151+ # 尝试用 Python 进行格式化 (最稳定)
152+ # 注意:这里需要项目根目录,但脚本中没定义 projectRoot,我们通过当前脚本位置反推
153+ $scriptDir = Split-Path $MyInvocation.MyCommand.Path - Parent
154+ $rootPath = Split-Path $scriptDir - Parent # scripts 目录的上级
155+ $pythonPath = Join-Path $rootPath " .venv\Scripts\python.exe"
156+ if (-not (Test-Path $pythonPath )) { $pythonPath = " python" }
157+
158+ try {
159+ $bytes = [System.Text.Encoding ]::UTF8.GetBytes($jsonRaw )
160+ $base64 = [System.Convert ]::ToBase64String($bytes )
161+ $pyCmd = " import json, base64, sys; data=json.loads(base64.b64decode('$base64 ').decode('utf-8')); print(json.dumps(data, indent=2, ensure_ascii=False))"
162+ # 使用 Out-String 确保保留从 Python 输出的换行符
163+ $newJson = & $pythonPath - c $pyCmd | Out-String
164+ if ($LASTEXITCODE -ne 0 -or [string ]::IsNullOrWhiteSpace($newJson )) { throw " Python 格式化失败" }
165+ } catch {
166+ Write-Host " ⚠ 警告: Python 格式化失败,使用备用正则修复缩进" - ForegroundColor Yellow
167+ $newJson = $jsonContent | ConvertTo-Json - Depth 10
168+ $newJson = [regex ]::Replace($newJson , " (?m)^( +)" , {
169+ param ($m )
170+ $len = $m.Value.Length
171+ $newLen = [int ]($len / 4 ) * 2
172+ if ($newLen -lt 2 ) { $newLen = 2 }
173+ return " " * $newLen
174+ })
175+ $newJson = $newJson -replace ' ": +' , ' ": '
176+ }
177+
178+ # 使用无 BOM 的 UTF-8 编码
179+ $utf8NoBom = New-Object System.Text.UTF8Encoding $false
180+ [System.IO.File ]::WriteAllText($updaterJsonPath , $newJson , $utf8NoBom )
181+
182+ Write-Host " `n ✓ 已更新 updater_android.json" - ForegroundColor Green
183+ } catch {
184+ Write-Host " `n ✗ 更新 updater_android.json 失败: $_ " - ForegroundColor Red
185+ }
101186}
102187
103188Write-Host " 所有APK已保存到: $OutputDir " - ForegroundColor Green
0 commit comments