Skip to content

Commit

Permalink
ref(pbxproj): deref root object to object collection
Browse files Browse the repository at this point in the history
  • Loading branch information
kkharji committed Jun 8, 2022
1 parent 23f1a17 commit 9c2a66c
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 42 deletions.
12 changes: 4 additions & 8 deletions src/pbxproj/object/build/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ impl XCConfigurationList {
&'a self,
data: &'a PBXRootObject,
) -> Vec<&'a XCBuildConfiguration> {
let objects = data.objects();
self.build_configuration_references
.iter()
.map(|r| Some(objects.get(r)?.as_xc_build_configuration()?))
.map(|r| Some(data.get(r)?.as_xc_build_configuration()?))
.flatten()
.collect()
}
Expand All @@ -50,16 +49,14 @@ impl XCConfigurationList {
let mut configurations = vec![];
let debug = XCBuildConfiguration::new("Debug".into(), Default::default(), None);
let debug_id = uuid::Uuid::new_v4().to_string();
data.objects_mut()
.insert(debug_id.clone(), PBXObject::XCBuildConfiguration(debug));
data.insert(debug_id.clone(), PBXObject::XCBuildConfiguration(debug));

configurations.push(debug_id);

let release = XCBuildConfiguration::new("Release".into(), Default::default(), None);
let release_id = uuid::Uuid::new_v4().to_string();

data.objects_mut()
.insert(release_id.clone(), PBXObject::XCBuildConfiguration(release));
data.insert(release_id.clone(), PBXObject::XCBuildConfiguration(release));

configurations.push(release_id);

Expand All @@ -70,8 +67,7 @@ impl XCConfigurationList {
pub fn object_with_configuration_list(&self, data: &PBXRootObject) -> Option<&PBXObject> {
// projects, Native target, aggregateTargets, legacyTargets build_configuration_list_reference

let _objects = data.objects();
// objects.iter().find(|o| {
// data.iter().find(|o| {
// match o {
// PBXObject::PBXProject(p) => p
// }
Expand Down
25 changes: 25 additions & 0 deletions src/pbxproj/object/collection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use super::PBXObject;
use std::collections::HashMap;

/// [`PBXObject`] storage with convenient helper methods
#[derive(Debug, derive_new::new, derive_deref_rs::Deref)]
pub struct PBXObjectCollection(HashMap<String, PBXObject>);

impl PBXObjectCollection {
/// Get PBXTarget by the target name
pub fn get_target_by_name(&self, target_name: &str) -> Option<(&String, &PBXObject)> {
self.0.iter().find(|(_, o)| {
let target = match o {
PBXObject::PBXAggregateTarget(ref v) => &v.inner,
PBXObject::PBXLegacyTarget(ref v) => &v.inner,
PBXObject::PBXNativeTarget(ref v) => &v.inner,
_ => return false,
};
if let Some(name) = target.name.as_ref() {
name == target_name
} else {
false
}
})
}
}
3 changes: 1 addition & 2 deletions src/pbxproj/object/file/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ impl PBXGroup {

/// Group children.
pub fn children<'a>(&'a self, data: &'a PBXRootObject) -> Vec<&'a PBXObject> {
let objects = data.objects();
self.children_references
.iter()
.map(|r| objects.get(r))
.map(|r| data.get(r))
.flatten()
.collect::<Vec<_>>()
}
Expand Down
4 changes: 4 additions & 0 deletions src/pbxproj/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ mod project;
mod swift_package;
mod target;

mod collection;
mod kind;
mod product_type;

pub use kind::*;
pub use product_type::*;

Expand All @@ -17,3 +19,5 @@ pub use project::*;

pub use swift_package::*;
pub use target::*;

pub use collection::*;
10 changes: 8 additions & 2 deletions src/pbxproj/object/swift_package/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct XCRemoteSwiftPackageReference {
/// Repository url.
pub repository_url: Option<String>,
/// Version rules.
pub requirement: Option<XCVersionRequirement>,
pub version_requirement: Option<XCVersionRequirement>,
}

impl XCRemoteSwiftPackageReference {
Expand All @@ -21,6 +21,12 @@ impl XCRemoteSwiftPackageReference {
.map(|s| s.split("/").last())
.flatten()
}

/// Get a reference to the xcremote swift package reference's version requirement.
#[must_use]
pub fn version_requirement(&self) -> Option<&XCVersionRequirement> {
self.version_requirement.as_ref()
}
}

impl TryFrom<PBXHashMap> for XCRemoteSwiftPackageReference {
Expand All @@ -32,7 +38,7 @@ impl TryFrom<PBXHashMap> for XCRemoteSwiftPackageReference {
.remove_value("repository_url")
.map(|v| v.try_into().ok())
.flatten(),
requirement: value
version_requirement: value
.remove_value("requirement")
.map(|v| v.try_into().ok())
.flatten(),
Expand Down
2 changes: 1 addition & 1 deletion src/pbxproj/object/swift_package/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::HashMap;
use tap::Pipe;

/// [`XCRemoteSwiftPackageReference`] version rules.
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub enum XCVersionRequirement {
/// Version can be bumped up to the next major version.
UpToNextMajorVersion(String),
Expand Down
2 changes: 1 addition & 1 deletion src/pbxproj/object/target/aggregated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use derive_deref_rs::Deref;
#[derive(Debug, Deref, derive_new::new)]
pub struct PBXAggregateTarget {
#[deref]
inner: PBXTarget,
pub(crate) inner: PBXTarget,
}

impl TryFrom<PBXHashMap> for PBXAggregateTarget {
Expand Down
2 changes: 1 addition & 1 deletion src/pbxproj/object/target/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct PBXLegacyTarget {
/// The directory where the build tool will be invoked during a build
pub build_working_directory: Option<String>,
#[deref]
inner: PBXTarget,
pub(crate) inner: PBXTarget,
}

impl TryFrom<PBXHashMap> for PBXLegacyTarget {
Expand Down
2 changes: 1 addition & 1 deletion src/pbxproj/object/target/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct PBXNativeTarget {
/// Target product install path.
pub product_install_path: Option<String>,
#[deref]
inner: PBXTarget,
pub(crate) inner: PBXTarget,
}

impl PBXNativeTarget {
Expand Down
32 changes: 6 additions & 26 deletions src/pbxproj/rep.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{PBXHashMap, PBXObject};
use super::{PBXHashMap, PBXObject, PBXObjectCollection};
use anyhow::Result;
use std::{
collections::HashMap,
Expand All @@ -7,7 +7,7 @@ use std::{
use tap::Pipe;

/// `Main` Representation of project.pbxproj file
#[derive(Debug, derive_new::new)]
#[derive(Debug, derive_new::new, derive_deref_rs::Deref)]
pub struct PBXRootObject {
/// archiveVersion
archive_version: u8,
Expand All @@ -16,7 +16,8 @@ pub struct PBXRootObject {
/// classes
classes: PBXHashMap,
/// Objects
objects: HashMap<String, PBXObject>,
#[deref]
objects: PBXObjectCollection,
/// rootObjectReference
root_object_reference: String,
}
Expand Down Expand Up @@ -45,28 +46,6 @@ impl PBXRootObject {
pub fn root_object_reference(&self) -> &str {
self.root_object_reference.as_ref()
}

/// Get a reference to the pbxproject data's objects.
// #[must_use]
pub fn objects(&self) -> &HashMap<String, PBXObject> {
&self.objects
}

/// Get a mutable reference to the pbxproject data's objects.
// #[must_use]
pub fn objects_mut(&mut self) -> &mut HashMap<String, PBXObject> {
&mut self.objects
}

/// Get object by reference.
pub fn get_object(&self, reference: &str) -> Option<&PBXObject> {
self.objects.get(reference)
}

/// Get mutable object by reference.
pub fn get_mut_object(&mut self, reference: &str) -> Option<&mut PBXObject> {
self.objects.get_mut(reference)
}
}

impl TryFrom<PBXHashMap> for PBXRootObject {
Expand All @@ -87,7 +66,8 @@ impl TryFrom<PBXHashMap> for PBXRootObject {
.into_iter()
.map(|(k, v)| anyhow::Ok((k, PBXObject::try_from(v)?)))
.flatten()
.collect::<HashMap<_, _>>(),
.collect::<HashMap<_, _>>()
.pipe(PBXObjectCollection::new),

root_object_reference,
})
Expand Down

0 comments on commit 9c2a66c

Please sign in to comment.