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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
//! ref: composer/src/Composer/Command/ValidateCommand.php
use anyhow::Result;
use shirabe_external_packages::symfony::component::console::input::input_interface::InputInterface;
use shirabe_external_packages::symfony::component::console::output::output_interface::OutputInterface;
use crate::command::base_command::{BaseCommand, BaseCommandData, HasBaseCommandData};
use crate::composer::Composer;
use crate::console::input::input_argument::InputArgument;
use crate::console::input::input_option::InputOption;
use crate::factory::Factory;
use crate::io::io_interface::IOInterface;
use crate::package::loader::validating_array_loader::ValidatingArrayLoader;
use crate::plugin::command_event::CommandEvent;
use crate::plugin::plugin_events::PluginEvents;
use crate::util::config_validator::ConfigValidator;
use crate::util::filesystem::Filesystem;
#[derive(Debug)]
pub struct ValidateCommand {
base_command_data: BaseCommandData,
}
impl ValidateCommand {
pub fn configure(&mut self) {
self.set_name("validate")
.set_description("Validates a composer.json and composer.lock")
.set_definition(&[
InputOption::new(
"no-check-all",
None,
Some(InputOption::VALUE_NONE),
"Do not validate requires for overly strict/loose constraints",
None,
)
.unwrap()
.into(),
InputOption::new(
"check-lock",
None,
Some(InputOption::VALUE_NONE),
"Check if lock file is up to date (even when config.lock is false)",
None,
)
.unwrap()
.into(),
InputOption::new(
"no-check-lock",
None,
Some(InputOption::VALUE_NONE),
"Do not check if lock file is up to date",
None,
)
.unwrap()
.into(),
InputOption::new(
"no-check-publish",
None,
Some(InputOption::VALUE_NONE),
"Do not check for publish errors",
None,
)
.unwrap()
.into(),
InputOption::new(
"no-check-version",
None,
Some(InputOption::VALUE_NONE),
"Do not report a warning if the version field is present",
None,
)
.unwrap()
.into(),
InputOption::new(
"with-dependencies",
Some(shirabe_php_shim::PhpMixed::String("A".to_string())),
Some(InputOption::VALUE_NONE),
"Also validate the composer.json of all installed dependencies",
None,
)
.unwrap()
.into(),
InputOption::new(
"strict",
None,
Some(InputOption::VALUE_NONE),
"Return a non-zero exit code for warnings as well as errors",
None,
)
.unwrap()
.into(),
InputArgument::new(
"file",
Some(InputArgument::OPTIONAL),
"path to composer.json file",
None,
)
.unwrap()
.into(),
])
.set_help(
"The validate command validates a given composer.json and composer.lock\n\n\
Exit codes in case of errors are:\n\
1 validation warning(s), only when --strict is given\n\
2 validation error(s)\n\
3 file unreadable or missing\n\n\
Read more at https://getcomposer.org/doc/03-cli.md#validate",
);
}
pub fn execute(
&mut self,
input: &dyn InputInterface,
output: &dyn OutputInterface,
) -> Result<i64> {
let file = input
.get_argument("file")
.as_string_opt()
.map(|s| s.to_string())
.map(Ok)
.unwrap_or_else(Factory::get_composer_file)?;
// TODO(phase-b): get_io() takes &mut self via BaseCommand; clone_box to release the borrow.
let io_box = self.get_io().clone_box();
let io: &dyn IOInterface = io_box.as_ref();
if !std::path::Path::new(&file).exists() {
io.write_error(&format!("<error>{} not found.</error>", file));
return Ok(3);
}
if !Filesystem::is_readable(&file) {
io.write_error(&format!("<error>{} is not readable.</error>", file));
return Ok(3);
}
let validator = ConfigValidator::new(io.clone_box());
let check_all = if input.get_option("no-check-all").as_bool().unwrap_or(false) {
0
} else {
ValidatingArrayLoader::CHECK_ALL
};
let check_publish = !input
.get_option("no-check-publish")
.as_bool()
.unwrap_or(false);
let check_lock = !input.get_option("no-check-lock").as_bool().unwrap_or(false);
let check_version = if input
.get_option("no-check-version")
.as_bool()
.unwrap_or(false)
{
0
} else {
ConfigValidator::CHECK_VERSION
};
let is_strict = input.get_option("strict").as_bool().unwrap_or(false);
let (mut errors, mut publish_errors, mut warnings) =
validator.validate(&file, check_all, check_version);
let mut lock_errors: Vec<String> = vec![];
let mut composer = self.create_composer_instance(input, io, None, false, None)?;
let check_lock = (check_lock
&& composer
.get_config()
.borrow_mut()
.get("lock")
.as_bool()
.unwrap_or(true))
|| input.get_option("check-lock").as_bool().unwrap_or(false);
// TODO(phase-b): get_missing_requirement_info needs &package from composer while
// locker holds &mut composer; cloning lock state isn't trivial. Use todo!() for the
// package-arg subexpression below.
let locker = composer.get_locker_mut();
if locker.is_locked() && !locker.is_fresh()? {
lock_errors.push("- The lock file is not up to date with the latest changes in composer.json, it is recommended that you run `composer update` or `composer update <package name>`.".to_string());
}
if locker.is_locked() {
// TODO(phase-b): borrows composer twice; use todo!() for the package arg.
lock_errors.extend(locker.get_missing_requirement_info(todo!(), true)?);
}
self.output_result(
io,
&file,
&mut errors,
&mut warnings,
check_publish,
&mut publish_errors,
check_lock,
&mut lock_errors,
true,
);
let exit_code = if !errors.is_empty() {
2
} else if is_strict && !warnings.is_empty() {
1
} else {
0
};
let mut exit_code = exit_code;
if input
.get_option("with-dependencies")
.as_bool()
.unwrap_or(false)
{
let packages = composer
.get_repository_manager()
.get_local_repository()
.get_packages();
for package in packages {
let path = composer
.get_installation_manager_mut()
.get_install_path(package.as_ref());
let path = match path {
Some(p) => p,
None => continue,
};
let dep_file = format!("{}/composer.json", path);
if std::path::Path::new(&path).is_dir() && std::path::Path::new(&dep_file).exists()
{
let (mut dep_errors, mut dep_publish_errors, mut dep_warnings) =
validator.validate(&dep_file, check_all, check_version);
self.output_result(
io,
package.get_pretty_name(),
&mut dep_errors,
&mut dep_warnings,
check_publish,
&mut dep_publish_errors,
false,
&mut vec![],
false,
);
let dep_code = if !dep_errors.is_empty() {
2
} else if is_strict && !dep_warnings.is_empty() {
1
} else {
0
};
exit_code = exit_code.max(dep_code);
}
}
}
// TODO(plugin): dispatch CommandEvent
let command_event = CommandEvent::new(PluginEvents::COMMAND, "validate", input, output);
let event_code = composer
.get_event_dispatcher()
.borrow_mut()
.dispatch(Some(command_event.get_name()), None)?;
Ok(exit_code.max(event_code))
}
fn output_result(
&self,
io: &dyn IOInterface,
name: &str,
errors: &mut Vec<String>,
warnings: &mut Vec<String>,
check_publish: bool,
publish_errors: &mut Vec<String>,
check_lock: bool,
lock_errors: &mut Vec<String>,
print_schema_url: bool,
) {
let mut do_print_schema_url = false;
if !errors.is_empty() {
io.write_error(&format!(
"<error>{} is invalid, the following errors/warnings were found:</error>",
name
));
} else if !publish_errors.is_empty() && check_publish {
io.write_error(&format!(
"<info>{} is valid for simple usage with Composer but has</info>",
name
));
io.write_error(
"<info>strict errors that make it unable to be published as a package</info>",
);
do_print_schema_url = print_schema_url;
} else if !warnings.is_empty() {
io.write_error(&format!(
"<info>{} is valid, but with a few warnings</info>",
name
));
do_print_schema_url = print_schema_url;
} else if !lock_errors.is_empty() {
io.write(&format!(
"<info>{} is valid but your composer.lock has some {}</info>",
name,
if check_lock { "errors" } else { "warnings" }
));
} else {
io.write(&format!("<info>{} is valid</info>", name));
}
if do_print_schema_url {
io.write_error("<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>");
}
if !errors.is_empty() {
*errors = errors.iter().map(|e| format!("- {}", e)).collect();
errors.insert(0, "# General errors".to_string());
}
if !warnings.is_empty() {
*warnings = warnings.iter().map(|w| format!("- {}", w)).collect();
warnings.insert(0, "# General warnings".to_string());
}
let mut extra_warnings: Vec<String> = vec![];
if !publish_errors.is_empty() && check_publish {
*publish_errors = publish_errors.iter().map(|e| format!("- {}", e)).collect();
publish_errors.insert(0, "# Publish errors".to_string());
errors.extend(publish_errors.drain(..));
}
if !lock_errors.is_empty() {
if check_lock {
lock_errors.insert(0, "# Lock file errors".to_string());
errors.extend(lock_errors.drain(..));
} else {
lock_errors.insert(0, "# Lock file warnings".to_string());
extra_warnings.extend(lock_errors.drain(..));
}
}
let all_warnings: Vec<String> = warnings.iter().cloned().chain(extra_warnings).collect();
for msg in errors.iter() {
if msg.starts_with('#') {
io.write_error(&format!("<error>{}</error>", msg));
} else {
io.write_error(msg);
}
}
for msg in &all_warnings {
if msg.starts_with('#') {
io.write_error(&format!("<warning>{}</warning>", msg));
} else {
io.write_error(msg);
}
}
}
}
impl HasBaseCommandData for ValidateCommand {
fn base_command_data(&self) -> &BaseCommandData {
&self.base_command_data
}
fn base_command_data_mut(&mut self) -> &mut BaseCommandData {
&mut self.base_command_data
}
}
|