Skip to content

Commit 0313b76

Browse files
committed
ref(runner): simplify api
1 parent d6e9ffb commit 0313b76

File tree

20 files changed

+291
-240
lines changed

20 files changed

+291
-240
lines changed

lua/xbase/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
---@class Device
4343
---@field info DeviceInfo
44+
---@field platform Platform
4445

4546
---@class DeviceInfo
4647
---@field availabilityError string?,

lua/xbase/util.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ local get_runners = function(platform)
44
local devices = {}
55

66
if platform then
7-
for _, device in ipairs(vim.g.xbase.devices) do
8-
if device.info.runtime_identifier:match(platform) then
7+
for _, device in pairs(vim.g.xbase.devices) do
8+
if platform == device.platform then
99
table.insert(devices, {
1010
name = device.info.name,
1111
udid = device.info.udid,
12-
platform = platform,
1312
is_on = device.info.state ~= "Shutdown",
1413
})
1514
end
1615
end
1716
end
17+
1818
return devices
1919
end
2020

src/compile.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,16 @@ pub async fn ensure_server_support<'a>(
258258
.pipe(|msg| state.clients.echo_err(&root, &name, msg))
259259
.await;
260260

261-
for (pid, client) in state.clients.iter() {
261+
for (pid, nvim) in state.clients.iter() {
262262
if pid::exists(pid, || {}) {
263-
let mut logger = client.new_logger(format!("Compile:{name}"), &None);
263+
let mut logger = nvim.logger();
264+
265+
logger.set_title(format!("Compile:{name}"));
264266
logger.set_running().await.ok();
267+
265268
logger.open_win().await.ok();
266269
logger.log(err.to_string()).await.ok();
270+
267271
logger.set_status_end(false, true).await.ok();
268272
}
269273
}

src/daemon/requests/build.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::fmt::Debug;
55
#[cfg(feature = "daemon")]
66
use {
77
crate::constants::DAEMON_STATE,
8+
crate::util::serde::value_or_default,
89
crate::xcode::{append_build_root, build_with_loggger},
910
};
1011

@@ -13,7 +14,8 @@ use {
1314
pub struct Build {
1415
pub client: Client,
1516
pub config: BuildConfiguration,
16-
pub direction: Option<BufferDirection>,
17+
#[cfg_attr(feature = "daemon", serde(deserialize_with = "value_or_default"))]
18+
pub direction: BufferDirection,
1719
}
1820

1921
#[cfg(feature = "daemon")]
@@ -31,7 +33,11 @@ impl Handler for Build {
3133

3234
let args = append_build_root(&root, config.as_args())?;
3335

34-
let ref mut logger = nvim.new_logger(format!("Build:{}", config.target), &direction);
36+
let ref mut logger = nvim.logger();
37+
38+
logger.set_title(format!("Build:{}", config.target));
39+
logger.set_direction(&direction);
40+
3541
let success = build_with_loggger(logger, &root, &args, true, true).await?;
3642

3743
if !success {
@@ -62,7 +68,7 @@ impl<'a> FromLua<'a> for Build {
6268
Ok(Self {
6369
client: table.get("client")?,
6470
config: table.get("config")?,
65-
direction: table.get("direction").ok(),
71+
direction: table.get("direction").unwrap_or_default(),
6672
})
6773
}
6874
}

src/daemon/requests/run.rs

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,62 @@
11
use {
2-
super::*,
3-
crate::nvim::BufferDirection,
4-
crate::types::{BuildConfiguration, Platform},
2+
super::*, crate::nvim::BufferDirection, crate::store::DeviceLookup,
3+
crate::types::BuildConfiguration,
54
};
65

7-
#[derive(Debug, Serialize, Deserialize)]
8-
pub struct DeviceLookup {
9-
name: String,
10-
udid: String,
11-
platform: Platform,
12-
}
13-
14-
/// Run a project.
15-
#[derive(Debug, Serialize, Deserialize)]
16-
pub struct Run {
17-
pub client: Client,
18-
pub config: BuildConfiguration,
19-
pub device: Option<DeviceLookup>,
20-
pub direction: Option<BufferDirection>,
21-
}
22-
236
#[cfg(feature = "daemon")]
247
use {
258
crate::constants::DAEMON_STATE,
269
crate::runner::Runner,
10+
crate::types::Platform,
11+
crate::util::serde::value_or_default,
2712
crate::xcode::{append_build_root, build_with_loggger},
2813
crate::Error,
2914
xcodebuild::runner::build_settings,
3015
};
3116

17+
/// Run a project.
18+
#[derive(Debug, Serialize, Deserialize)]
19+
pub struct Run {
20+
pub client: Client,
21+
pub config: BuildConfiguration,
22+
#[cfg_attr(feature = "daemon", serde(deserialize_with = "value_or_default"))]
23+
pub device: DeviceLookup,
24+
#[cfg_attr(feature = "daemon", serde(deserialize_with = "value_or_default"))]
25+
pub direction: BufferDirection,
26+
}
27+
3228
#[cfg(feature = "daemon")]
3329
#[async_trait::async_trait]
3430
impl Handler for Run {
3531
async fn handle(self) -> Result<()> {
3632
let Client { pid, root, .. } = &self.client;
33+
tracing::info!("{:#?}", self);
3734

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

4037
let state = DAEMON_STATE.clone().lock_owned().await;
41-
let direction = self.direction.clone();
42-
let platform = if let Some(ref d) = self.device {
43-
tracing::info!("{:#?}", d.platform);
44-
Some(d.platform.clone())
45-
} else {
46-
None
47-
};
38+
let device = state.devices.from_lookup(self.device);
39+
tracing::info!("{:#?}", device);
4840

4941
let nvim = state.clients.get(&pid)?;
5042
let args = {
5143
let mut args = self.config.as_args();
52-
if let Some(ref platform) = platform {
53-
args.extend(platform.sdk_simulator_args())
44+
if let Some(ref device) = device {
45+
args.extend(device.special_build_args())
5446
}
5547
append_build_root(&root, args)?
5648
};
5749

58-
let ref mut logger = nvim.new_logger(format!("Run:{}", self.config.target), &direction);
50+
let ref mut logger = nvim.logger();
51+
52+
logger.set_title(format!("Run:{}", self.config.target));
53+
logger.set_direction(&self.direction);
5954

6055
let settings = build_settings(&root, &args).await?;
61-
let platform = match platform {
62-
Some(v) => v,
63-
None => Platform::from_display(&settings.platform_display_name)?,
64-
};
56+
let platform = device
57+
.as_ref()
58+
.map(|d| d.platform.clone())
59+
.unwrap_or_else(|| Platform::from_display(&settings.platform_display_name).unwrap());
6560

6661
let success = build_with_loggger(logger, &root, &args, true, true).await?;
6762
if !success {
@@ -81,8 +76,8 @@ impl Handler for Run {
8176
client: self.client,
8277
state,
8378
args,
84-
udid: self.device.map(|d| d.udid),
85-
direction,
79+
udid: device.map(|d| d.udid.clone()),
80+
direction: self.direction,
8681
}
8782
.run(settings)
8883
.await?;
@@ -101,8 +96,8 @@ impl<'a> Requester<'a, Run> for Run {
10196

10297
impl ToString for Run {
10398
fn to_string(&self) -> String {
104-
if let Some(ref device) = self.device {
105-
format!("run [{}] with {}", device.name, self.config.to_string())
99+
if let Some(ref name) = self.device.name {
100+
format!("run [{}] with {}", name, self.config.to_string())
106101
} else {
107102
format!("run with {}", self.config.to_string())
108103
}
@@ -122,19 +117,15 @@ impl<'a> FromLua<'a> for Run {
122117
Ok(Self {
123118
client: table.get("client")?,
124119
config: table.get("config")?,
125-
direction: table.get("direction").ok(),
120+
direction: table.get("direction").unwrap_or_default(),
126121
device: device
127122
.map(|d| {
128123
let name = d.get("name").ok()?;
129124
let udid = d.get("udid").ok()?;
130-
let platform = d.get("platform").ok()?;
131-
Some(DeviceLookup {
132-
name,
133-
udid,
134-
platform,
135-
})
125+
Some(DeviceLookup { name, udid })
136126
})
137-
.flatten(),
127+
.flatten()
128+
.unwrap_or_default(),
138129
})
139130
}
140131
}

src/nvim.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
77

88
#[cfg(feature = "daemon")]
99
use {
10-
crate::{types::Client, util::fmt, Result},
10+
crate::{types::Client, Result},
1111
nvim_rs::{compat::tokio::Compat, create::tokio::new_path as connect, rpc::handler::Dummy},
1212
};
1313

@@ -57,18 +57,6 @@ impl NvimClient {
5757
self.exec_lua(update_state_script, vec![]).await?;
5858
Ok(())
5959
}
60-
61-
pub fn new_logger<'a>(
62-
&'a self,
63-
title: String,
64-
direction: &'a Option<BufferDirection>,
65-
) -> Logger<'a> {
66-
Logger::new(self, fmt::as_section(title), direction.clone())
67-
}
68-
69-
pub fn new_unamed_logger<'a>(&'a self) -> Logger<'a> {
70-
Logger::new(self, "".into(), None)
71-
}
7260
}
7361

7462
#[cfg(feature = "daemon")]

src/nvim/buffer.rs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
#[cfg(feature = "daemon")]
2-
use super::NvimClient;
3-
#[cfg(feature = "daemon")]
4-
use crate::Result;
51
use serde::{Deserialize, Serialize};
62

7-
#[derive(Clone, Debug, strum::EnumString, Serialize, Deserialize)]
3+
#[derive(Default, Clone, Debug, strum::EnumString, Serialize, Deserialize)]
84
#[strum(ascii_case_insensitive)]
95
pub enum BufferDirection {
6+
#[default]
107
Default,
118
Vertical,
129
Horizontal,
@@ -24,35 +21,6 @@ impl BufferDirection {
2421
BufferDirection::TabEdit => format!("tabe {bufnr}"),
2522
}
2623
}
27-
28-
pub async fn get_window_direction(
29-
nvim: &NvimClient,
30-
direction: Option<BufferDirection>,
31-
bufnr: i64,
32-
) -> Result<String> {
33-
use std::str::FromStr;
34-
use tap::Pipe;
35-
36-
if let Some(direction) = direction {
37-
return Ok(direction.to_nvim_command(bufnr));
38-
};
39-
40-
match "return require'xbase.config'.values.default_log_buffer_direction"
41-
.pipe(|str| nvim.exec_lua(str, vec![]))
42-
.await?
43-
.as_str()
44-
.ok_or_else(|| anyhow::anyhow!("Unable to covnert value to string"))?
45-
.pipe(Self::from_str)
46-
.map(|d| d.to_nvim_command(bufnr))
47-
{
48-
Ok(open_command) => open_command,
49-
Err(e) => {
50-
tracing::error!("Unable to convert value to string {e}");
51-
Self::Horizontal.to_nvim_command(bufnr)
52-
}
53-
}
54-
.pipe(Ok)
55-
}
5624
}
5725

5826
#[cfg(feature = "lua")]

0 commit comments

Comments
 (0)