|
1 | 1 | //! pbxproj file serialize and deserializer |
2 | 2 | mod object; |
3 | | -mod rep; |
4 | 3 | mod value; |
5 | 4 |
|
6 | 5 | pub(crate) mod pest; |
7 | 6 | pub use object::*; |
8 | | -pub use rep::*; |
9 | 7 | pub use value::*; |
| 8 | + |
| 9 | +use anyhow::Result; |
| 10 | +use std::path::{Path, PathBuf}; |
| 11 | +use tap::Pipe; |
| 12 | + |
| 13 | +/// `Main` Representation of project.pbxproj file |
| 14 | +#[derive(Debug, derive_new::new, derive_deref_rs::Deref)] |
| 15 | +pub struct PBXRootObject { |
| 16 | + /// archiveVersion |
| 17 | + archive_version: u8, |
| 18 | + /// objectVersion |
| 19 | + object_version: u8, |
| 20 | + /// classes |
| 21 | + classes: PBXHashMap, |
| 22 | + /// Objects |
| 23 | + #[deref] |
| 24 | + objects: PBXObjectCollection, |
| 25 | + /// rootObjectReference |
| 26 | + root_object_reference: String, |
| 27 | +} |
| 28 | + |
| 29 | +impl PBXRootObject { |
| 30 | + /// Get the pbxproject's archive version. |
| 31 | + #[must_use] |
| 32 | + pub fn archive_version(&self) -> u8 { |
| 33 | + self.archive_version |
| 34 | + } |
| 35 | + |
| 36 | + /// Get the pbxproject's object version. |
| 37 | + #[must_use] |
| 38 | + pub fn object_version(&self) -> u8 { |
| 39 | + self.object_version |
| 40 | + } |
| 41 | + |
| 42 | + /// Get a reference to the pbxproject's classes. |
| 43 | + #[must_use] |
| 44 | + pub fn classes(&self) -> &PBXHashMap { |
| 45 | + &self.classes |
| 46 | + } |
| 47 | + |
| 48 | + /// Get a reference to the pbxproject's root object reference. |
| 49 | + #[must_use] |
| 50 | + pub fn root_object_reference(&self) -> &str { |
| 51 | + self.root_object_reference.as_ref() |
| 52 | + } |
| 53 | + |
| 54 | + /// Get Root PBXProject |
| 55 | + pub fn root_project(&self) -> PBXProject { |
| 56 | + self.objects |
| 57 | + .projects() |
| 58 | + .into_iter() |
| 59 | + .find(|o| o.id == self.root_object_reference()) |
| 60 | + .unwrap() |
| 61 | + } |
| 62 | + |
| 63 | + /// Get root group |
| 64 | + pub fn root_group(&self) -> PBXFSReference { |
| 65 | + self.root_project().main_group |
| 66 | + } |
| 67 | + |
| 68 | + /// Get a reference to the pbxroot object's objects. |
| 69 | + #[must_use] |
| 70 | + pub fn objects(&self) -> &PBXObjectCollection { |
| 71 | + &self.objects |
| 72 | + } |
| 73 | + |
| 74 | + /// Get a mutable reference to the pbxroot object's objects. |
| 75 | + #[must_use] |
| 76 | + pub fn objects_mut(&mut self) -> &mut PBXObjectCollection { |
| 77 | + &mut self.objects |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl TryFrom<PBXHashMap> for PBXRootObject { |
| 82 | + type Error = anyhow::Error; |
| 83 | + fn try_from(mut map: PBXHashMap) -> Result<Self> { |
| 84 | + let archive_version = map.try_remove_number("archiveVersion")? as u8; |
| 85 | + let object_version = map.try_remove_number("objectVersion")? as u8; |
| 86 | + let classes = map.try_remove_object("classes").unwrap_or_default(); |
| 87 | + let root_object_reference = map.try_remove_string("rootObject")?; |
| 88 | + let objects = PBXObjectCollection( |
| 89 | + map.try_remove_object("objects")? |
| 90 | + .0 |
| 91 | + .into_iter() |
| 92 | + .map(|(k, v)| (k, v.try_into_object().unwrap())) |
| 93 | + .collect(), |
| 94 | + ); |
| 95 | + |
| 96 | + Ok(Self { |
| 97 | + archive_version, |
| 98 | + object_version, |
| 99 | + classes, |
| 100 | + objects, |
| 101 | + root_object_reference, |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl TryFrom<&str> for PBXRootObject { |
| 107 | + type Error = anyhow::Error; |
| 108 | + fn try_from(content: &str) -> Result<Self> { |
| 109 | + use crate::pbxproj::pest::PBXProjectParser; |
| 110 | + |
| 111 | + PBXProjectParser::try_from_str(content)?.pipe(Self::try_from) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl TryFrom<String> for PBXRootObject { |
| 116 | + type Error = anyhow::Error; |
| 117 | + fn try_from(content: String) -> Result<Self> { |
| 118 | + Self::try_from(content.as_str()) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl TryFrom<&Path> for PBXRootObject { |
| 123 | + type Error = anyhow::Error; |
| 124 | + |
| 125 | + fn try_from(value: &Path) -> Result<Self> { |
| 126 | + std::fs::read_to_string(&value) |
| 127 | + .map_err(|e| anyhow::anyhow!("PBXProjectData from path {value:?}: {e}"))? |
| 128 | + .pipe(TryFrom::try_from) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +impl TryFrom<PathBuf> for PBXRootObject { |
| 133 | + type Error = anyhow::Error; |
| 134 | + |
| 135 | + fn try_from(value: PathBuf) -> Result<Self> { |
| 136 | + Self::try_from(value.as_path()) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +#[test] |
| 141 | +fn test_demo1_representation() { |
| 142 | + let test_content = include_str!("../../tests/samples/demo1.pbxproj"); |
| 143 | + let project = PBXRootObject::try_from(test_content).unwrap(); |
| 144 | + let targets = project.targets(); |
| 145 | + |
| 146 | + assert_eq!(1, targets.len()); |
| 147 | + assert_eq!(&PBXTargetKind::Native, targets[0].kind); |
| 148 | + assert_eq!(Some(&String::from("Wordle")), targets[0].product_name); |
| 149 | + assert_eq!(Some(&String::from("Wordle")), targets[0].name); |
| 150 | + assert_eq!(PBXProductType::Application, targets[0].product_type); |
| 151 | + assert_eq!(None, targets[0].build_tool_path); |
| 152 | + assert_eq!(None, targets[0].build_arguments_string); |
| 153 | + assert_eq!(None, targets[0].build_working_directory); |
| 154 | + assert_eq!(None, targets[0].pass_build_settings_in_environment); |
| 155 | + assert_eq!(3, targets[0].build_phases.len()); |
| 156 | + assert_eq!( |
| 157 | + vec![ |
| 158 | + (&PBXBuildPhaseKind::Sources, 12), // 12 |
| 159 | + (&PBXBuildPhaseKind::Resources, 3), // 3 |
| 160 | + (&PBXBuildPhaseKind::Frameworks, 1) // 1 |
| 161 | + ], |
| 162 | + targets[0] |
| 163 | + .build_phases |
| 164 | + .iter() |
| 165 | + .map(|phase| (&phase.kind, phase.files.len())) |
| 166 | + .collect::<Vec<_>>() |
| 167 | + ); |
| 168 | + |
| 169 | + assert_eq!(1, project.projects().len()); |
| 170 | + |
| 171 | + let root_group = project.root_group(); |
| 172 | + assert_eq!(17, project.files().len()); |
| 173 | + println!("{:#?}", root_group.children); |
| 174 | + assert_eq!(3, root_group.children.len()); |
| 175 | + assert_eq!(None, root_group.name); |
| 176 | + assert_eq!(None, root_group.path); |
| 177 | +} |
| 178 | + |
| 179 | +#[cfg(test)] |
| 180 | +macro_rules! test_demo_file { |
| 181 | + ($name:expr) => {{ |
| 182 | + let (root, name) = (env!("CARGO_MANIFEST_DIR"), stringify!($name)); |
| 183 | + let path = format!("{root}/tests/samples/{name}.pbxproj"); |
| 184 | + let file = crate::pbxproj::PBXRootObject::try_from(std::path::PathBuf::from(path)); |
| 185 | + if file.is_err() { |
| 186 | + println!("Error: {:#?}", file.as_ref().unwrap_err()) |
| 187 | + } |
| 188 | + assert!(file.is_ok()); |
| 189 | + file.unwrap() |
| 190 | + }}; |
| 191 | +} |
| 192 | + |
| 193 | +#[cfg(test)] |
| 194 | +mod tests { |
| 195 | + macro_rules! test_samples { |
| 196 | + ($($name:ident),*) => { |
| 197 | + $(#[test] |
| 198 | + fn $name() { |
| 199 | + test_demo_file!($name); |
| 200 | + })* |
| 201 | + }; |
| 202 | + } |
| 203 | + |
| 204 | + test_samples![demo1, demo2, demo3, demo4, demo5, demo6, demo7, demo8, demo9]; |
| 205 | +} |
0 commit comments