Skip to content

Commit d5b4ea1

Browse files
committed
ref: switch to tarpc framework
1 parent 7380150 commit d5b4ea1

File tree

27 files changed

+1185
-516
lines changed

27 files changed

+1185
-516
lines changed

Cargo.lock

Lines changed: 461 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

daemon/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2021"
77

88
# Internal
99
log = { path = "./../log/" }
10-
xbase-proto = { path = "./../proto/" }
10+
xbase-proto = { path = "./../proto/", features = ["server"] }
1111

1212
# Error Handling
1313
anyhow = { version = "^1.0.58" }
@@ -20,9 +20,8 @@ serde_yaml = { version = "0.8.23" }
2020
strum = { version = "0.24.0", features = ["derive"] }
2121

2222
# Async Runtime Feature
23-
tokio = { version = "1.17.0", features = ["full"] }
23+
tokio = { version = "1.19.2", features = ["full"] }
2424
futures = { version = "0.3.21" }
25-
tokio-util = { version = "0.7.1", features = ["codec"] }
2625
async-trait = { version = "0.1.52" }
2726
async-stream = { version = "0.3.3" }
2827
parity-tokio-ipc = { version = "0.9.0" }
@@ -43,7 +42,7 @@ nvim-rs = { version = "0.4.0", features = ["use_tokio"] }
4342
# Other
4443
tap = "1.0.1"
4544
xclog = { version = "0.2.*", features = ["with_tracing"] }
46-
simctl = { git = "https://github.com/tami5/simctl" }
45+
simctl = { git = "https://github.com/xbase-lab/simctl" }
4746
derive-deref-rs = { version = "0.1.1"}
4847
shell-words = { version = "1.1.0" }
4948

@@ -53,3 +52,4 @@ libproc = { version = "0.12.0" }
5352
paste = "1.0.7"
5453
which = "4.2.5"
5554
erased-serde = "0.3.21"
55+
tarpc = { version = "0.29.0", features = ["serde-transport", "tokio1", "serde1"] }

daemon/src/error.rs

Lines changed: 0 additions & 155 deletions
This file was deleted.

daemon/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ pub mod compile;
33
pub mod constants;
44
pub mod device;
55
pub mod drop;
6-
pub mod error;
76
pub mod nvim;
87
pub mod project;
98
pub mod register;
@@ -13,11 +12,10 @@ pub mod store;
1312
pub mod util;
1413
pub mod watch;
1514

16-
pub use error::{CompileError, Error, LoopError};
1715
use process_stream::{ProcessItem, Stream};
1816
use std::pin::Pin;
1917

20-
pub type Result<T> = std::result::Result<T, Error>;
18+
pub use xbase_proto::{Error, IntoResult, Result};
2119
pub type OutputStream = Pin<Box<dyn Stream<Item = ProcessItem> + Send>>;
2220
pub type StringStream = Pin<Box<dyn Stream<Item = String> + Send>>;
2321

daemon/src/main.rs

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,69 @@
11
use log::Level;
2+
use std::path::PathBuf;
23
use tap::Pipe;
34
use tokio::fs::{metadata, read_to_string, remove_file, write};
4-
use tokio::io::AsyncReadExt;
55
use tokio::net::UnixListener;
66
use xbase::util::pid;
7-
use xbase::Result;
87
use xbase::{constants::*, RequestHandler};
9-
use xbase_proto::{Message, Request};
8+
use xbase_proto::*;
109

