4

I'm trying to run a playground in my project. The problem being that the project contains thousands of files that are tightly coupled. I've created a cocoa touch framework that I can import to utilize the apps source in the playground. The only problem being that clicking on each source file and adding it to the target will take hours. Even when selecting multiple files. (if you select across groups the add to target option isn't available).

Is there a way to use Ruby's xcodeproj library to add all files programmatically? I'm not familiar with Ruby or that library and so discovering the precise code for this would be prohibitively time consuming. If not that, is there a way to effectively add all files in my workspace to this new framework through Xcode's UI itself?

If adding the entirety of the workspaces source to a framework turns out not to be a viable solution for other reasons, is there some way of getting a playground (with access to the app source) operational in this workspace?

Alex Bollbach
  • 4,370
  • 9
  • 32
  • 80

2 Answers2

3

It looks as if the new_file method does what you need. There's an issue on GitHub that shows how to recursively go through a directory and add files to a target:

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

It's not perfect. Some file names might include ".m" without having an 'm' extension, for instance. But the basic idea of using recursion is sound.

Graham
  • 7,431
  • 18
  • 59
  • 84
Kathryn
  • 1,557
  • 8
  • 12
3
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)   
Shuangquan Wei
  • 121
  • 1
  • 3