1+ use tap:: Pipe ;
2+
13use crate :: pbxproj:: * ;
24use 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
115226impl 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