Skip to content

Instantly share code, notes, and snippets.

@yasuoza
Last active August 29, 2015 14:01
Show Gist options
  • Save yasuoza/b3e35066b3f6b0e699c0 to your computer and use it in GitHub Desktop.
Save yasuoza/b3e35066b3f6b0e699c0 to your computer and use it in GitHub Desktop.
add file to xcode project
# FYI:
# http://www.j7lg.com/archives/1732
require 'xcodeproj'
def bail_if_nil(obj, msg)
return unless obj.nil?
puts msg
exit
end
def remove_first_path_component(path)
arr = path.split('/')
arr.delete_at(0)
arr.join('/')
end
def find_subpath_from_main_group(project, group, name, path)
project.main_group.find_subpath(path, true);
end
if (ARGV.length < 3)
puts 'Usage: ' + $0 + ' <Project Name> <Group Name> <File Name>'
exit
end
project = Xcodeproj::Project.open(ARGV[0] + '.xcodeproj')
target = project.targets.find { |element| element.name == ARGV[0] }
bail_if_nil(target, ARGV[0] + ' target not found.')
group = project.main_group.find_subpath(ARGV[1], true); # create new group if it does not exist
bail_if_nil(group, ARGV[1] + ' group not found.')
target.add_file_references([group.new_file(ARGV[2])])
project.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment