Skip to content

Commit 7245899

Browse files
committed
ref(runner): better architecture
1 parent 6ee4138 commit 7245899

File tree

20 files changed

+564
-518
lines changed

20 files changed

+564
-518
lines changed

Cargo.lock

Lines changed: 139 additions & 141 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ tokio = { version = "1.17.0", features = ["full"], optional = tr
7373
tokio-util = { version = "0.7.1", features = ["codec"], optional = true }
7474
async-trait = { version = "0.1.52", optional = true }
7575
async-stream = { version = "0.3.3", optional = true }
76-
process-stream = { version = "0.1.3", optional = true }
76+
process-stream = { path = "../../rust/process-stream/", optional = true }
7777
# Logging Feature
7878
tracing = { version = "0.1.32", optional = true }
7979
tracing-subscriber = { version = "0.3.9", features = ["env-filter"], optional = true}

lua/xbase/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn libxbase(l: &Lua) -> LuaResult<LuaTable> {
1919
("register", fun!(Register, l)),
2020
("drop", fun!(Drop, l)),
2121
("build", fun!(Build, l)),
22-
("run", fun!(Run, l)),
22+
("run", fun!(RunRequest, l)),
2323
("watch_target", fun!(WatchTarget, l)),
2424
])
2525
}
@@ -34,6 +34,7 @@ pub fn is_running(_: &Lua, _: ()) -> LuaResult<bool> {
3434

3535
/// Ensure that daemon is currently running in background
3636
pub fn ensure(lua: &Lua, _: ()) -> LuaResult<bool> {
37+
// FIXME(dameon): resulting in connection refused
3738
if is_running(lua, ()).unwrap() {
3839
Ok(false)
3940
} else if Command::new(DAEMON_BINARY).spawn().is_ok() {

src/daemon/message.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Request {
2121
#[derive(Debug, Serialize, Deserialize)]
2222
pub enum Message {
2323
Build(Build),
24-
Run(Run),
24+
RunRequest(RunRequest),
2525
Register(Register),
2626
Drop(Drop),
2727
RenameFile(RenameFile),
@@ -33,7 +33,7 @@ impl Message {
3333
pub async fn handle(self) -> crate::Result<()> {
3434
match self {
3535
Self::Build(c) => Handler::handle(c).await,
36-
Self::Run(c) => Handler::handle(c).await,
36+
Self::RunRequest(c) => Handler::handle(c).await,
3737
Self::RenameFile(c) => Handler::handle(c).await,
3838
Self::Register(c) => Handler::handle(c).await,
3939
Self::Drop(c) => Handler::handle(c).await,

src/daemon/requests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ macro_rules! convertable {
4545
};
4646
}
4747
convertable!(Build);
48-
convertable!(Run);
48+
convertable!(RunRequest);
4949
convertable!(Register);
5050
convertable!(RenameFile);
5151
convertable!(Drop);

src/daemon/requests/build.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use std::fmt::Debug;
44

55
#[cfg(feature = "daemon")]
66
use {
7-
crate::constants::DAEMON_STATE,
8-
crate::util::serde::value_or_default,
9-
crate::xcode::{append_build_root, build_with_loggger},
7+
crate::constants::DAEMON_STATE, crate::util::serde::value_or_default,
8+
crate::xcode::build_with_logger,
109
};
1110

1211
/// Build a project.
@@ -32,15 +31,14 @@ impl Handler for Build {
3231

3332
let nvim = client.nvim(state)?;
3433
let direction = self.direction.clone();
35-
36-
let args = append_build_root(&root, config.as_args())?;
34+
let args = config.args(&root, &None)?;
3735

3836
let ref mut logger = nvim.logger();
3937

4038
logger.set_title(format!("Build:{}", config.target));
4139
logger.set_direction(&direction);
4240

43-
let success = build_with_loggger(logger, &root, &args, true, true).await?;
41+
let success = build_with_logger(logger, root, &args, true, true).await?;
4442

4543
if !success {
4644
let ref msg = format!("Failed: {} ", config.to_string());

src/daemon/requests/run.rs

Lines changed: 12 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,12 @@ use {
55

66
#[cfg(feature = "daemon")]
77
use {
8-
crate::constants::DAEMON_STATE,
9-
crate::runner::Runner,
10-
crate::types::Platform,
11-
crate::util::serde::value_or_default,
12-
crate::xcode::{append_build_root, build_with_loggger},
13-
crate::Error,
14-
xcodebuild::runner::build_settings,
8+
crate::constants::DAEMON_STATE, crate::run::RunService, crate::util::serde::value_or_default,
159
};
1610

1711
/// Run a project.
1812
#[derive(Debug, Serialize, Deserialize)]
19-
pub struct Run {
13+
pub struct RunRequest {
2014
pub client: Client,
2115
pub config: BuildConfiguration,
2216
#[cfg_attr(feature = "daemon", serde(deserialize_with = "value_or_default"))]
@@ -27,72 +21,29 @@ pub struct Run {
2721

2822
#[cfg(feature = "daemon")]
2923
#[async_trait::async_trait]
30-
impl Handler for Run {
24+
impl Handler for RunRequest {
3125
async fn handle(self) -> Result<()> {
32-
let Client { root, .. } = &self.client;
33-
34-
tracing::info!("⚙️ Running command: {}", self.config.to_string());
26+
tracing::info!("⚙️ Running: {}", self.config.to_string());
3527

3628
let state = DAEMON_STATE.clone();
37-
let ref state = state.lock().await;
38-
let device = state.devices.from_lookup(self.device);
39-
40-
let nvim = self.client.nvim(state)?;
41-
let args = {
42-
let mut args = self.config.as_args();
43-
if let Some(ref device) = device {
44-
args.extend(device.special_build_args())
45-
}
46-
append_build_root(&root, args)?
47-
};
48-
49-
let ref mut logger = nvim.logger();
29+
let ref mut state = state.lock().await;
5030

51-
logger.set_title(format!("Run:{}", self.config.target));
52-
logger.set_direction(&self.direction);
31+
// TODO: Insert runner into state.runners
32+
RunService::new(state, self).await?;
5333

54-
let settings = build_settings(&root, &args).await?;
55-
let platform = device
56-
.as_ref()
57-
.map(|d| d.platform.clone())
58-
.unwrap_or_else(|| Platform::from_display(&settings.platform_display_name).unwrap());
59-
60-
let success = build_with_loggger(logger, &root, &args, true, true).await?;
61-
if !success {
62-
let msg = format!("Failed: {} ", self.config.to_string());
63-
nvim.echo_err(&msg).await?;
64-
return Err(Error::Build(msg));
65-
}
66-
67-
// TODO(daemon): insert handler to state.runners
68-
// TODO(nvim): provide mapping to close runners.
69-
//
70-
// If there is more then one runner then pick, else close from current buffer.
71-
// C-c in normal/insert mode should close that process
72-
Runner {
73-
target: self.config.target,
74-
platform,
75-
client: self.client,
76-
args,
77-
udid: device.map(|d| d.udid.clone()),
78-
direction: self.direction,
79-
}
80-
.run(state, settings)
81-
.await?;
82-
83-
Ok(())
34+
todo!();
8435
}
8536
}
8637

8738
#[cfg(feature = "lua")]
88-
impl<'a> Requester<'a, Run> for Run {
89-
fn pre(lua: &Lua, msg: &Run) -> LuaResult<()> {
39+
impl<'a> Requester<'a, RunRequest> for RunRequest {
40+
fn pre(lua: &Lua, msg: &RunRequest) -> LuaResult<()> {
9041
lua.print(&msg.to_string());
9142
Ok(())
9243
}
9344
}
9445

95-
impl ToString for Run {
46+
impl ToString for RunRequest {
9647
fn to_string(&self) -> String {
9748
if let Some(ref name) = self.device.name {
9849
format!("run [{}] with {}", name, self.config.to_string())
@@ -103,7 +54,7 @@ impl ToString for Run {
10354
}
10455

10556
#[cfg(feature = "lua")]
106-
impl<'a> FromLua<'a> for Run {
57+
impl<'a> FromLua<'a> for RunRequest {
10758
fn from_lua(lua_value: LuaValue<'a>, _lua: &'a Lua) -> LuaResult<Self> {
10859
let table = match lua_value {
10960
LuaValue::Table(t) => Ok(t),

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub mod store;
3737
mod error;
3838

3939
#[cfg(feature = "daemon")]
40-
mod runner;
40+
mod run;
4141

4242
#[cfg(any(feature = "daemon", feature = "server"))]
4343
pub use error::{CompileError, Error, LoopError, WatchError};

src/nvim/logger.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ impl<'a> Logger<'a> {
4141
})
4242
}
4343

44+
// TODO(logger): append title
4445
pub async fn log(&mut self, msg: String) -> Result<()> {
4546
tracing::debug!("{msg}");
4647

src/run.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
mod bin;
2+
mod handler;
3+
mod meduim;
4+
mod simulator;
5+
6+
use crate::{
7+
client::Client, daemon::RunRequest, state::State, types::Device, xcode::build_with_logger,
8+
Error, Result,
9+
};
10+
use tokio::sync::MutexGuard;
11+
use xcodebuild::runner::build_settings;
12+
use {handler::RunServiceHandler, meduim::RunMedium};
13+
14+
/// Run Service
15+
pub struct RunService {
16+
pub client: Client,
17+
pub handler: RunServiceHandler,
18+
pub medium: RunMedium,
19+
}
20+
21+
impl RunService {
22+
pub async fn new(state: &mut MutexGuard<'_, State>, req: RunRequest) -> Result<Self> {
23+
let ref target = req.config.target;
24+
let ref root = req.client.root;
25+
let device = state.devices.from_lookup(req.device);
26+
let build_args = req.config.args(root, &device)?;
27+
let nvim = req.client.nvim(state)?;
28+
29+
let ref mut logger = nvim.logger();
30+
31+
logger.set_title(format!("Run:{target}"));
32+
logger.open_win().await?;
33+
logger.set_direction(&req.direction);
34+
logger.set_running().await?;
35+
36+
let build_settings = build_settings(root, &build_args).await?;
37+
let build_success = build_with_logger(logger, root, &build_args, false, false).await?;
38+
39+
if !build_success {
40+
let msg = format!("Failed: {}", req.config);
41+
nvim.echo_err(&msg).await?;
42+
return Err(Error::Build(msg));
43+
}
44+
45+
let medium = RunMedium::from_device_or_settings(device, build_settings, req.config)?;
46+
let process = medium.run(logger).await?;
47+
let handler = RunServiceHandler::new(req.client.clone(), process)?;
48+
49+
Ok(Self {
50+
client: req.client,
51+
handler,
52+
medium,
53+
})
54+
}
55+
}

0 commit comments

Comments
 (0)