Skip to content

Commit

Permalink
feat(runner): ensure Simulator app is running
Browse files Browse the repository at this point in the history
  • Loading branch information
kkharji committed May 22, 2022
1 parent 0f54252 commit 710f450
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
nvim::BufferDirection,
state::State,
types::{Client, Platform},
util::fmt,
util::{fmt, pid},
Error, Result,
};
use {
Expand Down Expand Up @@ -114,11 +114,32 @@ impl Runner {
};

let mut launcher = runner.launch(logger).await?;
let mut stream = launcher.stream()?;
// TODO(daemon): ensure Simulator.app is running

match pid::get_by_name("Simulator") {
Err(Error::NotFound(_, _)) => {
let msg = format!("[Simulator] Launching");
tracing::info!("{msg}");
logger.log(msg).await?;
tokio::process::Command::new("open")
.args(&["-a", "Simulator"])
.spawn()?
.wait()
.await?;
let msg = format!("[Simulator] Connected");
logger.log(msg).await?;
}
Err(err) => {
let msg = err.to_string();
tracing::error!("{msg}");
logger.log(msg).await?;
}
_ => {}
};

logger.log(fmt::separator()).await?;

let mut stream = launcher.stream()?;

tokio::spawn(async move {
while let Some(output) = stream.next().await {
let state = DAEMON_STATE.clone();
Expand Down
50 changes: 50 additions & 0 deletions src/util/pid.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{ffi::OsStr, fmt::Display, string::String};

#[cfg(feature = "daemon")]
/// Kill process using kill command
pub async fn kill(pid_str: &String) -> anyhow::Result<bool> {
Expand All @@ -19,3 +21,51 @@ pub fn exists(pid: &i32, cb: impl FnOnce()) -> bool {
true
}
}

/// Get process pid by name.
///
/// If an error occured during searching an error will be returned,
/// otherwise process with given name is not, an Error::NotFound will be returned.
///
/// WARNNING: The first match will be returned, and duplicates will be ignored
pub fn get_by_name<S>(name: S) -> crate::Result<i32>
where
S: AsRef<OsStr> + Display,
String: PartialEq<S>,
{
use libproc::libproc::proc_pid;

let pids = proc_pid::listpids(proc_pid::ProcType::ProcAllPIDS)?;

for pid in pids {
let pid = pid as i32;
match proc_pid::name(pid).ok() {
Some(process) if process.eq(&name) => return Ok(pid),
_ => continue,
}
}

Err(crate::Error::NotFound("Process".into(), format!("{name}")))
}

#[test]
fn test_get_by_name() {
let existing_process = get_by_name("DockHelper");
let not_process = get_by_name("afsd8439f");

assert!(existing_process.is_ok());
assert!(not_process.is_err());
}

#[test]
#[ignore = "internal"]
fn test_get_os_processes() {
use libproc::libproc::proc_pid::*;
let pids = listpids(ProcType::ProcAllPIDS).unwrap();

for pid in pids {
if let Some(name) = name(pid as i32).ok() {
println!("{name}")
}
}
}

0 comments on commit 710f450

Please sign in to comment.