-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathxcodegen.rs
198 lines (168 loc) · 6.24 KB
/
xcodegen.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
use super::*;
use crate::util::fs::which;
use crate::watcher::Event;
use crate::Result;
use futures::StreamExt;
use process_stream::{Process, ProcessExt};
use serde::Serialize;
use std::{collections::HashMap, path::PathBuf};
use xcodeproj::XCodeProject;
#[derive(Debug, Serialize, Default)]
#[serde(default)]
pub struct XCodeGenProject {
root: PathBuf,
targets: HashMap<String, TargetInfo>,
num_clients: i32,
watchignore: Vec<String>,
#[serde(skip)]
xcodeproj: xcodeproj::XCodeProject,
}
impl ProjectData for XCodeGenProject {
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 XCodeGenProject {
async fn update_compile_database(&self, broadcast: &Arc<Broadcast>) -> Result<()> {
use xclog::XCCompilationDatabase as CC;
let root = self.root();
let name = self.root().name().unwrap();
let cache_root = self.build_cache_root()?;
let mut arguments = self.compile_arguments();
let task = Task::new(TaskKind::Compile, &name, broadcast.clone());
arguments.push(format!("SYMROOT={cache_root}"));
task.debug(format!("xcodebuild {}", arguments.join(" ")));
let xclogger = XCLogger::new(&root, &arguments)?;
let compile_commands = xclogger.compile_commands.clone();
let success = task
.consume(Box::new(xclogger))?
.recv()
.await
.unwrap_or_default();
if success {
let compile_db = CC::new(compile_commands.lock().await.to_vec());
if compile_db.is_empty() {
broadcast.warn("No compile command was generated!");
}
let json = serde_json::to_vec_pretty(&compile_db)?;
tokio::fs::write(root.join(".compile"), &json).await?;
broadcast.reload_lsp_server();
Ok(())
} else {
Err(Error::Compile)
}
}
}
#[async_trait::async_trait]
impl ProjectGenerate for XCodeGenProject {
fn should_generate(&self, event: &Event) -> bool {
let is_config_file = event.file_name() == "project.yml";
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 mut process: Process = vec![which("xcodegen")?.as_str(), "generate", "-c"].into();
let name = self.root().name().unwrap();
let task = Task::new(TaskKind::Generate, &name, broadcast.clone());
process.current_dir(self.root());
let mut logs = process
.spawn_and_stream()
.context("Spawn xcodegen")?
.collect::<Vec<_>>()
.await;
let success = logs.pop().unwrap().is_success().unwrap_or_default();
if !success {
let logs = logs.into_iter().map(|p| p.to_string()).collect::<Vec<_>>();
for log in logs {
tracing::error!("{log}");
task.error(log)
}
}
let xcodeproj_paths = self.get_xcodeproj_paths()?;
if xcodeproj_paths.len() > 1 {
let using = xcodeproj_paths[0].display();
tracing::warn!("[{name}] Found more then on xcodeproj, using {using}",);
}
self.xcodeproj = XCodeProject::new(&xcodeproj_paths[0]).context("Reading Project")?;
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(())
}
}
#[async_trait::async_trait]
impl Project for XCodeGenProject {
#[tracing::instrument(parent = None, name = "Project", skip_all, fields(name = root.name().unwrap(), kind = "xcodegen"))]
async fn new(root: &PathBuf, broadcast: &Arc<Broadcast>) -> Result<Self> {
tracing::info!("Processing");
let mut watchignore = generate_watchignore(root).await;
watchignore.extend(["**/*.xcodeproj/**".into(), "**/*.xcworkspace/**".into()]);
let mut project = Self {
root: root.clone(),
watchignore,
num_clients: 1,
..Self::default()
};
tracing::debug!("Searching for *.xcodeproj");
let xcodeproj_paths = project.get_xcodeproj_paths()?;
if xcodeproj_paths.len() > 1 {
tracing::warn!(
"Found more then one *.xcodeproj, using {:?}",
xcodeproj_paths[0]
);
}
if !xcodeproj_paths.is_empty() {
let xcpath = &xcodeproj_paths[0];
tracing::debug!("Using {}", xcpath.abbrv().unwrap().display());
project.xcodeproj = XCodeProject::new(xcpath).context("Reading XCodeProject")?;
tracing::debug!("Identifying targets");
project.targets = project
.xcodeproj
.targets_info()
.into_iter()
.map(|(k, info)| (k, info.into()))
.collect();
tracing::debug!("Targets: {:?} ", project.targets);
} else {
tracing::info!("Generating xcodeproj ...");
if let Err(err) = project.generate(broadcast).await {
return Err(Error::Setup(
project.name().to_string(),
format!("Generation failure {err}"),
));
};
}
tracing::info!("Created");
Ok(project)
}
}
#[async_trait::async_trait]
impl ProjectBuild for XCodeGenProject {}
#[async_trait::async_trait]
impl ProjectRun for XCodeGenProject {}