#!/usr/bin/env ruby # frozen_string_literal: true

require “fileutils”

# deployment script generated by rockstart:deployment

# path to your application root. APP_ROOT = File.expand_path(“..”, __dir__)

def system!(*args)

system(*args) || abort("\n== Command #{args} failed ==")

end

FileUtils.chdir APP_ROOT do

puts "== Installing Heroku CLI =="
system! "which heroku && brew upgrade heroku/brew/heroku || " \
        "(brew tap heroku/brew && brew install heroku)"

puts "\n== Creating Development App =="
system! "git remote show heroku || heroku create"

puts "\n== Configuring APP_HOST =="
current_app_host = `heroku config:get APP_HOST -s`.split("=").last
if current_app_host.strip.empty?
  require "json"
  app_host = JSON.parse(`heroku domains --json`).first.fetch("hostname")
  system! "heroku config:set APP_HOST=#{app_host}"
else
  puts "Skipping: APP_HOST has already been set to #{current_app_host}"
end

puts "\n== Pushing to Heroku =="
system! "git push heroku master"

puts "\n== Dumping Heroku Config =="
if !File.exists?(File.join(APP_ROOT, ".env"))
  system! "bundle exec rake heroku:dump_config"
else
  puts "Skipping: a .env file already exists."
end

end