require 'xcjobs'
require 'json'

def destinations(platform: 'iphonesimulator')
  if platform == 'iphonesimulator'
    [ 'name=iPhone 5,OS=10.0',
      'name=iPhone 5s,OS=10.0',
      'name=iPhone 6,OS=10.0',
      'name=iPhone 6s Plus,OS=10.0',
      'name=iPhone SE,OS=10.0',
      'name=iPad Air 2,OS=10.0',
      'name=iPad Pro (9.7 inch),OS=10.0',
      'name=iPad Pro (12.9 inch),OS=10.0'
    ]
  elsif platform == 'watchsimulator'
    [ 'name=Apple Watch - 38mm,OS=3.0',
      'name=Apple Watch - 42mm,OS=3.0'
    ]
  elsif platform == 'appletvsimulator'
    [ 'name=Apple TV 1080p,OS=10.0'
    ]
  else
    []
  end
end

def supportedPlatforms
  ['macosx', 'iphoneos', 'iphonesimulator', 'watchos', 'watchsimulator', 'appletvos', 'appletvsimulator']
end

def configurations
  ['Debug', 'Release']
end

desc "build for all platforms"
task :build do |t|
  supportedPlatforms
    .product(configurations)
    .map { |platform, configuration| Rake::Task["build:#{platform}:#{configuration.downcase}"] }
    .map(&:invoke)
end

namespace :build do
  supportedPlatforms.product(configurations).each do |platform, configuration|
    XCJobs::Build.new("#{platform}:#{configuration.downcase}") do |t|
      t.project = 'KeychainAccess'
      t.scheme = 'KeychainAccess'
      t.sdk = platform
      if platform == 'iphonesimulator'
        t.add_destination('name=iPhone 6,OS=10.0')
      elsif platform == 'watchsimulator'
        t.add_destination('name=Apple Watch - 42mm,OS=3.0')
      elsif platform == 'appletvsimulator'
        t.add_destination('name=Apple TV 1080p,OS=10.0')
      end
      t.configuration = configuration
      t.build_dir = 'build'
      t.formatter = 'xcpretty -c'
      if ENV['CI']
        t.add_build_setting('CODE_SIGN_IDENTITY', '')
        t.add_build_setting('CODE_SIGNING_REQUIRED', 'NO')
      end
    end
  end
  
  task :carthage do
    sh %[echo 'github \"kishikawakatsumi/KeychainAccess\" \"#{branch_name}\"' > Cartfile]
    sh %[carthage update --no-use-binaries]
  end
  
  def branch_name
    if ENV['CI']
      pull_req = %[https://api.github.com/repos/#{ENV['TRAVIS_REPO_SLUG']}/pulls/#{ENV['TRAVIS_PULL_REQUEST']}]
      auth_token = 'kishikawakatsumi:209558699492df7782fbe62dda1891d6a6ba010f'
      (ENV['TRAVIS_PULL_REQUEST'] == 'false' ? ENV['TRAVIS_BRANCH'] : JSON.parse(`curl -u #{auth_token} -s #{pull_req}`)['head']['ref']).strip
    else
      %x[git rev-parse --abbrev-ref HEAD]
    end
  end
end

namespace :test do
  supportedPlatforms
    .select { |platform| platform == 'macosx' || platform.end_with?('simulator') }.map do |platform|
      task platform do |t|
        configurations
          .map { |configuration| Rake::Task["test:#{platform}:#{configuration.downcase}"] }
          .map(&:invoke)
      end
    end
end
    
namespace :test do
  supportedPlatforms.product(configurations).each do |platform, configuration|
    XCJobs::Test.new("#{platform}:#{configuration.downcase}") do |t|
      t.project = 'KeychainAccess'
      t.scheme = 'KeychainAccess'
      t.sdk = platform
      destinations(platform: platform).each do |destination|
        t.add_destination(destination)
      end
      t.configuration = configuration
      t.coverage = true
      t.build_dir = 'build'
      t.formatter = 'xcpretty -c'
    end
  end
end

XCJobs::Coverage::Coveralls.new()
