Skip to content

Commit

Permalink
fix(runner): simctl runner
Browse files Browse the repository at this point in the history
  • Loading branch information
kkharji committed May 20, 2022
1 parent 9326c3b commit 0f26775
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 52 deletions.
2 changes: 1 addition & 1 deletion lua/xbase/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ M.setup = function(opts)
M.try_register(root, opts)

vim.api.nvim_create_autocmd({ "BufEnter", "BufWinEnter" }, {
pattern = { "*.m", "*.swift", "*.c" },
pattern = { "*.m", "*.swift", "*.c", "*.yml" },
callback = function()
vim.keymap.set("n", "<leader>ef", require("xbase.pickers").actions, { buffer = true })
end,
Expand Down
11 changes: 8 additions & 3 deletions src/daemon/requests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use {
crate::runner::Runner,
crate::xcode::{append_build_root, build_with_loggger},
crate::Error,
std::str::FromStr,
xcodebuild::runner::build_settings,
};

Expand All @@ -36,11 +35,13 @@ impl Handler for Run {
async fn handle(self) -> Result<()> {
let Client { pid, root, .. } = &self.client;

tracing::info!("⚙️ Running command: {:?}", self);
tracing::info!("⚙️ Running command: {}", self.config.to_string());

let state = DAEMON_STATE.clone().lock_owned().await;
let direction = self.direction.clone();
let platform = if let Some(d) = self.device.as_ref() {
let platform = if let Some(ref d) = self.device {
tracing::info!("{:#?}", d.platform);
Some(d.platform.clone())
} else {
None
Expand All @@ -56,8 +57,12 @@ impl Handler for Run {
};

let ref mut logger = nvim.new_logger("Build", &self.config.target, &direction);

let settings = build_settings(&root, &args).await?;
let platform = platform.unwrap_or(Platform::from_str(&settings.platform_display_name)?);
let platform = match platform {
Some(v) => v,
None => Platform::from_display(&settings.platform_display_name)?,
};

let success = build_with_loggger(logger, &root, &args, true, true).await?;
if !success {
Expand Down
8 changes: 8 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub enum Error {
Run(String),
#[error("[Error] (WatchError) {0}")]
Watch(#[from] WatchError),
#[error("[Error] (Message) {0}")]
Message(String),
}

#[derive(ThisError, Debug)]
Expand Down Expand Up @@ -68,6 +70,12 @@ pub enum WatchError {
FailToStart,
}

impl From<String> for Error {
fn from(s: String) -> Self {
Self::Message(s)
}
}

#[cfg(feature = "daemon")]
impl WatchError {
pub fn stop(err: Error) -> WatchError {
Expand Down
30 changes: 14 additions & 16 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,38 +85,36 @@ impl Runner {

/// Simctl Runner
impl Runner {
pub async fn run_with_simctl(self, settings: BuildSettings) -> Result<JoinHandle<Result<()>>> {
pub async fn run_with_simctl(
mut 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 ref 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?;
logger.set_running().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;
device.try_boot(logger).await?;
device.try_install(&path_to_app, &app_id, logger).await?;
device.try_launch(&app_id, logger).await?;

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

// NOTE: This is required so when neovim exist this should also exit
tokio::spawn(async move {
let state = DAEMON_STATE.clone().lock_owned().await;
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?;

Expand Down
66 changes: 49 additions & 17 deletions src/types/project/platform.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
use super::*;
use strum::{Display, EnumString};
#[derive(
Clone, Debug, Default, Deserialize, Serialize, Hash, PartialEq, Eq, Display, EnumString,
)]
use std::str::FromStr;

#[derive(Clone, Debug, Default, Deserialize, Serialize, Hash, PartialEq, Eq)]
pub enum Platform {
#[serde(rename = "iOS")]
#[strum(serialize = "iOS")]
IOS,
#[serde(rename = "watchOS")]
#[strum(serialize = "watchOS")]
WatchOS,
#[serde(rename = "tvOS")]
#[strum(serialize = "tvOS")]
TvOS,
#[strum(serialize = "macOS")]
#[serde(rename = "macOS")]
MacOS,
#[default]
#[strum(serialize = "")]
None,
}

Expand Down Expand Up @@ -49,6 +43,24 @@ impl Platform {
pub fn is_mac_os(&self) -> bool {
matches!(self, Self::MacOS)
}

#[cfg(feature = "daemon")]
pub fn from_display(display: &String) -> Result<Platform> {
let value = if display.contains("Simulator") {
display
.split(" ")
.map(ToString::to_string)
.collect::<Vec<String>>()
.get(0)
.ok_or_else(|| {
crate::Error::Message(format!("Unable to get Platfrom from `{display}`"))
})?
.to_string()
} else {
display.into()
};
Self::from_str(&value).map_err(|s| crate::Error::Message(s))
}
}

#[cfg(feature = "lua")]
Expand All @@ -58,18 +70,38 @@ use mlua::prelude::*;
impl<'a> FromLua<'a> for Platform {
fn from_lua(lua_value: LuaValue<'a>, _lua: &'a Lua) -> LuaResult<Self> {
if let LuaValue::String(value) = lua_value {
match value.to_str()? {
"iOS" => Platform::IOS,
"watchOS" => Platform::WatchOS,
"tvOS" => Platform::TvOS,
"macOS" => Platform::MacOS,
_ => return Err(LuaError::external("Fail to deserialize Platform")),
}
.pipe(Ok)
value.to_str()?.pipe(FromStr::from_str).to_lua_err()
} else {
Err(LuaError::external(
"Fail to deserialize Platform, expected string",
))
}
}
}

impl FromStr for Platform {
type Err = String;

fn from_str(s: &str) -> std::result::Result<Self, String> {
match s {
"iOS" => Ok(Platform::IOS),
"watchOS" => Ok(Platform::WatchOS),
"tvOS" => Ok(Platform::TvOS),
"macOS" => Ok(Platform::MacOS),
_ => Err(format!("Platfrom {s}")),
}
}
}

impl ToString for Platform {
fn to_string(&self) -> String {
match self {
Platform::IOS => "iOS",
Platform::WatchOS => "watchOS",
Platform::TvOS => "tvOS",
Platform::MacOS => "macOS",
_ => "",
}
.into()
}
}
38 changes: 23 additions & 15 deletions src/types/simdevice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,22 @@ impl SimDevice {

pub async fn try_boot<'a>(&mut self, logger: &mut Logger<'a>) -> Result<()> {
// FIX(run): DeviceState can get out of sync when the user shutdown device manually
if let DeviceState::Shutdown = &self.state {
logger.log(format!("[Booting] ({})", self.name)).await?;
logger
.log(string_as_section(format!("Booting: {}", self.name)))
.await?;

self.boot()
.pipe(|res| self.handle_error(res, logger))
.await?;
self.state = DeviceState::Booted;
}
if let Err(e) = self.boot() {
let err = Error::from(e);
let err_msg = err.to_string();
if !err_msg.contains("current state Booted") {
logger.log(err_msg).await?;
logger.set_status_end(false, true).await?;
self.is_running = false;
return Err(err);
}
};

self.state = DeviceState::Booted;

Ok(())
}
Expand All @@ -74,20 +82,20 @@ impl SimDevice {
app_id: &String,
logger: &mut Logger<'a>,
) -> Result<()> {
logger
.log(string_as_section(format!("Installing: {app_id}",)))
.await?;
self.install(path_to_app)
.pipe(|res| self.handle_error(res, logger))
.await?;
logger
.log(format!("[Installing] ({}) {app_id}", self.name))
.await
Ok(())
}

pub async fn try_launch<'a>(&mut self, app_id: &String, logger: &mut Logger<'a>) -> Result<()> {
logger
.log(string_as_section(format!("Launching: {app_id}")))
.await?;
if !self.is_running {
logger
.log(format!("[Launching] ({}) {app_id}", self.name))
.await?;

self.launch(app_id)
.stdout(&"/tmp/wordle_log")
.stderr(&"/tmp/wordle_log")
Expand All @@ -97,7 +105,7 @@ impl SimDevice {

self.is_running = true;

logger.log(string_as_section("Launched".into())).await?;
logger.log(string_as_section("Connected".into())).await?;
}

Ok(())
Expand Down

0 comments on commit 0f26775

Please sign in to comment.