Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fs] ensure we are not adding a duplication #4

Open
github-actions bot opened this issue Jun 12, 2022 · 0 comments
Open

[fs] ensure we are not adding a duplication #4

github-actions bot opened this issue Jun 12, 2022 · 0 comments
Labels

Comments

@github-actions
Copy link
Contributor

NOTE: This function error because self is already borrowed mutably

if let Some((file_reference, existing_file)) =

objects.files().into_iter().find(|(_, file_reference)| {

let existing_file_ref = file_reference.borrow();

let existing_file_path = if let Some(path) = existing_file_ref.path() {

PathBuf::from(path)

} else {

return file_path

== existing_file_ref

.full_path(&source_root)

.unwrap_or_default();

};

if existing_file_path.components().last() == file_path.components().last() {

file_path == existing_file_ref.full_path(source_root).unwrap_or_default()

} else {

false

}

})

{

if !self

.children_references

.as_ref()

.map(|r| r.contains(&file_reference))

.unwrap_or_default()

{

https://github.com/tami5/xcodeproj/blob/ce7c4f6e52549992e5ea8bdcac697d7a1c92225a/src/pbxproj/object/fs/mod.rs#L173

            }
        })
    }

    /// Add file to a group
    ///
    /// NOTE: This will return None if self is file
    pub fn add_file<P: AsRef<Path>>(
        &mut self,
        file_path: P,
        source_root: P,
        source_tree: Option<PBXSourceTree>,
    ) -> Result<Rc<RefCell<PBXFSReference>>> {
        let (file_path, source_root) = (file_path.as_ref(), source_root.as_ref());

        // if !file_path.exists() {
        //     bail!("Trying to add non-existing file {file_path:?}")
        // }

        let group_path = self.full_path(source_root)?;
        let objects = self
            .objects
            .upgrade()
            .ok_or_else(|| anyhow::anyhow!("objects already released!"))?;
        let mut objects = objects.borrow_mut();

        // TODO(fs): ensure we are not adding a duplication
        //
        // NOTE: This function error because self is already borrowed mutably
        //
        // if let Some((file_reference, existing_file)) =
        //     objects.files().into_iter().find(|(_, file_reference)| {
        //         let existing_file_ref = file_reference.borrow();
        //         let existing_file_path = if let Some(path) = existing_file_ref.path() {
        //             PathBuf::from(path)
        //         } else {
        //             return file_path
        //                 == existing_file_ref
        //                     .full_path(&source_root)
        //                     .unwrap_or_default();
        //         };
        //         if existing_file_path.components().last() == file_path.components().last() {
        //             file_path == existing_file_ref.full_path(source_root).unwrap_or_default()
        //         } else {
        //             false
        //         }
        //     })
        // {
        //     if !self
        //         .children_references
        //         .as_ref()
        //         .map(|r| r.contains(&file_reference))
        //         .unwrap_or_default()
        //     {
        //         // TODO: file exists but doesn't exists in self.
        //     }
        //     return Ok(existing_file);
        // }

        let source_tree = source_tree.unwrap_or_else(|| PBXSourceTree::Group);
        let path: Option<PathBuf> = match &source_tree {
            PBXSourceTree::Group => Some(file_path.strip_prefix(group_path)?.to_path_buf()),
            PBXSourceTree::SourceRoot => Some(file_path.strip_prefix(source_root)?.to_path_buf()),
            PBXSourceTree::Absolute | PBXSourceTree::SdkRoot | PBXSourceTree::DeveloperDir => {
                Some(file_path.to_path_buf())
            }
            _ => None,
        };

        let mut file_reference = PBXFSReference::default();
        file_reference.set_source_tree(source_tree);
        file_reference.set_name(
            file_path
                .file_name()
                .map(|s| s.to_string_lossy().to_string()),
        );

        if let Some(path) = path {
            let path = path.to_string_lossy().to_string().into();
            file_reference.set_path(path);
        }

        let file_extension = file_path.extension().unwrap_or_default().to_string_lossy();
        let file_extension = xcode_file_type(file_extension);

        file_reference.set_explicit_file_type(file_extension.clone());
        file_reference.set_last_known_file_type(file_extension);
        file_reference.set_kind(PBXFSReferenceKind::File);

        let file_reference = Rc::new(RefCell::new(file_reference));
        let reference = objects.push(file_reference.clone());

        let children_references = self.children_references.get_or_insert(Default::default());

        if !children_references.contains(&reference) {
            children_references.insert(reference);
        };

        Ok(file_reference)
    }
}

impl Eq for PBXFSReference {}
@github-actions github-actions bot added the todo label Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants