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

+5
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

+5
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

+5
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

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/api/app.rs

-47
This file was deleted.

core/tauri/src/api/mod.rs

+3-5
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.

core/tauri/src/api/command.rs renamed to core/tauri/src/api/process.rs

+34-1
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

+4
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

+1-18
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
}

core/tauri/src/endpoints/process.rs

+37
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+
}

core/tauri/src/endpoints/shell.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::{collections::HashMap, path::PathBuf};
1111

1212
type ChildId = u32;
1313
#[cfg(shell_execute)]
14-
type ChildStore = Arc<Mutex<HashMap<ChildId, crate::api::command::CommandChild>>>;
14+
type ChildStore = Arc<Mutex<HashMap<ChildId, crate::api::process::CommandChild>>>;
1515

1616
#[cfg(shell_execute)]
1717
fn command_childs() -> &'static ChildStore {
@@ -83,9 +83,9 @@ impl Cmd {
8383
#[cfg(shell_execute)]
8484
{
8585
let mut command = if options.sidecar {
86-
crate::api::command::Command::new_sidecar(program)?
86+
crate::api::process::Command::new_sidecar(program)?
8787
} else {
88-
crate::api::command::Command::new(program)
88+
crate::api::process::Command::new(program)
8989
};
9090
command = command.args(args);
9191
if let Some(cwd) = options.cwd {
@@ -103,7 +103,7 @@ impl Cmd {
103103

104104
crate::async_runtime::spawn(async move {
105105
while let Some(event) = rx.recv().await {
106-
if matches!(event, crate::api::command::CommandEvent::Terminated(_)) {
106+
if matches!(event, crate::api::process::CommandEvent::Terminated(_)) {
107107
command_childs().lock().unwrap().remove(&pid);
108108
}
109109
let js = crate::api::rpc::format_callback(on_event_fn.clone(), &event)

core/tauri/src/updater/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,9 @@ pub use self::error::Error;
342342
use crate::runtime::manager::tauri_event;
343343
use crate::{
344344
api::{
345-
app::restart_application,
346345
config::UpdaterConfig,
347346
dialog::{ask, AskResponse},
347+
process::restart,
348348
},
349349
Params, Window,
350350
};
@@ -573,7 +573,7 @@ Release Notes:
573573
);
574574
match should_exit {
575575
AskResponse::Yes => {
576-
restart_application(None);
576+
restart();
577577
// safely exit even if the process
578578
// should be killed
579579
return Ok(());

tooling/api/rollup.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export default [
2727
window: './src/window.ts',
2828
cli: './src/cli.ts',
2929
notification: './src/notification.ts',
30-
globalShortcut: './src/globalShortcut.ts'
30+
globalShortcut: './src/globalShortcut.ts',
31+
process: './src/process.ts'
3132
},
3233
treeshake: true,
3334
perf: true,

tooling/api/src/app.ts

+1-33
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,4 @@ async function getTauriVersion(): Promise<string> {
4949
})
5050
}
5151

52-
/**
53-
* Exits immediately with the given `exitCode`.
54-
*
55-
* @param exitCode The exit code to use
56-
* @returns
57-
*/
58-
async function exit(exitCode: number = 0): Promise<void> {
59-
return invokeTauriCommand({
60-
__tauriModule: 'App',
61-
mainThread: true,
62-
message: {
63-
cmd: 'exit',
64-
exitCode
65-
}
66-
})
67-
}
68-
69-
/**
70-
* Exits the current instance of the app then relaunches it.
71-
*
72-
* @returns
73-
*/
74-
async function relaunch(): Promise<void> {
75-
return invokeTauriCommand({
76-
__tauriModule: 'App',
77-
mainThread: true,
78-
message: {
79-
cmd: 'relaunch'
80-
}
81-
})
82-
}
83-
84-
export { getName, getVersion, getTauriVersion, relaunch, exit }
52+
export { getName, getVersion, getTauriVersion }

tooling/api/src/helpers/tauri.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type TauriModule =
1616
| 'Notification'
1717
| 'Http'
1818
| 'GlobalShortcut'
19+
| 'Process'
1920

2021
export interface TauriCommand {
2122
__tauriModule: TauriModule

tooling/api/src/process.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
import { invokeTauriCommand } from './helpers/tauri'
6+
7+
/**
8+
* Exits immediately with the given `exitCode`.
9+
*
10+
* @param exitCode The exit code to use
11+
* @returns
12+
*/
13+
async function exit(exitCode: number = 0): Promise<void> {
14+
return invokeTauriCommand({
15+
__tauriModule: 'Process',
16+
mainThread: true,
17+
message: {
18+
cmd: 'exit',
19+
exitCode
20+
}
21+
})
22+
}
23+
24+
/**
25+
* Exits the current instance of the app then relaunches it.
26+
*
27+
* @returns
28+
*/
29+
async function relaunch(): Promise<void> {
30+
return invokeTauriCommand({
31+
__tauriModule: 'Process',
32+
mainThread: true,
33+
message: {
34+
cmd: 'relaunch'
35+
}
36+
})
37+
}
38+
39+
export { exit, relaunch }

0 commit comments

Comments
 (0)