Skip to content

Commit fc47395

Browse files
authored
Merge pull request #43 from qianmoQ/dev-25.0.3
fix (core): 修复自动更新导致应用无法启动问题
2 parents 7a80072 + a008710 commit fc47395

8 files changed

Lines changed: 467 additions & 13 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "codeforge",
33
"private": true,
4-
"version": "25.0.2",
4+
"version": "25.0.3",
55
"type": "module",
66
"scripts": {
77
"dev": "vite",

src-tauri/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "CodeForge"
3-
version = "25.0.2"
3+
version = "25.0.3"
44
description = "CodeForge 是一款轻量级、高性能的桌面代码执行器,专为开发者、学生和编程爱好者设计。"
55
authors = ["devlive-community"]
66
edition = "2024"

src-tauri/src/update.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ use serde::{Deserialize, Serialize};
33
use std::time::SystemTime;
44
use tauri::{AppHandle, Emitter, command};
55

6+
mod update_linux;
7+
mod update_mac;
8+
mod update_windows;
9+
610
#[derive(Debug, Clone, Serialize, Deserialize)]
711
pub struct UpdateInfo {
812
pub version: String,
@@ -118,14 +122,21 @@ async fn get_latest_release(
118122
// 查找平台对应的文件
119123
fn get_platform_asset(assets: &[GitHubAsset]) -> Option<&GitHubAsset> {
120124
let os = std::env::consts::OS;
121-
info!("检查更新 -> 当前平台: {}", os);
125+
let arch = std::env::consts::ARCH;
126+
info!("检查更新 -> 当前平台: {} {}", os, arch);
122127

123128
for asset in assets {
124129
let name = asset.name.to_lowercase();
125130

126131
let matches = match os {
127-
"windows" => name.contains("windows") || name.contains("win") || name.ends_with(".exe"),
128-
"macos" => name.contains("macos") || name.contains("darwin") || name.ends_with(".dmg"),
132+
"windows" => {
133+
if arch == "x86_64" {
134+
name.contains("x64") && (name.ends_with(".exe") || name.ends_with(".msi"))
135+
} else {
136+
name.contains("windows") || name.ends_with(".exe") || name.ends_with(".msi")
137+
}
138+
}
139+
"macos" => name.contains("universal") && name.ends_with(".dmg"),
129140
"linux" => name.contains("linux") || name.ends_with(".appimage"),
130141
_ => false,
131142
};
@@ -136,7 +147,6 @@ fn get_platform_asset(assets: &[GitHubAsset]) -> Option<&GitHubAsset> {
136147
}
137148
}
138149

139-
// 如果没找到匹配的,返回第一个
140150
assets.first()
141151
}
142152

@@ -179,7 +189,22 @@ async fn do_update(
179189
let temp_dir = std::env::temp_dir().join("codeforge_update");
180190
std::fs::create_dir_all(&temp_dir)?;
181191

182-
let file_name = format!("update_{}.exe", update_info.version);
192+
// 根据平台确定文件扩展名
193+
let os = std::env::consts::OS;
194+
let file_extension = match os {
195+
"windows" => {
196+
if update_info.url.contains(".msi") {
197+
"msi"
198+
} else {
199+
"exe"
200+
}
201+
}
202+
"macos" => "dmg",
203+
"linux" => "appimage",
204+
_ => "bin",
205+
};
206+
207+
let file_name = format!("update_{}.{}", update_info.version, file_extension);
183208
let download_path = temp_dir.join(&file_name);
184209

185210
info!("检查更新 -> 下载更新文件: {}", update_info.url);
@@ -257,12 +282,16 @@ async fn do_update(
257282
Ok(())
258283
}
259284

260-
// 安装更新
285+
// 安装更新函数
261286
async fn do_install(
262287
update_path: &std::path::Path,
263288
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
264-
let current_exe = std::env::current_exe()?;
265-
std::fs::copy(update_path, &current_exe)?;
289+
let os = std::env::consts::OS;
266290

267-
Ok(())
291+
match os {
292+
"windows" => update_windows::install(update_path).await,
293+
"macos" => update_mac::install(update_path).await,
294+
"linux" => update_linux::install(update_path).await,
295+
_ => Err("不支持的操作系统".into()),
296+
}
268297
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use log::info;
2+
3+
pub async fn install(
4+
update_path: &std::path::Path,
5+
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
6+
info!("安装更新 -> Linux 更新安装: {}", update_path.display());
7+
8+
let current_exe = std::env::current_exe()?;
9+
info!("安装更新 -> 当前可执行文件: {}", current_exe.display());
10+
11+
// 设置更新文件权限
12+
set_executable_permission(update_path)?;
13+
14+
// 创建备份
15+
let backup_path = create_backup(&current_exe)?;
16+
17+
// 替换可执行文件
18+
match replace_executable(update_path, &current_exe) {
19+
Ok(_) => {
20+
info!("安装更新 -> 文件替换成功");
21+
// 删除备份
22+
let _ = std::fs::remove_file(backup_path);
23+
}
24+
Err(e) => {
25+
info!("安装更新 -> 文件替换失败,恢复备份");
26+
// 恢复备份
27+
let _ = std::fs::copy(&backup_path, &current_exe);
28+
let _ = std::fs::remove_file(backup_path);
29+
return Err(e);
30+
}
31+
}
32+
33+
info!("安装更新 -> Linux 安装完成");
34+
Ok(())
35+
}
36+
37+
fn set_executable_permission(
38+
update_path: &std::path::Path,
39+
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
40+
info!("安装更新 -> 设置更新文件可执行权限");
41+
42+
let _ = std::process::Command::new("chmod")
43+
.args(["+x"])
44+
.arg(update_path)
45+
.output();
46+
47+
Ok(())
48+
}
49+
50+
fn create_backup(
51+
current_exe: &std::path::Path,
52+
) -> Result<std::path::PathBuf, Box<dyn std::error::Error + Send + Sync>> {
53+
let backup_path = current_exe.with_extension("backup");
54+
info!("安装更新 -> 创建备份: {}", backup_path.display());
55+
56+
std::fs::copy(current_exe, &backup_path)?;
57+
Ok(backup_path)
58+
}
59+
60+
fn replace_executable(
61+
update_path: &std::path::Path,
62+
current_exe: &std::path::Path,
63+
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
64+
info!("安装更新 -> 替换可执行文件");
65+
66+
// 复制新文件
67+
std::fs::copy(update_path, current_exe)?;
68+
69+
// 设置可执行权限
70+
#[cfg(unix)]
71+
{
72+
use std::os::unix::fs::PermissionsExt;
73+
let mut perms = std::fs::metadata(current_exe)?.permissions();
74+
perms.set_mode(0o755);
75+
std::fs::set_permissions(current_exe, perms)?;
76+
}
77+
info!("安装更新 -> 可执行文件权限设置完成");
78+
79+
Ok(())
80+
}

0 commit comments

Comments
 (0)