CocoaPods / Xcodeproj

Create and modify Xcode projects from Ruby.
http://rubygems.org/gems/xcodeproj
MIT License
2.32k stars 452 forks source link

Sample please #554

Closed jwilson85 closed 6 years ago

jwilson85 commented 6 years ago

I have spent some time trying to do what I think is simple. However, I'm not able to find the solution. I would like to add any missing files in a disk folder to the corresponding group in xcode. Could you guys please help me with some sample code. My ruby expertise is as of 3 days ago.

If this direction is not valid I could just add the file as I generate it with a bash script I wrote. So if that's a better solution then could you show me how to add a single file to a current xcode group. Based on its disk location.

jwilson85 commented 6 years ago

So I figured it out. The API reference is a little difficult to parse but I figured it out.

Here is some sample code:

#!/usr/bin/env ruby
# Open the existing Xcode project
inputs = ARGV
puts "inputs #{inputs[0]} #{inputs[1]}"
require 'xcodeproj'
project_path = "./#{inputs[0]}.xcodeproj"
project = Xcodeproj::Project.open(project_path)

#############################
def addfiles (direc, current_group, iOS_Core_target, iOS_Core_beta_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)
            puts "--------------Created New Group--------------"
            addfiles("#{item}/*", created_group, iOS_Core_target, iOS_Core_beta_target)
        else 
            file_name = File.basename(item)

            new_path = item.gsub(file_name, "")
            new_path = new_path.gsub("./iOS-Core/", "")
            sub_child = current_group.find_subpath("#{new_path}", true)

            path_to_new_file = item.gsub("./iOS-Core/client/", "")

            puts "Adding #{file_name} to #{sub_child.name} with full path #{item} using relative_path #{path_to_new_file}"
            i = sub_child.new_file(path_to_new_file)

            if item.include? ".swift"
              iOS_Core_target.add_file_references([i])
              iOS_Core_beta_target.add_file_references([i])
            end
        end
    end
end
#############################

iOS_Core = project.groups.first

iOS_Core_target = nil
iOS_Core_beta_target = nil
#puts "num #{iOS_Core.path}"
project.targets.each do |target|
    #puts target.name

    if target.name == 'iOS-Core'
        #puts "set iOS_Core with #{target}"
        iOS_Core_target = target
    elsif target.name == 'iOS-Core-beta'
        #puts "set iOS_Core_beta with #{target}"
        iOS_Core_beta_target = target
    end
    #addfiles("#{uipath}/*", kit_group, target)    

end
#puts "1 #{iOS_Core_target} 2 #{iOS_Core_beta_target}"

addfiles("#{inputs[1]}", iOS_Core, iOS_Core_target, iOS_Core_beta_target)
project.save(project_path)

What this is doing is taking some arguments when you run the ruby command like so ruby script.rb input1 input2

Im using input1 as a project name. and input2 as the filepath that xcode needs to know about.