-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.rs
285 lines (253 loc) · 8.33 KB
/
project.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
use crate::{
build_system::{
build_system_from_str, RootIdentificationResult, BUILD_SYSTEMS,
},
editor_config::EditorConfig,
mk_info::MkInfo,
Error, Result,
};
use std::{
collections::HashMap,
env, io, iter,
path::{Path, PathBuf},
process::{Command, ExitStatus},
time::SystemTime,
};
use crate::{build_system::BuildSystem, Opts};
const VCS_ROOT_DIRS: &[&str] = &[".git", ".hg", "_darcs", ".bzr"];
const VCS_TIL_ROOT_DIRS: &[&str] = &[".svn", "CVS"];
fn has_root_editor_config(path: &Path) -> Result<bool> {
path.join(".editorconfig")
.exists()
.then(|| EditorConfig::from_file(path).map(|x| x.root))
.unwrap_or(Ok(false))
}
fn is_project_root(path: &Path) -> Result<RootIdentificationResult> {
use RootIdentificationResult::*;
Ok(if VCS_ROOT_DIRS.iter().any(|x| path.join(x).is_dir()) {
IsRoot
} else if VCS_TIL_ROOT_DIRS.iter().any(|x| path.join(x).is_dir()) {
MaybeRoot
} else if has_root_editor_config(path)? {
IsRoot
} else {
NotRoot
})
}
pub struct RootInfo {
build_system: Option<&'static dyn BuildSystem>,
project_dir: PathBuf,
}
impl RootInfo {
fn new(
build_system: Option<&'static dyn BuildSystem>,
project_dir: &Path,
) -> Self {
Self {
build_system,
project_dir: project_dir.to_path_buf(),
}
}
}
pub fn find_root(path: &Path) -> Result<RootInfo> {
use RootIdentificationResult::*;
let mut maybe_build_system = None;
let mut maybe_root = false;
for candidate in path.ancestors() {
let last_maybe_build_system = maybe_build_system.take();
for build_system in BUILD_SYSTEMS {
match build_system.is_project_root(candidate)? {
IsRoot => {
return Ok(RootInfo::new(Some(*build_system), candidate))
}
MaybeRoot => {
maybe_build_system =
Some(RootInfo::new(Some(*build_system), candidate));
}
NotRoot => {}
}
}
if maybe_build_system.is_none() && last_maybe_build_system.is_some() {
maybe_build_system = last_maybe_build_system;
break;
}
match is_project_root(candidate)? {
IsRoot => break,
MaybeRoot => {
maybe_root = true;
if maybe_build_system.is_none() {
maybe_build_system = Some(RootInfo::new(None, candidate))
}
}
NotRoot => {
if maybe_root {
break;
}
}
}
}
maybe_build_system.ok_or(Error::NoProjectRootFound)
}
pub struct Project {
pub container_image: Option<String>,
pub container_args: Option<Vec<String>>,
pub mk_info_path: Option<PathBuf>,
pub project_dir: PathBuf,
pub work_dir: PathBuf,
pub build_dir: PathBuf,
pub configure_args: Vec<String>,
pub args: Vec<String>,
pub env: HashMap<String, String>,
pub build_system: &'static (dyn BuildSystem),
pub container: bool,
}
impl Project {
pub fn from_opts(opts: &Opts) -> Result<Self> {
let work_dir = opts
.cwd
.canonicalize()
.map_err(|e| Error::Io(opts.cwd.clone(), e))?;
let RootInfo {
build_system,
project_dir,
} = find_root(&work_dir)?;
let mk_info_path = if let Ok(mk_info) = env::var("MKINFO") {
Some(PathBuf::from(mk_info))
} else {
MkInfo::find_root_path(&project_dir)?
};
let mk_info = if let Some(mk_info_path) = &mk_info_path {
MkInfo::from_path(mk_info_path)?
} else {
MkInfo::default()
};
let configure_args = mk_info.configure.clone().unwrap_or_default();
let build_dir = opts.build_dir.clone().unwrap_or("build".into());
let build_dir = project_dir.join(build_dir);
let container = opts.container;
let container_image = mk_info.image().map(|x| x.to_string());
let container_args = mk_info.container_args().map(|x| x.to_vec());
let args = if opts.args.is_empty() {
mk_info.default.unwrap_or_default()
} else {
opts.args.clone()
};
let build_system = if let Some(build_system) = mk_info.build_system {
build_system_from_str(&build_system)
} else {
build_system
}
.ok_or(Error::NoBuildSystemFound)?;
let env = mk_info.env.unwrap_or_default();
Ok(Self {
container,
container_image,
container_args,
mk_info_path,
project_dir,
work_dir,
build_dir,
build_system,
configure_args,
args,
env,
})
}
pub fn clean(&self) -> Result<()> {
match std::fs::remove_dir_all(&self.build_dir) {
Ok(()) => Ok(()),
Err(e) => {
if e.kind() == io::ErrorKind::NotFound {
Ok(())
} else {
Err(Error::Io(self.build_dir.clone(), e))
}
}
}
}
pub fn find_container_runtime(&self) -> Result<PathBuf> {
if let Some(runtime) = std::env::var("CONTAINER_RUNTIME").ok() {
return Ok(PathBuf::from(runtime));
}
let runtimes = ["podman", "docker"];
let Ok(path_var) = std::env::var("PATH") else {
return Err(Error::NoContainerRuntimeFound);
};
for path in path_var.split(':') {
for rt in &runtimes {
let path = Path::new(path).join(rt);
if path.exists() {
return Ok(path);
}
}
}
Err(Error::NoContainerRuntimeFound)
}
pub fn run(&self, command: &[String]) -> Result<ExitStatus> {
let command = if self.container {
let container_image = self
.container_image
.clone()
.ok_or(Error::MissingContainerImage)?;
let container_runtime = self.find_container_runtime()?;
iter::once(container_runtime.to_string_lossy().to_string())
.chain([
"run".to_string(),
"-ti".to_string(),
"--rm".to_string(),
"-v".to_string(),
format!("{0}:{0}", self.project_dir.display()),
"--workdir".to_string(),
self.work_dir.display().to_string(),
])
.chain(self.env.iter().map(|(k, v)| format!("-e{}={}", k, v)))
.chain(self.container_args.clone().unwrap_or_default())
.chain(["--".to_string(), container_image])
.chain(command.iter().cloned())
.collect::<Vec<String>>()
} else {
command.to_vec()
};
Command::new(&command[0])
.args(command.iter().skip(1))
.envs(&self.env)
.current_dir(&self.work_dir)
.status()
.map_err(|e| Error::Command(command[0].clone(), e))
}
pub fn build(&self) -> Result<ExitStatus> {
let cmd = self.build_system.build_command(self);
self.run(&cmd)
}
pub fn configure(&self) -> Result<ExitStatus> {
let cmd = self.build_system.configure_command(self);
if cmd.len() > 1 {
self.run(&cmd)
} else {
Ok(ExitStatus::default())
}
}
fn get_mtime(path: &Path) -> Result<SystemTime> {
path.metadata()
.and_then(|x| x.modified())
.map_err(|e| Error::Io(path.into(), e))
}
pub fn is_configured(&self) -> Result<bool> {
let marker =
if let Some(marker) = self.build_system.configure_marker(self)? {
marker
} else {
return Ok(true);
};
if !marker.exists() {
return Ok(false);
}
let marker_time = Self::get_mtime(&marker)?;
let mk_info_time = if let Some(mk_info_path) = &self.mk_info_path {
Self::get_mtime(mk_info_path)?
} else {
return Ok(true);
};
Ok(marker_time > mk_info_time)
}
}