<commit_before>require 'safe_yaml'
require 'uri'
require 'tachikoma'
require 'octokit'
require 'fileutils'

module Tachikoma
  # Main logic of Tachikoma
  class Application
    include FileUtils

    def self.run(strategy)
      new.run(strategy)
    end

    def run(strategy)
      load
      fetch
      send(strategy) if respond_to?(strategy)
      pull_request
    end

    def load
      @build_for = ENV['BUILD_FOR']
      @github_token = ENV[github_token_key(@build_for)]

      base_config_path = File.join(Tachikoma.original_data_path, 'default.yaml')
      base_config = YAML.safe_load_file(base_config_path) || {}
      user_config_path = File.join(Tachikoma.data_path, '__user_config__.yaml')
      user_config = YAML.safe_load_file(user_config_path) if File.exist?(user_config_path)
      user_config ||= {}
      each_config_path = File.join(Tachikoma.data_path, "#{@build_for}.yaml")
      each_config = YAML.safe_load_file(each_config_path) if File.exist?(each_config_path)
      unless each_config
        fail %(Something wrong, BUILD_FOR: #{@build_for}, your config_path: #{each_config_path})
      end

      @configure = base_config.merge(user_config).merge(each_config)

      @commiter_name = @configure['commiter_name']
      @commiter_email = @configure['commiter_email']
      @github_account = @configure['github_account']
      @url = @configure['url']
      @type = @configure['type']
      @base_remote_branch = @configure['base_remote_branch']
      @authorized_compare_url = authorized_compare_url_with_type(@url, @type, @github_token, @github_account)
      @authorized_base_url = authorized_base_url_with_type(@url, @type, @github_token, @github_account)
      @timestamp_format = @configure['timestamp_format']
      @readable_time = Time.now.utc.strftime(@timestamp_format)
      @parallel_option = bundler_parallel_option(Bundler::VERSION, @configure['bundler_parallel_number'])

      @target_head = target_repository_user(@type, @url, @github_account)
      @pull_request_url = repository_identity(@url)
      @pull_request_body = @configure['pull_request_body']
      @pull_request_base = @configure['pull_request_base']
      @pull_request_head = "#{@target_head}:tachikoma/update-#{@readable_time}"
      @pull_request_title = "Exec tachikoma update #{@readable_time}"
    end

    def clean
      mkdir_p(Tachikoma.repos_path)
      rm_rf(Dir.glob(File.join(Tachikoma.repos_path, '*')))
    end

    def fetch
      clean
      sh "git clone #{@authorized_base_url} #{Tachikoma.repos_path}/#{@build_for}"
    end

    def bundler
      Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
        Bundler.with_clean_env do
          sh(*['ruby', '-i', '-pe', '$_.gsub! /^ruby/, "#ruby"', 'Gemfile'])
          sh(*['git', 'config', 'user.name', @commiter_name])
          sh(*['git', 'config', 'user.email', @commiter_email])
          sh(*['git', 'checkout', '-b', "tachikoma/update-#{@readable_time}", @base_remote_branch])
          sh(*(['bundle', '--gemfile', 'Gemfile', '--no-deployment', '--without', 'nothing', '--path', 'vendor/bundle', @parallel_option].compact))
          sh(*%w(bundle update))
          sh(*['git', 'add', 'Gemfile.lock'])
          sh(*['git', 'commit', '-m', "Bundle update #{@readable_time}"]) do
            # ignore exitstatus
          end
          sh(*['git', 'push', @authorized_compare_url, "tachikoma/update-#{@readable_time}"])
        end
      end
    end

    def bundle
      warn '[DEPRECATION] `bundle` is deprecated. Please use `bundler` instead.'
      bundler
    end

    def carton
      Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
        sh "git config user.name #{@commiter_name}"
        sh "git config user.email #{@commiter_email}"
        sh "git checkout -b tachikoma/update-#{@readable_time} #{@base_remote_branch}"
        sh 'carton install'
        sh 'carton update'
        sh 'git add carton.lock' if File.exist?('carton.lock')
        sh 'git add cpanfile.snapshot' if File.exist?('cpanfile.snapshot')
        sh %(git commit -m "Carton update #{@readable_time}") do
          # ignore exitstatus
        end
        sh "git push #{@authorized_compare_url} tachikoma/update-#{@readable_time}"
      end
    end

    def none
      Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
        sh "git config user.name #{@commiter_name}"<commit_msg>refactor(app): use splat operator<commit_after>require 'safe_yaml'
require 'uri'
require 'tachikoma'
require 'octokit'
require 'fileutils'

module Tachikoma
  # Main logic of Tachikoma
  class Application
    include FileUtils

    def self.run(strategy)
      new.run(strategy)
    end

    def run(strategy)
      load
      fetch
      send(strategy) if respond_to?(strategy)
      pull_request
    end

    def load
      @build_for = ENV['BUILD_FOR']
      @github_token = ENV[github_token_key(@build_for)]

      base_config_path = File.join(Tachikoma.original_data_path, 'default.yaml')
      base_config = YAML.safe_load_file(base_config_path) || {}
      user_config_path = File.join(Tachikoma.data_path, '__user_config__.yaml')
      user_config = YAML.safe_load_file(user_config_path) if File.exist?(user_config_path)
      user_config ||= {}
      each_config_path = File.join(Tachikoma.data_path, "#{@build_for}.yaml")
      each_config = YAML.safe_load_file(each_config_path) if File.exist?(each_config_path)
      unless each_config
        fail %(Something wrong, BUILD_FOR: #{@build_for}, your config_path: #{each_config_path})
      end

      @configure = base_config.merge(user_config).merge(each_config)

      @commiter_name = @configure['commiter_name']
      @commiter_email = @configure['commiter_email']
      @github_account = @configure['github_account']
      @url = @configure['url']
      @type = @configure['type']
      @base_remote_branch = @configure['base_remote_branch']
      @authorized_compare_url = authorized_compare_url_with_type(@url, @type, @github_token, @github_account)
      @authorized_base_url = authorized_base_url_with_type(@url, @type, @github_token, @github_account)
      @timestamp_format = @configure['timestamp_format']
      @readable_time = Time.now.utc.strftime(@timestamp_format)
      @parallel_option = bundler_parallel_option(Bundler::VERSION, @configure['bundler_parallel_number'])

      @target_head = target_repository_user(@type, @url, @github_account)
      @pull_request_url = repository_identity(@url)
      @pull_request_body = @configure['pull_request_body']
      @pull_request_base = @configure['pull_request_base']
      @pull_request_head = "#{@target_head}:tachikoma/update-#{@readable_time}"
      @pull_request_title = "Exec tachikoma update #{@readable_time}"
    end

    def clean
      mkdir_p(Tachikoma.repos_path)
      rm_rf(Dir.glob(File.join(Tachikoma.repos_path, '*')))
    end

    def fetch
      clean
      sh(*['git', 'clone', @authorized_base_url, "#{Tachikoma.repos_path}/#{@build_for}"])
    end

    def bundler
      Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
        Bundler.with_clean_env do
          sh(*['ruby', '-i', '-pe', '$_.gsub! /^ruby/, "#ruby"', 'Gemfile'])
          sh(*['git', 'config', 'user.name', @commiter_name])
          sh(*['git', 'config', 'user.email', @commiter_email])
          sh(*['git', 'checkout', '-b', "tachikoma/update-#{@readable_time}", @base_remote_branch])
          sh(*(['bundle', '--gemfile', 'Gemfile', '--no-deployment', '--without', 'nothing', '--path', 'vendor/bundle', @parallel_option].compact))
          sh(*%w(bundle update))
          sh(*['git', 'add', 'Gemfile.lock'])
          sh(*['git', 'commit', '-m', "Bundle update #{@readable_time}"]) do
            # ignore exitstatus
          end
          sh(*['git', 'push', @authorized_compare_url, "tachikoma/update-#{@readable_time}"])
        end
      end
    end

    def bundle
      warn '[DEPRECATION] `bundle` is deprecated. Please use `bundler` instead.'
      bundler
    end

    def carton
      Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
        sh(*['git', 'config', 'user.name', @commiter_name])
        sh(*['git', 'config', 'user.email', @commiter_email])
        sh(*['git', 'checkout', '-b', "tachikoma/update-#{@readable_time}", @base_remote_branch])
        sh(*%w(carton install))
        sh(*%w(carton update))
        sh(*['git', 'add', 'carton.lock']) if File.exist?('carton.lock')
        sh(*['git', 'add', 'cpanfile.snapshot']) if File.exist?('cpanfile.snapshot')
        sh(*['git', 'commit', '-m', "Carton update #{@readable_time}"]) do
          # ignore exitstatus
        end
        sh(*['git', 'push', @authorized_compare_url, "tachikoma/update-#{@readable_time}"])
      end
    end

    def none
      Dir.chdir("#{Tachikoma.repos_path}/#{@build_for}") do
        sh "git config user.name #{@commiter_name}"
{'fraction_non_alphanumeric': 0.151271905031091, 'fraction_numerical': 0.0, 'mean_word_length': 3.5457348406988696, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2106834', 'n_tokens_mistral': 2991, 'n_tokens_neox': 2948, 'n_words': 528}