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
extern crate boolinator;
extern crate downcast_rs;
extern crate failure;
use super::command::ExtCommand;
use super::config::*;
use super::error::Error;
use super::executor::*;
use super::{execute, Stage};
use crate::config::FromYaml;
use boolinator::Boolinator;
use downcast_rs::{impl_downcast, Downcast};
use failure::ResultExt;
use log::warn;
use std::convert::TryFrom;
use std::fmt::Debug;
use std::fs::create_dir_all;
use std::path::{Path, PathBuf};
use yaml_rust::Yaml;
pub trait PisaSource: Debug + Downcast {
fn executor(&self, config: &Config) -> Result<Box<dyn PisaExecutor>, Error>;
}
impl_downcast!(PisaSource);
impl dyn PisaSource {
fn parse_git_source(yaml: &Yaml) -> Result<GitSource, Error> {
match (yaml["url"].as_str(), yaml["branch"].as_str()) {
(None, _) => Err("missing source.url".into()),
(_, None) => Err("missing source.branch".into()),
(Some(url), Some(branch)) => Ok(GitSource::new(url, branch)),
}
}
fn parse_path_source(yaml: &Yaml) -> Result<CustomPathSource, Error> {
match yaml["path"].as_str() {
None => Err("missing source.path".into()),
Some(path) => Ok(CustomPathSource::from(path)),
}
}
fn parse_docker_source(yaml: &Yaml) -> Result<DockerSource, Error> {
match yaml["tag"].as_str() {
None => Err("missing source.tag".into()),
Some(tag) => Ok(DockerSource {
tag: String::from(tag),
}),
}
}
pub fn parse(yaml: &Yaml) -> Result<Box<dyn PisaSource>, Error> {
match yaml["type"].as_str() {
Some(typ) => match typ {
"git" => Ok(Box::new(PisaSource::parse_git_source(&yaml)?)),
"path" => Ok(Box::new(PisaSource::parse_path_source(&yaml)?)),
"docker" => Ok(Box::new(PisaSource::parse_docker_source(&yaml)?)),
typ => Err(format!("unknown source type: {}", typ).into()),
},
None => Err("missing or corrupted source.type".into()),
}
}
}
#[allow(clippy::use_self)]
impl FromYaml for Box<dyn PisaSource> {
fn from_yaml(yaml: &Yaml) -> Result<Self, Error> {
match yaml.parse_field::<String>("type")?.as_ref() {
"git" => Ok(Box::new(PisaSource::parse_git_source(&yaml)?)),
"path" => Ok(Box::new(PisaSource::parse_path_source(&yaml)?)),
"docker" => Ok(Box::new(PisaSource::parse_docker_source(&yaml)?)),
typ => Err(format!("unknown source type: {}", typ).into()),
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct CustomPathSource {
bin: PathBuf,
}
impl<P> From<P> for CustomPathSource
where
P: AsRef<Path>,
{
fn from(bin: P) -> Self {
Self {
bin: bin.as_ref().to_path_buf(),
}
}
}
impl PisaSource for CustomPathSource {
fn executor(&self, config: &Config) -> Result<Box<dyn PisaExecutor>, Error> {
let bin = if self.bin.is_absolute() {
self.bin.clone()
} else {
config.workdir.join(&self.bin)
};
Ok(Box::new(CustomPathExecutor::try_from(bin)?))
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct GitSource {
url: String,
branch: String,
}
impl GitSource {
pub fn new(url: &str, branch: &str) -> Self {
Self {
url: String::from(url),
branch: String::from(branch),
}
}
}
impl PisaSource for GitSource {
fn executor(&self, config: &Config) -> Result<Box<dyn PisaExecutor>, Error> {
init_git(config, self.url.as_ref(), self.branch.as_ref())
}
}
#[derive(Debug, PartialEq)]
pub struct DockerSource {
tag: String,
}
impl PisaSource for DockerSource {
#[cfg_attr(tarpaulin, skip)]
fn executor(&self, _config: &Config) -> Result<Box<dyn PisaExecutor>, Error> {
unimplemented!();
}
}
fn process(args: &'static str) -> ExtCommand {
let mut args = args.split(' ');
ExtCommand::new(args.next().unwrap()).args(args.collect::<Vec<&str>>())
}
fn init_git(config: &Config, url: &str, branch: &str) -> Result<Box<dyn PisaExecutor>, Error> {
let dir = config.workdir.join("pisa");
if !dir.exists() {
let clone = ExtCommand::new("git").args(&["clone", &url, dir.to_str().unwrap()]);
execute!(clone; "cloning failed");
};
let build_dir = dir.join("build");
create_dir_all(&build_dir).context("Could not create build directory")?;
if config.is_suppressed(Stage::Compile) {
warn!("Compilation has been suppressed");
} else {
let checkout = ExtCommand::new("git").args(&["checkout", branch]);
execute!(checkout.current_dir(&dir); "checkout failed");
let cmake = process("cmake -DCMAKE_BUILD_TYPE=Release ..");
execute!(cmake.current_dir(&build_dir); "cmake failed");
let build = process("cmake --build .");
execute!(build.current_dir(&build_dir); "build failed");
}
let executor = CustomPathExecutor::try_from(build_dir.join("bin"))?;
Ok(Box::new(executor))
}
#[cfg(test)]
mod tests;