Skip to content

Commit 5052874

Browse files
committed
feat(target): got the extra mile to find sdkroot
1 parent e919309 commit 5052874

File tree

5 files changed

+513
-7
lines changed

5 files changed

+513
-7
lines changed

src/pbxproj/mod.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,48 @@ fn test_demo1_representation() {
178178
assert_eq!(None, root_group.path);
179179
}
180180

181+
#[test]
182+
fn test_demo10_representation() {
183+
let test_content = include_str!("../../tests/samples/demo10.pbxproj");
184+
let project = PBXRootObject::try_from(test_content).unwrap();
185+
let targets = project.targets();
186+
187+
assert_eq!(1, targets.len());
188+
assert_eq!(&PBXTargetKind::Native, targets[0].kind);
189+
assert_eq!(Some(&String::from("Scrumdinger")), targets[0].product_name);
190+
assert_eq!(Some(&String::from("Scrumdinger")), targets[0].name);
191+
assert!(!targets[0].sdkroots(project.objects()).is_empty());
192+
assert_eq!(PBXProductType::Application, targets[0].product_type);
193+
assert_eq!(None, targets[0].build_tool_path);
194+
assert_eq!(None, targets[0].build_arguments_string);
195+
assert_eq!(None, targets[0].build_working_directory);
196+
assert_eq!(None, targets[0].pass_build_settings_in_environment);
197+
assert_eq!(3, targets[0].build_phases.len());
198+
assert_eq!(
199+
vec![
200+
(&PBXBuildPhaseKind::Sources, 11),
201+
(&PBXBuildPhaseKind::Frameworks, 0),
202+
(&PBXBuildPhaseKind::Resources, 4)
203+
],
204+
targets[0]
205+
.build_phases
206+
.iter()
207+
.map(|phase| (&phase.kind, phase.files.len()))
208+
.collect::<Vec<_>>()
209+
);
210+
211+
assert_eq!(1, project.projects().len());
212+
let root_project = project.root_project();
213+
println!("{:#?}", root_project.targets[0]);
214+
215+
let root_group = project.root_group();
216+
assert_eq!(17, project.files().len());
217+
// println!("{:#?}", root_group.children);
218+
assert_eq!(5, root_group.children.len());
219+
assert_eq!(None, root_group.name);
220+
assert_eq!(None, root_group.path);
221+
}
222+
181223
#[cfg(test)]
182224
macro_rules! test_demo_file {
183225
($name:expr) => {{

src/pbxproj/object/collection.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl PBXObjectCollection {
5858
/// Get vector by vector of T by predict
5959
pub fn get_vec_by<'a, T: AsPBXObject<'a> + 'a>(
6060
&'a self,
61-
predict: fn(&(&String, &PBXHashMap)) -> bool,
61+
predict: impl Fn(&(&String, &PBXHashMap)) -> bool,
6262
) -> Vec<T> {
6363
self.iter()
6464
.filter(predict)
@@ -93,6 +93,15 @@ impl PBXObjectCollection {
9393
})
9494
}
9595

96+
/// Get all build phases
97+
pub fn build_configurations<'a>(&'a self) -> Vec<PBXBuildPhase<'a>> {
98+
self.get_vec_by(|(_, v)| {
99+
v.get_kind("isa")
100+
.map(|k| k.is_xc_build_configuration())
101+
.unwrap_or_default()
102+
})
103+
}
104+
96105
/// Get all build phases
97106
pub fn build_files<'a>(&'a self) -> Vec<PBXBuildFile<'a>> {
98107
self.get_vec_by(|(_, v)| {
@@ -226,6 +235,22 @@ impl PBXObjectCollection {
226235
})
227236
}
228237

238+
/// Get build configurations shearing a given baseConfiguration id
239+
pub fn get_build_configurations_by_base_id<S: AsRef<str>>(
240+
&self,
241+
id: S,
242+
) -> Vec<XCBuildConfiguration> {
243+
let key = id.as_ref();
244+
self.get_vec_by(move |(_, v)| {
245+
v.get_kind("isa")
246+
.map(|o| o.is_xc_build_configuration())
247+
.unwrap_or_default()
248+
&& v.get_string("baseConfigurationReference")
249+
.map(|s| s.as_str())
250+
== Some(key)
251+
})
252+
}
253+
229254
/// Get XCSwiftPackageProductDependency by reference
230255
pub fn get_swift_package_product_dependency<'a>(
231256
&'a self,

src/pbxproj/object/target.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,31 @@ pub struct PBXTarget<'a> {
4646

4747
impl<'a> PBXTarget<'a> {
4848
/// get target's sdk roots from all build configuration settings
49-
pub fn sdkroots(&self) -> Vec<&String> {
49+
pub fn sdkroots(&'a self, objects: &'a PBXObjectCollection) -> Vec<&'a String> {
5050
if let Some(ref bclist) = self.build_configuration_list {
51-
bclist
51+
let mut sdkroots = bclist
5252
.build_configurations
5353
.iter()
5454
.flat_map(|b| b.build_settings.get_string("SDKROOT"))
55-
.collect::<Vec<&String>>()
55+
.collect::<Vec<&String>>();
56+
if sdkroots.is_empty() {
57+
// sdkroot isn't defined in current build settings.
58+
// Here, we need to find all build configurations sharing
59+
// the same base configuration id
60+
bclist
61+
.build_configurations
62+
.iter()
63+
.flat_map(|b| Some(b.base_configuration.as_ref()?.id.as_str()))
64+
.flat_map(|id| objects.get_build_configurations_by_base_id(id))
65+
.flat_map(|b| b.build_settings.get_string("SDKROOT"))
66+
.for_each(|root| sdkroots.push(root));
67+
}
68+
if sdkroots.is_empty() {
69+
tracing::error!("No SDKROOT found for {:?}", self.name);
70+
}
71+
sdkroots
5672
} else {
73+
tracing::error!("No build configuration list for {:?}", self.name);
5774
Default::default()
5875
}
5976
}

tests/samples/demo1.pbxproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@
158158
LastUpgradeCheck = 1200;
159159
TargetAttributes = {
160160
0B66A7280B2FFE35D5F8E9A6 = {
161-
DevelopmentTeam = 7N5BMV2F5G;
161+
DevelopmentTeam = XXXXXXXXXX;
162162
};
163163
};
164164
};
@@ -331,7 +331,7 @@
331331
buildSettings = {
332332
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
333333
CODE_SIGN_IDENTITY = "iPhone Developer";
334-
DEVELOPMENT_TEAM = 7N5BMV2F5G;
334+
DEVELOPMENT_TEAM = XXXXXXXXXX;
335335
DISABLE_MANUAL_TARGET_ORDER_BUILD_WARNING = YES;
336336
INFOPLIST_FILE = Resources/Info.plist;
337337
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
@@ -350,7 +350,7 @@
350350
buildSettings = {
351351
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
352352
CODE_SIGN_IDENTITY = "iPhone Developer";
353-
DEVELOPMENT_TEAM = 7N5BMV2F5G;
353+
DEVELOPMENT_TEAM = XXXXXXXXXX;
354354
DISABLE_MANUAL_TARGET_ORDER_BUILD_WARNING = YES;
355355
INFOPLIST_FILE = Resources/Info.plist;
356356
IPHONEOS_DEPLOYMENT_TARGET = 15.0;

0 commit comments

Comments
 (0)