Skip to content

Commit 507fc2d

Browse files
authored
Merge pull request #64 from qianmoQ/dev-25.0.5
2 parents 2e502b1 + 68b1138 commit 507fc2d

File tree

20 files changed

+1401
-25
lines changed

20 files changed

+1401
-25
lines changed

bin/set_version.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash
2+
3+
# 版本设置脚本
4+
# 用法: ./bin/set_version.sh <version>
5+
# 例如: ./bin/set_version.sh 25.0.5
6+
7+
set -e
8+
9+
if [ -z "$1" ]; then
10+
echo "错误: 请提供版本号"
11+
echo "用法: $0 <version>"
12+
echo "例如: $0 25.0.5"
13+
exit 1
14+
fi
15+
16+
NEW_VERSION=$1
17+
CURRENT_BRANCH=$(git branch --show-current)
18+
19+
echo "=========================================="
20+
echo "设置新版本: $NEW_VERSION"
21+
echo "当前分支: $CURRENT_BRANCH"
22+
echo "=========================================="
23+
24+
# 1. 更新 package.json
25+
echo "更新 package.json..."
26+
if [ -f "package.json" ]; then
27+
sed -i.bak "s/\"version\": \".*\"/\"version\": \"$NEW_VERSION\"/" package.json
28+
rm -f package.json.bak
29+
echo "✓ package.json 已更新"
30+
else
31+
echo "⚠ package.json 不存在"
32+
fi
33+
34+
# 2. 更新 src-tauri/Cargo.toml
35+
echo "更新 src-tauri/Cargo.toml..."
36+
if [ -f "src-tauri/Cargo.toml" ]; then
37+
sed -i.bak "s/^version = \".*\"/version = \"$NEW_VERSION\"/" src-tauri/Cargo.toml
38+
rm -f src-tauri/Cargo.toml.bak
39+
echo "✓ src-tauri/Cargo.toml 已更新"
40+
else
41+
echo "⚠ src-tauri/Cargo.toml 不存在"
42+
fi
43+
44+
# 3. 更新 src-tauri/tauri.conf.json
45+
echo "更新 src-tauri/tauri.conf.json..."
46+
if [ -f "src-tauri/tauri.conf.json" ]; then
47+
sed -i.bak "s/\"version\": \".*\"/\"version\": \"$NEW_VERSION\"/" src-tauri/tauri.conf.json
48+
rm -f src-tauri/tauri.conf.json.bak
49+
echo "✓ src-tauri/tauri.conf.json 已更新"
50+
else
51+
echo "⚠ src-tauri/tauri.conf.json 不存在"
52+
fi
53+
54+
# 4. 创建新的开发分支
55+
NEW_BRANCH="dev-$NEW_VERSION"
56+
echo ""
57+
echo "创建新的开发分支: $NEW_BRANCH"
58+
read -p "是否创建新分支并切换? (y/n) " -n 1 -r
59+
echo
60+
if [[ $REPLY =~ ^[Yy]$ ]]; then
61+
git checkout -b "$NEW_BRANCH"
62+
echo "✓ 已创建并切换到分支: $NEW_BRANCH"
63+
else
64+
echo "跳过创建新分支"
65+
fi
66+
67+
echo ""
68+
echo "=========================================="
69+
echo "版本更新完成!"
70+
echo "新版本: $NEW_VERSION"
71+
if [[ $REPLY =~ ^[Yy]$ ]]; then
72+
echo "新分支: $NEW_BRANCH"
73+
fi
74+
echo "=========================================="
75+
echo ""
76+
echo "接下来的步骤:"
77+
echo "1. 检查更改: git diff"
78+
echo "2. 提交更改: git add . && git commit -m 'chore: bump version to $NEW_VERSION'"
79+
echo "3. 推送到远程: git push -u origin $NEW_BRANCH"

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.4",
4+
"version": "25.0.5",
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.4"
3+
version = "25.0.5"
44
description = "CodeForge 是一款轻量级、高性能的桌面代码执行器,专为开发者、学生和编程爱好者设计。"
55
authors = ["devlive-community"]
66
edition = "2024"

src-tauri/src/config.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ pub struct EditorConfig {
2222
pub space_dot_omission: Option<bool>, // 是否显示空格省略
2323
}
2424

