Class: ShopifyCLI::Heroku

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify_cli/heroku.rb

Constant Summary collapse

DOWNLOAD_URLS =
{
  linux: "https://cli-assets.heroku.com/heroku-linux-x64.tar.gz",
  mac: "https://cli-assets.heroku.com/heroku-darwin-x64.tar.gz",
  windows: "https://cli-assets.heroku.com/heroku-x64.exe",
  mac_m1: "https://cli-assets.heroku.com/heroku-darwin-x64.tar.gz",
}

Instance Method Summary collapse

Constructor Details

#initialize(ctx) ⇒ Heroku

Returns a new instance of Heroku.



10
11
12
# File 'lib/shopify_cli/heroku.rb', line 10

def initialize(ctx)
  @ctx = ctx
end

Instance Method Details

#add_buildpacks(buildpacks) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/shopify_cli/heroku.rb', line 84

def add_buildpacks(buildpacks)
  result = @ctx.system(heroku_command, "buildpacks:clear")
  msg = @ctx.message("core.heroku.error.add_buildpacks")
  @ctx.abort(msg) unless result.success?

  buildpacks.each do |buildpack|
    result = @ctx.system(heroku_command, "buildpacks:add", buildpack)
    msg = @ctx.message("core.heroku.error.add_buildpacks")
    @ctx.abort(msg) unless result.success?
  end
end

#appObject



14
15
16
17
18
19
# File 'lib/shopify_cli/heroku.rb', line 14

def app
  return nil unless (app = git_remote)
  app = app.split("/").last
  app = app.split(".").first
  app
end

#authenticateObject



21
22
23
24
# File 'lib/shopify_cli/heroku.rb', line 21

def authenticate
  result = @ctx.system(heroku_command, "login")
  @ctx.abort(@ctx.message("core.heroku.error.authentication")) unless result.success?
end

#create_new_appObject



26
27
28
29
30
# File 'lib/shopify_cli/heroku.rb', line 26

def create_new_app
  output, status = @ctx.capture2e(heroku_command, "create")
  @ctx.abort(@ctx.message("core.heroku.error.creation")) unless status.success?
  @ctx.puts(output)
end

#deploy(branch_to_deploy) ⇒ Object



32
33
34
35
# File 'lib/shopify_cli/heroku.rb', line 32

def deploy(branch_to_deploy)
  result = @ctx.system("git", "push", "-u", "heroku", "#{branch_to_deploy}:master")
  @ctx.abort(@ctx.message("core.heroku.error.deploy")) unless result.success?
end

#downloadObject



37
38
39
40
41
42
43
# File 'lib/shopify_cli/heroku.rb', line 37

def download
  return if installed?

  result = @ctx.system("curl", "-o", download_path, DOWNLOAD_URLS[@ctx.os], chdir: ShopifyCLI.cache_dir)
  @ctx.abort(@ctx.message("core.heroku.error.download")) unless result.success?
  @ctx.abort(@ctx.message("core.heroku.error.download")) unless File.exist?(download_path)
end

#get_config(config) ⇒ Object



71
72
73
74
75
# File 'lib/shopify_cli/heroku.rb', line 71

def get_config(config)
  output, status = @ctx.capture2e(heroku_command, "config:get", config.to_s)
  return output.strip if status.success?
  nil
end

#heroku_commandObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/shopify_cli/heroku.rb', line 96

def heroku_command
  local_path = File.join(ShopifyCLI.cache_dir, "heroku", "bin", "heroku").to_s
  if File.exist?(local_path)
    local_path
  elsif @ctx.windows?
    begin
      # Check if Heroku exists in the Windows registry and run it from there
      require "win32/registry"

      windows_path = Win32::Registry::HKEY_CURRENT_USER.open('SOFTWARE\heroku') do |reg|
        reg[""] # This reads the 'Default' registry key
      end

      File.join(windows_path, "bin", "heroku").to_s
    rescue StandardError, LoadError
      "heroku"
    end
  else
    "heroku"
  end
end

#installObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/shopify_cli/heroku.rb', line 45

def install
  return if installed?

  result = if @ctx.windows?
    @ctx.system("\"#{download_path}\"")
  else
    @ctx.system("tar", "-xf", download_path, chdir: ShopifyCLI.cache_dir)
  end
  @ctx.abort(@ctx.message("core.heroku.error.install")) unless result.success?

  @ctx.rm(download_path)
end

#select_existing_app(app_name) ⇒ Object



58
59
60
61
62
63
# File 'lib/shopify_cli/heroku.rb', line 58

def select_existing_app(app_name)
  result = @ctx.system(heroku_command, "git:remote", "-a", app_name)

  msg = @ctx.message("core.heroku.error.could_not_select_app", app_name)
  @ctx.abort(msg) unless result.success?
end

#set_config(config, value) ⇒ Object



77
78
79
80
81
82
# File 'lib/shopify_cli/heroku.rb', line 77

def set_config(config, value)
  result = @ctx.system(heroku_command, "config:set", "#{config}=#{value}")

  msg = @ctx.message("core.heroku.error.set_config", config, value)
  @ctx.abort(msg) unless result.success?
end

#whoamiObject



65
66
67
68
69
# File 'lib/shopify_cli/heroku.rb', line 65

def whoami
  output, status = @ctx.capture2e(heroku_command, "whoami")
  return output.strip if status.success?
  nil
end