美文网首页
【iOS 工程管理】自动集成构建资源

【iOS 工程管理】自动集成构建资源

作者: 24c41111e47b | 来源:发表于2022-11-28 14:34 被阅读0次

iOS工程开发中,资源主要分为代码文件资源和资源包资源(如: 图片,js脚本等),xcode目录管理形式分为有实体文件夹的Folder形式和没有实体文件夹的Group逻辑形式。
本文记录一次,自动将资源包资源集成到项目依赖编译中,然后随项目一起打包的操作方式


需求背景

cocos项目需要将游戏加载器放在Xcode工程中,然后构建的时候一起打入资源中,由于手动集成有一定的操作问题,所以这里进行流程化,用脚本进行自动集成

依赖知识点

  • .xcodeproj 结构
  • ruby如何操作.xcodeproj

脚本内容

require 'xcodeproj'

def add_file_reference_to_group(target, project, directory_path, to_group)
    puts '#cocos iOS# 开始自动组装加载器'
    if to_group and File::exist?(directory_path) then
        puts '#cocos iOS# 清空旧的加载器引用'
        to_group.clear()
        Dir.foreach(directory_path) do |entry|
            if entry != "." and entry != ".." and entry != ".DS_Store"
                pb_gen_file_path = directory_path + '/' + entry
                if to_group.find_file_by_path(pb_gen_file_path)
                    puts pb_gen_file_path + " reference exist"
                else
                    puts '文件路径:' + pb_gen_file_path
                    file_reference = to_group.new_reference(pb_gen_file_path)
                    # 搜索 build_phases
                    target.build_phases.each do |rerouce_build_phases|
                    #    puts target_build_phases.display_name
                        # 找到 Copy Bundle Resources
                        if rerouce_build_phases.display_name == 'Resources'
                            rerouce_build_phases.add_file_reference(file_reference, true)
                        end
                    end
                    
                end
            end
        end
        project.save
        puts '#cocos iOS# 自动组装结束'
    end
end

puts '#cocos iOS# 准备自动组装加载器'

#打开项目工程A.xcodeproj
basePath =File.dirname(__FILE__) # 当前根路径
# 获取工程路径
# TODO 非Flutter工程 修改工程路径
project_path = basePath + '/Runner.xcodeproj'
# 根据工程文件创建project类
project = Xcodeproj::Project.open(project_path)

# 在target数组里,找到第一个,在本工程即Runner
target = project.targets.first

# 获取CocosData group
# 第一个参数是Group名,第二个参数true,group不存在则自动创建
DataCocos_group = project.main_group.find_subpath(File.join('DataCocos'), true)
#resources_group = project.main_group.find_subpath(File.join('Runner/Resources'), true)

if !File::exist?(File.join('DataCocos'))
    puts "#cocos iOS# ./ios/DataCocos 不存在, 请确认加载器已经下载 ❌"
    puts "#cocos iOS# fial ❌"
    exit 1
end

# 开始组装
add_file_reference_to_group(target, project, File.join('DataCocos'), DataCocos_group)


puts "#cocos iOS# success✅"
exit 0





【参考】
Ruby操作.xcodeproj
project.pbxproj剖析
Xcodeproj API

相关文章

网友评论

      本文标题:【iOS 工程管理】自动集成构建资源

      本文链接:https://www.haomeiwen.com/subject/zptkfdtx.html