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
|
//! ref: composer/src/Composer/Command/StatusCommand.php
use anyhow::Result;
use indexmap::IndexMap;
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_option::InputOption;
use crate::io::io_interface::IOInterface;
use crate::package::dumper::array_dumper::ArrayDumper;
use crate::package::version::version_guesser::VersionGuesser;
use crate::package::version::version_parser::VersionParser;
use crate::plugin::command_event::CommandEvent;
use crate::plugin::plugin_events::PluginEvents;
use crate::script::script_events::ScriptEvents;
use crate::util::process_executor::ProcessExecutor;
#[derive(Debug)]
pub struct StatusCommand {
base_command_data: BaseCommandData,
}
impl StatusCommand {
const EXIT_CODE_ERRORS: i64 = 1;
const EXIT_CODE_UNPUSHED_CHANGES: i64 = 2;
const EXIT_CODE_VERSION_CHANGES: i64 = 4;
pub fn configure(&mut self) {
self
.set_name("status")
.set_description("Shows a list of locally modified packages")
.set_definition(&[
InputOption::new("verbose", Some(shirabe_php_shim::PhpMixed::String("v|vv|vvv".to_string())), Some(InputOption::VALUE_NONE), "Show modified files for each directory that contains changes.", None).unwrap().into(),
])
.set_help(
"The status command displays a list of dependencies that have\nbeen modified locally.\n\nRead more at https://getcomposer.org/doc/03-cli.md#status"
);
}
pub fn execute(&self, input: &dyn InputInterface, output: &dyn OutputInterface) -> Result<i64> {
let composer = self.require_composer(None, None)?;
// TODO(plugin): dispatch CommandEvent
let command_event = CommandEvent::new(PluginEvents::COMMAND, "status", input, output);
composer
.get_event_dispatcher()
.dispatch(Some(command_event.get_name()), None);
composer.get_event_dispatcher().dispatch_script(
ScriptEvents::PRE_STATUS_CMD,
true,
vec![],
indexmap::IndexMap::new(),
);
let exit_code = self.do_execute(input)?;
composer.get_event_dispatcher().dispatch_script(
ScriptEvents::POST_STATUS_CMD,
true,
vec![],
indexmap::IndexMap::new(),
);
Ok(exit_code)
}
fn do_execute(&self, input: &dyn InputInterface) -> Result<i64> {
let composer = self.require_composer(None, None)?;
let installed_repo = composer.get_repository_manager().get_local_repository();
let dm = composer.get_download_manager();
let im = composer.get_installation_manager();
let mut errors: IndexMap<String, String> = IndexMap::new();
let io = self.get_io();
let mut unpushed_changes: IndexMap<String, String> = IndexMap::new();
let mut vcs_version_changes: IndexMap<String, IndexMap<String, IndexMap<String, String>>> =
IndexMap::new();
let parser = VersionParser::new();
let process_executor = composer
.get_loop()
.borrow()
.get_process_executor()
.map(std::rc::Rc::clone)
.unwrap_or_else(|| std::rc::Rc::new(std::cell::RefCell::new(ProcessExecutor::new(io))));
let guesser = VersionGuesser::new(
std::rc::Rc::clone(composer.get_config()),
std::rc::Rc::clone(&process_executor),
parser.clone(),
Some(io.clone_box()),
);
let dumper = ArrayDumper::new();
for package in installed_repo.get_canonical_packages() {
let downloader = dm.borrow().get_downloader_for_package(package.as_ref());
let target_dir = im.get_install_path(package.as_ref());
let target_dir = match target_dir {
Some(d) => d,
None => continue,
};
// TODO(phase-b): isinstance checks using ChangeReportInterface/VcsCapableDownloaderInterface/DvcsDownloaderInterface
if let Some(change_reporter) = downloader.as_change_report_interface() {
if std::path::Path::new(&target_dir).is_symlink() {
errors.insert(
target_dir.clone(),
format!("{} is a symbolic link.", target_dir),
);
}
if let Some(changes) =
change_reporter.get_local_changes(package.as_ref(), &target_dir)?
{
errors.insert(target_dir.clone(), changes);
}
}
if let Some(vcs_downloader) = downloader.as_vcs_capable_downloader_interface() {
if vcs_downloader
.get_vcs_reference(package.as_ref(), target_dir.clone())
.is_some()
{
let previous_ref = match package.get_installation_source().as_deref() {
Some("source") => package.get_source_reference().map(|s| s.to_string()),
Some("dist") => package.get_dist_reference().map(|s| s.to_string()),
_ => None,
};
let current_version =
guesser.guess_version(&dumper.dump(package.as_ref()), &target_dir);
if let (Some(prev_ref), Some(cur_version)) = (&previous_ref, ¤t_version) {
if cur_version.get("commit").map(|s| s.as_str()) != Some(prev_ref.as_str())
&& cur_version.get("pretty_version").map(|s| s.as_str())
!= Some(prev_ref.as_str())
{
let mut previous = IndexMap::new();
previous.insert(
"version".to_string(),
package.get_pretty_version().to_string(),
);
previous.insert("ref".to_string(), prev_ref.clone());
let mut current = IndexMap::new();
current.insert(
"version".to_string(),
cur_version
.get("pretty_version")
.cloned()
.unwrap_or_default(),
);
current.insert(
"ref".to_string(),
cur_version.get("commit").cloned().unwrap_or_default(),
);
let mut change = IndexMap::new();
change.insert("previous".to_string(), previous);
change.insert("current".to_string(), current);
vcs_version_changes.insert(target_dir.clone(), change);
}
}
}
}
if let Some(dvcs_downloader) = downloader.as_dvcs_downloader_interface() {
if let Some(unpushed) =
dvcs_downloader.get_unpushed_changes(package.as_ref(), target_dir.clone())
{
unpushed_changes.insert(target_dir, unpushed);
}
}
}
if errors.is_empty() && unpushed_changes.is_empty() && vcs_version_changes.is_empty() {
io.write_error("<info>No local changes</info>");
return Ok(0);
}
if !errors.is_empty() {
io.write_error("<error>You have changes in the following dependencies:</error>");
for (path, changes) in &errors {
if input.get_option("verbose").as_bool().unwrap_or(false) {
let indented_changes = changes
.lines()
.map(|line| format!(" {}", line.trim_start()))
.collect::<Vec<_>>()
.join("\n");
io.write(&format!("<info>{}</info>:", path));
io.write(&indented_changes);
} else {
io.write(path);
}
}
}
if !unpushed_changes.is_empty() {
io.write_error("<warning>You have unpushed changes on the current branch in the following dependencies:</warning>");
for (path, changes) in &unpushed_changes {
if input.get_option("verbose").as_bool().unwrap_or(false) {
let indented_changes = changes
.lines()
.map(|line| format!(" {}", line.trim_start()))
.collect::<Vec<_>>()
.join("\n");
io.write(&format!("<info>{}</info>:", path));
io.write(&indented_changes);
} else {
io.write(path);
}
}
}
if !vcs_version_changes.is_empty() {
io.write_error(
"<warning>You have version variations in the following dependencies:</warning>",
);
for (path, changes) in &vcs_version_changes {
if input.get_option("verbose").as_bool().unwrap_or(false) {
let current_version = {
let v = changes["current"]
.get("version")
.map(|s| s.as_str())
.unwrap_or("");
let r = changes["current"]
.get("ref")
.map(|s| s.as_str())
.unwrap_or("");
if v.is_empty() {
r.to_string()
} else {
v.to_string()
}
};
let previous_version = {
let v = changes["previous"]
.get("version")
.map(|s| s.as_str())
.unwrap_or("");
let r = changes["previous"]
.get("ref")
.map(|s| s.as_str())
.unwrap_or("");
if v.is_empty() {
r.to_string()
} else {
v.to_string()
}
};
let (current_display, previous_display) = if io.is_very_verbose() {
let cur_ref = changes["current"]
.get("ref")
.map(|s| s.as_str())
.unwrap_or("");
let prev_ref = changes["previous"]
.get("ref")
.map(|s| s.as_str())
.unwrap_or("");
(
format!("{} ({})", current_version, cur_ref),
format!("{} ({})", previous_version, prev_ref),
)
} else {
(current_version, previous_version)
};
io.write(&format!("<info>{}</info>:", path));
io.write(&format!(
" From <comment>{}</comment> to <comment>{}</comment>",
previous_display, current_display
));
} else {
io.write(path);
}
}
}
if (!errors.is_empty() || !unpushed_changes.is_empty() || !vcs_version_changes.is_empty())
&& !input.get_option("verbose").as_bool().unwrap_or(false)
{
io.write_error("Use --verbose (-v) to see a list of files");
}
let exit_code = (if !errors.is_empty() {
Self::EXIT_CODE_ERRORS
} else {
0
}) + (if !unpushed_changes.is_empty() {
Self::EXIT_CODE_UNPUSHED_CHANGES
} else {
0
}) + (if !vcs_version_changes.is_empty() {
Self::EXIT_CODE_VERSION_CHANGES
} else {
0
});
Ok(exit_code)
}
}
impl HasBaseCommandData for StatusCommand {
fn base_command_data(&self) -> &BaseCommandData {
&self.base_command_data
}
fn base_command_data_mut(&mut self) -> &mut BaseCommandData {
&mut self.base_command_data
}
}
|