Skip to content

Instantly share code, notes, and snippets.

@gabriel
Last active January 2, 2016 16:09
Show Gist options
  • Save gabriel/8328418 to your computer and use it in GitHub Desktop.
Save gabriel/8328418 to your computer and use it in GitHub Desktop.
Install a test target
require 'xcodeproj'
require 'xcodeproj/ext'
require 'fileutils'
project_path = "TheProj.xcodeproj"
project_name = "TheProj"
test_project_name = "#{project_name}Tests"
FileUtils.mkdir_p(test_project_name)
File.open("#{test_project_name}/main.m", "w") do |f|
content = <<-EOS
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, @"GHUnitIOSAppDelegate");
}
}
EOS
f.write(content)
end
# Write the Tests-Info.plist
test_info = {
"CFBundleDisplayName" => "${PRODUCT_NAME}",
"CFBundleExecutable" => "${EXECUTABLE_NAME}",
"CFBundleIdentifier" => "tests.${PRODUCT_NAME:rfc1034identifier}",
"CFBundleInfoDictionaryVersion" => "6.0",
"CFBundleName" => "${PRODUCT_NAME}",
"CFBundlePackageType" => "APPL",
"CFBundleShortVersionString" => "1.0",
"CFBundleVersion" => "1.0",
"LSRequiresIPhoneOS" => true,
"UISupportedInterfaceOrientations" => ["UIInterfaceOrientationPortrait"]
}
test_info_path = "#{test_project_name}/#{test_project_name}-Info.plist"
Xcodeproj.write_plist(test_info, test_info_path)
project = Xcodeproj::Project.open(project_path)
# Find the main target for the test dependency
main_target = project.targets.select { |t| t.name == project_name }.first
raise "No target with name #{project_name}" unless main_target
# Create the test target
test_target = project.new_target(:application, test_project_name, :ios, "7.0")
test_target.add_dependency(main_target)
tests_group = project.new_group(test_project_name)
test_main_file = tests_group.new_file("#{test_project_name}/main.m")
test_target.add_file_references([test_main_file])
# Use same resources build phase as main target
# Have to compare with class name because of funky const loading in xcodeproj gem
resources_build_phase = main_target.build_phases.select { |p|
p.class.to_s == "Xcodeproj::Project::Object::PBXResourcesBuildPhase" }.first
test_target.build_phases << resources_build_phase if resources_build_phase
# Clear default OTHER_LDFLAGS (otherwise CocoaPods gives a warning)
test_target.build_configurations.each do |c|
c.build_settings.delete("OTHER_LDFLAGS")
c.build_settings["INFOPLIST_FILE"] = test_info_path
end
project.save
scheme = Xcodeproj::XCScheme.new
scheme.set_launch_target(test_target)
scheme.save_as(project_path, test_project_name)
@aybarsyalcin
Copy link

Hi,
I have a problem for running script. "ruby installer.rb " run this on terminal and I got error.

This error is
/Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:128:in require': cannot load such file -- xcodeproj/ext (LoadError) from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:128:inrescue in require'
from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:39:in require' from xcodeproj_test.rb:3:in

'"

Please help me. Thanks...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment