Last active
January 2, 2016 16:09
-
-
Save gabriel/8328418 to your computer and use it in GitHub Desktop.
Install a test target
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:in
rescue 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...