Class: ShopifyCLI::Services::App::Deploy::Heroku::PHPService

Inherits:
BaseService
  • Object
show all
Defined in:
lib/shopify_cli/services/app/deploy/heroku/php_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

call

Constructor Details

#initialize(context:) ⇒ PHPService

Returns a new instance of PHPService.



9
10
11
12
# File 'lib/shopify_cli/services/app/deploy/heroku/php_service.rb', line 9

def initialize(context:)
  @context = context
  super()
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/shopify_cli/services/app/deploy/heroku/php_service.rb', line 7

def context
  @context
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/shopify_cli/services/app/deploy/heroku/php_service.rb', line 14

def call
  spin_group = CLI::UI::SpinGroup.new
  heroku_service = ShopifyCLI::Heroku.new(context)

  spin_group.add(context.message("core.app.deploy.heroku.downloading")) do |spinner|
    heroku_service.download
    spinner.update_title(context.message("core.app.deploy.heroku.downloaded"))
  end
  spin_group.wait

  install_message = context.message(
    context.windows? ? "core.app.deploy.heroku.installing_windows" : "core.app.deploy.heroku.installing"
  )
  spin_group.add(install_message) do |spinner|
    heroku_service.install
    spinner.update_title(context.message("core.app.deploy.heroku.installed"))
  end
  spin_group.wait

  spin_group.add(context.message("core.app.deploy.heroku.git.checking")) do |spinner|
    ShopifyCLI::Git.init(context)
    spinner.update_title(context.message("core.app.deploy.heroku.git.initialized"))
  end
  spin_group.wait

  if ( = heroku_service.whoami)
    context.puts(context.message("core.app.deploy.heroku.authenticated_with_account", ))
  else
    CLI::UI::Frame.open(
      context.message("core.app.deploy.heroku.authenticating"),
      success_text: context.message("core.app.deploy.heroku.authenticated")
    ) do
      heroku_service.authenticate
    end
  end

  if (app_name = heroku_service.app)
    context.puts(context.message("core.app.deploy.heroku.app.selected", app_name))
  else
    app_type = CLI::UI::Prompt.ask(context.message("core.app.deploy.heroku.app.no_apps_found")) do |handler|
      handler.option(context.message("core.app.deploy.heroku.app.create")) { :new }
      handler.option(context.message("core.app.deploy.heroku.app.select")) { :existing }
    end

    if app_type == :existing
      app_name = CLI::UI::Prompt.ask(context.message("core.app.deploy.heroku.app.name"))
      CLI::UI::Frame.open(
        context.message("core.app.deploy.heroku.app.selecting", app_name),
        success_text: context.message("core.app.deploy.heroku.app.selected", app_name)
      ) do
        heroku_service.select_existing_app(app_name)
      end
    elsif app_type == :new
      CLI::UI::Frame.open(
        context.message("core.app.deploy.heroku.app.creating"),
        success_text: context.message("core.app.deploy.heroku.app.created")
      ) do
        heroku_service.create_new_app
        app_name = heroku_service.app
      end
    end
  end

  branches = ShopifyCLI::Git.branches(context)
  if branches.length == 1
    branch_to_deploy = branches[0]
    context.puts(context.message("core.app.deploy.heroku.git.branch_selected", branch_to_deploy))
  else
    prompt_question = context.message("core.app.deploy.heroku.git.what_branch")
    branch_to_deploy = CLI::UI::Prompt.ask(prompt_question) do |handler|
      branches.each do |branch|
        handler.option(branch) { branch }
      end
    end
  end

  app_url = "https://#{app_name}.herokuapp.com"

  CLI::UI::Frame.open(
    context.message("core.app.deploy.heroku.app.setting_configs"),
    success_text: context.message("core.app.deploy.heroku.app.configs_set")
  ) do
    allowed_configs = [/SHOPIFY_API_KEY/, /SHOPIFY_API_SECRET/, /SCOPES/, /HOST/]

    ShopifyCLI::Project.current.env.to_h.each do |config, value|
      next unless allowed_configs.any? { |pattern| pattern.match?(config) }

      value = app_url if config == "HOST"

      current = heroku_service.get_config(config)
      heroku_service.set_config(config, value) if current.nil? || current != value
    end

    current_key = heroku_service.get_config("APP_KEY")
    if current_key.nil? || current_key.empty?
      output, status = context.capture2e("php", "artisan", "key:generate", "--show")

      abort_message = context.message("core.app.deploy.heroku.php.error.generate_app_key")
      context.abort(abort_message) unless status.success?

      heroku_service.set_config("APP_KEY", output.strip) if status.success?
    end

    heroku_service.add_buildpacks(["heroku/php", "heroku/nodejs"])
  end

  CLI::UI::Frame.open(
    context.message("core.app.deploy.heroku.deploying"),
    success_text: context.message("core.app.deploy.heroku.deployed")
  ) do
    heroku_service.deploy(branch_to_deploy)
  end

  heroku_command = heroku_service.heroku_command
  context.puts(context.message("core.app.deploy.heroku.php.post_deploy", app_url, heroku_command))
end