Skip to content

Commit 12ab085

Browse files
committed
feat: command execution & tauri comunication
1 parent e25da27 commit 12ab085

File tree

7 files changed

+114
-29
lines changed

7 files changed

+114
-29
lines changed

src-tauri/Cargo.lock

Lines changed: 2 additions & 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "hello-node"
2+
name = "hello_node"
33
version = "0.0.0"
44
description = "A Tauri App"
55
authors = ["you"]
@@ -11,6 +11,7 @@ edition = "2021"
1111
tauri-build = { version = "1", features = [] }
1212

1313
[dependencies]
14+
lazy_static = "1.4"
1415
tauri = { version = "1", features = [ "system-tray", "dialog-message", "shell-open"] }
1516
serde = { version = "1", features = ["derive"] }
1617
serde_json = "1"

src-tauri/src/main.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
11
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
22
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
33

4+
mod system_utils;
5+
6+
use lazy_static::lazy_static;
7+
use tauri::Window;
48
use tauri::{CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu};
59
use tauri_plugin_autostart::MacosLauncher;
10+
use system_utils::executor::run_command_with_logging;
11+
12+
// create a mutex to store the main window
13+
lazy_static! {
14+
static ref MAIN_WINDOW: std::sync::Mutex<Option<Window>> = std::sync::Mutex::new(None);
15+
}
616

7-
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
17+
fn logger(message: String, js_function: String) {
18+
if let Ok(window) = MAIN_WINDOW.lock() {
19+
if let Some(win) = &*window {
20+
let js_code = format!("{}('{}');", js_function, message);
21+
win.eval(&js_code).unwrap();
22+
}
23+
}
24+
}
25+
#[allow(non_snake_case)]
826
#[tauri::command]
9-
fn greet(name: &str) -> String {
10-
format!("Hello, {}! You've been greeted from Rust!", name)
27+
fn executeEcho(){
28+
match run_command_with_logging("echo", &["hello"], logger) {
29+
Ok(_) => println!("Command executed successfully"),
30+
Err(e) => println!("Failed to execute command: {}", e),
31+
}
1132
}
1233

34+
1335
fn main() {
1436
// creating a menu for the system tray, with a quit option (add more options later if needed)
1537
let quit = CustomMenuItem::new("quit".to_string(), "Quit").accelerator("Cmd+Q");
16-
38+
1739
let system_tray_menu = SystemTrayMenu::new().add_item(quit);
1840
tauri::Builder::default()
41+
.setup(|app| {
42+
let main_window = app.get_window("main").unwrap();
43+
*MAIN_WINDOW.lock().unwrap() = Some(main_window);
44+
Ok(())
45+
})
1946
.plugin(tauri_plugin_autostart::init(
2047
MacosLauncher::LaunchAgent,
2148
Some(vec![]),
@@ -51,7 +78,7 @@ fn main() {
5178
},
5279
_ => {}
5380
})
54-
.invoke_handler(tauri::generate_handler![greet])
81+
.invoke_handler(tauri::generate_handler![executeEcho])
5582
.run(tauri::generate_context!())
5683
.expect("error while running tauri application");
5784
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::io::{BufRead, BufReader};
2+
use std::process::{Command, Stdio};
3+
use std::sync::mpsc::{self};
4+
use std::thread;
5+
6+
pub type Logger = fn(String, String);
7+
8+
// run a command and log the output
9+
pub fn run_command_with_logging(
10+
command: &str,
11+
args: &[&str],
12+
logger: Logger,
13+
) -> std::io::Result<()> {
14+
println!("executing {} {}", command, args.join(" "));
15+
16+
let shell = if cfg!(windows) { "cmd" } else { "sh" };
17+
18+
let command_to_execute = format!("{} {}", command, args.join(" "));
19+
20+
let arg = if cfg!(windows) {
21+
&["/C", command_to_execute.as_str()]
22+
} else {
23+
&["-c", command_to_execute.as_str()]
24+
};
25+
26+
let mut cmd = Command::new(shell)
27+
.args(arg)
28+
.stdout(Stdio::piped())
29+
.stderr(Stdio::piped())
30+
.spawn()?;
31+
32+
let stdout = cmd.stdout.take().expect("Failed to open stdout");
33+
let stderr = cmd.stderr.take().expect("Failed to open stderr");
34+
35+
let (tx, rx) = mpsc::channel();
36+
37+
let tx_clone = tx.clone();
38+
thread::spawn(move || {
39+
let reader = BufReader::new(stdout);
40+
for line in reader.lines() {
41+
if let Ok(line) = line {
42+
tx_clone.send(line).unwrap();
43+
}
44+
}
45+
});
46+
47+
thread::spawn(move || {
48+
let reader = BufReader::new(stderr);
49+
for line in reader.lines() {
50+
if let Ok(line) = line {
51+
tx.send(line).unwrap();
52+
}
53+
}
54+
});
55+
56+
for line in rx {
57+
println!("{}", line);
58+
logger(line, "FunctionOutputLogger".to_string());
59+
}
60+
61+
cmd.wait()?;
62+
Ok(())
63+
}

src-tauri/src/system_utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod executor;

src/App.tsx

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@ import { enable, isEnabled, disable } from "tauri-plugin-autostart-api";
44
import "./App.css";
55

66
function App() {
7-
const [greetMsg, setGreetMsg] = useState("");
8-
const [name, setName] = useState("");
97
const [enabled, setEnabled] = useState(false);
108

119
async function greet() {
12-
setGreetMsg(await invoke("greet", { name }));
10+
await invoke("executeEcho");
1311
}
1412

1513
useEffect(() => {
1614
isEnabled().then((e) => {
1715
if (e) {
1816
setEnabled(true);
19-
}
17+
}
2018
});
2119
}, []);
2220

@@ -32,26 +30,16 @@ function App() {
3230
}
3331
}
3432

33+
window.FunctionOutputLogger = function (msg: string) {
34+
console.log(msg);
35+
};
36+
3537
return (
3638
<div className="container">
37-
<form
38-
className="row"
39-
onSubmit={(e) => {
40-
e.preventDefault();
41-
greet();
42-
}}
43-
>
44-
<input
45-
id="greet-input"
46-
onChange={(e) => setName(e.currentTarget.value)}
47-
placeholder="Enter a name..."
48-
/>
49-
<button type="submit">Greet</button>
50-
</form>
51-
52-
<button onClick={switchAutostart}>{enabled ? "Disable autostart" : "Enable autostart"}</button>
53-
54-
<p>{greetMsg}</p>
39+
<button onClick={switchAutostart}>
40+
{enabled ? "Disable autostart" : "Enable autostart"}
41+
</button>
42+
<button onClick={greet}>test</button>
5543
</div>
5644
);
5745
}

src/global.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
interface Window {
3+
FunctionOutputLogger: (message: string) => void;
4+
}

0 commit comments

Comments
 (0)