11#[ cfg( feature = "mlua" ) ]
22use crate :: daemon:: Daemon ;
33
4+ #[ cfg( feature = "daemon" ) ]
5+ use crate :: daemon:: nvim:: WindowType ;
6+
7+ #[ cfg( feature = "daemon" ) ]
8+ use async_stream:: stream;
9+
10+ #[ cfg( feature = "daemon" ) ]
11+ use tokio_stream:: StreamExt ;
12+
13+ #[ cfg( feature = "daemon" ) ]
14+ use tap:: Pipe ;
15+
416#[ cfg( feature = "daemon" ) ]
517use crate :: daemon:: { DaemonRequestHandler , DaemonState } ;
618
@@ -10,6 +22,8 @@ use anyhow::Result;
1022/// Build a project.
1123#[ derive( Debug ) ]
1224pub struct Build {
25+ pub pid : i32 ,
26+ pub root : String ,
1327 pub target : Option < String > ,
1428 pub configuration : Option < String > ,
1529 pub scheme : Option < String > ,
@@ -27,29 +41,89 @@ impl Build {
2741#[ cfg( feature = "daemon" ) ]
2842#[ async_trait:: async_trait]
2943impl DaemonRequestHandler < Build > for Build {
30- fn parse ( _args : Vec < & str > ) -> Result < Self > {
31- Ok ( Self {
32- target : None ,
33- configuration : None ,
34- scheme : None ,
35- } )
44+ fn parse ( args : Vec < & str > ) -> Result < Self > {
45+ if let ( Some ( pid) , Some ( root) ) = ( args. get ( 0 ) , args. get ( 1 ) ) {
46+ Ok ( Self {
47+ pid : pid. parse :: < i32 > ( ) ?,
48+ root : root. to_string ( ) ,
49+ target : args. get ( 2 ) . map ( ToString :: to_string) ,
50+ configuration : args. get ( 3 ) . map ( ToString :: to_string) ,
51+ scheme : args. get ( 4 ) . map ( ToString :: to_string) ,
52+ } )
53+ } else {
54+ anyhow:: bail!( "Missing arugments: {:?}" , args)
55+ }
3656 }
3757
38- async fn handle ( & self , _state : DaemonState ) -> Result < ( ) > {
39- tracing:: info!( "build command" ) ;
58+ async fn handle ( & self , state : DaemonState ) -> Result < ( ) > {
59+ tracing:: debug!( "Handling build request.." ) ;
60+ let state = state. lock ( ) . await ;
61+ let ws = match state. workspaces . get ( & self . root ) {
62+ Some ( ws) => ws,
63+ None => anyhow:: bail!( "No workspace for {}" , self . root) ,
64+ } ;
65+
66+ ws. project
67+ . xcodebuild ( & [ "build" ] )
68+ . await ?
69+ . pipe ( |mut logs| {
70+ stream ! {
71+ while let Some ( step) = logs. next( ) . await {
72+ let line = match step {
73+ xcodebuild:: parser:: Step :: Exit ( _) => { continue ; }
74+ step => step. to_string( ) . trim( ) . to_string( ) ,
75+ } ;
76+
77+ if !line. is_empty( ) {
78+ for line in line. split( "\n " ) {
79+ yield line. to_string( ) ;
80+ }
81+ }
82+ }
83+ }
84+ } )
85+ . pipe ( Box :: pin)
86+ . pipe ( |stream| async {
87+ let nvim = match ws. clients . get ( & self . pid ) {
88+ Some ( nvim) => nvim,
89+ None => anyhow:: bail!( "No nvim client found to build project." ) ,
90+ } ;
91+ nvim. log_to_buffer ( "Build" , WindowType :: Vertical , stream, true )
92+ . await
93+ } )
94+ . await ?;
95+
4096 Ok ( ( ) )
4197 }
4298}
4399
44100#[ cfg( feature = "lua" ) ]
45101impl Build {
46- pub fn lua ( lua : & mlua:: Lua , ( t, c, s) : ( String , String , String ) ) -> mlua:: Result < ( ) > {
102+ pub fn lua (
103+ lua : & mlua:: Lua ,
104+ ( pid, root, t, c, s) : ( i32 , String , Option < String > , Option < String > , Option < String > ) ,
105+ ) -> mlua:: Result < ( ) > {
47106 use crate :: util:: mlua:: LuaExtension ;
48- lua. trace ( format ! ( "Build (target: {t} configuration: {c}, scheme: {s})" ) . as_ref ( ) ) ?;
49- Self :: request ( & t, & c, & s) . map_err ( mlua:: Error :: external)
50- }
107+ lua. trace (
108+ format ! (
109+ "Build (target: {:?} configuration: {:?}, scheme: {:?})" ,
110+ t, c, s
111+ )
112+ . as_ref ( ) ,
113+ ) ?;
114+
115+ let mut args = vec ! [ "build" . into( ) , pid. to_string( ) , root] ;
116+
117+ if let Some ( target) = t {
118+ args. push ( target)
119+ }
120+ if let Some ( configuration) = c {
121+ args. push ( configuration)
122+ }
123+ if let Some ( scheme) = s {
124+ args. push ( scheme)
125+ }
51126
52- pub fn request ( target : & str , configuration : & str , scheme : & str ) -> mlua:: Result < ( ) > {
53- Daemon :: execute ( & [ "build" , target, configuration, scheme] )
127+ Daemon :: execute ( & args. join ( " " ) . split ( " " ) . collect :: < Vec < & str > > ( ) )
54128 }
55129}
0 commit comments