Skip to content

Commit

Permalink
fix(cli): kill before dev command recursively on Unix, closes #2794 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Apr 3, 2022
1 parent 42a32ee commit e251e1b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changes/fix-before-dev-kill-unix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---

Kill the `beforeDevCommand` process recursively on Unix.
2 changes: 1 addition & 1 deletion tooling/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ include = [
"src/",
"/templates",
"MergeModules/",
"scripts/",
"*.json",
"*.rs",
"vswhere.exe",
"tauri.gitignore"
]

Expand Down
10 changes: 10 additions & 0 deletions tooling/cli/scripts/kill-children.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function getcpid() {
cpids=`pgrep -P $1|xargs`
for cpid in $cpids;
do
echo "$cpid"
getcpid $cpid
done
}

kill $(getcpid $1)
File renamed without changes.
26 changes: 21 additions & 5 deletions tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ use std::{
static BEFORE_DEV: OnceCell<Mutex<Arc<SharedChild>>> = OnceCell::new();
static KILL_BEFORE_DEV_FLAG: OnceCell<AtomicBool> = OnceCell::new();

#[cfg(unix)]
const KILL_CHILDREN_SCRIPT: &[u8] = include_bytes!("../scripts/kill-children.sh");

#[derive(Debug, Parser)]
#[clap(about = "Tauri dev", trailing_var_arg(true))]
pub struct Options {
Expand Down Expand Up @@ -298,11 +301,24 @@ fn kill_before_dev_process() {
.arg("-Command")
.arg(format!("function Kill-Tree {{ Param([int]$ppid); Get-CimInstance Win32_Process | Where-Object {{ $_.ParentProcessId -eq $ppid }} | ForEach-Object {{ Kill-Tree $_.ProcessId }}; Stop-Process -Id $ppid -ErrorAction SilentlyContinue }}; Kill-Tree {}", child.id()))
.status();
#[cfg(not(windows))]
let _ = Command::new("pkill")
.args(&["-TERM", "-P"])
.arg(child.id().to_string())
.status();
#[cfg(unix)]
{
let mut kill_children_script_path = std::env::temp_dir();
kill_children_script_path.push("kill-children.sh");

if !kill_children_script_path.exists() {
if let Ok(mut file) = std::fs::File::create(&kill_children_script_path) {
use std::{io::Write, os::unix::fs::PermissionsExt};
let _ = file.write_all(KILL_CHILDREN_SCRIPT);
let mut permissions = file.metadata().unwrap().permissions();
permissions.set_mode(0o770);
let _ = file.set_permissions(permissions);
}
}
let _ = Command::new(&kill_children_script_path)
.arg(child.id().to_string())
.output();
}
let _ = child.kill();
}
}
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,15 @@ struct VsInstanceInfo {
}

#[cfg(windows)]
const VSWHERE: &[u8] = include_bytes!("../vswhere.exe");
const VSWHERE: &[u8] = include_bytes!("../scripts/vswhere.exe");

#[cfg(windows)]
fn build_tools_version() -> crate::Result<Option<Vec<String>>> {
let mut vswhere = std::env::temp_dir();
vswhere.push("vswhere.exe");

if !vswhere.exists() {
if let Ok(mut file) = std::fs::File::create(vswhere.clone()) {
if let Ok(mut file) = std::fs::File::create(&vswhere) {
use std::io::Write;
let _ = file.write_all(VSWHERE);
}
Expand Down

0 comments on commit e251e1b

Please sign in to comment.