# frozen_string_literal: truerequire'xcodeproj/project/object/helpers/groupable_helper'moduleXcodeprojclassProjectmoduleObjectclassFileReferencesFactoryclass<<self# Creates a new reference with the given path and adds it to the# given group. The reference is configured according to the extension# of the path.## @param [PBXGroup] group# The group to which to add the reference.## @param [#to_s] path# The, preferably absolute, path of the reference.## @param [Symbol] source_tree# The source tree key to use to configure the path (@see# GroupableHelper::SOURCE_TREES_BY_KEY).## @return [PBXFileReference, XCVersionGroup] The new reference.#defnew_reference(group,path,source_tree)ref=caseFile.extname(path).downcasewhen'.xcdatamodeld'new_xcdatamodeld(group,path,source_tree)when'.xcodeproj'new_subproject(group,path,source_tree)elsenew_file_reference(group,path,source_tree)endconfigure_defaults_for_file_reference(ref)refend# Creates a file reference to a static library and adds it to the# given group.## @param [PBXGroup] group# The group to which to add the reference.## @param [#to_s] product_basename# The name of the static library.## @return [PBXFileReference] The new file reference.#defnew_product_ref_for_target(group,product_basename,product_type)ifproduct_type==:static_libraryprefix='lib'endextension=Constants::PRODUCT_UTI_EXTENSIONS[product_type]path="#{prefix}#{product_basename}"path+=".#{extension}"ifextensionref=new_reference(group,path,:built_products)ref.include_in_index='0'ref.set_explicit_file_typerefend# Creates a file reference to a new bundle and adds it to the given# group.## @param [PBXGroup] group# The group to which to add the reference.## @param [#to_s] product_basename# The name of the bundle.## @return [PBXFileReference] The new file reference.#defnew_bundle(group,product_basename)ref=new_reference(group,"#{product_basename}.bundle",:built_products)ref.include_in_index='0'ref.set_explicit_file_type('wrapper.cfbundle')refendprivate# @group Private Helpers#-------------------------------------------------------------------## Creates a new file reference with the given path and adds it to the# given group.## @param [PBXGroup] group# The group to which to add the reference.## @param [#to_s] path# The, preferably absolute, path of the reference.## @param [Symbol] source_tree# The source tree key to use to configure the path (@see# GroupableHelper::SOURCE_TREES_BY_KEY).## @return [PBXFileReference] The new file reference.#defnew_file_reference(group,path,source_tree)path=Pathname.new(path)ref=group.project.new(PBXFileReference)group.children<<refGroupableHelper.set_path_with_source_tree(ref,path,source_tree)ref.set_last_known_file_typerefend# Creates a new version group reference to an xcdatamodeled adding# the xcdatamodel files included in the wrapper as children file# references.## @param [PBXGroup] group# The group to which to add the reference.## @param [#to_s] path# The, preferably absolute, path of the reference.## @param [Symbol] source_tree# The source tree key to use to configure the path (@see# GroupableHelper::SOURCE_TREES_BY_KEY).## @note To match Xcode behaviour the current version is read from# the .xccurrentversion file, if it doesn't exist the last# xcdatamodel according to its path is set as the current# version.## @return [XCVersionGroup] The new reference.#defnew_xcdatamodeld(group,path,source_tree)path=Pathname.new(path)ref=group.project.new(XCVersionGroup)group.children<<refGroupableHelper.set_path_with_source_tree(ref,path,source_tree)ref.version_group_type='wrapper.xcdatamodel'real_path=group.real_path.join(path)current_version_name=nilifreal_path.exist?real_path.children.eachdo|child_path|ifFile.extname(child_path)=='.xcdatamodel'new_file_reference(ref,child_path,:group)elsifFile.basename(child_path)=='.xccurrentversion'full_path=real_path+File.basename(child_path)xccurrentversion=Plist.read_from_path(full_path)current_version_name=xccurrentversion['_XCCurrentVersionName']endendifcurrent_version_nameref.current_version=ref.children.finddo|obj|obj.path.split('/').last==current_version_nameendendendrefend# Creates a file reference to another Xcode subproject and setups the# proxies to the targets.## @param [PBXGroup] group# The group to which to add the reference.## @param [#to_s] path# The, preferably absolute, path of the reference.## @param [Symbol] source_tree# The source tree key to use to configure the path (@see# GroupableHelper::SOURCE_TREES_BY_KEY).## @note To analyze the targets the given project is read and thus# it should already exist in the disk.## @return [PBXFileReference] The new file reference.#defnew_subproject(group,path,source_tree)ref=new_file_reference(group,path,source_tree)ref.include_in_index=nilproduct_group_ref=find_products_group_ref(group,true)subproj=Project.open(path)subproj.products_group.files.eachdo|product_reference|container_proxy=group.project.new(PBXContainerItemProxy)container_proxy.container_portal=ref.uuidcontainer_proxy.proxy_type=Constants::PROXY_TYPES[:reference]container_proxy.remote_global_id_string=product_reference.uuidcontainer_proxy.remote_info='Subproject'reference_proxy=group.project.new(PBXReferenceProxy)extension=File.extname(product_reference.path)[1..-1]reference_proxy.file_type=Constants::FILE_TYPES_BY_EXTENSION[extension]reference_proxy.path=product_reference.pathreference_proxy.remote_ref=container_proxyreference_proxy.source_tree='BUILT_PRODUCTS_DIR'product_group_ref<<reference_proxyendattribute=PBXProject.references_by_keys_attributes.find{|attrb|attrb.name==:project_references}project_reference=ObjectDictionary.new(attribute,group.project.root_object)project_reference[:project_ref]=refproject_reference[:product_group]=product_group_refgroup.project.root_object.project_references<<project_referencerefend# Configures a file reference according to the extension to math# Xcode behaviour.## @param [PBXFileReference] ref# The file reference to configure.## @note To closely match the Xcode behaviour the name attribute of# the file reference is set only if the path of the file is# not equal to the path of the group.## @return [void]#defconfigure_defaults_for_file_reference(ref)ifref.path.include?('/')ref.name=ref.path.split('/').lastendifFile.extname(ref.path).downcase=='.framework'ref.include_in_index=nilendenddeffind_products_group_ref(group,should_create=false)product_group_ref=(group.project.root_object.product_ref_group||=group.project.main_group.find_subpath('Products',should_create))product_group_refend#-------------------------------------------------------------------#endendendendend