Skip to content

Commit b85fbd0

Browse files
committed
ref(pbxproj): move full_path fn to another file
1 parent 089d681 commit b85fbd0

File tree

2 files changed

+166
-159
lines changed

2 files changed

+166
-159
lines changed

src/pbxproj/object/fs/full_path.rs

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
use super::*;
2+
3+
impl PBXFSReference {
4+
/// Returns a file path to current fs reference using source root.
5+
pub fn full_path<P: AsRef<Path>>(&self, source_root: P) -> Result<PathBuf> {
6+
let source_root = source_root.as_ref();
7+
8+
let path = || {
9+
self.path()
10+
.ok_or_else(|| anyhow::anyhow!("Expected path to be set in file element!!"))
11+
};
12+
13+
fn get_parts(path: &String) -> Vec<&str> {
14+
if path.contains("/") {
15+
path.split("/").collect()
16+
} else {
17+
vec![path]
18+
}
19+
}
20+
21+
match self.source_tree() {
22+
Some(PBXSourceTree::Absolute) => path()?.pipe(PathBuf::from),
23+
Some(PBXSourceTree::SourceRoot) => {
24+
let mut root = source_root.to_path_buf();
25+
root.extend(get_parts(path()?));
26+
root
27+
}
28+
Some(PBXSourceTree::Group) => {
29+
let mut group_path: PathBuf;
30+
31+
if let Some(parent) = self.parent() {
32+
println!("Using parent path");
33+
group_path = parent.borrow().full_path(&source_root)?;
34+
if let Some(path) = self.path() {
35+
group_path.extend(get_parts(path))
36+
}
37+
} else {
38+
let objects = self
39+
.objects
40+
.upgrade()
41+
.ok_or_else(|| anyhow::anyhow!("objects is released already!"))?;
42+
43+
let objects = objects.borrow();
44+
45+
if objects
46+
.projects()
47+
.into_iter()
48+
.find(|(_, p)| &*p.borrow().main_group().borrow() == self)
49+
.is_some()
50+
{
51+
if let Some(path) = self.path() {
52+
let mut root = source_root.to_path_buf();
53+
root.extend(get_parts(path));
54+
println!("Joining {source_root:?} with {path:?}");
55+
return Ok(root);
56+
} else {
57+
println!("Self is main group and return source_root as is!");
58+
return Ok(source_root.to_path_buf());
59+
}
60+
}
61+
62+
println!("Falling back to search through all groups");
63+
64+
// Fallback if parent is nil and it's not root element
65+
let group = objects
66+
.groups()
67+
.into_iter()
68+
.find(|(_, o)| {
69+
o.borrow()
70+
.children()
71+
.into_iter()
72+
.any(|o| &*o.borrow() == self)
73+
})
74+
.map(|(_, o)| o)
75+
.ok_or_else(|| {
76+
anyhow::anyhow!(
77+
"Invalid group path {source_root:?} with {:?}",
78+
self.path()
79+
)
80+
})?;
81+
82+
group_path = group.borrow().full_path(source_root)?;
83+
}
84+
group_path
85+
}
86+
_ => {
87+
bail!("Can't get full_path from {:#?}", self)
88+
}
89+
}
90+
.pipe(Ok)
91+
}
92+
}
93+
94+
#[cfg(test)]
95+
mod tests {
96+
use super::*;
97+
#[test]
98+
fn get_root_full_path() {
99+
use crate::pbxproj::test_demo_file;
100+
let project = test_demo_file!(demo1);
101+
let main_group = project
102+
.objects()
103+
.projects()
104+
.first()
105+
.unwrap()
106+
.1
107+
.borrow()
108+
.main_group();
109+
110+
let root = PathBuf::from("/path/to/project");
111+
let main_group = main_group.borrow();
112+
let main_group_full_path = main_group.full_path(&root);
113+
assert_eq!(main_group_full_path.unwrap(), root);
114+
}
115+
116+
#[test]
117+
fn get_subgroup_full_path() {
118+
let root = PathBuf::from("/path/to/project");
119+
let project = crate::pbxproj::test_demo_file!(demo1);
120+
121+
let source_group = project
122+
.objects()
123+
.groups()
124+
.into_iter()
125+
.find(|(_, o)| o.borrow().path().map(|p| p == "Source").unwrap_or_default())
126+
.map(|(_, o)| o.clone())
127+
.unwrap();
128+
129+
let source_group = source_group.borrow();
130+
let source_group_full_path = source_group.full_path(&root);
131+
assert_eq!(source_group_full_path.unwrap(), root.join("Source"));
132+
}
133+
134+
#[test]
135+
fn get_file_full_path() {
136+
let root = PathBuf::from("/path/to/project");
137+
let project = crate::pbxproj::test_demo_file!(demo1);
138+
139+
let mut expected_file_path = root.clone();
140+
expected_file_path.extend(&["Source", "Views", "GuessView.swift"]);
141+
142+
let file = project
143+
.objects()
144+
.get_fs_references(|fs_reference| {
145+
fs_reference
146+
.path()
147+
.map(|name| name == "GuessView.swift")
148+
.unwrap_or_default()
149+
})
150+
.first()
151+
.map(|(_, o)| o.clone())
152+
.unwrap();
153+
154+
let file = file.borrow();
155+
156+
assert_eq!(file.full_path(root).unwrap(), expected_file_path)
157+
}
158+
}

