Skip to content

Instantly share code, notes, and snippets.

@Gladkov-Art
Last active December 20, 2019 07:14
Show Gist options
  • Save Gladkov-Art/a01d8ba969e411ff36ac2518ae577757 to your computer and use it in GitHub Desktop.
Save Gladkov-Art/a01d8ba969e411ff36ac2518ae577757 to your computer and use it in GitHub Desktop.
require 'optparse'
require 'xcodeproj'
options = {}
OptionParser.new do |opts|
opts.banner = " This script fills \"Compile sources\" and add headers to the project section. \n
Usage: fill_sources_headers.rb [options]"
opts.on("-s [sources_path]", "--sources [sources_path]", "Path to the sources files (required)") do |sources_path|
options[:sources_path] = sources_path
end
opts.on("-r [headers_path]", "--headers [headers_path]", "Path to the headers files (required)") do |headers_path|
options[:headers_path] = headers_path
end
opts.on("-x [xcodeproj_path]", "--xcodeproj [xcodeproj_path]", "Path to xcodeproj container (required)") do |xcodeproj_path|
options[:xcodeproj_path] = xcodeproj_path
end
opts.on("-h", "--help", "Show help") do
puts opts
exit
end
end.parse!
if (!options[:xcodeproj_path])
puts "Path to the .xcodeproj is required"
exit 1
end
if (!options[:sources_path])
puts "Path to the source files is required"
exit 1
end
file_pbxproj = options[:xcodeproj_path]
if !(Dir.exists?(file_pbxproj))
puts "Your project file couldn't be located."
exit 1
end
headers = options[:headers_path]
sources = options[:sources_path]
project = Xcodeproj::Project.open(file_pbxproj)
#Using first target. If your project contains more targets make sure that the right one is selected
target = project.targets.first
headers_phase = target.headers_build_phase
source_build_phase = target.source_build_phase
#create new group for sources
sources_group_name = 'Sources'
sources_group = project.groups.find do |group|
group.name == sources_group_name
end
if (!sources_group)
sources_group = project.new_group(sources_group_name)
end
#create new group for headers
headers_group_name = 'Headers'
headers_group = project.groups.find do |group|
group.name == headers_group_name
end
if (!headers_group)
headers_group = project.new_group(headers_group_name)
end
imported_headers_count = 0;
Dir.glob(headers+"/*.h") do |file|
file_reference = headers_group.files.find do |ref|
ref.path == file
end
if (!file_reference)
imported_headers_count+=1;
file_reference = headers_group.new_file(file)
end
end
imported_sources_count = 0;
Dir.glob(sources+"/**/*.c") do |file|
file_reference = sources_group.files.find do |ref|
ref.path == file
end
if (!file_reference)
imported_sources_count+=1
file_reference = sources_group.new_file(file)
end
source_build_phase.add_file_reference(file_reference,avoid_duplicates: true)
end
project.save
puts "#{imported_sources_count} source files have been added!"
puts "#{imported_headers_count} headers files have been added!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment