Skip to content

Commit 53813b7

Browse files
committed
feat(pbxproj): PBXProject helper methods
1 parent f1be75c commit 53813b7

File tree

2 files changed

+133
-20
lines changed

2 files changed

+133
-20
lines changed

src/pbxproj/object/product_type.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use tap::Pipe;
22

33
/// Target Product Type
4-
#[derive(Default, Debug, Hash, PartialEq, Eq)]
4+
#[derive(Debug, Hash, PartialEq, Eq)]
55
pub enum PBXProductType {
66
/// Application
77
Application,
@@ -62,10 +62,15 @@ pub enum PBXProductType {
6262
/// SystemExtension
6363
SystemExtension,
6464
/// None Identified
65-
#[default]
6665
None,
6766
}
6867

68+
impl Default for PBXProductType {
69+
fn default() -> Self {
70+
Self::None
71+
}
72+
}
73+
6974
impl PBXProductType {
7075
/// Return file extension for product type
7176
pub fn file_extension(&self) -> Option<&str> {

src/pbxproj/object/project.rs

Lines changed: 126 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use tap::Pipe;
2+
13
use crate::pbxproj::*;
24
use std::{cell::RefCell, collections::HashMap, rc::Rc};
35

@@ -32,7 +34,7 @@ pub struct PBXProject {
3234
/// The objects are a reference to a PBXTarget element.
3335
target_references: Vec<String>,
3436
/// Project references.
35-
project_references: Vec<HashMap<String, String>>,
37+
project_references: Option<Vec<HashMap<String, String>>>,
3638
/// The object is a reference to a PBXGroup element.
3739
products_group_reference: Option<String>,
3840
/// The object is a reference to a PBXGroup element.
@@ -64,7 +66,6 @@ impl PBXProject {
6466
}
6567

6668
/// Get targets for given reference
67-
/// TODO: Wrap target
6869
#[must_use]
6970
pub fn targets(&self) -> Vec<Rc<RefCell<PBXTarget>>> {
7071
let objects = if let Some(objects) = self.objects.upgrade() {
@@ -84,12 +85,111 @@ impl PBXProject {
8485

8586
/// Returns the attributes of a given target.
8687
#[must_use]
87-
fn get_attributes_for_target_reference(&self, target_reference: &str) -> Option<&PBXHashMap> {
88+
pub fn get_attributes_for_target_reference(
89+
&self,
90+
target_reference: &str,
91+
) -> Option<&PBXHashMap> {
8892
self.target_attribute_references
8993
.as_ref()?
9094
.get(target_reference)?
9195
.as_object()
9296
}
97+
98+
/// Returns the attributes of a given target.
99+
#[must_use]
100+
pub fn get_attributes_for_target_reference_mut(
101+
&mut self,
102+
target_reference: &str,
103+
) -> Option<&mut PBXHashMap> {
104+
self.target_attribute_references
105+
.as_mut()?
106+
.get_mut(target_reference)?
107+
.as_object_mut()
108+
}
109+
110+
/// Git build configuration list
111+
pub fn build_configuration_list(&self) -> Option<Rc<RefCell<XCConfigurationList>>> {
112+
self.objects
113+
.upgrade()?
114+
.borrow()
115+
.get(&self.build_configuration_list_reference)?
116+
.as_xc_configuration_list()?
117+
.clone()
118+
.pipe(Some)
119+
}
120+
121+
/// Get project projects
122+
pub fn projects(&self) -> Option<Vec<HashMap<&String, PBXObject>>> {
123+
self.project_references
124+
.as_ref()?
125+
.iter()
126+
.map(|v| {
127+
v.iter()
128+
.map(|(k, v)| Some((k, self.objects.upgrade()?.borrow().get(v)?.clone())))
129+
.flatten()
130+
.collect::<HashMap<_, _>>()
131+
})
132+
.collect::<Vec<_>>()
133+
.pipe(Some)
134+
}
135+
136+
/// Get Project main group.
137+
pub fn main_group(&self) -> Rc<RefCell<PBXGroup>> {
138+
self.objects
139+
.upgrade()
140+
.expect("objects weak is valid")
141+
.borrow()
142+
.get(&self.main_group_reference)
143+
.expect("PBXProject should contain mainGroup")
144+
.as_pbx_group()
145+
.expect("given reference point to PBXGroup")
146+
.clone()
147+
}
148+
149+
/// Products Group
150+
pub fn products_group(&self) -> Option<Rc<RefCell<PBXGroup>>> {
151+
let products_group = self.products_group_reference.as_ref()?;
152+
self.objects
153+
.upgrade()?
154+
.borrow()
155+
.get(products_group)?
156+
.as_pbx_group()?
157+
.clone()
158+
.pipe(Some)
159+
}
160+
161+
/// Get attributes for a given target reference
162+
pub fn get_target_attributes(&mut self, target_reference: &str) -> Option<&PBXHashMap> {
163+
let target_attributes = self.target_attribute_references.as_mut()?;
164+
target_attributes.get(target_reference)?.as_object()
165+
}
166+
167+
/// Sets the attributes for the given target.
168+
pub fn set_target_attributes(&mut self, attributes: PBXHashMap, target_reference: &str) {
169+
let target_attributes = self
170+
.target_attribute_references
171+
.get_or_insert(Default::default());
172+
target_attributes.insert(target_reference.into(), attributes.into());
173+
}
174+
175+
/// Remove attributes for a given target reference
176+
pub fn remove_target_attributes(&mut self, target_reference: &str) -> Option<PBXHashMap> {
177+
if let Some(target_attributes) = self.target_attribute_references.as_mut() {
178+
target_attributes
179+
.remove(target_reference)?
180+
.into_object()
181+
.ok()
182+
} else {
183+
None
184+
}
185+
}
186+
187+
/// Removes the all the target attributes
188+
pub fn clear_all_target_attributes(&mut self) {
189+
if let Some(target_attributes) = self.target_attribute_references.as_mut() {
190+
target_attributes.clear();
191+
}
192+
}
93193
}
94194

95195
#[test]
@@ -105,11 +205,22 @@ fn test_collections() {
105205
.find(|s| s.1.is_pbx_project())
106206
.map(|(_, o)| o.as_pbx_project().unwrap().clone())
107207
.unwrap();
208+
108209
let project = project.borrow();
109210
let packages = project.packages();
110211
let targets = project.targets();
212+
let build_configuration_list = project.build_configuration_list();
213+
let projects = project.projects();
214+
let main_group = project.main_group();
215+
let products_group = project.products_group();
216+
217+
println!("Project: {:#?}", project);
111218
println!("Packages: {:#?}", packages);
112219
println!("Targets: {:#?}", targets);
220+
println!("build_configuration_list: {:#?}", build_configuration_list);
221+
println!("projects: {:#?}", projects);
222+
println!("main_group: {:#?}", main_group);
223+
println!("products_group: {:#?}", products_group);
113224
}
114225

115226
impl PBXObjectExt for PBXProject {
@@ -156,21 +267,18 @@ impl PBXObjectExt for PBXProject {
156267
.try_remove_value("buildConfigurationList")?
157268
.try_into()?,
158269
target_references: value.try_remove_value("targets")?.try_into()?,
159-
project_references: value
160-
.remove_vec("projectReferences")
161-
.map(|v| {
162-
v.0.into_iter()
163-
.map(|v| v.try_into_object())
164-
.flatten()
165-
.map(|v| {
166-
v.0.into_iter()
167-
.map(|(k, v)| anyhow::Ok((k, v.try_into_string()?)))
168-
.flatten()
169-
.collect()
170-
})
171-
.collect::<Vec<_>>()
172-
})
173-
.unwrap_or_default(),
270+
project_references: value.remove_vec("projectReferences").map(|v| {
271+
v.0.into_iter()
272+
.map(|v| v.try_into_object())
273+
.flatten()
274+
.map(|v| {
275+
v.0.into_iter()
276+
.map(|(k, v)| anyhow::Ok((k, v.try_into_string()?)))
277+
.flatten()
278+
.collect()
279+
})
280+
.collect::<Vec<_>>()
281+
}),
174282
main_group_reference: value.try_remove_value("mainGroup")?.try_into()?,
175283
products_group_reference: value
176284
.remove_value("productRefGroup")

0 commit comments

Comments
 (0)