This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 202
/
rails_service.rb
215 lines (181 loc) · 8.4 KB
/
rails_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
require "semantic/semantic"
module ShopifyCLI
module Services
module App
module Create
class RailsService < BaseService
USER_AGENT_CODE = <<~USERAGENT
module ShopifyAPI
class Base < ActiveResource::Base
self.headers['User-Agent'] << " | ShopifyApp/\#{ShopifyApp::VERSION} | Shopify CLI"
end
end
USERAGENT
DEFAULT_RAILS_FLAGS = %w(--skip-spring)
attr_reader :name, :organization_id, :store_domain, :type, :db, :rails_opts, :context
def initialize(name:, organization_id:, store_domain:, type:, db:, rails_opts:, context:)
@name = name
@organization_id = organization_id
@store_domain = store_domain
@type = type
@db = db
@rails_opts = rails_opts
@context = context
super()
end
def call
form_options = {
name: name,
organization_id: organization_id,
shop_domain: store_domain,
type: type,
}
form_options[:db] = db unless db.nil?
form_options[:rails_opts] = rails_opts unless rails_opts.nil?
form = form_data(form_options)
raise ShopifyCLI::AbortSilent if form.nil?
ruby_version = Rails::Ruby.version(context)
context.abort(context.message("core.app.create.rails.error.invalid_ruby_version")) unless
ruby_version.satisfies?("~>2.5") || ruby_version.satisfies?("~>3.0.0")
check_node
check_yarn
build(form.name, form.db)
set_custom_ua
ShopifyCLI::Project.write(
context,
project_type: "rails",
organization_id: form.organization_id,
)
api_client = if ShopifyCLI::Environment.acceptance_test?
{
"apiKey" => "public_api_key",
"apiSecretKeys" => [
{
"secret" => "api_secret_key",
},
],
}
else
ShopifyCLI::Tasks::CreateApiClient.call(
context,
org_id: form.organization_id,
title: form.name,
type: form.type,
)
end
ShopifyCLI::Resources::EnvFile.new(
api_key: api_client["apiKey"],
secret: api_client["apiSecretKeys"].first["secret"],
shop: form.shop_domain,
scopes: "write_products,write_customers,write_draft_orders",
).write(context)
partners_url = ShopifyCLI::PartnersAPI.partners_url_for(form.organization_id, api_client["id"])
context.puts(context.message("apps.create.info.created", form.name, partners_url))
context.puts(context.message("apps.create.info.serve", form.name, ShopifyCLI::TOOL_NAME))
unless ShopifyCLI::Shopifolk.acting_as_shopify_organization?
context.puts(context.message("apps.create.info.install", partners_url, form.name))
end
end
private
def form_data(form_options)
if ShopifyCLI::Environment.acceptance_test?
Struct.new(:name, :organization_id, :type, :shop_domain, :db, keyword_init: true).new(
name: form_options[:name],
organization_id: "123",
shop_domain: "test.shopify.io",
type: "public",
db: form_options[:db]
)
else
Rails::Forms::Create.ask(context, [], form_options)
end
end
def check_node
cmd_path = context.which("node")
if cmd_path.nil?
context.abort(context.message("core.app.create.rails.error.node_required")) unless context.windows?
context.puts("{{x}} {{red:" + context.message("core.app.create.rails.error.node_required") + "}}")
context.puts(context.message("core.app.create.rails.info.open_new_shell", "node"))
raise ShopifyCLI::AbortSilent
end
version, stat = context.capture2e("node", "-v")
unless stat.success?
context.abort(context.message("core.app.create.rails.error.node_version_failure")) unless context.windows?
# execution stops above if not Windows
context.puts("{{x}} {{red:" + context.message("core.app.create.rails.error.node_version_failure") + "}}")
context.puts(context.message("core.app.create.rails.info.open_new_shell", "node"))
raise ShopifyCLI::AbortSilent
end
context.done(context.message("core.app.create.rails.node_version", version))
end
def check_yarn
cmd_path = context.which("yarn")
if cmd_path.nil?
context.abort(context.message("core.app.create.rails.error.yarn_required")) unless context.windows?
context.puts("{{x}} {{red:" + context.message("core.app.create.rails.error.yarn_required") + "}}")
context.puts(context.message("core.app.create.rails.info.open_new_shell", "yarn"))
raise ShopifyCLI::AbortSilent
end
version, stat = context.capture2e("yarn", "-v")
unless stat.success?
context.abort(context.message("core.app.create.rails.error.yarn_version_failure")) unless context.windows?
context.puts("{{x}} {{red:" + context.message("core.app.create.rails.error.yarn_version_failure") + "}}")
context.puts(context.message("core.app.create.rails.info.open_new_shell", "yarn"))
raise ShopifyCLI::AbortSilent
end
context.done(context.message("core.app.create.rails.yarn_version", version))
end
def build(name, db)
context.abort(context.message("core.app.create.rails.error.install_failure",
"rails")) unless install_gem("rails",
"<6.1")
context.abort(context.message("core.app.create.rails.error.install_failure", "bundler ~>2.0")) unless
install_gem("bundler", "~>2.0")
full_path = File.join(context.root, name)
context.abort(context.message("core.app.create.rails.error.dir_exists", name)) if Dir.exist?(full_path)
CLI::UI::Frame.open(context.message("core.app.create.rails.generating_app", name)) do
new_command = %w(rails new)
new_command << name
new_command += DEFAULT_RAILS_FLAGS
new_command << "--database=#{db}"
new_command += rails_opts.split unless rails_opts.nil?
syscall(new_command)
end
context.root = full_path
File.open(File.join(context.root, ".gitignore"), "a") { |f| f.write(".env") }
context.puts(context.message("core.app.create.rails.adding_shopify_gem"))
File.open(File.join(context.root, "Gemfile"), "a") do |f|
f.puts "\ngem 'shopify_app', '>=17.0.3'"
end
CLI::UI::Frame.open(context.message("core.app.create.rails.running_bundle_install")) do
syscall(%w(bundle install))
end
CLI::UI::Frame.open(context.message("core.app.create.rails.running_generator")) do
syscall(%w(rails generate shopify_app --new-shopify-cli-app))
end
CLI::UI::Frame.open(context.message("core.app.create.rails.running_migrations")) do
syscall(%w(rails db:create))
syscall(%w(rails db:migrate RAILS_ENV=development))
end
unless File.exist?(File.join(context.root, "config/webpacker.yml"))
CLI::UI::Frame.open(context.message("core.app.create.rails.running_webpacker_install")) do
syscall(%w(rails webpacker:install))
end
end
end
def set_custom_ua
ua_path = File.join("config", "initializers", "user_agent.rb")
context.write(ua_path, USER_AGENT_CODE)
end
def syscall(args)
args[0] = Rails::Gem.binary_path_for(context, args[0])
context.system(*args, chdir: context.root)
end
def install_gem(name, version = nil)
Rails::Gem.install(context, name, version)
end
end
end
end
end
end