Skip to content

Commit 98ac2b5

Browse files
committed
feat: proper process handling and stop
- fix how stop works - proper process tracking - prevent multiple instances - some cleanup - needs a bit more work :)
1 parent 00431d4 commit 98ac2b5

File tree

8 files changed

+159
-98
lines changed

8 files changed

+159
-98
lines changed

Cargo.lock

Lines changed: 22 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@ edition = "2024"
66
[dependencies]
77
clap = { version = "4.5.41", features = ["derive"] }
88
anyhow = "1.0.98"
9-
thiserror = "2.0.12"
10-
glob = "0.3.2"
119
serde = { version = "1.0", features = ["derive"] }
1210
serde_json = "1.0"
1311
serde_yaml = "0.9"
1412
dialoguer = "0.11.0"
1513
colored = "3.0.0"
1614
walkdir = "2.5.0"
17-
console = "0.16.0"
1815
clap_complete = "4.5.55"
1916
regex = "1.11.1"
17+
nix = { version = "0.30.1", features = ["process", "signal"] }
2018

2119
[[bin]]
2220
name = "flatplay"

src/command.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn command_succeeds(cmd: &str, args: &[&str]) -> bool {
1919
.stdout(Stdio::null())
2020
.stderr(Stdio::null())
2121
.status()
22-
.map_or(false, |s| s.success())
22+
.is_ok_and(|s| s.success())
2323
}
2424

