require "bundler"
require "rake"
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require 'quandl/command'
require 'pry'

task :default => :spec

Dir[File.expand_path("../tasks/*.rake", __FILE__)].each do |task|
  puts task
  load task
end

task :console do |t,args|
  binding.pry
end

desc "Run all specs"
RSpec::Core::RakeTask.new(:spec) do |task|
  task.pattern = "spec/**/*_spec.rb"
end

def checkout_master!
  raise "You have unstaged commits." if %x{git status} =~ /Changes not staged for commit/
  sh "git checkout master"
  raise "Unable to checkout master" unless %x{git branch | grep '*'} =~ /master/
end

def verify_version!(version)
  raise ArgumentError, "Expected version format is: MAJOR.MINOR.PATCH, got: #{version}" unless version =~ /[0-9]+\.[0-9]+\.[0-9]+/
  raise "Version '#{version}' has already been documented in UPGRADE.md" if file_contains_matching("UPGRADE.md", /## #{version}/)  
end

def file_contains_matching(file_path, matching)
  matched = false
  f = File.open(file_path)
  f.each_line do |line|
    if line =~ matching
      matched = true
      break
    end
  end
  f.close
  matched
end

namespace :quandl do
  desc "bump version and generate UPGRADE documentation"
  task :bump, :version do |t,args|
    version = args['version']
    checkout_master!
    verify_version!(version)
    # update UPGRADE.md with commits
    Rake::Task["quandl:generate:documentation"].execute args.to_hash.stringify_keys!
    Rake::Task["quandl:version:bump"].execute args.to_hash.stringify_keys!    
  end
  desc "build and push gem & distros"
  task :release do |t,args|
    checkout_master!
    # tag git revision with version, build quandl.gem, push to rubygems
    Rake::Task["release"].execute
    # build windows exe, mac pkg, tarball
    Rake::Task["toolbelt:release:all"].execute
  end
  namespace :version do
    task :bump, :version do |t,args|
      version_file = 'lib/quandl/command/version.rb'
      IO.write(version_file, File.open(version_file) do |f|
        f.read.gsub(Quandl::Command::VERSION, args['version'])
      end
      )
      sh(%Q{git commit -am "Bump version to: #{args['version']}"})
    end
  end
  namespace :generate do
    task :documentation, :version do |t,args|
      version = args['version']
      verify_version!(version)
      # collect commits that match JIRA syntax
      commits = %x{ git --no-pager log --since="v#{Quandl::Command::VERSION}" --pretty=oneline --grep='^QUGC' }
      # split newlines and exclude reference, select uniq
      commits = commits.split("\n").collect{|c| "* #{c[41..-1]}" }.uniq
      # compose prepend string
      commits = "## #{version} \n\n" + commits.join("\n") + "\n\n\n\n"
      # prepend to UPGRADE.md
      File.write( 'UPGRADE.md', commits + File.read('UPGRADE.md') )
    end
  end
end

namespace :rubies do

  rubies = [
    'ruby-1.9.3-p194',
    'ruby-1.9.3-p484',
    'ruby-2.0.0-p353',
  ]
  
  desc "rspec all the rubies"
  task :spec do |t, args|
    rubies.each do |ruby|
      cmd = "rvm #{ruby} do bundle exec rspec"
      puts(cmd)
      system(cmd)
    end
  end
  
end