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
|
//! ref: composer/src/Composer/Command/ScriptAliasCommand.php
use crate::command::BaseCommand;
use crate::command::BaseCommandData;
use crate::command::base_command::base_command_initialize;
use crate::console::input::InputArgument;
use crate::console::input::InputOption;
use crate::util::Platform;
use shirabe_php_shim::{
InvalidArgumentException, LogicException, PhpMixed, impl_php_class, is_string, php_regex,
preg_replace2,
};
use shirabe_symfony_console::command::Command;
use shirabe_symfony_console::input::InputInterface;
use shirabe_symfony_console::output::OutputInterface;
#[derive(Debug)]
pub struct ScriptAliasCommand {
base_command_data: BaseCommandData,
script: String,
description: String,
aliases: Vec<String>,
}
impl_php_class!(ScriptAliasCommand, r"Composer\Command\ScriptAliasCommand");
impl ScriptAliasCommand {
pub fn new(
script: String,
description: Option<String>,
aliases: Vec<String>,
) -> anyhow::Result<Self> {
let description = description
.unwrap_or_else(|| format!("Runs the {} script as defined in composer.json", script));
for alias in &aliases {
if !is_string(&PhpMixed::String(alias.clone())) {
return Err(InvalidArgumentException::new(
r#""scripts-aliases" element array values should contain only strings"#
.to_string(),
)
.into());
}
}
let command = Self {
base_command_data: BaseCommandData::new(None),
script,
description,
aliases,
};
command.ignore_validation_errors();
command
.configure()
.expect("ScriptAliasCommand::configure uses constructor-provided metadata");
Ok(command)
}
}
impl Command for ScriptAliasCommand {
fn configure(&self) -> anyhow::Result<()> {
let name = self.script.clone();
self.set_name(&name)?;
let description = self.description.clone();
self.set_description(&description);
self.set_aliases(self.aliases.clone())?;
self.set_definition(&[
InputOption::new(
"dev",
None,
Some(InputOption::VALUE_NONE),
"Sets the dev mode.",
None,
)
.unwrap()
.into(),
InputOption::new(
"no-dev",
None,
Some(InputOption::VALUE_NONE),
"Disables the dev mode.",
None,
)
.unwrap()
.into(),
InputArgument::new(
"args",
Some(InputArgument::IS_ARRAY | InputArgument::OPTIONAL),
"",
None,
)
.unwrap()
.into(),
]);
self.set_help(
"The <info>run-script</info> command runs scripts defined in composer.json:\n\n\
<info>shirabe run-script post-update-cmd</info>\n\n\
Read more at https://getcomposer.org/doc/03-cli.md#run-script-run",
);
Ok(())
}
fn execute(
&self,
input: std::rc::Rc<std::cell::RefCell<dyn InputInterface>>,
_output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>,
) -> anyhow::Result<i64> {
let composer = self.require_composer(None, None)?;
let dispatcher = crate::composer::composer_full(&composer)
.get_event_dispatcher()
.clone();
let args = input.borrow().get_arguments();
// TODO(symfony): InputInterface has_to_string/get_class_name not modeled in Rust
// TODO remove for Symfony 6+ as it is then in the interface
if false {
return Err(LogicException::new(
"Expected an Input instance that is stringable".to_string(),
)
.into());
}
let dev_mode = input.borrow().get_option("dev")?.as_bool().unwrap_or(false)
|| !input
.borrow()
.get_option("no-dev")?
.as_bool()
.unwrap_or(false);
Platform::put_env("COMPOSER_DEV_MODE", if dev_mode { "1" } else { "0" });
// TODO(symfony): InputInterface lacks to_string; use a placeholder until it is modeled.
let input_as_string = String::new();
let _ = input;
let script_alias_input =
preg_replace2(php_regex!(r"{^\S+ ?}"), "", &input_as_string, 1, None);
let mut flags = indexmap::IndexMap::new();
flags.insert(
"script-alias-input".to_string(),
PhpMixed::String(script_alias_input),
);
let args_value: Vec<String> = args
.get("args")
.and_then(|v| v.as_array())
.map(<[String]>::to_vec)
.unwrap_or_default();
dispatcher
.borrow_mut()
.dispatch_script(&self.script, dev_mode, args_value, flags)
}
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)
}
fn complete(
&self,
input: &shirabe_symfony_console::completion::CompletionInput,
suggestions: &mut shirabe_symfony_console::completion::CompletionSuggestions,
) -> anyhow::Result<()> {
crate::command::base_command::base_command_complete(self, input, suggestions)
}
shirabe_symfony_console::delegate_command_trait_impls_to_inner!(base_command_data);
}
impl BaseCommand for ScriptAliasCommand {
fn base_command_data(&self) -> &crate::command::BaseCommandData {
&self.base_command_data
}
crate::delegate_base_command_trait_impls_to_inner!(base_command_data);
}
|