2525
// Runs a command, handling Flatpak sandbox and container specifics.
@@ -80,10 +80,10 @@ pub fn run_command(command: &str, args: &[&str]) -> Result<()> {
8080
// Runs flatpak-builder, preferring the native binary, then the Flatpak app.
8181
pub fn flatpak_builder(args: &[&str]) -> Result<()> {
8282
if command_succeeds("flatpak-builder", &["--version"]) {
83-
run_command("flatpak-builder", &args)
83+
run_command("flatpak-builder", args)
8484
} else if command_succeeds("flatpak", &["run", "org.flatpak.Builder", "--version"]) {
8585
let mut new_args = vec!["run", "org.flatpak.Builder"];
86-
new_args.extend_from_slice(&args);
86+
new_args.extend_from_slice(args);
8787
run_command("flatpak", &new_args)
8888
} else {
8989
Err(anyhow::anyhow!(

src/lib.rs

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
pub mod command;
22
pub mod manifest;
3+
pub mod process;
34
pub mod state;
45
pub mod utils;
56

67
use anyhow::Result;
78
use colored::*;
89

910
use command::{flatpak_builder, run_command};
10-
use dialoguer::{Select, theme::ColorfulTheme};
11+
use dialoguer::{theme::ColorfulTheme, Select};
1112
use manifest::Manifest;
1213
use state::State;
1314
use std::fs;
1415
use utils::{get_a11y_bus_args, get_host_env};
1516
use walkdir::WalkDir;
1617

18+
use crate::process::kill_process_group;
19+
1720
const BUILD_DIR: &str = ".flatplay";
1821

1922
pub struct FlatpakManager {
@@ -67,8 +70,7 @@ impl FlatpakManager {
6770
Ok(())
6871
}
6972

70-
pub fn new() -> Result<Self> {
71-
let state = State::load()?;
73+
pub fn new(state: State) -> Result<Self> {
7274
let manifest = if let Some(path) = &state.active_manifest {
7375
Some(Manifest::from_file(path)?)
7476
} else {
@@ -82,10 +84,10 @@ impl FlatpakManager {
8284
}
8385

8486
fn is_build_initialized(&self) -> Result<bool> {
85-
let repo_dir = format!("{}/repo", BUILD_DIR);
86-
let metadata_file = format!("{}/metadata", repo_dir);
87-
let files_dir = format!("{}/files", repo_dir);
88-
let var_dir = format!("{}/var", repo_dir);
87+
let repo_dir = format!("{BUILD_DIR}/repo");
88+
let metadata_file = format!("{repo_dir}/metadata");
89+
let files_dir = format!("{repo_dir}/files");
90+
let var_dir = format!("{repo_dir}/var");
8991

9092
// Check if all required directories and files exist
9193
// From gnome-builder: https://gitlab.gnome.org/GNOME/gnome-builder/-/blob/8579055f5047a0af5462e8a587b0742014d71d64/src/plugins/flatpak/gbp-flatpak-pipeline-addin.c#L220
@@ -96,7 +98,7 @@ impl FlatpakManager {
9698

9799
fn init_build(&self) -> Result<()> {
98100
let manifest = self.manifest.as_ref().unwrap();
99-
let repo_dir = format!("{}/repo", BUILD_DIR);
101+
let repo_dir = format!("{BUILD_DIR}/repo");
100102

101103
println!("{}", "Initializing build environment...".bold());
102104
run_command(
@@ -127,7 +129,7 @@ impl FlatpakManager {
127129

128130
fn build_application(&self) -> Result<()> {
129131
let manifest = self.manifest.as_ref().unwrap();
130-
let repo_dir = format!("{}/repo", BUILD_DIR);
132+
let repo_dir = format!("{BUILD_DIR}/repo");
131133

132134
if let Some(module) = manifest.modules.last() {
133135
match module {
@@ -140,7 +142,7 @@ impl FlatpakManager {
140142
let buildsystem = buildsystem.as_deref();
141143
match buildsystem {
142144
Some("meson") => {
143-
let build_dir = format!("{}/_build", BUILD_DIR);
145+
let build_dir = format!("{BUILD_DIR}/_build");
144146
let mut meson_args = vec!["build", &repo_dir, "meson", "setup"];
145147
if let Some(config_opts) = config_opts {
146148
meson_args.extend(config_opts.iter().map(|s| s.as_str()));
@@ -157,8 +159,8 @@ impl FlatpakManager {
157159
)?;
158160
}
159161
Some("cmake") | Some("cmake-ninja") => {
160-
let build_dir = format!("{}/_build", BUILD_DIR);
161-
let b_flag = format!("-B{}", build_dir);
162+
let build_dir = format!("{BUILD_DIR}/_build");
163+
let b_flag = format!("-B{build_dir}");
162164
let mut cmake_args = vec![
163165
"build",
164166
&repo_dir,
@@ -212,14 +214,14 @@ impl FlatpakManager {
212214
}
213215
}
214216

215-
if let Some(module) = manifest.modules.last() {
216-
if let crate::manifest::Module::Object { post_install, .. } = module {
217-
if let Some(post_install) = post_install {
218-
for command in post_install {
219-
let args: Vec<&str> = command.split_whitespace().collect();
220-
run_command(args[0], &args[1..])?;
221-
}
222-
}
217+
if let Some(crate::manifest::Module::Object {
218+
post_install: Some(post_install),
219+
..
220+
}) = manifest.modules.last()
221+
{
222+
for command in post_install {
223+
let args: Vec<&str> = command.split_whitespace().collect();
224+
run_command(args[0], &args[1..])?;
223225
}
224226
}
225227

@@ -230,15 +232,15 @@ impl FlatpakManager {
230232
println!("{}", "Building dependencies...".bold());
231233
let manifest = self.manifest.as_ref().unwrap();
232234
let manifest_path = self.state.active_manifest.as_ref().unwrap();
233-
let repo_dir = format!("{}/repo", BUILD_DIR);
235+
let repo_dir = format!("{BUILD_DIR}/repo");
234236
flatpak_builder(&[
235237
"--ccache",
236238
"--force-clean",
237239
"--disable-updates",
238240
"--disable-download",
239241
"--build-only",
240242
"--keep-build-dirs",
241-
&format!("--state-dir={}/flatpak-builder", BUILD_DIR),
243+
&format!("--state-dir={BUILD_DIR}/flatpak-builder"),
242244
&format!(
243245
"--stop-at={}",
244246
match manifest.modules.last().unwrap() {
@@ -261,13 +263,13 @@ impl FlatpakManager {
261263

262264
let manifest = self.manifest.as_ref().unwrap();
263265
let manifest_path = self.state.active_manifest.as_ref().unwrap();
264-
let repo_dir = format!("{}/repo", BUILD_DIR);
266+
let repo_dir = format!("{BUILD_DIR}/repo");
265267
flatpak_builder(&[
266268
"--ccache",
267269
"--force-clean",
268270
"--disable-updates",
269271
"--download-only",
270-
&format!("--state-dir={}/flatpak-builder", BUILD_DIR),
272+
&format!("--state-dir={BUILD_DIR}/flatpak-builder"),
271273
&format!(
272274
"--stop-at={}",
273275
match manifest.modules.last().unwrap() {
@@ -315,18 +317,8 @@ impl FlatpakManager {
315317
self.run()
316318
}
317319

318-
pub fn stop(&self) -> Result<()> {
319-
println!("{}", "Stopping running tasks...".yellow());
320-
let output = std::process::Command::new("ps").arg("-ef").output()?;
321-
let output = String::from_utf8_lossy(&output.stdout);
322-
for line in output.lines() {
323-
if line.contains("flatpak-builder") || line.contains("flatpak run") {
324-
let mut parts = line.split_whitespace();
325-
let pid = parts.nth(1).unwrap();
326-
run_command("kill", &["-9", pid])?;
327-
}
328-
}
329-
Ok(())
320+
pub fn stop(&mut self) -> Result<()> {
321+
kill_process_group(&mut self.state)
330322
}
331323

332324
pub fn run(&self) -> Result<()> {
@@ -348,7 +340,7 @@ impl FlatpakManager {
348340
}
349341

350342
let manifest = self.manifest.as_ref().unwrap();
351-
let repo_dir = format!("{}/repo", BUILD_DIR);
343+
let repo_dir = format!("{BUILD_DIR}/repo");
352344

353345
let mut args = vec![
354346
"build".to_string(),
@@ -359,7 +351,7 @@ impl FlatpakManager {
359351
];
360352

361353
for (key, value) in get_host_env() {
362-
args.push(format!("--env={}={}", key, value));
354+
args.push(format!("--env={key}={value}"));
363355
}
364356

365357
for arg in get_a11y_bus_args() {
@@ -413,7 +405,7 @@ impl FlatpakManager {
413405
}
414406
let manifest = self.manifest.as_ref().unwrap();
415407
let _app_id = &manifest.id;
416-
let repo_dir = format!("{}/repo", BUILD_DIR);
408+
let repo_dir = format!("{BUILD_DIR}/repo");
417409
run_command("flatpak", &["build", &repo_dir, "bash"])
418410
}
419411

@@ -448,7 +440,7 @@ impl FlatpakManager {
448440
return format!("{} {}", "*".green().bold(), path_str);
449441
}
450442
}
451-
format!(" {}", path_str)
443+
format!(" {path_str}")
452444
})
453445
.collect();
454446

0 commit comments

Comments
 (0)