Skip to content

Commit 2aac0e5

Browse files
committed
Simple build script
1 parent ed1c4aa commit 2aac0e5

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

build.ps1

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
param(
2+
[Parameter(Mandatory=$true, ValueFromRemainingArguments=$true)]
3+
[ValidateSet("xfce", "lxqt", "gxde")]
4+
[string[]]$DesktopEnvs,
5+
6+
[string]$NameSuffix
7+
)
8+
9+
# 设置路径
10+
$SourceDir = "C:\Users\29513\Downloads"
11+
$AssetsDir = "assets"
12+
13+
# 分割文件函数 - 使用xaa, xab, xac...命名
14+
function Split-File {
15+
param(
16+
[string]$Path,
17+
[long]$PartSizeBytes,
18+
[string]$DestinationPath
19+
)
20+
21+
$stream = [System.IO.File]::OpenRead($Path)
22+
$buffer = New-Object byte[] $PartSizeBytes
23+
$partNumber = 0
24+
25+
try {
26+
while (($bytesRead = $stream.Read($buffer, 0, $buffer.Length)) -gt 0) {
27+
# 生成类似xaa, xab, xac...的文件名
28+
$partName = Get-SplitFileName $partNumber
29+
$partPath = Join-Path $DestinationPath $partName
30+
31+
$partStream = [System.IO.File]::OpenWrite($partPath)
32+
try {
33+
$partStream.Write($buffer, 0, $bytesRead)
34+
} finally {
35+
$partStream.Close()
36+
}
37+
38+
Write-Host "创建分片: $partName"
39+
$partNumber++
40+
}
41+
} finally {
42+
$stream.Close()
43+
}
44+
45+
return $partNumber
46+
}
47+
48+
# 生成split风格的文件名 (xaa, xab, xac, ..., xaz)
49+
function Get-SplitFileName {
50+
param([int]$index)
51+
52+
# 计算字母偏移量(0-25对应a-z)
53+
$charOffset = $index % 26
54+
# 将数字转换为对应的字母字符
55+
$char = [char]([byte][char]'a' + $charOffset)
56+
57+
return "xa$char"
58+
}
59+
60+
# 处理每个桌面环境
61+
foreach ($DesktopEnv in $DesktopEnvs) {
62+
Write-Host "`n开始处理 $DesktopEnv 桌面环境..." -ForegroundColor Green
63+
64+
# 设置文件路径
65+
$TarFile = "debian-$DesktopEnv.tar.xz"
66+
$SourcePath = Join-Path $SourceDir $TarFile
67+
68+
# 检查源文件是否存在
69+
if (-not (Test-Path $SourcePath)) {
70+
Write-Error "错误:找不到文件 $SourcePath"
71+
continue
72+
}
73+
74+
# 删除assets文件夹中已有的xa*文件
75+
if (Test-Path $AssetsDir) {
76+
Write-Host "正在清理assets文件夹中的旧文件..."
77+
Get-ChildItem -Path $AssetsDir -Filter "xa*" | Remove-Item -Force
78+
} else {
79+
Write-Host "创建assets文件夹..."
80+
New-Item -ItemType Directory -Path $AssetsDir | Out-Null
81+
}
82+
83+
# 分割文件 (98MB = 98 * 1024 * 1024 = 102760448 bytes)
84+
Write-Host "正在分割 $TarFile 文件..."
85+
$partCount = Split-File -Path $SourcePath -PartSizeBytes 102760448 -DestinationPath $AssetsDir
86+
87+
Write-Host "文件分割完成,共创建 $partCount 个分片文件"
88+
89+
# 运行Flutter构建
90+
Write-Host "正在运行Flutter构建..."
91+
flutter build apk --target-platform android-arm64 --split-per-abi --obfuscate --split-debug-info=tiny_computer/sdi
92+
93+
if ($LASTEXITCODE -ne 0) {
94+
Write-Error "错误:Flutter构建失败"
95+
continue
96+
}
97+
98+
# 构建APK文件名
99+
$ApkBaseName = "tiny-computer-$DesktopEnv"
100+
if (-not [string]::IsNullOrEmpty($NameSuffix)) {
101+
$ApkBaseName += "-$NameSuffix"
102+
}
103+
104+
# 重命名APK和SHA1文件
105+
$ApkSource = "build\app\outputs\flutter-apk\app-arm64-v8a-release.apk"
106+
$Sha1Source = "build\app\outputs\flutter-apk\app-arm64-v8a-release.apk.sha1"
107+
108+
if (Test-Path $ApkSource) {
109+
Rename-Item -Path $ApkSource -NewName "$ApkBaseName.apk"
110+
Write-Host "已重命名APK文件: $ApkBaseName.apk"
111+
} else {
112+
Write-Error "错误:找不到APK文件 $ApkSource"
113+
continue
114+
}
115+
116+
if (Test-Path $Sha1Source) {
117+
Rename-Item -Path $Sha1Source -NewName "$ApkBaseName.apk.sha1"
118+
Write-Host "已重命名SHA1文件: $ApkBaseName.apk.sha1"
119+
} else {
120+
Write-Warning "警告:找不到SHA1文件 $Sha1Source"
121+
}
122+
123+
Write-Host "$DesktopEnv 处理完成!" -ForegroundColor Green
124+
}
125+
126+
Write-Host "`n所有桌面环境处理完成!" -ForegroundColor Cyan
127+
128+
# 既然是开源,我认为应该把prompt开源出来才算,毕竟这个脚本更像编译后的产物,而不是源代码本身。
129+
130+
# 帮我写一个自动化脚本,做以下几件事:
131+
# 1. 脚本所在目录是项目的根目录,脚本应该运行在windows电脑上,接收一个参数,这个参数的值会是xfce, lxqt或gxde。
132+
# 2. 在C:\Users\29513\Downloads文件夹有debian-xfce.tar.xz,debian-lxqt.tar.xz和debian-gxde.tar.xz,需要根据之前的参数对应选择,然后分成98MB的小份,命名为xa*(就像linux上的split -b 98M debian.tar.xz),放到项目的assets文件夹。注意这个文件夹可能有之前残留的xa*文件,需要先彻底删除这些xa*文件。
133+
# 3. 然后在当前目录运行flutter build apk --target-platform android-arm64 --split-per-abi --obfuscate --split-debug-info=tiny_computer/sdi编译。
134+
# 4. 在build\app\outputs\flutter-apk文件夹会有app-arm64-v8a-release.apk和app-arm64-v8a-release.apk.sha1两个文件,需要重命名为tiny-computer-xfce.apk和tiny-computer-xfce.apk.sha1(以xfce为例,具体名称根据参数来定)
135+
136+
# 直接写成一个ps1脚本行吗
137+
138+
# 请再添加一些功能:首先可以传入多个选项,比如传入xfce lxqt就可以自动进行这两个构建;其实需要一个新参数允许在生成的apk名字加入后缀,比如添加targetSdk35后缀,就会生成tiny-computer-xfce-targetSdk35.apk和tiny-computer-xfce-targetSdk35.apk.sha1
139+
140+
# xa*文件的命名不对。要按照split命令默认的那样,命名为xaa,xab,xac... 另外我确定分割后的文件数量不多,不会超过xaz。
141+
142+
# Cannot convert value "97" to type "System.Char". Error: "Invalid cast from 'Decimal' to 'Char'."
143+
# At C:\Users\29513\FlutterProjects\tiny_computer\build.ps1:52 char:5
144+
# + $firstChar = [char](97 + [math]::Floor($index / 26)) # a-z
145+
# + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
146+
# + CategoryInfo : InvalidArgument: (:) [], RuntimeException
147+
# + FullyQualifiedErrorId : InvalidCastIConvertible

0 commit comments

Comments
 (0)