Skip to content

Commit

Permalink
fix(build): remove pseudocode
Browse files Browse the repository at this point in the history
  • Loading branch information
kkharji committed May 15, 2022
1 parent 33e2528 commit 31abafa
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 72 deletions.
6 changes: 2 additions & 4 deletions lua/xbase/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ config.values = _XBASECONFIG

---@class xbaseOptions
local defaults = {
--- Log level. Set to error to ignore everything
--- { "trace", "debug", "info", "warn", "error" }
--- Log level. Set to error to ignore everything: { "trace", "debug", "info", "warn", "error" }
log_level = "debug",
--- Default log buffer direction.
--- { "horizontal", "vertical", "float" }
--- Default log buffer direction: { "horizontal", "vertical", "float" }
default_log_buffer_direction = "horizontal",
--- Statusline provider configurations
statusline = {
Expand Down
8 changes: 3 additions & 5 deletions lua/xbase/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ M.try_register = function(root, opts)
opts = opts or {}
if M.should_register(root, opts) then
M.register()
vim.cmd [[ autocmd VimLeavePre * lua require'xbase'.drop()]]
vim.cmd [[ autocmd VimLeavePre * lua require'xbase'.drop(true)]]
end
end

M.drop = function()
lib.drop {
remove_client = true,
}
M.drop = function(remove_client)
lib.drop { remove_client = remove_client }
end

M.build = function(opts)
Expand Down
8 changes: 5 additions & 3 deletions lua/xbase/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ M.build_run = function(command, opts)

results[#results + 1] = {
command = command,
target = target,
configuration = configuration,
value = display,
device = nil, -- reserverd later for run command
config = {
target = target,
configuration = configuration,
device = nil, -- reserverd later for run command
},
}
end
end
Expand Down
40 changes: 20 additions & 20 deletions src/daemon/requests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{nvim::BufferDirection, types::BuildConfiguration};
use std::fmt::Debug;

#[cfg(feature = "daemon")]
use crate::{constants::DAEMON_STATE, nvim::WatchLogger, xcode::stream};
use crate::{constants::DAEMON_STATE, nvim::WatchLogger, xcode::stream_build};

/// Build a project.
#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -20,21 +20,18 @@ impl Handler for Build {
let Self { client, config, .. } = &self;
let Client { pid, root } = client;

tracing::debug!("Handling build request..");
tracing::debug!("Handling build request {:#?}", self.config);

let state = DAEMON_STATE.lock().await;
let nvim = state
.clients
.get(pid)
.ok_or_else(|| anyhow::anyhow!("no client found with {}", self.client.pid))?;

let direction = self.direction.clone();

WatchLogger::new(nvim, "Build", &config)
.log_stream(
stream(&root, vec!["build".to_string()], &config).await?,
None,
true,
false,
)
.log_stream(stream_build(&root, &config).await?, direction, true, true)
.await?;

Ok(())
Expand All @@ -53,18 +50,21 @@ impl<'a> Requester<'a, Build> for Build {
impl<'a> FromLua<'a> for Build {
fn from_lua(lua_value: LuaValue<'a>, _lua: &'a Lua) -> LuaResult<Self> {
use std::str::FromStr;
if let LuaValue::Table(table) = lua_value {
let mut direction = None;
if let Some(str) = table.get::<_, Option<String>>("direction")? {
direction = BufferDirection::from_str(&str).ok();
}
Ok(Self {
client: table.get("client")?,
config: table.get("config")?,
direction,
})
} else {
Err(LuaError::external("Fail to deserialize Build"))
let mut direction = None;

let table = match lua_value {
LuaValue::Table(table) => table,
_ => return Err(LuaError::external("Fail to deserialize Build")),
};

if let Some(str) = table.get::<_, Option<String>>("direction")? {
direction = BufferDirection::from_str(&str).ok();
}

Ok(Self {
client: table.get("client")?,
config: table.get("config")?,
direction,
})
}
}
2 changes: 1 addition & 1 deletion src/nvim/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::NvimClient;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

#[derive(Debug, strum::EnumString, Serialize, Deserialize)]
#[derive(Clone, Debug, strum::EnumString, Serialize, Deserialize)]
#[strum(ascii_case_insensitive)]
pub enum BufferDirection {
Float,
Expand Down
62 changes: 31 additions & 31 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,59 @@ pub use project::*;

pub type Root = PathBuf;

#[derive(Clone, Debug, Serialize, Deserialize, strum::Display, strum::EnumString)]
// #[serde(untagged)]
pub enum XConfiguration {
Debug,
Release,
Custom(String),
}

/// Fields required to build a project
#[derive(Clone, Default, Debug, Serialize, Deserialize, Hash)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BuildConfiguration {
/// TODO(nvim): make build config sysroot default to tmp in auto-build
pub sysroot: Option<String>,
/// Target to build
pub target: String,
/// Configuration to build with, default Debug
#[serde(default)]
pub configuration: XConfiguration,
/// Scheme to build with
pub scheme: Option<String>,
}

impl BuildConfiguration {
pub fn as_args(&self) -> Vec<String> {
let mut args = self
.to_string()
.split_whitespace()
.map(ToString::to_string)
.collect::<Vec<String>>();

args.remove(0);
args.insert(0, "build".to_string());
args
}
}

#[cfg(feature = "lua")]
impl<'a> FromLua<'a> for BuildConfiguration {
fn from_lua(lua_value: LuaValue<'a>, _lua: &'a Lua) -> LuaResult<Self> {
use std::str::FromStr;
use tap::Pipe;

if let LuaValue::Table(table) = lua_value {
Ok(Self {
sysroot: table.get("sysroot")?,
target: table.get("target")?,
configuration: table.get("configuration")?,
configuration: table
.get::<_, String>("configuration")?
.pipe(|s| XConfiguration::from_str(&s))
.to_lua_err()?,
scheme: table.get("scheme")?,
})
} else {
Ok(BuildConfiguration::default())
Err(LuaError::external("Fail to deserialize BuildConfiguration"))
}
}
}
Expand All @@ -57,30 +84,3 @@ impl Display for BuildConfiguration {
Ok(())
}
}

#[derive(Hash, Clone, Debug, Default, Serialize, Deserialize, strum::Display)]
#[serde(untagged)]
pub enum XConfiguration {
#[default]
Debug,
Release,
Custom(String),
}

#[cfg(feature = "lua")]
impl<'a> FromLua<'a> for XConfiguration {
fn from_lua(lua_value: LuaValue<'a>, _lua: &'a Lua) -> LuaResult<Self> {
if let LuaValue::String(config) = lua_value {
let value = config.to_str()?;
Ok(match value {
"debug" | "Debug" => Self::Debug,
"release" | "Release" => Self::Release,
_ => Self::Custom(value.to_string()),
})
} else if matches!(lua_value, LuaValue::Nil) {
Ok(Self::Debug)
} else {
Err(LuaError::external("Expected a table got XConfiguration"))
}
}
}
2 changes: 1 addition & 1 deletion src/watcher/handle/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub async fn create(req: WatchArguments) -> Result<(), WatchError> {
.ok_or_else(|| WatchError::Stop("Fail to find nvim instance with given pid".to_string()))?;

let stream = match kind {
WatchKind::Build => xcode::stream(&root, &["build"], &config)
WatchKind::Build => xcode::stream_build(&root, &config)
.await
.map_err(WatchError::r#continue)?,

Expand Down
10 changes: 3 additions & 7 deletions src/xcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ use xcodebuild::{
};

#[cfg(feature = "daemon")]
pub async fn stream<'a, I: 'a, S: 'a, P: 'a>(
pub async fn stream_build<'a, P: 'a>(
root: P,
args: I,
_config: &BuildConfiguration,
config: &BuildConfiguration,
) -> Result<impl Stream<Item = String> + 'a>
where
I: IntoIterator<Item = S>,
S: AsRef<std::ffi::OsStr>,
P: AsRef<Path>,
{
// TOOD: Process configuration
let mut stream = spawn(root, args).await?;
let mut stream = spawn(root, config.as_args()).await?;

Ok(Box::pin(stream! {
use xcodebuild::parser::Step::*;
Expand Down

0 comments on commit 31abafa

Please sign in to comment.