-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtuist.rs
305 lines (254 loc) · 9.48 KB
/
tuist.rs
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use super::*;
use crate::util::fs::which;
use crate::watcher::Event;
use crate::{Error, Result};
use futures::future::try_join_all;
use futures::StreamExt;
use process_stream::{Process, ProcessExt};
use serde::Serialize;
use std::{collections::HashMap, path::PathBuf};
use tap::Pipe;
use xcodeproj::XCodeProject;
#[derive(Debug, Serialize, Default)]
#[serde(default)]
pub struct TuistProject {
root: PathBuf,
targets: HashMap<String, TargetInfo>,
num_clients: i32,
watchignore: Vec<String>,
#[serde(skip)]
xcodeproj: XCodeProject,
#[serde(skip)]
xcodeproj_path: PathBuf,
#[serde(skip)]
manifest: XCodeProject,
#[serde(skip)]
manifest_path: PathBuf,
#[serde(skip)]
manifest_files: Vec<String>,
}
impl ProjectData for TuistProject {
fn root(&self) -> &PathBuf {
&self.root
}
fn name(&self) -> &str {
&self.xcodeproj.name()
}
fn targets(&self) -> &HashMap<String, TargetInfo> {
&self.targets
}
fn clients(&self) -> &i32 {
&self.num_clients
}
fn clients_mut(&mut self) -> &mut i32 {
&mut self.num_clients
}
fn watchignore(&self) -> &Vec<String> {
&self.watchignore
}
}
#[async_trait::async_trait]
impl ProjectCompile for TuistProject {
async fn update_compile_database(&self, broadcast: &Arc<Broadcast>) -> Result<()> {
use xclog::XCCompileCommand as C;
let name = self.name();
let root = self.root();
let cache_root = self.build_cache_root()?;
let args = self.compile_arguments();
let mut tasks_recvs = vec![];
let mut xccommands: Vec<Arc<Mutex<Vec<C>>>> = vec![];
let task = Task::new(TaskKind::Compile, self.name(), broadcast.clone());
{
// Compile manifests
let mut args = args.clone();
args.extend_from_slice(&[
format!("SYMROOT={cache_root}_tuist"),
"-workspace".into(),
"Manifests.xcworkspace".into(),
"-scheme".into(),
"Manifests".into(),
]);
let xclogger = XCLogger::new(&root, &args)?;
xccommands.push(xclogger.compile_commands.clone());
tasks_recvs.push(task.consume(Box::new(xclogger))?);
let argsstr = args.join(" ");
tracing::info!("Building Manifest ...");
tracing::trace!("\n\n xcodebuild {argsstr}\n\n");
task.debug(format!("[{name}] {argsstr}"));
}
for scheme in self.xcodeproj.schemes().into_iter() {
let mut args = args.clone();
args.extend_from_slice(&[
format!("SYMROOT={cache_root}"),
"-workspace".into(),
format!("{name}.xcworkspace"),
"-scheme".into(),
scheme.name.clone(),
]);
let xclogger = XCLogger::new(&root, &args)?;
xccommands.push(xclogger.compile_commands.clone());
tasks_recvs.push(task.consume(Box::new(xclogger))?);
let argsstr = args.join(" ");
tracing::info!("Building {} ...", scheme.name);
tracing::trace!("\n\n xcodebuild {argsstr}\n\n");
task.debug(format!("[{name}] {argsstr}"));
}
let _all_pass = tasks_recvs
.into_iter()
.map(|mut t| tokio::spawn(async move { t.recv().await.unwrap_or_default() }))
.pipe(try_join_all)
.await
.unwrap_or_default()
.into_iter()
.all(|f| f);
let mut xccommands = xccommands
.into_iter()
.map(|l| tokio::spawn(async move { l.lock().await.to_vec() }))
.pipe(try_join_all)
.await
.unwrap_or_default()
.into_iter()
.flatten()
.collect::<Vec<_>>();
xccommands.dedup();
if xccommands.is_empty() {
broadcast.warn("No compile command was generated!");
}
let json = serde_json::to_vec_pretty(&xccommands)?;
tokio::fs::write(root.join(".compile"), &json).await?;
Ok(())
}
}
#[async_trait::async_trait]
impl ProjectGenerate for TuistProject {
fn should_generate(&self, event: &Event) -> bool {
tracing::trace!("manifest files {:?}", self.manifest_files);
let is_config_file = self.manifest_files.contains(event.file_name());
let is_content_update = event.is_content_update_event();
let is_config_file_update = is_content_update && is_config_file;
is_config_file_update
|| event.is_create_event()
|| event.is_remove_event()
|| event.is_rename_event()
}
/// Generate xcodeproj
async fn generate(&mut self, broadcast: &Arc<Broadcast>) -> Result<()> {
let task = Task::new(TaskKind::Generate, self.name(), broadcast.clone());
self.tuist(&task, &["edit", "--permanent"]).await?;
self.tuist(&task, &["generate", "--no-open"]).await?;
let (xcodeproj_path, manifest_path) = self.xcodeproj_paths()?;
let (xcodeproj_path, manifest_path) = (xcodeproj_path.unwrap(), manifest_path.unwrap());
self.manifest = XCodeProject::new(&manifest_path)?;
self.manifest_path = manifest_path;
self.xcodeproj = XCodeProject::new(&xcodeproj_path)?;
self.xcodeproj_path = xcodeproj_path;
for (key, info) in self.xcodeproj.targets_info().into_iter() {
if self.targets.contains_key(&key) {
let existing_info = self.targets.get_mut(&key).unwrap();
*existing_info = info.into()
} else {
self.targets.insert(key, info.into());
}
}
Ok(())
}
}
impl TuistProject {
pub fn xcodeproj_paths(&self) -> Result<(Option<PathBuf>, Option<PathBuf>)> {
let paths = self.get_xcodeproj_paths()?;
if paths.is_empty() {
return Ok((None, None));
}
let (mut xcodeproj, mut manifest) = (None, None);
if paths.len() > 2 {
tracing::warn!(
"Expected `2` xcodeproj Manifest and Main but found `{}`",
paths.len()
)
}
paths.into_iter().for_each(|p| {
if p.file_name()
.and_then(|s| s.to_str())
.map(|s| s.starts_with("Manifests"))
.unwrap_or_default()
{
manifest = p.into();
} else {
xcodeproj = p.into();
}
});
Ok((xcodeproj, manifest))
}
/// Run tuist command with given args
async fn tuist(&mut self, task: &Task, args: &[&str]) -> Result<()> {
let mut process = Process::new(which("tuist")?);
process.args(args);
process.current_dir(self.root());
let mut logs = process.spawn_and_stream()?.collect::<Vec<_>>().await;
let success = logs.pop().unwrap().is_success().unwrap_or_default();
if !success {
task.error("Tuist Project Generation failed ");
let logs = logs.into_iter().map(|p| p.to_string()).collect::<Vec<_>>();
for log in logs {
task.error(log)
}
return Err(Error::Generate);
}
Ok(())
}
}
#[async_trait::async_trait]
impl Project for TuistProject {
async fn new(root: &PathBuf, broadcast: &Arc<Broadcast>) -> Result<Self> {
let mut watchignore = generate_watchignore(root).await;
watchignore.extend([
"**/*.xcodeproj/**".into(),
"**/*.xcworkspace/**".into(),
"**/Tuist/Dependencies/**".into(),
]);
let mut project = Self {
root: root.clone(),
watchignore,
num_clients: 1,
..Self::default()
};
let (xcodeproj_path, manifest_path) = match project.xcodeproj_paths()? {
(Some(xcodeproj_path), Some(manifest_path)) => (xcodeproj_path, manifest_path),
(Some(_), None) => {
let task = Task::new(TaskKind::Generate, "Manifest", broadcast.clone());
project.tuist(&task, &["edit", "--permanent"]).await?;
let (a, b) = project.xcodeproj_paths()?;
(a.unwrap(), b.unwrap())
}
(None, Some(_)) => {
let task = Task::new(TaskKind::Generate, project.name(), broadcast.clone());
project.tuist(&task, &["generate", "--no-open"]).await?;
let (a, b) = project.xcodeproj_paths()?;
(a.unwrap(), b.unwrap())
}
_ => {
tracing::info!("no xcodeproj found at {root:?}");
project.generate(broadcast).await?;
tracing::info!("[{}] targets: {:?}", project.name(), project.targets());
return Ok(project);
}
};
project.manifest = XCodeProject::new(&manifest_path)?;
project.manifest_path = manifest_path;
project.manifest_files = project.manifest.build_file_names();
project.xcodeproj = XCodeProject::new(&xcodeproj_path)?;
project.xcodeproj_path = xcodeproj_path;
project.targets = project
.xcodeproj
.targets_info()
.into_iter()
.map(|(k, info)| (k, info.into()))
.collect();
tracing::info!("[{}] targets: {:?}", project.name(), project.targets());
Ok(project)
}
}
#[async_trait::async_trait]
impl ProjectBuild for TuistProject {}
#[async_trait::async_trait]
impl ProjectRun for TuistProject {}