-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbarebone.rs
168 lines (141 loc) · 4.8 KB
/
barebone.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
use super::*;
use crate::*;
use futures::future::try_join_all;
use serde::Serialize;
use std::{collections::HashMap, path::PathBuf};
use tap::Pipe;
use xclog::XCCompileCommand;
use xcodeproj::XCodeProject;
#[derive(Debug, Serialize, Default)]
#[serde(default)]
pub struct BareboneProject {
root: PathBuf,
targets: HashMap<String, TargetInfo>,
num_clients: i32,
watchignore: Vec<String>,
#[serde(skip)]
xcodeproj: XCodeProject,
}
impl ProjectData for BareboneProject {
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 ProjectBuild for BareboneProject {}
#[async_trait::async_trait]
impl ProjectRun for BareboneProject {}
#[async_trait::async_trait]
impl ProjectCompile for BareboneProject {
async fn update_compile_database(&self, broadcast: &Arc<Broadcast>) -> Result<()> {
let (name, root) = (self.name(), self.root());
let cache_root = self.build_cache_root()?;
let mut args = self.compile_arguments();
let mut tasks_recvs = vec![];
let mut xccommands: Vec<Arc<Mutex<Vec<XCCompileCommand>>>> = vec![];
let task = Task::new(TaskKind::Compile, name, broadcast.clone());
args.push(format!("SYMROOT={cache_root}"));
let xcworkspace = format!("{name}.xcworkspace");
if self.root().join(&xcworkspace).exists() {
for scheme in self.xcodeproj.schemes().iter() {
let mut args = args.clone();
args.extend_from_slice(&[
"-workspace".into(),
xcworkspace.clone(),
"-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))?);
}
} else {
args.extend_from_slice(&["-project".into(), format!("{name}.xcodeproj")]);
let xclogger = XCLogger::new(&root, &args)?;
xccommands.push(xclogger.compile_commands.clone());
tasks_recvs.push(task.consume(Box::new(xclogger))?);
}
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_string_pretty(&xccommands)?
.replace("-use-frontend-parseable-output", "");
tokio::fs::write(root.join(".compile"), &json).await?;
Ok(())
}
}
#[async_trait::async_trait]
impl ProjectGenerate for BareboneProject {
fn should_generate(&self, _event: &Event) -> bool {
false
}
async fn generate(&mut self, _logger: &Arc<Broadcast>) -> Result<()> {
tracing::error!(
"New File created or removed but generate barebone project is not supported"
);
Ok(())
}
}
#[async_trait::async_trait]
impl Project for BareboneProject {
async fn new(root: &PathBuf, _logger: &Arc<Broadcast>) -> Result<Self> {
let mut project = Self {
root: root.clone(),
watchignore: generate_watchignore(root).await,
num_clients: 1,
..Self::default()
};
let xcodeproj_paths = project.get_xcodeproj_paths()?;
if xcodeproj_paths.len() > 1 {
tracing::warn!(
"Found more then on xcodeproj, using {:?}",
xcodeproj_paths[0]
);
}
if xcodeproj_paths.is_empty() {
return Err(Error::DefinitionLocating);
};
project.xcodeproj = XCodeProject::new(&xcodeproj_paths[0])?;
project.targets = project
.xcodeproj
.targets_info()
.into_iter()
.map(|(k, info)| (k, info.into()))
.collect();
tracing::info!("targets: {:?}", project.targets());
Ok(project)
}
}