#!/usr/bin/env ruby require 'rubygems' require 'fileutils' require 'term/ansicolor' require 'tmpdir' require 'yaml' include Term::ANSIColor def main banner check_args config = setup print_config config Dir.mkdir config['tmp_dir'] bump_versions(config) xcodebuild(config['src_dir'], config['project'], config['sdk'], config['target'], config['configuration']) commit_build(config) zip_build(config) copy_build(config) FileUtils.rm_rf config['tmp_dir'], :verbose => true success "All done!" end def banner puts "Harukaze v1.2.0" puts "© 2010 Andrey Subbotin " end def usage puts "" puts "Usage: harukaze []" puts "" puts " = adhoc | appstore" puts " = harukaze config file, default is rules.harukaze" end def check_args if (ARGV.length == 0) usage exit 1 elsif (ARGV.length == 1) unless (ARGV[0] == 'adhoc' || ARGV[0] == 'appstore') usage exit 1 end end end def setup result = Hash.new global_config_filename = File.expand_path('~/.harukaze') if (File.exist?(global_config_filename)) result.merge!(YAML.load_file(global_config_filename)) end config = YAML.load_file(ARGV[1].nil? ? 'rules.harukaze' : ARGV[1]) config = config['common'].merge(config[ARGV[0]]) config['tmp_dir'] = File.join Dir.tmpdir, "epl.builder.#{$$}" config['startwd'] = Dir.getwd config['src_dir'] = config['startwd'] config['build_results_dir'] = File.join config['src_dir'], 'build', "#{config['configuration']}-#{config['build_dir_suffix']}" config['payload_target'] = File.join config['tmp_dir'], 'payload' config['mode'] = ARGV[0] result.merge!(config) result['builds_folder'] = File.expand_path(result['builds_folder']) result['products_folder'] = File.expand_path(result['products_folder']) result end def print_config(config) # Print out settings status "CONFIG" y config end def exe(cmd) print magenta, "CMD: '#{cmd}'", reset, "\n" system cmd end def status(msg) print blue, "--- #{msg} ---", reset, "\n" end def error(msg) print red, bold, 'ERROR: ', msg, reset, "\n" exit 1 end def success(msg) print green, bold, 'SUCCESS: ', msg, reset, "\n" end def xcodebuild(src_dir, project, sdk, target, configuration) Dir.chdir src_dir status "BUILDING" rc = exe "xcodebuild -project '#{project}' -sdk '#{sdk}' -target '#{target}' -configuration '#{configuration}'" error "Failed to build the project!" unless rc Dir.chdir src_dir end def bump_versions(config) status "BUMPING VERSIONS" rc = exe "#{config['agvtool']} bump -all" error "Failed to bump the build number!" unless rc rc = exe "#{config['agvtool']} new-marketing-version #{config['marketing_version']}" error "Failed to set the marketing version!" unless rc config['current_version'] = `#{config['agvtool']} what-version -terse`.chomp! config['current_marketing_version'] = `#{config['agvtool']} what-marketing-version -terse1`.chomp! end def commit_build(config) # commit status "COMMITING BUILD" rc = exe "git commit -a -m 'Successful build #{config['current_version']} for v#{config['current_marketing_version']}'" error "Failed to commit the build!" unless rc # remember the SHA1 for the build config['git_sha'] = `git rev-parse --short HEAD`.chomp! # tag the build status "TAGGING BUILD" config['git_tag'] = "v#{config['current_marketing_version']}-b#{config['current_version']}"; rc = exe "git tag -s -m 'Tagging version #{config['current_marketing_version']}, build #{config['current_version']}' " + "'#{config['git_tag']}'" error "Failed to tag the build!" unless rc # # pull whatever is there # status "PULLING ORIGIN MASTER" # rc = exe "git pull origin master" # error "Failed to pull origin master!" unless rc # # # push whatever is here # status "PUSHING ORIGIN MASTER" # rc = exe "git push origin master" # error "Failed to push origin master!" unless rc # push the tag status "PUSHING THE NEW TAG" rc = exe "git push origin tag '#{config['git_tag']}'" error "Failed to push the tag!" unless rc end def zip_build(config) status "ZIPPING THE BUILD" Dir.chdir config['build_results_dir'] config['buildbase'] = ("#{config['zip_prefix']}_#{config['current_marketing_version']}_" + "#{config['configuration']}_b#{config['current_version']}_#{config['git_sha']}_" + Time.now.strftime('%Y%m%d')).gsub('.', '_').gsub('-', '_') config['zipname'] = config['buildbase'] + '.zip' config['zipname_absolute'] = File.join config['tmp_dir'], config['zipname'] exe "zip -r -9 -y '#{config['zipname_absolute']}' *.app" unless config['mode'] == 'appstore' exe "zip -r -9 -y '#{config['zipname_absolute']}' *.app.dSYM" end unless config['payload'].nil? Dir.chdir File.join(config['src_dir'], config['payload']) exe "zip -r -9 -y '#{config['zipname_absolute']}' ." end FileUtils.mv config['zipname_absolute'], config['products_folder'], :verbose => true end def copy_build(config) status "POPULATING BUILDS FOLDER" build_folder = File.join(config['builds_folder'], config['buildbase']) exe "mkdir -p \"#{build_folder}\"" FileUtils.cp_r Dir.glob(File.join(config['build_results_dir'], '*')), build_folder, :verbose => true end main exit 0