diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-16 21:59:35 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-19 22:17:11 +0900 |
| commit | fd733a87364b877d5e66b4973bd61b5379ec4d61 (patch) | |
| tree | 64bd6c37ca1c33581a851eba22e4261bf5b62b49 /crates/shirabe/src/command/run_script_command.rs | |
| parent | 7f3171b14a89762b2f0c71969d7243f2297af7c9 (diff) | |
| download | php-shirabe-fd733a87364b877d5e66b4973bd61b5379ec4d61.tar.gz php-shirabe-fd733a87364b877d5e66b4973bd61b5379ec4d61.tar.zst php-shirabe-fd733a87364b877d5e66b4973bd61b5379ec4d61.zip | |
feat(command): implement Symfony Command
Diffstat (limited to 'crates/shirabe/src/command/run_script_command.rs')
| -rw-r--r-- | crates/shirabe/src/command/run_script_command.rs | 341 |
1 files changed, 183 insertions, 158 deletions
diff --git a/crates/shirabe/src/command/run_script_command.rs b/crates/shirabe/src/command/run_script_command.rs index 199dbab..db6463e 100644 --- a/crates/shirabe/src/command/run_script_command.rs +++ b/crates/shirabe/src/command/run_script_command.rs @@ -2,13 +2,23 @@ use anyhow::Result; use indexmap::IndexMap; +use shirabe_external_packages::symfony::console::command::command::Command; use shirabe_external_packages::symfony::console::input::InputInterface; use shirabe_external_packages::symfony::console::output::OutputInterface; -use shirabe_php_shim::{InvalidArgumentException, PhpMixed, RuntimeException}; +use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{InvalidArgumentException, RuntimeException}; +use std::cell::RefCell; +use std::rc::Rc; -use crate::command::{BaseCommand, BaseCommandData, HasBaseCommandData}; +use crate::advisory::AuditConfig; +use crate::command::BaseCommand; +use crate::command::BaseCommandData; +use crate::command::base_command::base_command_initialize; +use crate::composer::PartialComposerHandle; +use crate::config::Config; use crate::console::input::InputArgument; use crate::console::input::InputOption; +use crate::filter::platform_requirement_filter::PlatformRequirementFilterInterface; use crate::io::IOInterface; use crate::io::IOInterfaceImmutable; use crate::script::Event as ScriptEvent; @@ -25,11 +35,8 @@ pub struct RunScriptCommand { impl RunScriptCommand { pub fn new() -> Self { - Self { - base_command_data: BaseCommandData { - composer: None, - io: None, - }, + let mut command = RunScriptCommand { + base_command_data: BaseCommandData::new(None), script_events: vec![ ScriptEvents::PRE_INSTALL_CMD, ScriptEvents::POST_INSTALL_CMD, @@ -44,120 +51,179 @@ impl RunScriptCommand { ScriptEvents::PRE_AUTOLOAD_DUMP, ScriptEvents::POST_AUTOLOAD_DUMP, ], - } - } - - pub fn configure(&mut self) { - self.set_name("run-script") - .set_aliases(&["run".to_string()]) - .set_description("Runs the scripts defined in composer.json") - .set_definition(&[ - // TODO(cli-completion): script-name completion was provided via a closure suggesting runtime script names - InputArgument::new( - "script", - Some(InputArgument::OPTIONAL), - "Script name to run.", - None, - ) - .unwrap() - .into(), - InputArgument::new( - "args", - Some(InputArgument::IS_ARRAY | InputArgument::OPTIONAL), - "", - None, - ) - .unwrap() - .into(), - InputOption::new( - "timeout", - None, - Some(InputOption::VALUE_REQUIRED), - "Sets script timeout in seconds, or 0 for never.", - None, - ) - .unwrap() - .into(), - 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(), - InputOption::new( - "list", - Some(PhpMixed::String("l".to_string())), - Some(InputOption::VALUE_NONE), - "List scripts.", - None, - ) - .unwrap() - .into(), - ]) - .set_help( - "The <info>run-script</info> command runs scripts defined in composer.json:\n\n\ - <info>php composer.phar run-script post-update-cmd</info>\n\n\ - Read more at https://getcomposer.org/doc/03-cli.md#run-script-run", - ); + }; + command + .configure() + .expect("RunScriptCommand::configure uses static, valid metadata"); + command } - pub fn interact( - &mut self, - input: std::rc::Rc<std::cell::RefCell<dyn InputInterface>>, - _output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>, - ) -> Result<()> { + fn list_scripts(&mut self, output: Rc<RefCell<dyn OutputInterface>>) -> Result<i64> { let scripts = self.get_scripts()?; if scripts.is_empty() { - return Ok(()); + return Ok(0); } - if input.borrow().get_argument("script")?.as_string().is_some() - || input - .borrow() - .get_option("list")? - .as_bool() - .unwrap_or(false) - { - return Ok(()); + let io = self.get_io(); + io.write_error("<info>scripts:</info>"); + let table: Vec<PhpMixed> = scripts + .iter() + .map(|(name, desc)| { + PhpMixed::List(vec![ + Box::new(PhpMixed::String(format!(" {}", name))), + Box::new(PhpMixed::String(desc.clone())), + ]) + }) + .collect(); + + self.render_table(table, output); + + Ok(0) + } + + fn get_scripts(&mut self) -> Result<Vec<(String, String)>> { + let composer = self.require_composer(None, None)?; + let scripts = crate::command::composer_full(&composer) + .get_package() + .get_scripts(); + drop(composer); + if scripts.is_empty() { + return Ok(vec![]); } - let mut options = indexmap::IndexMap::new(); - for script in &scripts { - options.insert(script.0.clone(), script.1.clone()); + let mut result: Vec<(String, String)> = vec![]; + for (name, _script) in scripts { + // PHP: $cmd = $this->getApplication()->find($name); $description = $cmd->getDescription(); + // TODO(phase-c): Application::find returns PhpMixed (the Symfony command registry is a + // todo!() stub) and get_application() is itself deferred, so the resolved command's + // getDescription() cannot be read; the description stays empty until the typed command + // registry is modelled. + // get_application() is deferred (returns the Symfony `dyn Application` back-reference, + // not the shirabe Application with find()); the description stays empty until the + // shared Application handle and typed command registry are modelled. + let description = String::new(); + result.push((name, description)); } - let io = self.get_io(); - let script = io.select( - "Script to run: ".to_string(), - options.keys().cloned().collect(), - PhpMixed::String(String::new()), - PhpMixed::Int(1), - "Invalid script name \"%s\"".to_string(), - false, + Ok(result) + } +} + +impl Command for RunScriptCommand { + fn configure(&mut self) -> anyhow::Result<()> { + self.set_name("run-script")?; + self.set_aliases(vec!["run".to_string()])?; + self.set_description("Runs the scripts defined in composer.json"); + self.set_definition(&[ + // TODO(cli-completion): script-name completion was provided via a closure suggesting runtime script names + InputArgument::new( + "script", + Some(InputArgument::OPTIONAL), + "Script name to run.", + None, + ) + .unwrap() + .into(), + InputArgument::new( + "args", + Some(InputArgument::IS_ARRAY | InputArgument::OPTIONAL), + "", + None, + ) + .unwrap() + .into(), + InputOption::new( + "timeout", + None, + Some(InputOption::VALUE_REQUIRED), + "Sets script timeout in seconds, or 0 for never.", + None, + ) + .unwrap() + .into(), + 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(), + InputOption::new( + "list", + Some(PhpMixed::String("l".to_string())), + Some(InputOption::VALUE_NONE), + "List scripts.", + None, + ) + .unwrap() + .into(), + ]); + self.set_help( + "The <info>run-script</info> command runs scripts defined in composer.json:\n\n\ + <info>php composer.phar run-script post-update-cmd</info>\n\n\ + Read more at https://getcomposer.org/doc/03-cli.md#run-script-run", ); + Ok(()) + } + + fn interact( + &mut self, + input: Rc<RefCell<dyn InputInterface>>, + _output: Rc<RefCell<dyn OutputInterface>>, + ) { + let _ = (|| -> anyhow::Result<()> { + let scripts = self.get_scripts()?; + if scripts.is_empty() { + return Ok(()); + } - input.borrow_mut().set_argument("script", script)?; + if input.borrow().get_argument("script")?.as_string().is_some() + || input + .borrow() + .get_option("list")? + .as_bool() + .unwrap_or(false) + { + return Ok(()); + } - Ok(()) + let mut options = indexmap::IndexMap::new(); + for script in &scripts { + options.insert(script.0.clone(), script.1.clone()); + } + + let io = self.get_io(); + let script = io.select( + "Script to run: ".to_string(), + options.keys().cloned().collect(), + PhpMixed::String(String::new()), + PhpMixed::Int(1), + "Invalid script name \"%s\"".to_string(), + false, + ); + + input.borrow_mut().set_argument("script", script)?; + + Ok(()) + })(); } - pub fn execute( + fn execute( &mut self, - input: std::rc::Rc<std::cell::RefCell<dyn InputInterface>>, - output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>, - ) -> Result<i64> { + input: Rc<RefCell<dyn InputInterface>>, + output: Rc<RefCell<dyn OutputInterface>>, + ) -> anyhow::Result<i64> { if input .borrow() .get_option("list")? @@ -253,64 +319,23 @@ impl RunScriptCommand { .dispatch_script(&script, dev_mode, args, IndexMap::new())?) } - fn list_scripts( + fn initialize( &mut self, - output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>, - ) -> Result<i64> { - let scripts = self.get_scripts()?; - if scripts.is_empty() { - return Ok(0); - } - - let io = self.get_io(); - io.write_error("<info>scripts:</info>"); - let table: Vec<PhpMixed> = scripts - .iter() - .map(|(name, desc)| { - PhpMixed::List(vec![ - Box::new(PhpMixed::String(format!(" {}", name))), - Box::new(PhpMixed::String(desc.clone())), - ]) - }) - .collect(); - - self.render_table(table, output); - - Ok(0) + input: Rc<RefCell<dyn InputInterface>>, + output: Rc<RefCell<dyn OutputInterface>>, + ) -> anyhow::Result<()> { + base_command_initialize(self, input, output) } - fn get_scripts(&mut self) -> Result<Vec<(String, String)>> { - let composer = self.require_composer(None, None)?; - let scripts = crate::command::composer_full(&composer) - .get_package() - .get_scripts(); - drop(composer); - if scripts.is_empty() { - return Ok(vec![]); - } - - let mut result: Vec<(String, String)> = vec![]; - for (name, _script) in scripts { - // PHP: $cmd = $this->getApplication()->find($name); $description = $cmd->getDescription(); - // TODO(phase-c): Application::find returns PhpMixed (the Symfony command registry is a - // todo!() stub) and get_application() is itself deferred, so the resolved command's - // getDescription() cannot be read; the description stays empty until the typed command - // registry is modelled. - let _ = self.get_application()?.find(&name); - let description = String::new(); - result.push((name, description)); - } - - Ok(result) - } + shirabe_external_packages::delegate_command_trait_impls_to_inner!(base_command_data); } -impl HasBaseCommandData for RunScriptCommand { - fn base_command_data(&self) -> &BaseCommandData { - &self.base_command_data +impl BaseCommand for RunScriptCommand { + fn command_data_mut( + &mut self, + ) -> &mut shirabe_external_packages::symfony::console::command::command::CommandData { + self.base_command_data.command_data_mut() } - fn base_command_data_mut(&mut self) -> &mut BaseCommandData { - &mut self.base_command_data - } + crate::delegate_base_command_trait_impls_to_inner!(base_command_data); } |
