<commit_before>require 'heroku-rails-saas'

HEROKU_CONFIG_FILE = File.join(HerokuRailsSaas::Config.root, 'config', 'heroku.yml')
HEROKU_APP_SPECIFIC_CONFIG_FILES = Dir.glob("#{File.join(HerokuRailsSaas::Config.root, 'config', 'heroku')}/*.yml")
HEROKU_CONFIG = HerokuRailsSaas::Config.new({:default => HEROKU_CONFIG_FILE, :apps => HEROKU_APP_SPECIFIC_CONFIG_FILES})
HEROKU_RUNNER = HerokuRailsSaas::Runner.new(HEROKU_CONFIG)

# create all the environment specific tasks
(HEROKU_CONFIG.apps).each do |app, hsh|
  hsh.each do |env, heroku_env|
    app_name = HerokuRailsSaas::Config.app_name(app, env)
    desc "Select #{app_name} Heroku app for later commands"
    task app_name do
      # callback switch_environment
      @heroku_app = {:env => heroku_env, :app_name => app_name}
      Rake::Task["heroku:switch_environment"].reenable
      Rake::Task["heroku:switch_environment"].invoke

      HEROKU_RUNNER.add_environment(app_name)
    end
  end
end

desc 'Select all Heroku apps for later command (production must be explicitly declared)'
task :all do
  HEROKU_RUNNER.all_environments(true)
end

(HEROKU_CONFIG.all_environments).each do |env|
  desc "Select all Heroku apps in #{env} environment"
  task "all:#{env}" do
    HEROKU_RUNNER.environments(env)
  end
end

