|
| 1 | +use super::commands::*; |
1 | 2 | use crate::app::notifications::{send_notification, send_notification_with_custom_title}; |
2 | 3 | use crate::app::store::structs::StoredScript; |
3 | 4 | use crate::{MainPageLogic, NotifStringEnum, NotifTypeEnum, Script, Scriptboard}; |
4 | 5 | use slint::{ComponentHandle, Model, VecModel, Weak}; |
5 | 6 | use std::io::{BufReader, Read}; |
6 | | -use std::process::{Command, Output, Stdio}; |
| 7 | +use std::process::Output; |
7 | 8 | use std::rc::Rc; |
8 | 9 |
|
9 | | -#[cfg(target_os = "windows")] |
10 | | -use std::os::windows::process::CommandExt; |
| 10 | +/// Prepare the scripts before its execution then start the execution. |
| 11 | +pub fn execute_script( |
| 12 | + ui_weak: Weak<Scriptboard>, |
| 13 | + scripts: Rc<VecModel<Script>>, |
| 14 | + script_index: usize, |
| 15 | +) { |
| 16 | + let mut script = scripts.row_data(script_index as usize).unwrap(); |
| 17 | + script.running = true; |
| 18 | + if !script.preserve_output { |
| 19 | + script.output = "".into(); |
| 20 | + } else { |
| 21 | + script.output.push_str("\n"); |
| 22 | + } |
| 23 | + scripts.set_row_data(script_index as usize, script.clone()); |
| 24 | + run_execution_script(ui_weak.clone(), script, script_index as usize); |
| 25 | +} |
11 | 26 |
|
12 | 27 | /// Execute the script and store its output. |
13 | 28 | /// This function updates the scripts list to update the UI. |
14 | | -pub fn execute_script(ui_weak: Weak<Scriptboard>, script: Script, script_index: usize) { |
| 29 | +fn run_execution_script(ui_weak: Weak<Scriptboard>, script: Script, script_index: usize) { |
15 | 30 | std::thread::spawn({ |
16 | 31 | let light_script: StoredScript = script.into(); |
17 | 32 | move || { |
@@ -152,81 +167,3 @@ fn finish_script_execution(ui: Scriptboard, output: Option<Output>, script_index |
152 | 167 | send_notification_with_custom_title(ui.as_weak(), theme, script.name.into(), message); |
153 | 168 | } |
154 | 169 | } |
155 | | - |
156 | | -#[cfg(target_os = "linux")] |
157 | | -fn get_linux_command(script: &StoredScript, parsed_args: Vec<String>) -> Command { |
158 | | - if !script.need_admin { |
159 | | - use std::process::Stdio; |
160 | | - |
161 | | - let mut cmd = Command::new(&script.interpreter); |
162 | | - cmd.arg(&script.path) |
163 | | - .args(parsed_args) |
164 | | - .stdout(Stdio::piped()); |
165 | | - return cmd; |
166 | | - } |
167 | | - |
168 | | - let mut cmd = Command::new("pkexec"); |
169 | | - cmd.args([&script.interpreter, &script.path]) |
170 | | - .args(parsed_args) |
171 | | - .stdout(Stdio::piped()); |
172 | | - |
173 | | - cmd |
174 | | -} |
175 | | - |
176 | | -#[cfg(target_os = "macos")] |
177 | | -fn get_macos_command(script: &StoredScript, parsed_args: Vec<String>) -> Command { |
178 | | - if !script.need_admin { |
179 | | - let mut cmd = Command::new(&script.interpreter); |
180 | | - cmd.arg(&script.path) |
181 | | - .args(parsed_args) |
182 | | - .stdout(Stdio::piped()); |
183 | | - return cmd; |
184 | | - } |
185 | | - |
186 | | - let osascript_arg = format!( |
187 | | - "do shell script \"{} {} {}\" with prompt \"{}\" with administrator privileges", |
188 | | - &script.interpreter, |
189 | | - &script.path, |
190 | | - parsed_args.join(" "), |
191 | | - &script.name, |
192 | | - ); |
193 | | - |
194 | | - let mut cmd = Command::new("osascript"); |
195 | | - cmd.arg("-e").arg(osascript_arg).stdout(Stdio::piped()); |
196 | | - |
197 | | - cmd |
198 | | -} |
199 | | - |
200 | | -#[cfg(target_os = "windows")] |
201 | | -fn get_windows_command(script: &StoredScript, parsed_args: Vec<String>) -> Command { |
202 | | - // Ask for admin password |
203 | | - // Start-Process cmd -ArgumentList "/C your_command_here" -Verb RunAs |
204 | | - // Example with custom interpreter : |
205 | | - // Start-Process cmd -ArgumentList "/C python -c \"print('Hello, World!')\"" -Verb RunAs |
206 | | - |
207 | | - if !script.need_admin { |
208 | | - let mut cmd = Command::new(&script.interpreter); |
209 | | - cmd.arg("-File") |
210 | | - .arg(&script.path) |
211 | | - .args(parsed_args) |
212 | | - .creation_flags(0x08000000) |
213 | | - .stdout(Stdio::piped()); |
214 | | - return cmd; |
215 | | - } |
216 | | - |
217 | | - let script_args = format!( |
218 | | - "\"/C {} {} {}\"", |
219 | | - &script.interpreter, |
220 | | - &script.path, |
221 | | - parsed_args.join(" ") |
222 | | - ); |
223 | | - |
224 | | - let mut cmd: Command = Command::new("Start-Process"); |
225 | | - cmd.arg("cmd") |
226 | | - .arg("-ArgumentList") |
227 | | - .arg(script_args) |
228 | | - .creation_flags(0x08000000) |
229 | | - .stdout(Stdio::piped()); |
230 | | - |
231 | | - cmd |
232 | | -} |
0 commit comments