From 2c1df4a7ba3a0ec0e9eb01aeb219eefad2c26018 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 12 May 2026 04:10:53 +0900 Subject: feat(port): port Tar.php --- crates/shirabe-php-shim/src/lib.rs | 30 ++++++++++++++++++++ crates/shirabe/src/util/tar.rs | 56 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) (limited to 'crates') diff --git a/crates/shirabe-php-shim/src/lib.rs b/crates/shirabe-php-shim/src/lib.rs index 7fd7ec1..33b3df3 100644 --- a/crates/shirabe-php-shim/src/lib.rs +++ b/crates/shirabe-php-shim/src/lib.rs @@ -58,6 +58,23 @@ impl Phar { } } +#[derive(Debug)] +pub struct PharFileInfo; + +impl PharFileInfo { + pub fn get_content(&self) -> String { + todo!() + } + + pub fn get_basename(&self) -> String { + todo!() + } + + pub fn is_dir(&self) -> bool { + todo!() + } +} + #[derive(Debug)] pub struct PharData { path: String, @@ -68,6 +85,19 @@ impl PharData { todo!() } + pub fn valid(&self) -> bool { + todo!() + } + + pub fn get(&self, key: &str) -> Option { + todo!() + } + + pub fn iter(&self) -> impl Iterator { + todo!(); + std::iter::empty() + } + pub fn extract_to(&self, a: &str, b: Option<()>, c: bool) { todo!() } diff --git a/crates/shirabe/src/util/tar.rs b/crates/shirabe/src/util/tar.rs index 4dc91ca..253f142 100644 --- a/crates/shirabe/src/util/tar.rs +++ b/crates/shirabe/src/util/tar.rs @@ -1 +1,57 @@ //! ref: composer/src/Composer/Util/Tar.php + +use anyhow::Result; +use indexmap::IndexMap; +use shirabe_php_shim::{PharData, RuntimeException}; + +pub struct Tar; + +impl Tar { + pub fn get_composer_json(path_to_archive: &str) -> Result> { + let phar = PharData::new(path_to_archive.to_string()); + + if !phar.valid() { + return Ok(None); + } + + Ok(Some(Self::extract_composer_json_from_folder(&phar)?)) + } + + fn extract_composer_json_from_folder(phar: &PharData) -> Result { + if let Some(file) = phar.get("composer.json") { + return Ok(file.get_content()); + } + + let mut top_level_paths: IndexMap = IndexMap::new(); + for folder_file in phar.iter() { + let name = folder_file.get_basename(); + if folder_file.is_dir() { + top_level_paths.insert(name, true); + if top_level_paths.len() > 1 { + return Err(anyhow::anyhow!(RuntimeException { + message: format!( + "Archive has more than one top level directories, and no composer.json was found on the top level, so it's an invalid archive. Top level paths found were: {}", + top_level_paths.keys().cloned().collect::>().join(",") + ), + code: 0, + })); + } + } + } + + let composer_json_path = format!( + "{}/composer.json", + top_level_paths.keys().next().cloned().unwrap_or_default() + ); + if !top_level_paths.is_empty() { + if let Some(file) = phar.get(&composer_json_path) { + return Ok(file.get_content()); + } + } + + Err(anyhow::anyhow!(RuntimeException { + message: "No composer.json found either at the top level or within the topmost directory".to_string(), + code: 0, + })) + } +} -- cgit v1.3.1