namespace :heroku do
  def system_with_echo(*args)
    HEROKU_RUNNER.system_with_echo(*args)
  end

  desc 'Add git remotes for all apps in this project'
  task :remotes do
    HEROKU_RUNNER.all_environments
    HEROKU_RUNNER.each_heroku_app do |heroku_env, app_name, repo|
      system_with_echo("git remote add #{app_name} #{repo}")
    end
  end

  desc 'Lists configured apps'
  task :apps do
    HEROKU_RUNNER.all_environments
    puts "\n"
    HEROKU_RUNNER.each_heroku_app do |heroku_env, app_name, repo|
      puts "#{heroku_env} maps to the Heroku app #{app_name} located at:"
      puts "  #{repo}"
      puts
    end
  end

  desc "Get remote server information on the heroku app"
  task :info do
    HEROKU_RUNNER.each_heroku_app do |heroku_env, app_name, repo|
      system_with_echo "heroku info --app #{app_name}"
      puts "\n"
    end
  end

  desc "Deploys, migrates and restarts latest git tag"
  task :deploy => "heroku:before_deploy" do |t, args|
    HEROKU_RUNNER.each_heroku_app do |heroku_env, app_name, repo|
      puts "\n\nDeploying to #{app_name}..."
      # set the current heroku_app so that callbacks can read the data
      @heroku_app = {:env => heroku_env, :app_name => app_name, :repo => repo}
      Rake::Task["heroku:before_each_deploy"].reenable
      Rake::Task["heroku:before_each_deploy"].invoke(app_name)

      cmd = HEROKU_CONFIG.cmd(heroku_env)

      if heroku_env[HEROKU_RUNNER.regex_for(:production)]
        all_tags = `git tag`
        target_tag = `git describe --tags --abbrev=0`.chomp # Set latest tag as default

        begin
          puts "\nGit tags:"
          puts all_tags
          print "\nPlease enter a tag to deploy (or hit Enter for \"#{target_tag}\"): "
          input_tag = STDIN.gets.chomp
          if input_tag.present?
            if all_tags[/^#{input_tag}\n/].present?
              target_tag = input_tag
              invalid = false
            else
              puts "\n\nInvalid git tag!"
              invalid = true
            end
          end
        end while invalid
        puts "Unable to determine the tag to deploy." and exit(1) if target_tag.empty?
        to_deploy = target_tag
      else
        to_deploy = `git branch`.scan(/^\* (.*)\n/).flatten.first.to_s
        puts "Unable to determine the current git branch, please checkout the branch you'd like to deploy." and exit(1) if to_deploy.empty?
      end

      @git_push_arguments ||= []
      @git_push_arguments << '--force'

      # ^0 is required so git dereferences the tag into a commit SHA (else Heroku's git server will throw up)
      system_with_echo "git push #{repo} #{@git_push_arguments.join(' ')} #{to_deploy}^0:refs/heads/master"

      system_with_echo "heroku maintenance:on --app #{app_name}"
<commit_msg>prevent --force --force args adding up when deploying to multiple apps in one go<commit_after>require 'heroku-rails-saas'

HEROKU_CONFIG_FILE = File.join(HerokuRailsSaas::Config.root, 'config', 'heroku.yml')
HEROKU_APP_SPECIFIC_CONFIG_FILES = Dir.glob("#{File.join(HerokuRailsSaas::Config.root, 'config', 'heroku')}/*.yml")
HEROKU_CONFIG = HerokuRailsSaas::Config.new({:default => HEROKU_CONFIG_FILE, :apps => HEROKU_APP_SPECIFIC_CONFIG_FILES})
HEROKU_RUNNER = HerokuRailsSaas::Runner.new(HEROKU_CONFIG)

# create all the environment specific tasks
(HEROKU_CONFIG.apps).each do |app, hsh|
  hsh.each do |env, heroku_env|
    app_name = HerokuRailsSaas::Config.app_name(app, env)
    desc "Select #{app_name} Heroku app for later commands"
    task app_name do
      # callback switch_environment
      @heroku_app = {:env => heroku_env, :app_name => app_name}
      Rake::Task["heroku:switch_environment"].reenable
      Rake::Task["heroku:switch_environment"].invoke

      HEROKU_RUNNER.add_environment(app_name)
    end
  end
end

desc 'Select all Heroku apps for later command (production must be explicitly declared)'
task :all do
  HEROKU_RUNNER.all_environments(true)
end

(HEROKU_CONFIG.all_environments).each do |env|
  desc "Select all Heroku apps in #{env} environment"
  task "all:#{env}" do
    HEROKU_RUNNER.environments(env)
  end
end

namespace :heroku do
  def system_with_echo(*args)
    HEROKU_RUNNER.system_with_echo(*args)
  end

  desc 'Add git remotes for all apps in this project'
  task :remotes do
    HEROKU_RUNNER.all_environments
    HEROKU_RUNNER.each_heroku_app do |heroku_env, app_name, repo|
      system_with_echo("git remote add #{app_name} #{repo}")
    end
  end

  desc 'Lists configured apps'
  task :apps do
    HEROKU_RUNNER.all_environments
    puts "\n"
    HEROKU_RUNNER.each_heroku_app do |heroku_env, app_name, repo|
      puts "#{heroku_env} maps to the Heroku app #{app_name} located at:"
      puts "  #{repo}"
      puts
    end
  end

  desc "Get remote server information on the heroku app"
  task :info do
    HEROKU_RUNNER.each_heroku_app do |heroku_env, app_name, repo|
      system_with_echo "heroku info --app #{app_name}"
      puts "\n"
    end
  end

  desc "Deploys, migrates and restarts latest git tag"
  task :deploy => "heroku:before_deploy" do |t, args|
    HEROKU_RUNNER.each_heroku_app do |heroku_env, app_name, repo|
      puts "\n\nDeploying to #{app_name}..."
      # set the current heroku_app so that callbacks can read the data
      @heroku_app = {:env => heroku_env, :app_name => app_name, :repo => repo}
      Rake::Task["heroku:before_each_deploy"].reenable
      Rake::Task["heroku:before_each_deploy"].invoke(app_name)

      cmd = HEROKU_CONFIG.cmd(heroku_env)

      if heroku_env[HEROKU_RUNNER.regex_for(:production)]
        all_tags = `git tag`
        target_tag = `git describe --tags --abbrev=0`.chomp # Set latest tag as default

        begin
          puts "\nGit tags:"
          puts all_tags
          print "\nPlease enter a tag to deploy (or hit Enter for \"#{target_tag}\"): "
          input_tag = STDIN.gets.chomp
          if input_tag.present?
            if all_tags[/^#{input_tag}\n/].present?
              target_tag = input_tag
              invalid = false
            else
              puts "\n\nInvalid git tag!"
              invalid = true
            end
          end
        end while invalid
        puts "Unable to determine the tag to deploy." and exit(1) if target_tag.empty?
        to_deploy = target_tag
      else
        to_deploy = `git branch`.scan(/^\* (.*)\n/).flatten.first.to_s
        puts "Unable to determine the current git branch, please checkout the branch you'd like to deploy." and exit(1) if to_deploy.empty?
      end

      @git_push_arguments ||= []
      @git_push_arguments << '--force'

      # ^0 is required so git dereferences the tag into a commit SHA (else Heroku's git server will throw up)
      system_with_echo "git push #{repo} #{@git_push_arguments.uniq.join(' ')} #{to_deploy}^0:refs/heads/master"

      system_with_echo "heroku maintenance:on --app #{app_name}"

{'fraction_non_alphanumeric': 0.10538555691554467, 'fraction_numerical': 0.0012239902080783353, 'mean_word_length': 3.2314862765406525, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '4088805', 'n_tokens_mistral': 2798, 'n_tokens_neox': 2724, 'n_words': 723}