-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmod.rs
More file actions
176 lines (149 loc) · 4.94 KB
/
mod.rs
File metadata and controls
176 lines (149 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
mod bin;
mod device;
mod simulator;
use crate::*;
use async_trait::async_trait;
use process_stream::{Process, ProcessExt, StreamExt};
use std::sync::Arc;
use std::sync::Weak;
use tokio::task::JoinHandle;
pub use {bin::*, device::*, simulator::*};
/// Run Service
pub struct RunService {
pub key: String,
pub root: PathBuf,
pub handler: Arc<Mutex<Option<RunHandler>>>,
pub settings: BuildSettings,
pub device: Option<Device>,
}
impl RunService {
pub fn new(
device: Option<Device>,
root: PathBuf,
settings: BuildSettings,
key: String,
) -> Self {
Self {
key,
root,
handler: Arc::new(Mutex::new(None)),
settings,
device,
}
}
}
impl std::fmt::Display for RunService {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.key)
}
}
#[async_trait::async_trait]
impl Watchable for RunService {
async fn trigger(
&self,
project: &mut ProjectImpl,
_event: &Event,
broadcast: &Arc<Broadcast>,
) -> Result<()> {
let Self { settings, .. } = self;
let mut handler = self.handler.clone().lock_owned().await;
handler.take().map(|v| {
v.process().abort();
v.inner().abort();
});
let device = self.device.as_ref();
let target = &settings.target;
let (runner, _args, mut recv) = project.get_runner(&settings, device, broadcast)?;
if !recv.recv().await.unwrap_or_default() {
return Err(crate::Error::Run(format!("{target} build failed")));
}
let task = Task::new(TaskKind::Run, target, broadcast.clone());
let runner = runner.run(&task).await?;
let broadcast = Arc::downgrade(broadcast);
*handler = Some(RunHandler::new(target, runner, broadcast)?);
Ok(())
}
/// A function that controls whether a a Watchable should restart
async fn should_trigger(&self, event: &Event) -> bool {
event.is_any_but_not_seen()
}
/// A function that controls whether a watchable should be droped
async fn should_discard(&self, _event: &Event) -> bool {
false
}
/// Drop watchable for watching a given file system
async fn discard(&self) {
self.handler.clone().lock_owned().await.take().map(|v| {
v.process().abort();
v.inner().abort();
});
}
}
/// Run Service Task Handler
pub struct RunHandler {
process: Process,
inner: JoinHandle<Result<()>>,
}
impl RunHandler {
// Change the status of the process to running
pub fn new(target: &String, mut process: Process, broadcast: Weak<Broadcast>) -> Result<Self> {
let target = target.clone();
let mut stream = process.spawn_and_stream()?;
let abort = process.aborter().unwrap();
let inner: _ = tokio::spawn(async move {
// TODO: find a better way to close this!
//
// Right now it just wait till the user try print something
while let Some(output) = stream.next().await {
let ref mut broadcast = match broadcast.upgrade() {
Some(broadcast) => broadcast,
None => {
tracing::warn!("No client instance listening, closing runner ..");
abort.notify_waiters();
break;
}
};
use process_stream::ProcessItem::*;
match output {
Output(msg) => {
if !msg.contains("ignoring singular matrix") {
broadcast.log_info(msg);
}
}
Error(msg) => {
broadcast.log_error(msg);
}
// TODO: this should be skipped when user re-run the app
Exit(code) => {
let success = &code == "0";
if success {
broadcast.log_info("Device Disconnected");
} else {
broadcast.log_error("Device Disconnected");
}
broadcast.finish_current_task(success);
tracing::info!("[{target}] Runner Closed");
break;
}
};
}
drop(stream);
Ok(())
});
Ok(Self { process, inner })
}
/// Get a reference to the run service handler's process.
#[must_use]
pub fn process(&self) -> &Process {
&self.process
}
/// Get a reference to the run service handler's handler.
#[must_use]
pub fn inner(&self) -> &JoinHandle<Result<()>> {
&self.inner
}
}
#[async_trait]
pub trait Runner {
async fn run<'a>(&self, task: &Task) -> Result<Process>;
}