# frozen_string_literal: true

require 'rake'
require 'fileutils'

namespace :shuttlerock_shared_config do
  task update: %i[update_codeclimate update_eslint update_rubocop update_stylelintrc update_dangerfile update_pull_request_template update_codecov update_gitleaks] do
  end

  desc 'Update .codeclimate.yml'
  task :update_codeclimate do
    input_path = File.expand_path('../../lib/templates/.codeclimate.yml', __dir__)
    FileUtils.copy(input_path, Dir.pwd)
    warn('Updated .codeclimate.yml')
  end

  desc 'Update .eslintrc'
  task :update_eslint do
    input_path = File.expand_path('../../lib/templates/.eslintrc', __dir__)
    FileUtils.copy(input_path, Dir.pwd)
    warn('Updated .eslintrc')
  end

  desc 'Update .rubocop.yml'
  task :update_rubocop do
    input_path = File.expand_path('../../lib/templates/.rubocop.yml', __dir__)
    FileUtils.copy(input_path, Dir.pwd)
    warn('Updated .rubocop.yml')
  end

  desc 'Update .stylelintrc'
  task :update_stylelintrc do
    input_path = File.expand_path('../../lib/templates/.stylelintrc', __dir__)
    FileUtils.copy(input_path, Dir.pwd)
    warn('Updated .stylelintrc')
  end

  desc 'Update Dangerfile'
  task :update_dangerfile do
    input_path = File.expand_path('../../lib/templates/Dangerfile', __dir__)
    FileUtils.copy(input_path, Dir.pwd)
    warn("Updated .Dangerfile\n" \
      "WARNING!!!\n" \
      "For danger.gem to work correctly, you need to add settings to:\n" \
      ".circleci/config.yml, doc/env_list.yml")
    input_path = File.expand_path('../../lib/templates/.env.example', __dir__)
    FileUtils.copy(input_path, Dir.pwd)
    input_path = File.expand_path('../../lib/templates/env_list.yml', __dir__)
    result_dir = Dir.pwd + '/doc'
    unless File.exist?(Dir.pwd + '/doc/env_list.yml')
      FileUtils.mkdir_p(result_dir)
      FileUtils.copy(input_path, result_dir)
      warn('Created /doc/env_list.yml')
    end
  end

  desc 'Update pull_request_template.md'
  task :update_pull_request_template do
    input_path = File.expand_path('../../lib/templates/PULL_REQUEST_TEMPLATE.md', __dir__)
    result_dir = Dir.pwd + '/.github'
    FileUtils.mkdir_p(result_dir) unless File.directory?(result_dir)
    FileUtils.copy(input_path, result_dir)
    warn('Updated pull_request_template.md')
  end

  desc 'Update codecov.yml'
  task :update_codecov do
    input_path = File.expand_path('../../lib/templates/codecov.yml', __dir__)
    FileUtils.copy(input_path, Dir.pwd)
    warn('Updated codecov.yml')
  end

  desc 'Update gitleaks'
  task :update_gitleaks do
    input_path = File.expand_path('../../lib/templates/gitleaks.yml', __dir__)
    result_dir = Dir.pwd + '/.github/workflows'
    FileUtils.mkdir_p(result_dir) unless File.directory?(result_dir)
    FileUtils.copy(input_path, result_dir)
    warn('Updated /.github/workflows/gitleaks.yml')

    input_path = File.expand_path('../../lib/templates/.gitleaks.toml', __dir__)
    FileUtils.copy(input_path, Dir.pwd)
    warn('Updated .gitleaks.toml')
  end
end
