From fc0b4cc5ff26076489f5dc8ab4dee4ed9e0510e5 Mon Sep 17 00:00:00 2001 From: Jaymes Waters and Brad Grzesiak Date: Fri, 23 Mar 2012 14:11:42 -0500 Subject: [PATCH] Refactor Rakefile even MOAR --- Rakefile | 166 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 98 insertions(+), 68 deletions(-) diff --git a/Rakefile b/Rakefile index bdfdcf0..4fc505e 100644 --- a/Rakefile +++ b/Rakefile @@ -1,33 +1,53 @@ require 'open3' require 'colorful' + +def app_name + File.basename(project_directory) +end + +def project_directory + Dir.pwd +end + +def path_to_automation + '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Instruments/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate' +end + +def build_directory + Dir.mkdir('build') unless Dir.exists?('build') + File.join(project_directory, 'build') +end + +def app + File.join(build_directory, "#{app_name}.app") +end + +def workspace + File.join(project_directory, "#{app_name}.xcworkspace") +end + +desc 'Remove result and trace files' task :clean do print 'Removing instrumentscli*.trace & automation/results/* ... ' system 'rm -rf instrumentscli*.trace automation/results/*' puts 'done.' end -task :clean_db do - puts "Cleaning the application's sqlite cache database" - system 'rm -rf ls -1d ~/Library/Application\ Support/iPhone\ Simulator/**/Applications/**/Library/Caches/TravisCI*.sqlite' -end - -task :coffeescript do - test_files = 'automation/*.coffee' - system "coffee -b -c #{test_files}" -end +# task :clean_db do + # puts "Cleaning the application's sqlite cache database" + # system 'rm -rf ls -1d ~/Library/Application\ Support/iPhone\ Simulator/**/Applications/**/Library/Caches/TravisCI*.sqlite' +# end +desc 'Compile the workspace' task :build do - @current_build ||=1 - build_device = @current_build == 1 ? "iphone" : "ipad" - workspace = '~/dev/ios/TravisCI/TravisCI.xcworkspace' - scheme = 'TravisCI' + + scheme = app_name configuration = 'Debug' - sdk = 'iphonesimulator5.0' + sdk = 'iphonesimulator5.1' variables = { - 'TARGETED_DEVICE_FAMILY' => @current_build, 'GCC_PREPROCESSOR_DEFINITIONS' => 'TEST_MODE=1', - 'CONFIGURATION_BUILD_DIR' => '~/dev/ios/TravisCI/build' + 'CONFIGURATION_BUILD_DIR' => build_directory }.map{|key,val| "#{key}=#{val}"}.join(' ') cmd = "xcodebuild \ @@ -38,46 +58,62 @@ task :build do #{variables} \ clean build" - stdin, stdout, stderr, wait_thr = Open3.popen3 cmd + Open3.popen2e(cmd) do |stdin, stdout, wait_thr| + + print "Building" + out_string = "" - print "Building for #{build_device}" - out_string = "" + stdout.each_line do |line| + out_string << line + print "." + end + + exit_status = wait_thr.value + puts - stdout.each_line do |line| - out_string += line - print "." + if exit_status == 0 + puts + puts "## Build Successful ##" + puts + else + puts out_string + raise 'Build failed' + end end - out_string += stderr.read - - stdin.close - stdout.close - stderr.close - exit_status = wait_thr.value - puts "" - - if exit_status == 0 - puts "" - puts "## Build Successful ##" - puts "" - else - puts out_string - raise 'Build failed' +end + +def results_path + File.join(project_directory, 'automation', 'results').tap do |dir_name| + Dir.mkdir_p(dir_name) unless Dir.exists?(dir_name) end end -def run_test_with_script path - project_dir = Dir.pwd - template = '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Instruments/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate' - app = File.join(project_dir, 'build', 'TravisCI.app') - results_path = File.join(project_dir, 'automation', 'results') +def set_simulator_to_run device_family + device_family_id = device_family == 'iphone' ? 1 : 2 + plistbuddy = '/usr/libexec/PlistBuddy' + plist_file = "#{app}/Info.plist" + system "#{plistbuddy} -c 'Delete :UIDeviceFamily' #{plist_file}" + system "#{plistbuddy} -c 'Add :UIDeviceFamily array' #{plist_file}" + system "#{plistbuddy} -c 'Add :UIDeviceFamily:0 integer #{device_family_id}' #{plist_file}" +end + +def run_test_with_script path, device_family = nil + device_family ||= ENV['FAMILY'] || 'iphone' + + set_simulator_to_run device_family + variables = { - 'UIASCRIPT' => File.join(project_dir, 'automation', path), + 'UIASCRIPT' => path, 'UIARESULTSPATH' => results_path }.map{|key,val| "-e #{key} #{val}"}.join(' ') - cmd = "mkdir -p #{results_path} && unix_instruments.sh -t #{template} #{app} #{variables}" + cmd = "mkdir -p #{results_path} && \ + unix_instruments.sh \ + -t #{path_to_automation} \ + #{app} \ + #{variables}" - Open3.popen2(cmd) {|stdin, stdout, wait_thr| + Open3.popen2e(cmd) do |stdin, stdout, wait_thr| stdout.each_line do |line| if line =~ /^\d{4}/ tokens = line.split(' ') @@ -89,37 +125,31 @@ def run_test_with_script path puts line end end - } + end end -namespace :ipad do - task :build do - @current_build = 2 - Rake::Task['clean_db'].execute - Rake::Task['build'].execute - end - - task :test => :coffeescript do - run_test_with_script 'ipad.js' - end +task :coffeescript do + test_files = 'automation/*.coffee' + system "coffee -b -c #{test_files}" end -namespace :iphone do - task :build do - @current_build = 1 - Rake::Task['clean_db'].execute - Rake::Task['build'].execute - end +device_families = %w(iphone ipad) - task :test => :coffeescript do - run_test_with_script 'iphone.js' - run_test_with_script 'iphone_follow.js' +device_families.each do |device_family| + namespace device_family do + desc "Run tests for #{device_family}" + task :test => :coffeescript do + run_test_with_script "automation/#{device_family}.js", device_family + end end + + desc "Run tests for #{device_family}" + task device_family => "#{device_family}:test" + end -task ipad: ['ipad:build', 'ipad:test'] -task iphone: ['iphone:build', 'iphone:test'] +desc 'Build and run tests (the default task)' +task :test => ([:build] + device_families) -task :test => [:iphone, :ipad] task :default => :test