# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

desc <<-DESC
  Update Rubygems versions in version.rb and *.gemspec files

  Example:

      $ rake update_version[5.0.0,5.0.1]
DESC
task :update_version, :old, :new do |_, args|
  require 'ansi'

  puts '[!!!] Required argument [old] missing'.ansi(:red) unless args[:old]
  puts '[!!!] Required argument [new] missing'.ansi(:red) unless args[:new]

  files = Dir['**/**/version.rb','**/**/*.gemspec']

  longest_line = files.map(&:size).max

  puts "\n", '= FILES '.ansi(:faint) + ('='*92).ansi(:faint), "\n"

  files.each do |file|
    begin
      content = File.read(file)
      if content.match Regexp.new(args[:old])
        content.gsub! Regexp.new(args[:old]), args[:new]
        puts "+ [#{file}]".ansi(:green).ljust(longest_line+20) + " [#{args[:old]}] -> [#{args[:new]}]".ansi(:green,:bold)
        File.open(file, 'w') { |f| f.puts content }
      else
        puts "- [#{file}]".ansi(:yellow).ljust(longest_line+20) + " -".ansi(:faint,:strike)
      end
    rescue Exception => e
      puts "[!!!] #{e.class} : #{e.message}".ansi(:red,:bold)
      raise e
    end
  end
end

task :update_changelog do
  puts "\n\n", '= CHANGELOG '.ansi(:faint) + ('='*88).ansi(:faint), "\n"

  log = `git --no-pager log --reverse --no-color --pretty='* %s' HEAD --not v#{args[:old]} elasticsearch*`.split("\n")

  puts log.join("\n")

  log_entries = {}
  log_entries[:client] = log.select { |l| l =~ /\[CLIENT\]/ }
  log_entries[:api] = log.select { |l| l =~ /\[API\]/ }
  log_entries[:xpack] = log.select { |l| l =~ /\[XPACK\]/ }

  changelog = File.read(File.open('CHANGELOG.md', 'r'))

  changelog_update = ''

  if log.any? { |l| l =~ /CLIENT|API/ }
    changelog_update << "## #{args[:new]}\n\n"
  end

  unless log_entries[:client].empty?
    changelog_update << "### Client\n\n"
    changelog_update << log_entries[:client]
                          .map { |l| l.gsub /\[CLIENT\] /, '' }
                          .map { |l| "#{l}" }
                          .join("\n")
    changelog_update << "\n\n"
  end

  unless log_entries[:api].empty?
    changelog_update << "### API\n\n"
    changelog_update << log_entries[:api]
                          .map { |l| l.gsub /\[API\] /, '' }
                          .map { |l| "#{l}" }
                          .join("\n")
    changelog_update << "\n\n"
  end

  unless log_entries[:xpack].empty?
    changelog_update << "### XPACK\n\n"
    changelog_update << log_entries[:xpack]
                            .map { |l| l.gsub /\[XPACK\] /, '' }
                            .map { |l| "#{l}" }
                            .join("\n")
    changelog_update << "\n\n"
  end

  File.open('CHANGELOG.md', 'w+') { |f| f.write changelog_update and f.write changelog }

  puts "\n\n", "= DIFF ".ansi(:faint) + ('='*93).ansi(:faint)

  diff = `git --no-pager diff --patch --word-diff=color --minimal elasticsearch*`.split("\n")

  puts diff
         .reject { |l| l =~ /^\e\[1mdiff \-\-git/ }
         .reject { |l| l =~ /^\e\[1mindex [a-z0-9]{7}/ }
         .reject { |l| l =~ /^\e\[1m\-\-\- i/ }
         .reject { |l| l =~ /^\e\[36m@@/ }
         .map    { |l| l =~ /^\e\[1m\+\+\+ w/ ? "\n#{l}   " + '-'*(104-l.size) : l }
         .join("\n")

  puts "\n\n", '= COMMIT '.ansi(:faint) + ('='*91).ansi(:faint), "\n"

  puts  'git add CHANGELOG.md elasticsearch*',
        "git commit --verbose --message='Release #{args[:new]}' --edit",
        'rake release',
        ''
end
