diff --git a/rs/src/exec.rs b/rs/src/exec.rs index a3f4f14..24b0248 100644 --- a/rs/src/exec.rs +++ b/rs/src/exec.rs @@ -1,5 +1,5 @@ use crate::config::MWUtilConfig; -use crate::types::Container; +use crate::types::{Container, ExecutionEnvironment}; use anyhow::{anyhow, Context}; use std::io::{BufRead, BufReader}; use std::path::Path; @@ -8,6 +8,42 @@ use std::thread; use std::thread::JoinHandle; use crate::farm::{get_db_name, get_wiki_from_param, Wiki}; +pub trait ExecutionHelper { + fn in_environment( + self, + config: &MWUtilConfig, + execution_environment: ExecutionEnvironment, + exec_options: Option<&[String]> + ) -> anyhow::Result where Self: Sized; +} + +impl ExecutionHelper for Command { + fn in_environment( + self, + config: &MWUtilConfig, + execution_environment: ExecutionEnvironment, + exec_options: Option<&[String]> + ) -> anyhow::Result { + match execution_environment { + ExecutionEnvironment::Fresh => { + let mut cmd = Command::new("fresh-node"); + let mut cmd_args: Vec = exec_options.unwrap_or_default().to_vec(); + cmd_args.extend([ + "--".into(), + self.get_program() + .to_string_lossy() + .to_string() + ]); + cmd_args.extend(self.get_args().map(|s| s.to_string_lossy().into_owned())); + cmd.args(&cmd_args); + Ok(cmd) + }, + ExecutionEnvironment::Host => Ok(self), + ExecutionEnvironment::MWContainer(container) => self.in_container(config, container, exec_options) + } + } +} + pub trait ContainerSupport { fn in_container( &self, diff --git a/rs/src/modules/lint.rs b/rs/src/modules/lint.rs index b338d6d..f07e53d 100644 --- a/rs/src/modules/lint.rs +++ b/rs/src/modules/lint.rs @@ -1,14 +1,14 @@ +use crate::Modules; use crate::config::MWUtilConfig; -use crate::exec::ContainerSupport; +use crate::exec::ExecutionHelper; use crate::modules::composer::ComposerArgs; use crate::modules::npm::NpmArgs; -use crate::types::Container; -use crate::Modules; +use crate::types::{Container, ExecutionEnvironment}; +use anyhow::bail; use clap::{Args, ValueEnum}; use std::path::PathBuf; use std::process::{Command, ExitStatus}; use std::str::FromStr; -use anyhow::bail; #[derive(Clone, ValueEnum)] enum LintType { @@ -46,10 +46,10 @@ impl LintType { } } - fn can_run_in_container(&self) -> bool { + fn get_execution_environment(&self) -> ExecutionEnvironment { match self { - LintType::Eslint => false, - LintType::Phan | LintType::Phpcs => true, + LintType::Eslint => ExecutionEnvironment::Fresh, + LintType::Phan | LintType::Phpcs => ExecutionEnvironment::MWContainer(Container::MediaWiki), } } @@ -62,9 +62,7 @@ impl LintType { cmd.current_dir(PathBuf::from_str(folder.as_str())?); } - if self.can_run_in_container() { - cmd = cmd.in_container(config, Container::MediaWiki, None)?; - } + cmd = cmd.in_environment(config, self.get_execution_environment(), None)?; let status = cmd.status()?; Ok(status) diff --git a/rs/src/types.rs b/rs/src/types.rs index 3e40a34..6ea3c8b 100644 --- a/rs/src/types.rs +++ b/rs/src/types.rs @@ -106,6 +106,12 @@ impl Display for MWVersion { } } +pub enum ExecutionEnvironment { + Fresh, + Host, + MWContainer(Container) +} + pub enum Container { Database(DBType), MediaWiki,