src/pbxproj/object/fs/mod.rs

Lines changed: 8 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod full_path;
12
mod kind;
23
mod obj;
34
mod setget;
@@ -123,95 +124,6 @@ impl PBXFSReference {
123124
pub fn parent(&self) -> Option<Rc<RefCell<Self>>> {
124125
self.parent.upgrade()
125126
}
126-
127-
/// Returns a file path to current fs reference using source root.
128-
pub fn full_path<P: AsRef<Path>>(&self, source_root: P) -> Result<PathBuf> {
129-
let source_root = source_root.as_ref();
130-
131-
let path = || {
132-
self.path()
133-
.ok_or_else(|| anyhow::anyhow!("Expected path to be set in file element!!"))
134-
};
135-
136-
fn get_parts(path: &String) -> Vec<&str> {
137-
if path.contains("/") {
138-
path.split("/").collect()
139-
} else {
140-
vec![path]
141-
}
142-
}
143-
144-
match self.source_tree() {
145-
Some(PBXSourceTree::Absolute) => path()?.pipe(PathBuf::from),
146-
Some(PBXSourceTree::SourceRoot) => {
147-
let mut root = source_root.to_path_buf();
148-
root.extend(get_parts(path()?));
149-
root
150-
}
151-
Some(PBXSourceTree::Group) => {
152-
let mut group_path: PathBuf;
153-
154-
if let Some(parent) = self.parent() {
155-
println!("Using parent path");
156-
group_path = parent.borrow().full_path(&source_root)?;
157-
if let Some(path) = self.path() {
158-
group_path.extend(get_parts(path))
159-
}
160-
} else {
161-
let objects = self
162-
.objects
163-
.upgrade()
164-
.ok_or_else(|| anyhow::anyhow!("objects is released already!"))?;
165-
166-
let objects = objects.borrow();
167-
168-
if objects
169-
.projects()
170-
.into_iter()
171-
.find(|(_, p)| &*p.borrow().main_group().borrow() == self)
172-
.is_some()
173-
{
174-
if let Some(path) = self.path() {
175-
let mut root = source_root.to_path_buf();
176-
root.extend(get_parts(path));
177-
println!("Joining {source_root:?} with {path:?}");
178-
return Ok(root);
179-
} else {
180-
println!("Self is main group and return source_root as is!");
181-
return Ok(source_root.to_path_buf());
182-
}
183-
}
184-
185-
println!("Falling back to search through all groups");
186-
187-
// Fallback if parent is nil and it's not root element
188-
let group = objects
189-
.groups()
190-
.into_iter()
191-
.find(|(_, o)| {
192-
o.borrow()
193-
.children()
194-
.into_iter()
195-
.any(|o| &*o.borrow() == self)
196-
})
197-
.map(|(_, o)| o)
198-
.ok_or_else(|| {
199-
anyhow::anyhow!(
200-
"Invalid group path {source_root:?} with {:?}",
201-
self.path()
202-
)
203-
})?;
204-
205-
group_path = group.borrow().full_path(source_root)?;
206-
}
207-
group_path
208-
}
209-
_ => {
210-
bail!("Can't get full_path from {:#?}", self)
211-
}
212-
}
213-
.pipe(Ok)
214-
}
215127
}
216128

