Skip to content

Commit 0fec427

Browse files
committed
feat(pbxproj): add extra helper methods to Root Object
1 parent dedff8c commit 0fec427

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/pbxproj/object/collection.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ impl PBXObjectCollection {
5858
.collect()
5959
}
6060

61+
/// Get all PBXTargets
62+
pub fn targets<'a>(&'a self) -> Vec<(String, Rc<RefCell<PBXTarget>>)> {
63+
self.iter()
64+
.filter(|o| o.1.is_pbx_target())
65+
.map(|(k, o)| (k.clone(), o.as_pbx_target().unwrap().clone()))
66+
.collect()
67+
}
68+
6169
pub(crate) fn get_fs_references<'a>(
6270
&'a self,
6371
predict: fn(Ref<PBXFSReference>) -> bool,

src/pbxproj/rep.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{PBXHashMap, PBXObject, PBXObjectCollection};
1+
use super::{PBXFSReference, PBXHashMap, PBXObject, PBXObjectCollection, PBXProject, PBXTarget};
22
use anyhow::Result;
33
use std::{
44
cell::{Ref, RefCell, RefMut},
@@ -65,6 +65,29 @@ impl PBXRootObject {
6565
pub fn clone_objects<'a>(&'a self) -> Weak<RefCell<PBXObjectCollection>> {
6666
Rc::downgrade(&self.objects)
6767
}
68+
69+
/// Get Project's targets
70+
pub fn targets(&self) -> Vec<Rc<RefCell<PBXTarget>>> {
71+
self.objects()
72+
.targets()
73+
.into_iter()
74+
.map(|(_, o)| o)
75+
.collect()
76+
}
77+
78+
/// Get Root PBXProject
79+
pub fn root_project(&self) -> Option<Rc<RefCell<PBXProject>>> {
80+
self.objects()
81+
.projects()
82+
.into_iter()
83+
.find(|(k, _)| k == self.root_object_reference())
84+
.map(|(_, o)| o)
85+
}
86+
87+
/// Get root group
88+
pub fn root_group(&self) -> Option<Rc<RefCell<PBXFSReference>>> {
89+
Some(self.root_project()?.borrow().main_group())
90+
}
6891
}
6992

7093
impl TryFrom<PBXHashMap> for PBXRootObject {
@@ -142,7 +165,9 @@ impl TryFrom<PathBuf> for PBXRootObject {
142165
fn test_parse() {
143166
let test_content = include_str!("../../tests/samples/demo1.pbxproj");
144167
let project = PBXRootObject::try_from(test_content).unwrap();
145-
println!("{project:#?}");
168+
169+
println!("{:#?}", project.root_project());
170+
println!("{:#?}", project.targets());
146171
}
147172

148173
#[cfg(test)]

0 commit comments

Comments
 (0)