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
|
//! ref: composer/src/Composer/Command/GlobalCommand.php
use crate::command::BaseCommand;
use crate::command::BaseCommandData;
use crate::command::base_command::base_command_initialize;
use crate::console::Application;
use crate::console::input::InputArgument;
use crate::factory::Factory;
use crate::util::Filesystem;
use crate::util::Platform;
use shirabe_external_packages::composer::pcre::Preg;
use shirabe_external_packages::symfony::console::command::command::Command;
use shirabe_external_packages::symfony::console::completion::completion_input::CompletionInput;
use shirabe_external_packages::symfony::console::completion::completion_suggestions::{
CompletionSuggestions, StringOrSuggestion,
};
use shirabe_external_packages::symfony::console::input::ArgvInput;
use shirabe_external_packages::symfony::console::input::ArrayInput;
use shirabe_external_packages::symfony::console::input::InputInterface;
use shirabe_external_packages::symfony::console::input::StringInput;
use shirabe_external_packages::symfony::console::output::OutputInterface;
use shirabe_php_shim::{LogicException, RuntimeException, chdir, impl_php_class, php_regex};
use std::path::Path;
#[derive(Debug)]
pub struct GlobalCommand {
base_command_data: BaseCommandData,
}
impl_php_class!(GlobalCommand, r"Composer\Command\GlobalCommand");
impl Default for GlobalCommand {
fn default() -> Self {
Self::new()
}
}
impl GlobalCommand {
pub fn new() -> Self {
let command = GlobalCommand {
base_command_data: BaseCommandData::new(None),
};
command
.configure()
.expect("GlobalCommand::configure uses static, valid metadata");
command
}
// TODO remove for Symfony 6+ as it is then in the interface.
// Mirrors PHP's `method_exists($input, '__toString')` guard followed by
// `$input->__toString()`. `InputInterface` does not declare `__toString`, so the
// concrete stringable input types are matched explicitly.
fn input_to_string(input: &dyn InputInterface) -> anyhow::Result<String> {
let input_any = input.as_any();
if let Some(argv_input) = input_any.downcast_ref::<ArgvInput>() {
Ok(argv_input.to_string())
} else if let Some(array_input) = input_any.downcast_ref::<ArrayInput>() {
Ok(array_input.to_string())
} else if let Some(string_input) = input_any.downcast_ref::<StringInput>() {
Ok(string_input.to_string())
} else if let Some(completion_input) = input_any.downcast_ref::<CompletionInput>() {
Ok(completion_input.to_string())
} else {
Err(LogicException {
message: "Expected an Input instance that is stringable".to_string(),
code: 0,
}
.into())
}
}
fn prepare_subcommand_input(
&self,
input: std::rc::Rc<std::cell::RefCell<dyn InputInterface>>,
quiet: bool,
) -> anyhow::Result<StringInput> {
if Platform::get_env("COMPOSER").is_some() {
Platform::clear_env("COMPOSER");
}
let config = Factory::create_config(None, None)?;
let home = config.get("home").as_string().unwrap_or("").to_string();
if !Path::new(&home).is_dir() {
let mut fs = Filesystem::new(None);
fs.ensure_directory_exists(&home)?;
if !Path::new(&home).is_dir() {
return Err(RuntimeException {
message: "Could not create home directory".to_string(),
code: 0,
}
.into());
}
}
chdir(&home).map_err(|_e| RuntimeException {
message: format!("Could not switch to home directory \"{}\"", home),
code: 0,
})?;
if !quiet {
self.get_io().borrow().write_error(&format!(
"<info>Changed current directory to {}</info>",
home
));
}
let new_input_str = Preg::replace4(
php_regex!(r"{\bg(?:l(?:o(?:b(?:a(?:l)?)?)?)?)?\b}"),
"",
&Self::input_to_string(&*input.borrow())?,
1,
);
self.reset_composer()?;
StringInput::new(&new_input_str)
}
}
impl Command for GlobalCommand {
fn configure(&self) -> anyhow::Result<()> {
self.set_name("global")?;
self.set_description("Allows running commands in the global composer dir ($COMPOSER_HOME)");
self.set_definition(&[
InputArgument::new("command-name", Some(InputArgument::REQUIRED), "", None)
.unwrap()
.into(),
InputArgument::new(
"args",
Some(InputArgument::IS_ARRAY | InputArgument::OPTIONAL),
"",
None,
)
.unwrap()
.into(),
]);
self.set_help(
"Use this command as a wrapper to run other Composer commands\n\
within the global context of COMPOSER_HOME.\n\n\
You can use this to install CLI utilities globally, all you need\n\
is to add the COMPOSER_HOME/vendor/bin dir to your PATH env var.\n\n\
COMPOSER_HOME is c:\\Users\\<user>\\AppData\\Roaming\\Composer on Windows\n\
and /home/<user>/.composer on unix systems.\n\n\
If your system uses freedesktop.org standards, then it will first check\n\
XDG_CONFIG_HOME or default to /home/<user>/.config/composer\n\n\
Note: This path may vary depending on customizations to bin-dir in\n\
composer.json or the environmental variable COMPOSER_BIN_DIR.\n\n\
Read more at https://getcomposer.org/doc/03-cli.md#global",
);
Ok(())
}
fn is_proxy_command(&self) -> bool {
true
}
fn complete(
&self,
input: &CompletionInput,
suggestions: &mut CompletionSuggestions,
) -> anyhow::Result<()> {
let application = self
.get_application()
.expect("a proxy command is always attached to its application");
if input.must_suggest_argument_values_for("command-name") {
// The application borrow must be dropped before suggest_values (harmless) and
// before any command re-entry below.
let values: Vec<StringOrSuggestion> = {
let mut app_ref = application.borrow_mut();
let app = app_ref
.as_any_mut()
.downcast_mut::<Application>()
.expect("shirabe always installs its own Application");
app.all(None)?
.values()
// PHP: $command->isHidden() ? null : $command->getName(), then
// array_filter drops the nulls.
.filter(|command| !command.borrow().is_hidden())
.filter_map(|command| command.borrow().get_name())
.map(StringOrSuggestion::String)
.collect()
};
suggestions.suggest_values(values);
return Ok(());
}
let command_name = input.get_argument("command-name")?.to_string();
let has = {
let mut app_ref = application.borrow_mut();
let app = app_ref
.as_any_mut()
.downcast_mut::<Application>()
.expect("shirabe always installs its own Application");
app.has(&command_name)
};
if has {
let prepared = self.prepare_subcommand_input(
std::rc::Rc::new(std::cell::RefCell::new(input.clone())),
true,
)?;
let mut input = CompletionInput::from_string(&prepared.to_string(), 2)?;
let command = {
let mut app_ref = application.borrow_mut();
let app = app_ref
.as_any_mut()
.downcast_mut::<Application>()
.expect("shirabe always installs its own Application");
app.find(&command_name)?
};
command.borrow().merge_application_definition(true);
{
let command_ref = command.borrow();
let definition = command_ref.get_definition();
input.bind(&definition)?;
}
command.borrow().complete(&input, suggestions)?;
}
Ok(())
}
fn run(
&self,
input: std::rc::Rc<std::cell::RefCell<dyn InputInterface>>,
output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>,
) -> anyhow::Result<i64> {
let tokens = Preg::split(
php_regex!(r"{\s+}"),
&Self::input_to_string(&*input.borrow())?,
);
let mut args: Vec<String> = vec![];
for token in &tokens {
if !token.is_empty() && !token.starts_with('-') {
args.push(token.clone());
if args.len() >= 2 {
break;
}
}
}
if args.len() < 2 {
return self.base_run(input, output);
}
let sub_input = self.prepare_subcommand_input(input, false)?;
let sub_input: std::rc::Rc<std::cell::RefCell<dyn InputInterface>> =
std::rc::Rc::new(std::cell::RefCell::new(sub_input));
let application = {
let application = self
.get_application()
.expect("a proxy command is always attached to its application");
let application = application.borrow();
application
.as_any()
.downcast_ref::<Application>()
.expect("shirabe always installs its own Application")
.shared()
};
Ok(application.run(Some(sub_input), Some(output))? as i64)
}
fn initialize(
&self,
input: std::rc::Rc<std::cell::RefCell<dyn InputInterface>>,
output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>,
) -> anyhow::Result<()> {
base_command_initialize(self, input, output)
}
shirabe_external_packages::delegate_command_trait_impls_to_inner!(base_command_data);
}
impl BaseCommand for GlobalCommand {
fn base_command_data(&self) -> &crate::command::BaseCommandData {
&self.base_command_data
}
crate::delegate_base_command_trait_impls_to_inner!(base_command_data);
fn is_proxy_command(&self) -> bool {
true
}
}
|