-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathvalidate.rs
More file actions
317 lines (285 loc) · 11.1 KB
/
Copy pathvalidate.rs
File metadata and controls
317 lines (285 loc) · 11.1 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
use std::{
fmt::Write,
fs::OpenOptions,
io::{BufReader, Read},
path::{Path, PathBuf},
sync::Arc,
};
use blue_build_process_management::ASYNC_RUNTIME;
use blue_build_recipe::{FromFileList, ModuleExt, Recipe, StagesExt};
use blue_build_utils::constants::{
MODULE_STAGE_LIST_V1_SCHEMA_URL, MODULE_V1_SCHEMA_URL, RECIPE_V1_SCHEMA_URL,
STAGE_V1_SCHEMA_URL,
};
use bon::Builder;
use clap::Args;
use colored::Colorize;
use log::{debug, info, trace};
use miette::{Context, IntoDiagnostic, Report, bail, miette};
use rayon::prelude::*;
use schema_validator::SchemaValidator;
use serde::de::DeserializeOwned;
use serde_json::Value;
use super::BlueBuildCommand;
mod location;
mod schema_validator;
mod yaml_span;
#[derive(Debug, Args, Builder)]
pub struct ValidateCommand {
/// The path to the recipe.
///
/// NOTE: In order for this to work,
/// you must be in the root of your
/// bluebuild repository.
pub recipe: PathBuf,
/// DEPRECATED
///
/// Display all errors that failed
/// validation of the recipe.
#[arg(short, long)]
#[builder(default)]
pub all_errors: bool,
#[clap(skip)]
recipe_validator: Option<SchemaValidator>,
#[clap(skip)]
stage_validator: Option<SchemaValidator>,
#[clap(skip)]
module_validator: Option<SchemaValidator>,
#[clap(skip)]
module_stage_list_validator: Option<SchemaValidator>,
}
impl BlueBuildCommand for ValidateCommand {
fn try_run(&mut self) -> miette::Result<()> {
let recipe_path_display = self.recipe.display().to_string().bold().italic();
if !self.recipe.is_file() {
bail!("File {recipe_path_display} must exist");
}
ASYNC_RUNTIME
.block_on(self.setup_validators())
.wrap_err("Failed to setup validators")?;
if let Err(errors) = self.validate_recipe() {
let errors = errors.into_iter().try_fold(
String::new(),
|mut full, err| -> miette::Result<String> {
write!(&mut full, "{err:?}").into_diagnostic()?;
Ok(full)
},
)?;
let main_err = format!("Recipe {recipe_path_display} failed to validate");
if self.all_errors {
return Err(miette!("{errors}").context(main_err));
}
return Err(miette!(
help = format!(
"Use `{}` to view more information.\n{}",
format!("bluebuild validate --all-errors {}", self.recipe.display()).bold(),
format_args!(
"If you're using a local module, be sure to add `{}` to the module entry",
"source: local".bold()
),
),
"{errors}",
)
.context(main_err));
}
info!("Recipe {recipe_path_display} is valid");
Ok(())
}
}
impl ValidateCommand {
async fn setup_validators(&mut self) -> Result<(), Report> {
let (rv, sv, mv, mslv) = tokio::try_join!(
SchemaValidator::builder().url(RECIPE_V1_SCHEMA_URL).build(),
SchemaValidator::builder().url(STAGE_V1_SCHEMA_URL).build(),
SchemaValidator::builder().url(MODULE_V1_SCHEMA_URL).build(),
SchemaValidator::builder()
.url(MODULE_STAGE_LIST_V1_SCHEMA_URL)
.build(),
)?;
self.recipe_validator = Some(rv);
self.stage_validator = Some(sv);
self.module_validator = Some(mv);
self.module_stage_list_validator = Some(mslv);
Ok(())
}
fn validate_file<DF>(
&self,
path: &Path,
traversed_files: &[&Path],
single_validator: &SchemaValidator,
) -> Vec<Report>
where
DF: DeserializeOwned + FromFileList,
{
let path_display = path.display().to_string().bold().italic();
if traversed_files.contains(&path) {
return vec![miette!(
"{} File {path_display} has already been parsed:\n{traversed_files:?}",
"Circular dependency detected!".bright_red(),
)];
}
let traversed_files = {
let mut files: Vec<&Path> = Vec::with_capacity(traversed_files.len() + 1);
files.extend_from_slice(traversed_files);
files.push(path);
files
};
let file_str = match read_file(path) {
Err(e) => return vec![e],
Ok(f) => Arc::new(f),
};
match serde_yaml::from_str::<Value>(&file_str)
.into_diagnostic()
.with_context(|| format!("Failed to deserialize file {path_display}"))
{
Ok(instance) => {
trace!("{path_display}:\n{instance}");
if instance.get(DF::LIST_KEY).is_some() {
debug!("{path_display} is a list file");
let err = self
.module_stage_list_validator
.as_ref()
.unwrap()
.process_validation(path, file_str.clone())
.err();
err.map_or_else(
|| {
serde_yaml::from_str::<DF>(&file_str)
.into_diagnostic()
.map_or_else(
|e| vec![e],
|file| {
let mut errs = file
.get_from_file_paths()
.par_iter()
.map(|file_path| {
self.validate_file::<DF>(
file_path,
&traversed_files,
single_validator,
)
})
.flatten()
.collect::<Vec<_>>();
errs.extend(
file.get_module_from_file_paths()
.par_iter()
.map(|file_path| {
self.validate_file::<ModuleExt>(
file_path,
&[],
self.module_validator.as_ref().unwrap(),
)
})
.flatten()
.collect::<Vec<_>>(),
);
errs
},
)
},
|err| vec![err.into()],
)
} else {
debug!("{path_display} is a single file file");
single_validator
.process_validation(path, file_str)
.map_or_else(|e| vec![e.into()], |()| Vec::new())
}
}
Err(e) => vec![e],
}
}
fn validate_recipe(&self) -> Result<(), Vec<Report>> {
let recipe_path_display = self.recipe.display().to_string().bold().italic();
debug!("Validating recipe {recipe_path_display}");
let recipe_str = Arc::new(read_file(&self.recipe).map_err(err_vec)?);
let recipe: Value = serde_yaml::from_str(&recipe_str)
.into_diagnostic()
.with_context(|| format!("Failed to deserialize recipe {recipe_path_display}"))
.map_err(err_vec)?;
trace!("{recipe_path_display}:\n{recipe}");
let schema_validator = self.recipe_validator.as_ref().unwrap();
let err = schema_validator
.process_validation(&self.recipe, recipe_str.clone())
.err();
if let Some(err) = err {
Err(vec![err.into()])
} else {
let recipe: Recipe = serde_yaml::from_str(&recipe_str)
.into_diagnostic()
.with_context(|| {
format!("Unable to convert Value to Recipe for {recipe_path_display}")
})
.map_err(err_vec)?;
let mut errors: Vec<Report> = Vec::new();
if let Some(stages) = &recipe.stages_ext {
debug!("Validating stages for recipe {recipe_path_display}");
errors.extend(
stages
.get_from_file_paths()
.par_iter()
.map(|stage_path| {
debug!(
"Found 'from-file' reference in {recipe_path_display} going to {}",
stage_path.display().to_string().italic().bold()
);
self.validate_file::<StagesExt>(
stage_path,
&[],
self.stage_validator.as_ref().unwrap(),
)
})
.flatten()
.collect::<Vec<_>>(),
);
}
debug!("Validating modules for recipe {recipe_path_display}");
errors.extend(
recipe
.modules_ext
.get_from_file_paths()
.par_iter()
.map(|module_path| {
debug!(
"Found 'from-file' reference in {recipe_path_display} going to {}",
module_path.display().to_string().italic().bold()
);
self.validate_file::<ModuleExt>(
module_path,
&[],
self.module_validator.as_ref().unwrap(),
)
})
.flatten()
.collect::<Vec<_>>(),
);
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
}
}
fn err_vec(err: Report) -> Vec<Report> {
vec![err]
}
fn read_file(path: &Path) -> Result<String, Report> {
let mut recipe = String::new();
BufReader::new(
OpenOptions::new()
.read(true)
.open(path)
.into_diagnostic()
.with_context(|| {
format!(
"Unable to open {}",
path.display().to_string().italic().bold()
)
})?,
)
.read_to_string(&mut recipe)
.into_diagnostic()?;
Ok(recipe)
}