10+
#[derive(Clone)]
11+
struct Server;
12+
13+
#[tarpc::server]
14+
impl xbase_proto::XBase for Server {
15+
/// Register project root with a path to setup logs
16+
async fn register(self, _: Context, req: RegisterRequest) -> Result<PathBuf> {
17+
req.handle().await?;
18+
Ok("/bin/cp".into())
19+
}
20+
/// Build Project and get path to where to build log will be located
21+
async fn build(self, _: Context, req: BuildRequest) -> Result<PathBuf> {
22+
// NOTE: Required because of nvim-rs
23+
tokio::spawn(async { req.handle().await });
24+
Ok(PathBuf::default())
25+
}
26+
/// Run Project and get path to where to Runtime log will be located
27+
async fn run(self, _: Context, req: RunRequest) -> Result<PathBuf> {
28+
// NOTE: Required because of nvim-rs
29+
tokio::spawn(async { req.handle().await });
30+
Ok(PathBuf::default())
31+
}
32+
/// Drop project root
33+
async fn drop(self, _: Context, req: DropRequest) -> Result<()> {
34+
// NOTE: Required because of nvim-rs
35+
tokio::spawn(async { req.handle().await });
36+
Ok(())
37+
}
38+
}
1139
#[tokio::main]
12-
async fn main() -> Result<()> {
40+
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
1341
ensure_single_instance().await?;
1442

1543
let listener = UnixListener::bind(DAEMON_SOCKET_PATH).unwrap();
16-
44+
let codec_builder = LengthDelimitedCodec::builder();
1745
log::setup("/tmp", "xbase-daemon.log", Level::DEBUG, true)?;
18-
1946
log::info!("Started");
2047

2148
loop {
22-
if let Ok((mut s, _)) = listener.accept().await {
49+
if let Ok((s, _)) = listener.accept().await {
2350
tokio::spawn(async move {
24-
let msg = {
25-
let mut msg = String::default();
26-
if let Err(e) = s.read_to_string(&mut msg).await {
27-
return log::error!("[Read Error]: {:?}", e);
28-
};
29-
msg
30-
};
31-
32-
if msg.is_empty() {
33-
return;
34-
}
35-
36-
let req = match Request::read(msg.clone()) {
37-
Err(e) => {
38-
return log::error!("[Parse Error]: {:?} message: {msg}", e);
39-
}
40-
Ok(req) => req,
41-
};
42-
43-
if let Err(e) = handle(req).await {
44-
return log::error!("[Failure]: Cause: ({:?})", e);
45-
};
51+
let framed = codec_builder.new_framed(s);
52+
let transport = transport::new(framed, Json::default());
53+
BaseChannel::with_defaults(transport)
54+
.execute(Server.serve())
55+
.await;
4656

4757
let state = DAEMON_STATE.clone();
4858
let mut state = state.lock().await;
4959
state.validate().await;
50-
51-
// update_watchers(state.clone()).await;
5260
});
5361
} else {
5462
log::error!("Fail to accept a connection")
5563
};
5664
}
5765
}
5866

59-
async fn handle(req: Request) -> Result<()> {
60-
match req.message {
61-
Message::Build(c) => RequestHandler::handle(c).await,
62-
Message::Run(c) => RequestHandler::handle(c).await,
63-
Message::Register(c) => RequestHandler::handle(c).await,
64-
Message::Drop(c) => RequestHandler::handle(c).await,
65-
}
66-
}
67-
6867
async fn ensure_single_instance() -> Result<()> {
6968
if metadata(DAEMON_SOCKET_PATH).await.ok().is_some() {
7069
remove_file(DAEMON_SOCKET_PATH).await.ok();

daemon/src/project/barebone.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl Project for BareboneProject {
118118
}
119119

120120
if xcodeproj_paths.is_empty() {
121-
return Err(Error::ProjectError("No XcodeProjectFound!".into()));
121+
return Err(Error::DefinitionLocating);
122122
};
123123

124124
project.xcodeproj = XCodeProject::new(&xcodeproj_paths[0])?;

daemon/src/project/swift.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,7 @@ impl ProjectGenerate for SwiftProject {
140140
let (success, logs) = consume_and_log(process.spawn_and_stream()?.boxed()).await;
141141

142142
if !success {
143-
return Err(Error::SwiftBuildForComplilation(
144-
self.name().into(),
145-
logs.join("\n"),
146-
));
143+
return Err(Error::Generate(logs.join("\n")));
147144
}
148145

149146
self.update_project_info().await?;
@@ -202,14 +199,15 @@ impl SwiftProject {
202199
.await?;
203200

204201
let map = if output.status.success() {
205-
serde_json::from_slice::<Map<String, Value>>(&output.stdout)?
202+
serde_json::from_slice::<Map<String, Value>>(&output.stdout)
203+
.map_err(|e| Error::DefinitionParsing(e.to_string()))?
206204
} else {
207205
let error = String::from_utf8(output.stderr)
208206
.unwrap_or_default()
209207
.split("\n")
210208
.collect();
211209
log::error!("Fail to read swift package information {error}");
212-
return Err(Error::SwiftPackageRead(self.name().into(), error));
210+
return Err(Error::DefinitionParsing(error));
213211
};
214212

215213
// TODO(swift-package): only provide run service for executables

daemon/src/project/tuist.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,7 @@ impl TuistProject {
183183

184184
let (success, logs) = consume_and_log(Box::pin(process.spawn_and_stream()?)).await;
185185
if !success {
186-
return Err(Error::XCodeProjectGenerate(
187-
"Project".into(),
188-
logs.join("\n"),
189-
));
186+
return Err(Error::Generate(logs.join("\n")));
190187
}
191188

192189
Ok(())

daemon/src/project/xcodegen.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@ impl ProjectGenerate for XCodeGenProject {
108108
self.xcodeproj = XCodeProject::new(&xcodeproj_paths[0])?;
109109
self.targets = self.xcodeproj.targets_platform();
110110
} else {
111-
return Err(Error::XCodeProjectGenerate(
112-
self.name().into(),
113-
logs.join("\n"),
114-
));
111+
return Err(Error::Generate(logs.join("\n")));
115112
}
116113

117114
Ok(())

0 commit comments

Comments
 (0)