25+
#[derive(Debug, Clone, Serialize, Deserialize)]
26+
pub struct EnvironmentMirrorConfig {
27+
pub enabled: Option<bool>,
28+
pub base_url: Option<String>,
29+
pub fallback_enabled: Option<bool>,
30+
}
31+
2532
#[derive(Debug, Clone, Serialize, Deserialize)]
2633
pub struct AppConfig {
2734
pub log_directory: Option<String>,
@@ -30,6 +37,7 @@ pub struct AppConfig {
3037
pub theme: Option<String>,
3138
pub plugins: Option<Vec<PluginConfig>>,
3239
pub editor: Option<EditorConfig>,
40+
pub environment_mirror: Option<EnvironmentMirrorConfig>,
3341
}
3442

3543
impl Default for AppConfig {
@@ -50,6 +58,11 @@ impl Default for AppConfig {
5058
show_function_help: Some(false),
5159
space_dot_omission: Some(false),
5260
}),
61+
environment_mirror: Some(EnvironmentMirrorConfig {
62+
enabled: Some(true),
63+
base_url: Some("http://cdn.global.devlive.top".to_string()),
64+
fallback_enabled: Some(false),
65+
}),
5366
}
5467
}
5568
}
@@ -113,6 +126,16 @@ impl ConfigManager {
113126
println!("读取配置 -> 添加默认 editor 配置");
114127
}
115128

129+
// 检查并设置 environment_mirror 默认配置
130+
if config.environment_mirror.is_none() {
131+
config.environment_mirror = Some(EnvironmentMirrorConfig {
132+
enabled: Some(true),
133+
base_url: Some("http://cdn.global.devlive.top".to_string()),
134+
fallback_enabled: Some(false),
135+
});
136+
println!("读取配置 -> 添加默认 environment_mirror 配置");
137+
}
138+
116139
Ok(config)
117140
}
118141
Err(e) => {
@@ -218,6 +241,11 @@ impl ConfigManager {
218241
show_function_help: Some(false),
219242
space_dot_omission: Some(false),
220243
}),
244+
environment_mirror: Some(EnvironmentMirrorConfig {
245+
enabled: Some(true),
246+
base_url: Some("http://cdn.global.devlive.top".to_string()),
247+
fallback_enabled: Some(false),
248+
}),
221249
}
222250
}
223251

src-tauri/src/env_commands.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::env_manager::{EnvironmentInfo, EnvironmentManager};
22
use log::info;
3-
use tauri::{AppHandle, State};
3+
use tauri::{AppHandle, Emitter, State};
44
use tokio::sync::Mutex;
55

66
pub type EnvironmentManagerState = Mutex<EnvironmentManager>;
@@ -24,20 +24,37 @@ pub async fn download_and_install_version(
2424
) -> Result<String, String> {
2525
info!("下载并安装 {} 版本 {}", language, version);
2626
let manager = env_manager.lock().await;
27-
manager
28-
.download_and_install_version(&language, &version, app_handle)
29-
.await
27+
let result = manager
28+
.download_and_install_version(&language, &version, app_handle.clone())
29+
.await;
30+
31+
if result.is_ok() {
32+
// 发送配置更新事件通知前端刷新配置
33+
app_handle.emit("config-updated", ()).ok();
34+
info!("已发送配置更新事件");
35+
}
36+
37+
result
3038
}
3139

3240
#[tauri::command]
3341
pub async fn switch_environment_version(
3442
language: String,
3543
version: String,
44+
app_handle: AppHandle,
3645
env_manager: State<'_, EnvironmentManagerState>,
3746
) -> Result<(), String> {
3847
info!("切换 {} 到版本 {}", language, version);
3948
let manager = env_manager.lock().await;
40-
manager.switch_version(&language, &version).await
49+
let result = manager.switch_version(&language, &version).await;
50+
51+
if result.is_ok() {
52+
// 发送配置更新事件通知前端刷新配置
53+
app_handle.emit("config-updated", ()).ok();
54+
info!("已发送配置更新事件");
55+
}
56+
57+
result
4158
}
4259

4360
#[tauri::command]

