Skip to content

Commit b0bb796

Browse files
authored
refactor: rename command mod to process, move restart_application (#1667)
* refactor: rename `command` mod to `process`, move restart_application * refactor(api): move `exit`, `relaunch` APIs to `process` module
1 parent 5c1fe52 commit b0bb796

File tree

16 files changed

+144
-112
lines changed

16 files changed

+144
-112
lines changed

.changes/command-api-module.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Renamed the `command` API module to `process`.

.changes/process-api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": patch
3+
---
4+
5+
Move `exit` and `relaunch` APIs from `app` to `process` module.

.changes/restart-application.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Move `restart_application` API from `app` module to `process` module.

core/tauri/scripts/bundle.js

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

core/tauri/src/api/app.rs

Lines changed: 0 additions & 47 deletions
This file was deleted.

core/tauri/src/api/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66
#![warn(missing_docs)]
77
// #![feature(const_int_pow)]
88

9-
/// The App API module allows you to manage application processes.
10-
pub mod app;
11-
/// The Command API module allows you to manage child processes.
12-
pub mod command;
13-
/// The Dialog API module allows you to show messages and prompt for file paths.
9+
/// A module for working with processes.
1410
pub mod dialog;
1511
/// The Dir module is a helper for file system directory management.
1612
pub mod dir;
@@ -20,6 +16,8 @@ pub mod file;
2016
pub mod http;
2117
/// The file system path operations API.
2218
pub mod path;
19+
/// The Command API module allows you to manage child processes.
20+
pub mod process;
2321
/// The RPC module includes utilities to send messages to the JS layer of the webview.
2422
pub mod rpc;
2523
/// The shell api.
Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use std::{
66
collections::HashMap,
7+
env,
78
io::{BufRead, BufReader, Write},
89
path::PathBuf,
9-
process::{Command as StdCommand, Stdio},
10+
process::{exit, Command as StdCommand, Stdio},
1011
sync::Arc,
1112
};
1213

@@ -67,6 +68,38 @@ macro_rules! get_std_command {
6768
}};
6869
}
6970

71+
/// Get the current binary
72+
pub fn current_binary() -> Option<PathBuf> {
73+
let mut current_binary = None;
74+
75+
// if we are running with an APP Image, we should return the app image path
76+
#[cfg(target_os = "linux")]
77+
if let Some(app_image_path) = env::var_os("APPIMAGE") {
78+
current_binary = Some(PathBuf::from(app_image_path));
79+
}
80+
81+
// if we didn't extracted binary in previous step,
82+
// let use the current_exe from current environment
83+
if current_binary.is_none() {
84+
if let Ok(current_process) = env::current_exe() {
85+
current_binary = Some(current_process);
86+
}
87+
}
88+
89+
current_binary
90+
}
91+
92+
/// Restart the process.
93+
pub fn restart() {
94+
if let Some(path) = current_binary() {
95+
StdCommand::new(path)
96+
.spawn()
97+
.expect("application failed to start");
98+
}
99+
100+
exit(0);
101+
}
102+
70103
/// API to spawn commands.
71104
pub struct Command {
72105
program: String,

core/tauri/src/endpoints.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod global_shortcut;
1919
mod http;
2020
mod internal;
2121
mod notification;
22+
mod process;
2223
mod shell;
2324
mod window;
2425

@@ -39,6 +40,7 @@ impl<T: Serialize> From<T> for InvokeResponse {
3940
#[serde(tag = "module", content = "message")]
4041
enum Module {
4142
App(app::Cmd),
43+
Process(process::Cmd),
4244
Fs(file_system::Cmd),
4345
Window(Box<window::Cmd>),
4446
Shell(shell::Cmd),
@@ -61,6 +63,8 @@ impl Module {
6163
.and_then(|r| r.json)
6264
.map_err(|e| e.to_string())
6365
}),
66+
Self::Process(cmd) => message
67+
.respond_async(async move { cmd.run().and_then(|r| r.json).map_err(|e| e.to_string()) }),
6468
Self::Fs(cmd) => message
6569
.respond_async(async move { cmd.run().and_then(|r| r.json).map_err(|e| e.to_string()) }),
6670
Self::Window(cmd) => message.respond_async(async move {

core/tauri/src/endpoints/app.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use std::process::exit;
6-
75
use super::InvokeResponse;
8-
use crate::api::{app::restart_application, PackageInfo};
6+
use crate::api::PackageInfo;
97
use serde::Deserialize;
108

119
/// The API descriptor.
@@ -18,11 +16,6 @@ pub enum Cmd {
1816
GetAppName,
1917
/// Get Tauri Version
2018
GetTauriVersion,
21-
/// Relaunch application
22-
Relaunch,
23-
/// Close application with provided exit_code
24-
#[serde(rename_all = "camelCase")]
25-
Exit { exit_code: i32 },
2619
}
2720

2821
impl Cmd {
@@ -31,16 +24,6 @@ impl Cmd {
3124
Self::GetAppVersion => Ok(package_info.version.into()),
3225
Self::GetAppName => Ok(package_info.name.into()),
3326
Self::GetTauriVersion => Ok(env!("CARGO_PKG_VERSION").into()),
34-
Self::Relaunch => Ok({
35-
restart_application(None);
36-
().into()
37-
}),
38-
Self::Exit { exit_code } => {
39-
// would be great if we can have a handler inside tauri
40-
// who close all window and emit an event that user can catch
41-
// if they want to process something before closing the app
42-
exit(exit_code);
43-
}
4427
}
4528
}
4629
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
use std::process::exit;
6+
7+
use super::InvokeResponse;
8+
use crate::api::process::restart;
9+
use serde::Deserialize;
10+
11+
/// The API descriptor.
12+
#[derive(Deserialize)]
13+
#[serde(tag = "cmd", rename_all = "camelCase")]
14+
pub enum Cmd {
15+
/// Relaunch application
16+
Relaunch,
17+
/// Close application with provided exit_code
18+
#[serde(rename_all = "camelCase")]
19+
Exit { exit_code: i32 },
20+
}
21+
22+
impl Cmd {
23+
pub fn run(self) -> crate::Result<InvokeResponse> {
24+
match self {
25+
Self::Relaunch => Ok({
26+
restart();
27+
().into()
28+
}),
29+
Self::Exit { exit_code } => {
30+
// would be great if we can have a handler inside tauri
31+
// who close all window and emit an event that user can catch
32+
// if they want to process something before closing the app
33+
exit(exit_code);
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)