Ruby脚本(xcodeproj)将所有源文件添加到目标

时间:2017-06-27 01:50:36

标签: ruby target xcodeproj

我试图在我的项目中运行游乐场。问题是该项目包含数千个紧密耦合的文件。我已经创建了一个可可触摸框架,我可以导入该框架以在操场中使用应用程序源。唯一的问题是单击每个源文件并将其添加到目标将花费数小时。即使选择多个文件。 (如果您选择跨群组,则添加到目标选项不可用)。

有没有办法使用Ruby的xcodeproj库以编程方式添加所有文件?我不熟悉Ruby或那个库,因此发现精确的代码会非常费时。如果不是这样,有没有办法通过Xcode的UI本身有效地将我的工作区中的所有文件添加到这个新框架?

如果将整个工作空间源添加到框架中,由于其他原因而不是一个可行的解决方案,是否有某种方法可以在此工作空间中操作(可访问应用程序源)?

2 个答案:

答案 0 :(得分:2)

看起来new_file method就像你需要的那样。有一个issue on GitHub,它显示了如何递归遍历目录并将文件添加到目标:

def addfiles (direc, current_group, main_target)
   Dir.glob(direc) do |item|
       next if item == '.' or item == '.DS_Store'

       if File.directory?(item)
           new_folder = File.basename(item)
           created_group = current_group.new_group(new_folder)
           addfiles("#{item}/*", created_group, main_target)
       else 
         i = current_group.new_file(item)
         if item.include? ".m"
             main_target.add_file_references([i])
         end
       end
   end
end

它并不完美。某些文件名可能包含" .m"没有' m'例如,扩展。但是使用递归的基本思想是合理的。

答案 1 :(得分:0)

def is_resource_group(file)
    extname= file[/\.[^\.]+$/]
    if extname == '.bundle' || extname == '.xcassets' then
        return true
    end
    return false
end

def add_files_togroup(project, target, group)

    if File.exist?(group.real_path) 

        Dir.foreach(group.real_path) do |entry|
            filePath = File.join(group.real_path, entry)

            # puts filePath

            if filePath.to_s.end_with?(".DS_Store", ".xcconfig") then
                # ignore

            elsif filePath.to_s.end_with?(".lproj") then
                if @variant_group.nil?
                    @variant_group = group.new_variant_group("Localizable.strings");
                end
                string_file = File.join(filePath, "Localizable.strings")
                fileReference = @variant_group.new_reference(string_file)
                target.add_resources([fileReference])

            elsif is_resource_group(entry) then
                fileReference = group.new_reference(filePath)
                target.add_resources([fileReference])
            elsif !File.directory?(filePath) then

                # 向group中增加文件引用
                fileReference = group.new_reference(filePath)
                # 如果不是头文件则继续增加到Build Phase中
                if filePath.to_s.end_with?(".m", ".mm", ".cpp") then
                    target.add_file_references([fileReference])
            # elsif filePath.to_s.end_with?("pbobjc.m", "pbobjc.mm") then
                    # target.add_file_references([fileReference], '-fno-objc-arc')

                elsif filePath.to_s.end_with?(".pch") then

                elsif filePath.to_s.end_with?("Info.plist") && entry == "Info.plist" then

                elsif filePath.to_s.end_with?(".h") then
                    # target.headers_build_phase.add_file_reference(fileReference)
                elsif filePath.to_s.end_with?(".framework") || filePath.to_s.end_with?(".a") then
                    target.frameworks_build_phases.add_file_reference(fileReference)
                elsif 
                    target.add_resources([fileReference])
                end
            # 目录情况下, 递归添加
            elsif File.directory?(filePath) && entry != '.' && entry != '..' then

                subGroup = group.find_subpath(entry, true)
                subGroup.set_source_tree(group.source_tree)
                subGroup.set_path(File.join(group.real_path, entry))
                add_files_togroup(project, target, subGroup)

            end
        end
    end
end



new_project_obj = Xcodeproj::Project.open(new_proj_fullname)

new_proj_target = new_project_obj.new_target(:application, "Targetname", :ios, "9.0", nil, :objc)

new_group = new_project_obj.main_group.find_subpath("GroupName", true)