<commit_before>class Recipes::Heroku < Rails::AppBuilder
  NAME_PREFIX = 'pl'

  attr_accessor :app_name_staging

  def initialize(args)
    super(args)
    set(:heroku_app_name_staging, app_name_for('staging'))
    set(:heroku_app_name_production, app_name_for('production'))
  end

  def ask
    heroku = answer(:heroku) do
      Ask.confirm("Are you going to deploy to heroku? (#{who_am_i})")
    end

    if heroku
      set(:heroku, heroku)
    end
  end

  def create
    add_heroku if get(:heroku)
  end

  def install
    if gem_exists?(/rails_stdout_logging/) && !force?
      info "Heroku is already installed"
    else
      add_heroku
    end
  end

  private

  def add_heroku
    gather_gems(:production, :staging) do
      gather_gem('rails_stdout_logging')
    end

    copy_file '../assets/Procfile', 'Procfile'
    copy_file '../assets/.buildpacks', '.buildpacks'

    if logged_in?
      %w(staging production).each do |environment|
        create_app_on_heroku(environment)
      end
      outro
    else
      puts "You are not logged in into heroku"
      login_command = "\e[33mheroku login\e[0m"
      puts "Run #{login_command} and enter your credentials"
      puts "You can install the heroku recipe again create the app in heroku"
      install_command = "\e[33mpostassium install heroku --force\e[0m"
      puts "Just run #{install_command}"
    end
  end

  def heroku_pipeline_name
    app_name.dasherize
  end

  def app_name_for(environment)
    "#{NAME_PREFIX}-#{app_name.dasherize}-#{environment}"
  end

  def logged_in?
    system('heroku auth:whoami &> /dev/null')
  end

  def who_am_i
    `heroku auth:whoami`.strip
  end

  def create_app_on_heroku(environment)
    rack_env = "RACK_ENV=#{environment} RAILS_ENV=#{environment}"
    staged_app_name = app_name_for(environment)

    run_toolbelt_command "create #{staged_app_name}", staged_app_name
    run_toolbelt_command "config:add #{rack_env}", staged_app_name

    set_rails_secrets(environment)
    add_app_to_pipeline(staged_app_name, environment)
  end

  def set_rails_secrets(environment)
    run_toolbelt_command(<commit_msg>fix(heroku): change how we check if we are logged in<commit_after>class Recipes::Heroku < Rails::AppBuilder
  NAME_PREFIX = 'pl'

  attr_accessor :app_name_staging

  def initialize(args)
    super(args)
    set(:heroku_app_name_staging, app_name_for('staging'))
    set(:heroku_app_name_production, app_name_for('production'))
  end

  def ask
    heroku = answer(:heroku) do
      Ask.confirm("Are you going to deploy to heroku? (#{who_am_i})")
    end

    if heroku
      set(:heroku, heroku)
    end
  end

  def create
    add_heroku if get(:heroku)
  end

  def install
    if gem_exists?(/rails_stdout_logging/) && !force?
      info "Heroku is already installed"
    else
      add_heroku
    end
  end

  private

  def add_heroku
    gather_gems(:production, :staging) do
      gather_gem('rails_stdout_logging')
    end

    copy_file '../assets/Procfile', 'Procfile'
    copy_file '../assets/.buildpacks', '.buildpacks'

    if logged_in?
      %w(staging production).each do |environment|
        create_app_on_heroku(environment)
      end
      outro
    else
      puts "You are not logged in into heroku"
      login_command = "\e[33mheroku login\e[0m"
      puts "Run #{login_command} and enter your credentials"
      puts "You can install the heroku recipe again create the app in heroku"
      install_command = "\e[33mpostassium install heroku --force\e[0m"
      puts "Just run #{install_command}"
    end
  end

  def heroku_pipeline_name
    app_name.dasherize
  end

  def app_name_for(environment)
    "#{NAME_PREFIX}-#{app_name.dasherize}-#{environment}"
  end

  def logged_in?
    !who_am_i.include? "not logged in"
  end

  def who_am_i
    `heroku auth:whoami`.strip
  end

  def create_app_on_heroku(environment)
    rack_env = "RACK_ENV=#{environment} RAILS_ENV=#{environment}"
    staged_app_name = app_name_for(environment)

    run_toolbelt_command "create #{staged_app_name}", staged_app_name
    run_toolbelt_command "config:add #{rack_env}", staged_app_name

    set_rails_secrets(environment)
    add_app_to_pipeline(staged_app_name, environment)
  end

  def set_rails_secrets(environment)
    run_toolbelt_command(
{'fraction_non_alphanumeric': 0.09360410830999066, 'fraction_numerical': 0.0028011204481792717, 'mean_word_length': 3.505783385909569, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, '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': '3494271', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1457, 'n_words': 333}