//! ref: composer/src/Composer/Downloader/FossilDownloader.php use crate::config::Config; use crate::downloader::DownloaderInterface; use crate::downloader::VcsDownloaderBase; use crate::io::IOInterface; use crate::io::IOInterfaceImmutable; use crate::package::PackageInterfaceHandle; use crate::util::Filesystem; use crate::util::ProcessExecutor; use anyhow::Result; use shirabe_external_packages::composer::pcre::Preg; use shirabe_php_shim::{PhpMixed, RuntimeException}; #[derive(Debug)] pub struct FossilDownloader { inner: VcsDownloaderBase, } impl FossilDownloader { pub fn new( io: std::rc::Rc>, config: std::rc::Rc>, process: std::rc::Rc>, fs: std::rc::Rc>, ) -> Self { Self { inner: VcsDownloaderBase::new(io, config, Some(process), Some(fs)), } } pub(crate) async fn do_download( &self, _package: PackageInterfaceHandle, _path: String, _url: String, _prev_package: Option, ) -> Result> { Ok(None) } pub(crate) async fn do_install( &self, package: PackageInterfaceHandle, path: String, url: String, ) -> Result> { self.inner.config.borrow_mut().prohibit_url_by_config( &url, Some(&*self.inner.io.borrow()), &indexmap::IndexMap::new(), )?; let repo_file = format!("{}.fossil", path); let real_path = shirabe_php_shim::realpath(&path); self.inner.io.write_error(&format!( "Cloning {}", package.get_source_reference().unwrap_or_default() )); let mut output = String::new(); self.execute( vec![ "fossil".to_string(), "clone".to_string(), "--".to_string(), url, repo_file.clone(), ], None, &mut output, )?; self.execute( vec![ "fossil".to_string(), "open".to_string(), "--nested".to_string(), "--".to_string(), repo_file, ], real_path.clone(), &mut output, )?; self.execute( vec![ "fossil".to_string(), "update".to_string(), "--".to_string(), package .get_source_reference() .unwrap_or_default() .to_string(), ], real_path, &mut output, )?; Ok(None) } pub(crate) async fn do_update( &self, _initial: PackageInterfaceHandle, target: PackageInterfaceHandle, path: String, url: String, ) -> Result> { self.inner.config.borrow_mut().prohibit_url_by_config( &url, Some(&*self.inner.io.borrow()), &indexmap::IndexMap::new(), )?; self.inner.io.write_error(&format!( " Updating to {}", target.get_source_reference().unwrap_or_default() )); if !self.has_metadata_repository(&path) { return Err(RuntimeException { message: format!( "The .fslckout file is missing from {}, see https://getcomposer.org/commit-deps for more information", path ), code: 0, }.into()); } let real_path = shirabe_php_shim::realpath(&path); let mut output = String::new(); self.execute( vec!["fossil".to_string(), "pull".to_string()], real_path.clone(), &mut output, )?; self.execute( vec![ "fossil".to_string(), "up".to_string(), "--".to_string(), target .get_source_reference() .unwrap_or_default() .to_string(), ], real_path, &mut output, )?; Ok(None) } pub fn get_local_changes( &self, _package: PackageInterfaceHandle, path: String, ) -> Option { if !self.has_metadata_repository(&path) { return None; } let mut output = String::new(); self.inner.process.borrow_mut().execute_args( &["fossil".to_string(), "changes".to_string()], &mut output, shirabe_php_shim::realpath(&path), ); let output = output.trim().to_string(); if output.len() > 0 { Some(output) } else { None } } pub(crate) fn get_commit_logs( &self, _from_reference: String, to_reference: String, path: String, ) -> Result { let mut output = String::new(); self.execute( vec![ "fossil".to_string(), "timeline".to_string(), "-t".to_string(), "ci".to_string(), "-W".to_string(), "0".to_string(), "-n".to_string(), "0".to_string(), "before".to_string(), to_reference.clone(), ], shirabe_php_shim::realpath(&path), &mut output, )?; let mut log = String::new(); let match_pattern = format!("/\\d\\d:\\d\\d:\\d\\d\\s+\\[{}\\]/", to_reference); let trimmed = output.trim().to_string(); let lines: Vec = if trimmed.is_empty() { vec![] } else { Preg::split(r"{\r?\n}", &trimmed)? }; for line in lines { if Preg::is_match(&match_pattern, &line)? { break; } log.push_str(&line); } Ok(log) } fn execute( &self, command: Vec, cwd: Option, output: &mut String, ) -> Result<()> { if self .inner .process .borrow_mut() .execute(&command, output, cwd)? != 0 { return Err(RuntimeException { message: format!( "Failed to execute {}\n\n{}", command.join(" "), self.inner.process.borrow().get_error_output() ), code: 0, } .into()); } Ok(()) } pub(crate) fn has_metadata_repository(&self, path: &str) -> bool { std::path::Path::new(&format!("{}/.fslckout", path)).is_file() || std::path::Path::new(&format!("{}/_FOSSIL_", path)).is_file() } } // TODO(phase-b): wire up VcsDownloader trait properly. FossilDownloader extends VcsDownloader // which implements DownloaderInterface in PHP. Delegating each trait method to todo!() until the // inner VcsDownloaderBase exposes the matching impl surface. #[async_trait::async_trait(?Send)] impl DownloaderInterface for FossilDownloader { fn get_installation_source(&self) -> String { todo!() } async fn download( &self, _package: PackageInterfaceHandle, _path: &str, _prev_package: Option, _output: bool, ) -> Result> { todo!() } async fn prepare( &self, _type: &str, _package: PackageInterfaceHandle, _path: &str, _prev_package: Option, ) -> Result> { todo!() } async fn install( &self, _package: PackageInterfaceHandle, _path: &str, _output: bool, ) -> Result> { todo!() } async fn update( &self, _initial: PackageInterfaceHandle, _target: PackageInterfaceHandle, _path: &str, ) -> Result> { todo!() } async fn remove( &self, _package: PackageInterfaceHandle, _path: &str, _output: bool, ) -> Result> { todo!() } async fn cleanup( &self, _type: &str, _package: PackageInterfaceHandle, _path: &str, _prev_package: Option, ) -> Result> { todo!() } }