-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathbuild_loongarch64.ps1
268 lines (235 loc) · 8.99 KB
/
build_loongarch64.ps1
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# ---------------------
# 配置参数
# ---------------------
param (
[string]$SolutionPath = "./src/BilibiliLiveTools.sln", # .sln 文件的相对路径,请根据需要修改
[string]$RemoteUser = "loong",
[string]$RemoteHost = "192.168.188.25",
[int]$RemotePort = 22,
[string]$RemoteProjectPath = "/home/loong/Projects/CSharpProject/src",
[switch]$UsePassword, # 如果不使用密码认证,可以去掉这个开关
[string]$ZipFileName = "CSharpProject.zip",
[string]$BuildConfiguration = "Release"
)
# ---------------------
# 导入必要的模块
# ---------------------
# 导入 Posh-SSH 模块,如果尚未安装则安装它
if (!(Get-Module -ListAvailable -Name Posh-SSH)) {
Write-Output "安装 Posh-SSH 模块..."
Install-Module -Name Posh-SSH -Force -Scope CurrentUser
}
Import-Module Posh-SSH
# 获取脚本所在目录作为项目根目录
$scriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition
Set-Location $scriptDirectory
# 验证 .sln 文件是否存在
if (-Not (Test-Path $SolutionPath)) {
Write-Error "无法找到指定的解决方案文件:$SolutionPath"
exit 1
}
# 获取项目目录路径(假设 .sln 文件位于项目根目录)
$localProjectPath = $scriptDirectory
# 认证方式
if ($UsePassword) {
# 提示输入密码(安全处理)
$password = Read-Host -Prompt "请输入远程主机的密码" -AsSecureString
$credential = New-Object System.Management.Automation.PSCredential ($RemoteUser, $password)
} else {
# 使用密钥认证
# 请确保本地 SSH 密钥已配置,并且远程主机接受密钥认证
$credential = Get-Credential -UserName $RemoteUser -Message "使用密钥认证,请确保配置了 SSH 密钥对。"
}
# 日志文件路径
$logFilePath = Join-Path -Path $scriptDirectory -ChildPath "build_log.txt"
# 清空或创建日志文件
New-Item -Path $logFilePath -ItemType File -Force | Out-Null
function Write-Log {
param (
[string]$Message,
[string]$Level = "INFO",
[switch]$NoConsole
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "$timestamp [$Level] : $Message"
if (-not $NoConsole) {
Write-Output $logMessage
}
Add-Content -Path $logFilePath -Value $logMessage
}
# ---------------------
# 处理 tmp 文件夹
# ---------------------
$tmpDirectory = Join-Path -Path $scriptDirectory -ChildPath "tmp"
if (Test-Path $tmpDirectory) {
Write-Log "检测到已存在的 tmp 文件夹,将进行删除..." "INFO" -NoConsole
try {
Remove-Item -Path $tmpDirectory -Recurse -Force
Write-Log "已删除现有的 tmp 文件夹:$tmpDirectory" "INFO" -NoConsole
} catch {
Write-Log "无法删除 tmp 文件夹:$_" "ERROR"
exit 1
}
}
# 创建 tmp 文件夹
try {
New-Item -Path $tmpDirectory -ItemType Directory -Force | Out-Null
Write-Log "已创建 tmp 文件夹:$tmpDirectory" "INFO" -NoConsole
} catch {
Write-Log "无法创建 tmp 文件夹:$_" "ERROR"
exit 1
}
# 压缩包路径调整为 tmp 文件夹内
$zipFilePath = Join-Path -Path $tmpDirectory -ChildPath $ZipFileName
# ---------------------
# 压缩本地项目
# ---------------------
Write-Log "压缩本地项目..." -NoConsole
try {
if (Test-Path $zipFilePath) {
Remove-Item $zipFilePath -Force
Write-Log "已删除旧的压缩包:$zipFilePath"
}
Compress-Archive -Path "$localProjectPath\src\*" -DestinationPath $zipFilePath -Force
Write-Log "项目已成功压缩到 $zipFilePath" -NoConsole
} catch {
Write-Log "压缩项目失败:$_" "ERROR"
exit 1
}
# ---------------------
# 建立 SSH 会话
# ---------------------
Write-Log "尝试建立到 $RemoteHost 的 SSH 连接..." -NoConsole
try {
$session = New-SSHSession -ComputerName $RemoteHost -Port $RemotePort -Credential $credential -AcceptKey -ErrorAction Stop
Write-Log "SSH 会话已成功建立。" -NoConsole
} catch {
Write-Log "无法建立 SSH 会话:$_" "ERROR"
exit 1
}
# ---------------------
# 检查远程环境
# ---------------------
Write-Log "检查远程主机的必要工具..." -NoConsole
$requiredCommands = @("unzip", "dotnet")
foreach ($cmd in $requiredCommands) {
$checkCmd = "which $cmd"
$result = Invoke-SSHCommand -SessionId $session.SessionId -Command $checkCmd
if ($result.Output.Trim() -eq "") {
Write-Log "远程主机缺少必要工具:$cmd" "ERROR"
Remove-SSHSession -SessionId $session.SessionId
exit 1
} else {
Write-Log "远程主机已安装 $cmd" -NoConsole
}
}
# ---------------------
# 创建远程目录
# ---------------------
Write-Log "在远程主机上创建项目目录:$RemoteProjectPath" -NoConsole
$mkdirCommand = "mkdir -p $RemoteProjectPath"
$mkdirResult = Invoke-SSHCommand -SessionId $session.SessionId -Command $mkdirCommand
if ($mkdirResult.ExitStatus -ne 0) {
Write-Log "创建远程目录失败:$($mkdirResult.Error)" "ERROR"
Remove-SSHSession -SessionId $session.SessionId
exit 1
} else {
Write-Log "远程目录已创建或已存在。" -NoConsole
}
# 清理远程目录中的旧文件
Write-Log "清理远程项目目录中的旧文件..." -NoConsole
$cleanupCommand = "rm -rf $RemoteProjectPath/*"
$cleanupResult = Invoke-SSHCommand -SessionId $session.SessionId -Command $cleanupCommand
if ($cleanupResult.ExitStatus -ne 0) {
Write-Log "清理远程目录失败:$($cleanupResult.Error)" "ERROR"
Remove-SSHSession -SessionId $session.SessionId
exit 1
} else {
Write-Log "远程目录已清理。" -NoConsole
}
# ---------------------
# 上传压缩包
# ---------------------
Write-Log "上传项目到远程主机..."
try {
Set-SCPItem -ComputerName $RemoteHost -Port $RemotePort -Credential $credential -AcceptKey -Path $zipFilePath -Destination $RemoteProjectPath
Write-Log "压缩包已上传。"
} catch {
Write-Log "上传压缩包失败:$_" "ERROR"
Remove-SSHSession -SessionId $session.SessionId
exit 1
}
# ---------------------
# 解压缩远程文件
# ---------------------
Write-Log "在远程主机上解压缩项目文件..." -NoConsole
$unzipCommand = "cd $RemoteProjectPath && unzip -o $ZipFileName && rm $ZipFileName"
$unzipResult = Invoke-SSHCommand -SessionId $session.SessionId -Command $unzipCommand
if ($unzipResult.ExitStatus -ne 0) {
Write-Log "解压缩失败:$($unzipResult.Error)" "ERROR"
Remove-SSHSession -SessionId $session.SessionId
exit 1
} else {
Write-Log "项目文件已成功解压缩。" -NoConsole
}
# ---------------------
# 执行远程编译
# ---------------------
Write-Log "开始编译项目..."
$buildCommand = "cd $RemoteProjectPath/../ && dotnet build $SolutionPath --configuration $BuildConfiguration"
$buildResult = Invoke-SSHCommand -SessionId $session.SessionId -Command $buildCommand
Write-Log "编译输出:"
Write-Log $buildResult.Output
# 检查编译是否成功
if ($buildResult.ExitStatus -eq 0) {
Write-Log "编译成功。"
} else {
Write-Log "编译失败。错误信息:$($buildResult.Error)" "ERROR"
Remove-SSHSession -SessionId $session.SessionId
exit 1
}
# 可选:执行单元测试或部署
# $runTests = Read-Host -Prompt "是否在远程主机上运行单元测试?(y/N)"
# if ($runTests -eq 'y' -or $runTests -eq 'Y') {
# Write-Log "在远程主机上运行单元测试..."
# $testCommand = "cd $RemoteProjectPath && dotnet test $SolutionPath --configuration $BuildConfiguration"
# $testResult = Invoke-SSHCommand -SessionId $session.SessionId -Command $testCommand
# Write-Log "测试输出:"
# Write-Log $testResult.Output
# if ($testResult.ExitStatus -eq 0) {
# Write-Log "所有单元测试通过。"
# } else {
# Write-Log "部分单元测试失败。错误信息:$($testResult.Error)" "ERROR"
# Remove-SSHSession -SessionId $session.SessionId
# exit 1
# }
# }
# ---------------------
# 可选:部署或运行应用程序
# ---------------------
# $deploy = Read-Host -Prompt "是否在远程主机上部署或运行应用程序?(y/N)"
# if ($deploy -eq 'y' -or $deploy -eq 'Y') {
# Write-Log "在远程主机上运行应用程序..."
# # 根据需要自定义运行命令,例如:
# $runCommand = "cd $RemoteProjectPath && dotnet run --configuration $BuildConfiguration"
# $runResult = Invoke-SSHCommand -SessionId $session.SessionId -Command $runCommand
# Write-Log "应用程序输出:"
# Write-Log $runResult.Output
# }
# ---------------------
# 关闭 SSH 会话
# ---------------------
Remove-SSHSession -SessionId $session.SessionId
Write-Log "会话已关闭。"
# ---------------------
# 清理本地压缩包
# ---------------------
try {
Remove-Item $zipFilePath -Force
Write-Log "本地压缩包已删除。" -NoConsole
Remove-Item -Path $tmpDirectory -Recurse -Force
Write-Log "已删除现有的 tmp 文件夹:$tmpDirectory" "INFO" -NoConsole
} catch {
Write-Log "无法删除本地压缩包:$_" "WARN"
}
Write-Log "日志请查看:$logFilePath"