217129
impl Eq for PBXFSReference {}
@@ -241,35 +153,10 @@ impl PartialEq for PBXFSReference {
241153
}
242154
}
243155

244-
#[test]
245-
fn test_parent() {
246-
use crate::pbxproj::test_demo_file;
247-
let project = test_demo_file!(demo1);
248-
let main_group = project
249-
.objects()
250-
.projects()
251-
.first()
252-
.unwrap()
253-
.1
254-
.borrow()
255-
.main_group();
256-
257-
let main_group = main_group.borrow();
258-
let source_group = main_group.get_subgroup("Source").unwrap();
259-
let source_group = source_group.borrow();
260-
let parent = source_group.parent();
261-
262-
assert_eq!(
263-
parent.unwrap().borrow().children_references(),
264-
main_group.children_references()
265-
)
266-
}
267156
#[cfg(test)]
268157
mod tests {
269-
use super::*;
270-
271158
#[test]
272-
fn get_root_full_path() {
159+
fn get_parent() {
273160
use crate::pbxproj::test_demo_file;
274161
let project = test_demo_file!(demo1);
275162
let main_group = project
@@ -281,52 +168,14 @@ mod tests {
281168
.borrow()
282169
.main_group();
283170

284-
let root = PathBuf::from("/path/to/project");
285171
let main_group = main_group.borrow();
286-
let main_group_full_path = main_group.full_path(&root);
287-
assert_eq!(main_group_full_path.unwrap(), root);
288-
}
289-
290-
#[test]
291-
fn get_subgroup_full_path() {
292-
let root = PathBuf::from("/path/to/project");
293-
let project = crate::pbxproj::test_demo_file!(demo1);
294-
295-
let source_group = project
296-
.objects()
297-
.groups()
298-
.into_iter()
299-
.find(|(_, o)| o.borrow().path().map(|p| p == "Source").unwrap_or_default())
300-
.map(|(_, o)| o.clone())
301-
.unwrap();
302-
172+
let source_group = main_group.get_subgroup("Source").unwrap();
303173
let source_group = source_group.borrow();
304-
let source_group_full_path = source_group.full_path(&root);
305-
assert_eq!(source_group_full_path.unwrap(), root.join("Source"));
306-
}
307-
308-
#[test]
309-
fn get_file_full_path() {
310-
let root = PathBuf::from("/path/to/project");
311-
let project = crate::pbxproj::test_demo_file!(demo1);
312-
313-
let mut expected_file_path = root.clone();
314-
expected_file_path.extend(&["Source", "Views", "GuessView.swift"]);
315-
316-
let file = project
317-
.objects()
318-
.get_fs_references(|fs_reference| {
319-
fs_reference
320-
.path()
321-
.map(|name| name == "GuessView.swift")
322-
.unwrap_or_default()
323-
})
324-
.first()
325-
.map(|(_, o)| o.clone())
326-
.unwrap();
327-
328-
let file = file.borrow();
174+
let parent = source_group.parent();
329175

330-
assert_eq!(file.full_path(root).unwrap(), expected_file_path)
176+
assert_eq!(
177+
parent.unwrap().borrow().children_references(),
178+
main_group.children_references()
179+
)
331180
}
332181
}

0 commit comments

Comments
 (0)