require 'rake/tasklib' require 'rubygems' require 'xcodebuild' require 'cucumber/rake/task' TEMPLATES_DIR = File.expand_path('../../../templates/', File.dirname(__FILE__)) module XcodeBuild def self.run(args = "", output_buffer = STDOUT) command = "xcodebuild #{args} DSTROOT=/tmp/artifacts 2>&1" #puts command IO.popen(command) do |io| begin while line = io.readline begin output_buffer << line rescue StandardError => e puts "Error from output buffer: #{e.inspect}" puts e.backtrace end end rescue EOFError end end $?.exitstatus end end module FWToolkit module Tasks class BuildTask < ::Rake::TaskLib include Rake::DSL if defined?(Rake::DSL) attr_accessor :workspace attr_accessor :scheme attr_accessor :bundle_identifier attr_accessor :certificate attr_accessor :provisioning_profile def initialize(&block) yield self if block_given? define end private def define namespace :xcode do XcodeBuild::Tasks::BuildTask.new :debug_simulator do |t| t.invoke_from_within = '.' t.configuration = "Debug" t.sdk = "iphonesimulator" t.workspace = @workspace t.scheme = @scheme t.formatter = XcodeBuild::Formatters::ProgressFormatter.new end XcodeBuild::Tasks::BuildTask.new :release_iphoneos do |t| t.invoke_from_within = '.' t.configuration = "Release" t.sdk = "iphoneos" t.workspace = @workspace t.scheme = @scheme t.formatter = XcodeBuild::Formatters::ProgressFormatter.new end desc 'Build IPA file' task :ipa do command = "xcrun -sdk 'iphoneos' PackageApplication -v '/tmp/artifacts/Applications/#{scheme}.app' -o '/tmp/artifacts/Applications/#{scheme}.ipa' --sign '#{certificate}' --embed '#{provisioning_profile}'" sh command end task :validate_auth_token do unless ENV['FWBUILD_AUTH_TOKEN'] puts 'Please set FWBUILD_AUTH_TOKEN environment variable' exit(-1) end end task :upload do auth_token = ENV['FWBUILD_AUTH_TOKEN'] file_name = "#{scheme}.ipa" key = File.join('uploads', file_name) sh("curl -X POST -F 'key=#{key}' -F 'acl=bucket-owner-full-control' -F 'file=@/tmp/artifacts/Applications/#{scheme}.ipa' https://fw.fwbuild.production.s3.amazonaws.com/ --progress-bar") sh("curl -X POST -d 'bundle_identifier=#{@bundle_identifier}&s3_upload_name=#{file_name}&auth_token=#{auth_token}&build[version]=#{DateTime.now.to_s}' http://fwbuild.futureworkshops.com/apps/builds.json") end desc 'Build and Upload IPA' task :build_upload => [:validate_auth_token, 'release_iphoneos:archive', :ipa, :upload] end namespace :frank do desc 'Setup Frank' task :setup do sh 'frank setup' test_dir = File.join(TEMPLATES_DIR, 'cucumber') features_dir = File.join('Frank', 'features') support_dir = File.join(features_dir, 'support') steps_dir = File.join(features_dir, 'step_definitions') views_dir = File.join(support_dir, 'views') remove_file(File.join(features_dir, 'my_first.feature')) cp(File.join(test_dir, 'env.rb'), support_dir) cp(File.join(test_dir, 'mimic.rb'), support_dir) mkdir(views_dir) cp(File.join(test_dir, 'my_objects_json.erb'), views_dir) cp(File.join(test_dir, 'my_objects_xml.erb'), views_dir) cp(File.join(test_dir, 'launch_steps.rb'), steps_dir) cp(File.join(test_dir, 'example.feature'), features_dir) cp(File.join(test_dir, 'AppDelegate+Frank.h'), './') cp(File.join(test_dir, 'AppDelegate+Frank.m'), './') end desc 'Build the frankified App' task :build do sh "frank build --workspace #{@workspace} --scheme #{@scheme}" end Cucumber::Rake::Task.new(:ci_test, 'Run Frank acceptance tests with CI options') do |t| ENV['APP_BUNDLE_PATH'] = File.join( Dir.pwd, 'Frank/frankified_build/Frankified.app' ) ENV['USE_SIM_LAUNCHER_SERVER'] = 'YES' artifacts_dir = ENV['CC_BUILD_ARTIFACTS'] ? ENV['CC_BUILD_ARTIFACTS'] : '/tmp' t.cucumber_opts = "Frank/features --tags ~@wip --format pretty --format html --out '#{artifacts_dir}/frank_results.html' --format rerun --out '#{artifacts_dir}/frank_rerun.txt'" end desc 'Run Frank acceptance tests' task :test, :cucumber_options do |t, args| args.with_defaults(:cucumber_options => 'Frank/features --tag ~@wip') ENV['APP_BUNDLE_PATH'] = File.join( Dir.pwd, 'Frank/frankified_build/Frankified.app' ) ENV['USE_SIM_LAUNCHER_SERVER'] = nil sh "cucumber #{args.cucumber_options}" end desc 'Run Frank acceptance tests for a tag' task :tag, :tag_name do |t, args| args.with_defaults(:tag_name => '') cucumber_options = "Frank/features --tag #{args.tag_name}" ENV['APP_BUNDLE_PATH'] = File.join( Dir.pwd, 'Frank/frankified_build/Frankified.app' ) ENV['USE_SIM_LAUNCHER_SERVER'] = nil sh "cucumber #{cucumber_options}" end end namespace :services do require 'daemons' desc 'Run a service' task :run, :service_name, :seeds_name do |t, args| args.with_defaults(:service_name => 'mimic', :seeds_name => nil) require 'fwtoolkit' Dir[File.join( Dir.pwd, 'Frank/features/support/models/*.rb')].each {|file| require file } Dir[File.join( Dir.pwd, 'Frank/features/support/views/*.rb')].each {|file| require file } require File.join( Dir.pwd, "Frank/features/support/#{args.service_name}.rb") require File.join( Dir.pwd, "Frank/features/support/#{args.seeds_name}.rb") if args.seeds_name puts "Service #{args.service_name} running" loop do sleep(5) end end end namespace :bundle do task :install do sh 'bundle' end end namespace :pod do task :clean do FileUtils.rm_rf('Pods') end task :install do sh 'pod install' end desc 'Clean and install Pods' task :clean_install => [:clean, :install] end namespace :fwbuild do task :upload do S3Cmd.put("path/to/a/file.ext", "s3://fw.fwbuild.production/uploads/") do |progress| puts progress end end task :build_upload => ["xcode:release_device:clean", "xcode:release_device:archive", "upload"] end namespace :ci do desc 'Clean, build and test' task :build => ["bundle:install", "xcode:debug_simulator:cleanbuild", "frank:build", "frank:ci_test"] desc 'Clean, build and test for Cocoapods projects' task :pod_build => ["bundle:install", "pod:clean_install", "ci:build"] end end end end end