require 'toolrack' include Antrapol::ToolRack::ConditionUtils

require 'devops_helper'

# include task like 'rake build' require 'bundler/gem_tasks' # another way to build the gem require 'rubygems/commands/build_command'

require 'fileutils'

desc “Some simple rake tasks to assist gem developer in releasing new version of gem”

namespace :devops do

task :gem_release do

  STDOUT.puts "Managed gem release V#{DevopsHelper::VERSION}" 

  wd = Dir.getwd
  tp = TTY::Prompt.new

  vh = DevopsHelper::VcsHelper.new(wd)
  if vh.is_workspace?
    # check if workspace has pending changes
    begin
      if vh.has_pending_changes? or vh.has_new_changes?
        cc = tp.yes?("There are pending changes not yet commit. Commit first? ")
        if cc
          STDOUT.puts "Commit the changes first before release"
          exit(0)
        end
      end
    rescue TTY::Reader::InputInterrupt
      STDOUT.puts "\n\nAborted"
      exit(1)
    end

  end # is_workspace?

  rh = DevopsHelper::GemReleaseHelper.new
  spec = rh.find_gemspec(wd) 

  # find version file
  vf = rh.find_gem_version_file(wd) 
  if vf.length > 1
    # more then one. User has to select
    svf = vf.first
  else
    svf = vf.first 
  end

  if not_empty?(spec) #and not_empty?(svf)

    vs = rh.get_version_record(wd) 
    gs = Gem::Specification.load(spec)
    lstVer = vs.last_version(gs.name)
    if not is_empty?(lstVer)
      targetVersion = rh.prompt_version(gs.name, lstVer[:version], lstVer[:created_at])
    else
      targetVersion = rh.prompt_version(gs.name, "", nil)
    end 

    if targetVersion == -1
      STDERR.puts "Aborted"
      exit(1)
    end

    if not_empty?(svf)
      rh.rewrite_gem_version_file(svf,targetVersion) 
      STDOUT.puts "Gem version file rewritten"

      if vh.is_workspace?
        # commit changes of the new version file before tagging
        vh.add(svf)
        vh.commit("Automated commit by DevOps Helper during release process", { files: [svf] })
      end

    end

    # tagging start here. Any new changes has to be done before this stage
    if vh.is_workspace?
      begin
        tagSrc = tp.yes?("\nTag the source code for this release? ")
        if tagSrc
          msg = tp.ask("Please provide message for this tag (optional) : ", default: "Tagged version '#{targetVersion}' by DevOps Helper")
          msg = "Automated tagging version '#{targetVersion}' by DevOps Helper" if is_empty?(msg)
          vh.create_tag(targetVersion, msg)
        end
      rescue TTY::Reader::InputInterrupt
        STDERR.puts "\n\nAborted"
        exit(1)
      end
    end
    # done tagging source code

    # build the gem
    res = rh.build_gem
    STDOUT.puts "Gem '#{gs.name}' built"
    # gem built

    vs.register_version(gs.name, targetVersion)
    vs.save(wd)
    STDOUT.puts "Version '#{targetVersion}' registered"

    # push gem? Optional anyway
    pub = tp.yes?("Do you want to publish the generated Gem file? ")
    if pub
      res, gemFile = rh.publish_gem(targetVersion) do |ops, vers|
        case ops
        when :multiple_built_gems
          sel = vers
          sel << "Quit"
          selGem = tp.select("There are multiple Gems with the same version number found. Please select one of the following to publish: ", sel)
          if selGem == "Quit"
            STDOUT.puts "No built gem shall be published."
          else
            selGem
          end
        end
      end

      if not res.success?
        STDERR.puts "Failed to publish the Gem."
      else
        STDOUT.puts "Gem '#{gemFile}' published"
      end
    else
      STDOUT.puts "Gem will not be published. Please manually publish the gem."
    end
    # end push gem

    # push source code?
    #pushSrc = tp.no?("Do you want to push the source code to remote?")

  elsif is_empty?(spec)
    STDERR.puts "gemspec file not found at '#{wd}'"
    exit(2)
  elsif is_empty?(svf)
    STDERR.puts "Cannot find version.rb to update GEM version"
    exit(2)
  end 

end # gem_release

end # devops