src-tauri/src/env_manager.rs

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use tauri::{AppHandle, Emitter};
1010
pub struct EnvironmentVersion {
1111
pub version: String,
1212
pub download_url: String,
13+
pub fallback_url: Option<String>, // 备用下载地址(如 GitHub URL)
1314
pub install_path: Option<String>,
1415
pub is_installed: bool,
1516
pub size: Option<u64>,
@@ -98,7 +99,7 @@ impl EnvironmentManager {
9899
let provider = self
99100
.providers
100101
.get(language)
101-
.ok_or_else(|| format!("暂未支持 {} 语言,请前往 github 提供 issus", language))?;
102+
.ok_or_else(|| format!("暂未支持 {} 语言,请前往 github 提供 issues", language))?;
102103

103104
info!("获取 {} 环境信息", language);
104105

@@ -126,7 +127,7 @@ impl EnvironmentManager {
126127
let provider = self
127128
.providers
128129
.get(language)
129-
.ok_or_else(|| format!("暂未支持 {} 语言,请前往 github 提供 issus", language))?;
130+
.ok_or_else(|| format!("暂未支持 {} 语言,请前往 github 提供 issues", language))?;
130131

131132
info!("开始下载并安装 {} 版本 {}", language, version);
132133
provider.download_and_install(version, app_handle).await
@@ -136,7 +137,7 @@ impl EnvironmentManager {
136137
let provider = self
137138
.providers
138139
.get(language)
139-
.ok_or_else(|| format!("暂未支持 {} 语言,请前往 github 提供 issus", language))?;
140+
.ok_or_else(|| format!("暂未支持 {} 语言,请前往 github 提供 issues", language))?;
140141

141142
info!("切换 {} 到版本 {}", language, version);
142143
provider.switch_version(version).await
@@ -175,3 +176,102 @@ pub fn emit_download_progress(
175176
error!("发送下载进度事件失败: {}", e);
176177
}
177178
}
179+
180+
// 将 GitHub 下载 URL 转换为 CDN 镜像 URL
181+
pub fn convert_to_cdn_url(
182+
original_url: &str,
183+
language: &str,
184+
version: &str,
185+
) -> Result<String, String> {
186+
use crate::config::get_app_config_internal;
187+
188+
let config = get_app_config_internal().map_err(|e| format!("获取配置失败: {}", e))?;
189+
190+
let mirror_config = config.environment_mirror.as_ref();
191+
let enabled = mirror_config.and_then(|m| m.enabled).unwrap_or(false);
192+
193+
if !enabled {
194+
return Ok(original_url.to_string());
195+
}
196+
197+
let base_url = mirror_config
198+
.and_then(|m| m.base_url.as_ref())
199+
.ok_or_else(|| "CDN 基础 URL 未配置".to_string())?;
200+
201+
// 从原始 URL 中提取文件名
202+
let file_name = original_url
203+
.split('/')
204+
.last()
205+
.ok_or_else(|| "无效的下载 URL".to_string())?;
206+
207+
// 构建 CDN URL: {base_url}/{language}/{version}/{filename}
208+
let cdn_url = format!(
209+
"{}/{}/{}/{}",
210+
base_url.trim_end_matches('/'),
211+
language,
212+
version,
213+
file_name
214+
);
215+
216+
info!("转换下载 URL: {} -> {}", original_url, cdn_url);
217+
Ok(cdn_url)
218+
}
219+
220+
// 尝试从 CDN 下载,失败则回退到原始 URL
221+
pub async fn download_with_fallback(
222+
client: &reqwest::Client,
223+
original_url: &str,
224+
language: &str,
225+
version: &str,
226+
) -> Result<reqwest::Response, String> {
227+
use crate::config::get_app_config_internal;
228+
229+
// 检查是否启用自动回退
230+
let fallback_enabled = get_app_config_internal()
231+
.ok()
232+
.and_then(|config| config.environment_mirror)
233+
.and_then(|mirror| mirror.fallback_enabled)
234+
.unwrap_or(false);
235+
236+
// 首先尝试从 CDN 下载
237+
match convert_to_cdn_url(original_url, language, version) {
238+
Ok(cdn_url) if cdn_url != original_url => {
239+
info!("尝试从 CDN 下载: {}", cdn_url);
240+
match client.get(&cdn_url).send().await {
241+
Ok(response) if response.status().is_success() => {
242+
info!("CDN 下载成功");
243+
return Ok(response);
244+
}
245+
Ok(response) => {
246+
let status = response.status();
247+
if fallback_enabled {
248+
info!("CDN 下载失败 (HTTP {}), 回退到原始 URL", status);
249+
} else {
250+
return Err(format!("CDN 下载失败 (HTTP {}), 未启用自动回退", status));
251+
}
252+
}
253+
Err(e) => {
254+
if fallback_enabled {
255+
info!("CDN 下载失败 ({}), 回退到原始 URL", e);
256+
} else {
257+
return Err(format!("CDN 下载失败 ({}), 未启用自动回退", e));
258+
}
259+
}
260+
}
261+
}
262+
Ok(_) => {
263+
info!("CDN 镜像未启用,使用原始 URL");
264+
}
265+
Err(e) => {
266+
info!("CDN URL 转换失败 ({}), 使用原始 URL", e);
267+
}
268+
}
269+
270+
// 回退到原始 URL
271+
info!("从原始 URL 下载: {}", original_url);
272+
client
273+
.get(original_url)
274+
.send()
275+
.await
276+
.map_err(|e| format!("下载失败: {}", e))
277+
}

0 commit comments

Comments
 (0)