Skip to content

Commit

Permalink
ref(runner): merge into one file
Browse files Browse the repository at this point in the history
  • Loading branch information
kkharji committed May 20, 2022
1 parent b7a947a commit 9326c3b
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 119 deletions.
127 changes: 114 additions & 13 deletions src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use crate::constants::DAEMON_STATE;
use crate::util::string_as_section;
use crate::{
constants::DAEMON_STATE,
nvim::BufferDirection,
state::State,
types::{Client, Platform},
Result,
types::{Client, Platform, SimDevice},
util::string_as_section,
Error, Result,
};
use {
tap::Pipe,
tokio::{sync::OwnedMutexGuard, task::JoinHandle},
tokio_stream::StreamExt,
xcodebuild::{parser::BuildSettings, runner},
};
use tap::Pipe;
use tokio::sync::OwnedMutexGuard;
use tokio::task::JoinHandle;
use tokio_stream::StreamExt;
use xcodebuild::parser::BuildSettings;
use xcodebuild::runner;

mod macos;
mod simctl;

pub struct Runner {
pub client: Client,
Expand All @@ -35,3 +32,107 @@ impl Runner {
}
}
}

/// MacOS Runner
impl Runner {
pub async fn run_as_macos_app(self, settings: BuildSettings) -> Result<JoinHandle<Result<()>>> {
let nvim = self.state.clients.get(&self.client.pid)?;
let ref mut logger = nvim.new_logger("Run", &self.target, &self.direction);

logger.log_title().await?;
logger.open_win().await?;

tokio::spawn(async move {
let program = settings.path_to_output_binary()?;
let mut stream = runner::run(&program).await?;

tracing::debug!("Running binary {program:?}");

use xcodebuild::runner::ProcessUpdate::*;
// NOTE: This is required so when neovim exist this should also exit
while let Some(update) = stream.next().await {
let state = DAEMON_STATE.clone();
let state = state.lock().await;
let nvim = state.clients.get(&self.client.pid)?;
let mut logger = nvim.new_logger("Run", &self.target, &self.direction);

// NOTE: NSLog get directed to error by default which is odd
match update {
Stdout(msg) => {
logger.log(msg).await?;
}
Error(msg) | Stderr(msg) => {
logger.log(format!("[Error] {msg}")).await?;
}
Exit(ref code) => {
let success = code == "0";
let msg = string_as_section(if success {
"".into()
} else {
format!("Panic {code}")
});

logger.log(msg).await?;
logger.set_status_end(success, true).await?;
}
}
}
Ok(())
})
.pipe(Ok)
}
}

/// Simctl Runner
impl Runner {
pub async fn run_with_simctl(self, settings: BuildSettings) -> Result<JoinHandle<Result<()>>> {
let nvim = self.state.clients.get(&self.client.pid)?;
let mut logger = nvim.new_logger("Run", &self.target, &self.direction);

let app_id = settings.product_bundle_identifier;
let path_to_app = settings.metal_library_output_dir;

tracing::debug!("{app_id}: {:?}", path_to_app);

logger.log_title().await?;
logger.open_win().await?;

let mut device = get_device(&self.state, self.udid)?;

// NOTE: This is required so when neovim exist this should also exit
let state = DAEMON_STATE.clone().lock_owned().await;

tokio::spawn(async move {
let nvim = state.clients.get(&self.client.pid)?;
let ref mut logger = nvim.new_logger("Run", &self.target, &self.direction);

logger.set_running().await?;

device.try_boot(logger).await?;
device.try_install(&path_to_app, &app_id, logger).await?;
device.try_launch(&app_id, logger).await?;

let mut state = DAEMON_STATE.clone().lock_owned().await;

// TODO(simctl): device might change outside state
state.devices.insert(device);

// TODO: Remove and replace with app logs
logger.set_status_end(true, false).await?;

state.sync_client_state().await?;

Ok(())
})
.pipe(Ok)
}
}

fn get_device<'a>(state: &'a OwnedMutexGuard<State>, udid: Option<String>) -> Result<SimDevice> {
if let Some(udid) = udid {
state.devices.iter().find(|d| d.udid == udid).cloned()
} else {
None
}
.ok_or_else(|| Error::Run("udid not found!!".to_string()))
}
50 changes: 0 additions & 50 deletions src/runner/macos.rs

This file was deleted.

56 changes: 0 additions & 56 deletions src/runner/simctl.rs

This file was deleted.

0 comments on commit 9326c3b

Please sign in to comment.