xbase-lab / xcodeproj

XcodeProj parser and data representation
5 stars 2 forks source link

file exists but doesn't exists in self. #5

Open github-actions[bot] opened 2 years ago

github-actions[bot] commented 2 years ago

}

return Ok(existing_file);

}

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


            }
        })
    }

    /// 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 {}