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
|
//! ref: composer/src/Composer/Installer/SuggestedPackagesReporter.php
use crate::io::IOInterface;
use crate::io::IOInterfaceImmutable;
use crate::package::PackageInterface;
use crate::repository::InstalledRepository;
use crate::repository::RepositoryInterface;
use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::Preg;
use shirabe_external_packages::symfony::component::console::formatter::OutputFormatter;
#[derive(Debug)]
pub struct SuggestedPackagesReporter {
suggested_packages: Vec<IndexMap<String, String>>,
io: std::rc::Rc<std::cell::RefCell<dyn IOInterface>>,
}
impl SuggestedPackagesReporter {
pub const MODE_LIST: i64 = 1;
pub const MODE_BY_PACKAGE: i64 = 2;
pub const MODE_BY_SUGGESTION: i64 = 4;
pub fn new(io: std::rc::Rc<std::cell::RefCell<dyn IOInterface>>) -> Self {
Self {
suggested_packages: Vec::new(),
io,
}
}
pub fn get_packages(&self) -> &Vec<IndexMap<String, String>> {
&self.suggested_packages
}
pub fn add_package(&mut self, source: String, target: String, reason: String) -> &mut Self {
let mut entry = IndexMap::new();
entry.insert("source".to_string(), source);
entry.insert("target".to_string(), target);
entry.insert("reason".to_string(), reason);
self.suggested_packages.push(entry);
self
}
pub fn add_suggestions_from_package(&mut self, package: &dyn PackageInterface) -> &mut Self {
let source = package.get_pretty_name().to_string();
for (target, reason) in package.get_suggests() {
self.add_package(source.clone(), target.clone(), reason.clone());
}
self
}
pub fn output(
&self,
mode: i64,
installed_repo: Option<&InstalledRepository>,
only_dependents_of: Option<&dyn PackageInterface>,
) {
let suggested_packages = self.get_filtered_suggestions(installed_repo, only_dependents_of);
let mut suggesters: IndexMap<String, IndexMap<String, String>> = IndexMap::new();
let mut suggested: IndexMap<String, IndexMap<String, String>> = IndexMap::new();
for suggestion in &suggested_packages {
suggesters
.entry(suggestion["source"].clone())
.or_insert_with(IndexMap::new)
.insert(suggestion["target"].clone(), suggestion["reason"].clone());
suggested
.entry(suggestion["target"].clone())
.or_insert_with(IndexMap::new)
.insert(suggestion["source"].clone(), suggestion["reason"].clone());
}
suggesters.sort_keys();
suggested.sort_keys();
// Simple mode
if mode & Self::MODE_LIST != 0 {
for name in suggested.keys() {
self.io.write(&format!("<info>{}</info>", name));
}
return;
}
// Grouped by package
if mode & Self::MODE_BY_PACKAGE != 0 {
for (suggester, suggestions) in &suggesters {
self.io
.write(&format!("<comment>{}</comment> suggests:", suggester));
for (suggestion, reason) in suggestions {
self.io.write(&format!(
" - <info>{}</info>{}",
suggestion,
if !reason.is_empty() {
format!(": {}", self.escape_output(reason))
} else {
String::new()
}
));
}
self.io.write("");
}
}
// Grouped by suggestion
if mode & Self::MODE_BY_SUGGESTION != 0 {
// Improve readability in full mode
if mode & Self::MODE_BY_PACKAGE != 0 {
self.io.write(&"-".repeat(78));
}
for (suggestion, suggesters) in &suggested {
self.io.write(&format!(
"<comment>{}</comment> is suggested by:",
suggestion
));
for (suggester, reason) in suggesters {
self.io.write(&format!(
" - <info>{}</info>{}",
suggester,
if !reason.is_empty() {
format!(": {}", self.escape_output(reason))
} else {
String::new()
}
));
}
self.io.write("");
}
}
if let Some(only_dependents_of) = only_dependents_of {
let all_suggested_packages = self.get_filtered_suggestions(installed_repo, None);
let diff = all_suggested_packages.len() as i64 - suggested_packages.len() as i64;
if diff != 0 {
self.io.write(&format!("<info>{} additional suggestions</info> by transitive dependencies can be shown with <info>--all</info>", diff));
}
}
}
pub fn output_minimalistic(
&self,
installed_repo: Option<&InstalledRepository>,
only_dependents_of: Option<&dyn PackageInterface>,
) {
let suggested_packages = self.get_filtered_suggestions(installed_repo, only_dependents_of);
if !suggested_packages.is_empty() {
self.io.write_error(&format!(
"<info>{} package suggestions were added by new dependencies, use `composer suggest` to see details.</info>",
suggested_packages.len()
));
}
}
fn get_filtered_suggestions(
&self,
installed_repo: Option<&InstalledRepository>,
only_dependents_of: Option<&dyn PackageInterface>,
) -> Vec<IndexMap<String, String>> {
let suggested_packages = self.get_packages();
let mut installed_names: Vec<String> = Vec::new();
if installed_repo.is_some() && !suggested_packages.is_empty() {
for package in installed_repo.unwrap().get_packages() {
installed_names.extend(package.get_names(true));
}
}
let mut source_filter: Vec<String> = Vec::new();
if let Some(only_dependents_of) = only_dependents_of {
source_filter = only_dependents_of
.get_requires()
.values()
.chain(only_dependents_of.get_dev_requires().values())
.map(|link| link.get_target().to_string())
.collect();
source_filter.push(only_dependents_of.get_name().to_string());
}
let mut suggestions: Vec<IndexMap<String, String>> = Vec::new();
for suggestion in suggested_packages {
if installed_names.contains(&suggestion["target"])
|| (!source_filter.is_empty() && !source_filter.contains(&suggestion["source"]))
{
continue;
}
suggestions.push(suggestion.clone());
}
suggestions
}
fn escape_output(&self, string: &str) -> String {
OutputFormatter::escape(&self.remove_control_characters(string))
}
fn remove_control_characters(&self, string: &str) -> String {
Preg::replace("/[[:cntrl:]]/", "", &string.replace('\n', " "))
.unwrap_or_else(|_| string.replace('\n', " "))
}
}
|