Skip to content

Commit c120c4a

Browse files
committed
refactor: enhance error handling in invoke and menu modules, optimize CI workflow with a dedicated CLI build job, and add a window show delay constant.
1 parent f796830 commit c120c4a

File tree

4 files changed

+240
-219
lines changed

4 files changed

+240
-219
lines changed

.github/workflows/single-app.yaml

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,31 @@ on:
5858
required: false
5959

6060
jobs:
61+
build-cli:
62+
name: Build CLI
63+
runs-on: ubuntu-latest
64+
steps:
65+
- name: Checkout repository
66+
uses: actions/checkout@v4
67+
68+
- name: Setup Node.js Environment
69+
uses: ./.github/actions/setup-env
70+
with:
71+
mode: 'node'
72+
73+
- name: Build CLI
74+
run: pnpm run cli:build
75+
76+
- name: Upload CLI Artifact
77+
uses: actions/upload-artifact@v4
78+
with:
79+
name: pake-cli-dist
80+
path: dist/
81+
retention-days: 1
82+
6183
build:
6284
name: ${{ inputs.title }} (${{ matrix.build }})
85+
needs: build-cli
6386
runs-on: ${{ matrix.os }}
6487
strategy:
6588
fail-fast: false
@@ -89,6 +112,12 @@ jobs:
89112
with:
90113
mode: ${{ (matrix.build == 'linux' || matrix.build == 'macos') && 'build' || 'node' }}
91114

115+
- name: Download CLI Artifact
116+
uses: actions/download-artifact@v4
117+
with:
118+
name: pake-cli-dist
119+
path: dist
120+
92121
- name: Rust cache restore
93122
uses: actions/cache/[email protected]
94123
id: cache_store
@@ -109,9 +138,6 @@ jobs:
109138
restore-keys: |
110139
${{ runner.os }}-pnpm-
111140
112-
- name: Build CLI
113-
run: pnpm run cli:build
114-
115141
- name: Build for Linux
116142
if: matrix.os == 'ubuntu-latest'
117143
timeout-minutes: 20

src-tauri/src/app/invoke.rs

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,48 @@ pub struct NotificationParams {
3232

3333
#[command]
3434
pub async fn download_file(app: AppHandle, params: DownloadFileParams) -> Result<(), String> {
35-
let window: WebviewWindow = app.get_webview_window("pake").unwrap();
35+
let window: WebviewWindow = app.get_webview_window("pake").ok_or("Window not found")?;
36+
3637
show_toast(
3738
&window,
3839
&get_download_message_with_lang(MessageType::Start, params.language.clone()),
3940
);
4041

41-
let output_path = app.path().download_dir().unwrap().join(params.filename);
42-
let file_path = check_file_or_append(output_path.to_str().unwrap());
43-
let client = ClientBuilder::new().build().unwrap();
42+
let download_dir = app
43+
.path()
44+
.download_dir()
45+
.map_err(|e| format!("Failed to get download dir: {}", e))?;
46+
47+
let output_path = download_dir.join(&params.filename);
48+
49+
let path_str = output_path.to_str().ok_or("Invalid output path")?;
50+
51+
let file_path = check_file_or_append(path_str);
4452

45-
let response = client
46-
.execute(Request::new(
47-
Method::GET,
48-
Url::from_str(&params.url).unwrap(),
49-
))
50-
.await;
53+
let client = ClientBuilder::new()
54+
.build()
55+
.map_err(|e| format!("Failed to build client: {}", e))?;
56+
57+
let url = Url::from_str(&params.url).map_err(|e| format!("Invalid URL: {}", e))?;
58+
59+
let request = Request::new(Method::GET, url);
60+
61+
let response = client.execute(request).await;
5162

5263
match response {
5364
Ok(mut res) => {
54-
let mut file = File::create(file_path).unwrap();
55-
while let Some(chunk) = res.chunk().await.unwrap() {
56-
file.write_all(&chunk).unwrap();
65+
let mut file =
66+
File::create(file_path).map_err(|e| format!("Failed to create file: {}", e))?;
67+
68+
while let Some(chunk) = res
69+
.chunk()
70+
.await
71+
.map_err(|e| format!("Failed to get chunk: {}", e))?
72+
{
73+
file.write_all(&chunk)
74+
.map_err(|e| format!("Failed to write chunk: {}", e))?;
5775
}
76+
5877
show_toast(
5978
&window,
6079
&get_download_message_with_lang(MessageType::Success, params.language.clone()),
@@ -76,15 +95,25 @@ pub async fn download_file_by_binary(
7695
app: AppHandle,
7796
params: BinaryDownloadParams,
7897
) -> Result<(), String> {
79-
let window: WebviewWindow = app.get_webview_window("pake").unwrap();
98+
let window: WebviewWindow = app.get_webview_window("pake").ok_or("Window not found")?;
99+
80100
show_toast(
81101
&window,
82102
&get_download_message_with_lang(MessageType::Start, params.language.clone()),
83103
);
84-
let output_path = app.path().download_dir().unwrap().join(params.filename);
85-
let file_path = check_file_or_append(output_path.to_str().unwrap());
86-
let download_file_result = fs::write(file_path, &params.binary);
87-
match download_file_result {
104+
105+
let download_dir = app
106+
.path()
107+
.download_dir()
108+
.map_err(|e| format!("Failed to get download dir: {}", e))?;
109+
110+
let output_path = download_dir.join(&params.filename);
111+
112+
let path_str = output_path.to_str().ok_or("Invalid output path")?;
113+
114+
let file_path = check_file_or_append(path_str);
115+
116+
match fs::write(file_path, &params.binary) {
88117
Ok(_) => {
89118
show_toast(
90119
&window,
@@ -111,21 +140,22 @@ pub fn send_notification(app: AppHandle, params: NotificationParams) -> Result<(
111140
.body(&params.body)
112141
.icon(&params.icon)
113142
.show()
114-
.unwrap();
143+
.map_err(|e| format!("Failed to show notification: {}", e))?;
115144
Ok(())
116145
}
117146

118147
#[command]
119148
pub async fn update_theme_mode(app: AppHandle, mode: String) {
120149
#[cfg(target_os = "macos")]
121150
{
122-
let window = app.get_webview_window("pake").unwrap();
123-
let theme = if mode == "dark" {
124-
Theme::Dark
125-
} else {
126-
Theme::Light
127-
};
128-
let _ = window.set_theme(Some(theme));
151+
if let Some(window) = app.get_webview_window("pake") {
152+
let theme = if mode == "dark" {
153+
Theme::Dark
154+
} else {
155+
Theme::Light
156+
};
157+
let _ = window.set_theme(Some(theme));
158+
}
129159
}
130160
#[cfg(not(target_os = "macos"))]
131161
{

0 commit comments

Comments
 (0)