Skip to content

Instantly share code, notes, and snippets.

@sandeeplearner
Last active August 17, 2020 21:22
Show Gist options
  • Save sandeeplearner/8fa132f9edf26027921925eb744f951f to your computer and use it in GitHub Desktop.
Save sandeeplearner/8fa132f9edf26027921925eb744f951f to your computer and use it in GitHub Desktop.
Add Group and file references to group
#!/usr/bin/ruby
require 'xcodeproj'
require 'fileutils'
require 'yaml'
class Update_Project_Settings
@parent_group_name = nil
@proto_group_name = nil
@project = nil
@parent_group = nil
@targets = []
def initialize(project_path)
config_options = YAML.load_file("#{__dir__}/config.yml")
@parent_group_name = config_options['parentGroupName']
@proto_group_name = config_options['protoGroupName']
@project = Xcodeproj::Project.open(project_path)
@targets = []
get_targets(@project, config_options)
end
def start_process
get_group(@project)
@project.save()
end
def recursively_look_for_group(group)
puts "Printing name of group #{group.path}"
if group.path == @parent_group_name
@parent_group = group
return
elsif group.groups.count > 0
group.groups.each do |internalGroup|
recursively_look_for_group(internalGroup)
end
else
return
end
end
def get_group(project)
project.groups.each do |group|
recursively_look_for_group(group)
if @parent_group != nil
break
end
end
if @parent_group == nil
puts "Could not find group"
else
find_proto_group(@parent_group)
end
end
def find_proto_group(parentGroup)
puts "Number of groups in parent is #{parentGroup.groups.count()}"
if parentGroup.groups.count() > 0
parentGroup.groups.each do |group|
if group.name == @proto_group_name
delete_group_and_files(group)
return
end
end
end
copy_files_to_folder(parentGroup)
end
def delete_group_and_files(proto_group)
mainGroup = proto_group.parent
groupPath = proto_group.real_path
proto_group.clear()
proto_group.remove_from_project()
if Dir.exist?(groupPath)
puts "Deleting folder using rm_rf"
FileUtils.rm_rf("#{groupPath}/.", secure: true)
Dir.delete(groupPath)
else
proto_group.children.each do |items|
File.delete("#{groupPath}/#{items.path}")
end
end
copy_files_to_folder(mainGroup)
end
def copy_files_to_folder(group)
puts "Current directory is #{group.path} and real path is #{group.real_path}"
FileUtils.cp_r(File.join(Dir.home, "Documents", "GitClone", "SwiftProtoFiles"), group.real_path)
if Dir.exists?(File.join(group.real_path, @proto_group_name))
add_group_to_project(group, @proto_group_name, @project)
end
end
def get_targets(project, config_options)
targets_in_config = config_options["targetsToAdd"]
puts "#{targets_in_config}"
project.native_targets.each do |target|
puts "target name is #{target.name}"
if targets_in_config.include?(target.name)
puts "about to insert #{target}"
puts "Targets array is #{@targets.count}"
@targets << target
end
end
puts "Found targets are #{@targets}"
end
def add_group_to_project(parentGroup, groupName, project)
puts "Inside add_group_to_project"
childGroup = parentGroup.new_group(groupName,groupName, :group)
fileReferenceArray = []
Dir.foreach("#{parentGroup.real_path}/#{@proto_group_name}") do |child|
if (child == "." || child == "..")
puts "Ignoring file #{child}"
else
fileRefernce = childGroup.new_file(child, childGroup.source_tree)
fileReferenceArray << fileRefernce
end
end
@targets.each do |target|
target.add_file_references(fileReferenceArray)
end
end
end
project_updater = Update_Project_Settings.new("#{__dir__}/swiggy.xcodeproj")
project_updater.start_process()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment