/
app.rb
269 lines (211 loc) · 6.58 KB
/
app.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
require "timeout"
module EmberCLI
class App
ADDON_VERSION = "0.0.11"
EMBER_CLI_VERSION = "~> 0.1.5"
class BuildError < StandardError; end
attr_reader :name, :options, :pid
def initialize(name, options={})
@name, @options = name.to_s, options
end
def compile
prepare
silence_stream(STDOUT){ exec command }
check_for_build_error!
end
def install_dependencies
exec "bundle install" if gemfile_path.exist?
exec "#{npm_path} install"
end
def run
prepare
cmd = command(watch: true)
@pid = exec(cmd, method: :spawn)
at_exit{ stop }
end
def run_tests
prepare
tests_pass = exec("#{ember_path} test")
exit 1 unless tests_pass
end
def stop
Process.kill "INT", pid if pid
@pid = nil
end
def exposed_js_assets
%W[#{name}/vendor #{name}/#{ember_app_name}]
end
def exposed_css_assets
%W[#{name}/vendor #{name}/#{ember_app_name}]
end
def wait
Timeout.timeout(build_timeout) do
wait_for_build_complete_or_error
end
rescue Timeout::Error
suggested_timeout = build_timeout + 5
warn <<-MSG.strip_heredoc
============================= WARNING! =============================
Seems like Ember #{name} application takes more than #{build_timeout}
seconds to compile.
To prevent race conditions consider adjusting build timeout
configuration in your ember initializer:
EmberCLI.configure do |config|
config.build_timeout = #{suggested_timeout} # in seconds
end
Alternatively, you can set build timeout per application like this:
EmberCLI.configure do |config|
config.app :#{name}, build_timeout: #{suggested_timeout}
end
============================= WARNING! =============================
MSG
end
def ember_path
@ember_path ||= app_path.join("node_modules", ".bin", "ember").tap do |path|
fail <<-MSG.strip_heredoc unless path.executable?
No local ember executable found. You should run `npm install`
inside the #{name} app located at #{app_path}
MSG
end
end
private
delegate :match_version?, :non_production?, to: Helpers
delegate :configuration, to: EmberCLI
delegate :tee_path, :npm_path, to: :configuration
def build_timeout
options.fetch(:build_timeout){ configuration.build_timeout }
end
def lockfile
@lockfile ||= tmp_path.join("build.lock")
end
def check_for_build_error!
raise_build_error! if build_error?
end
def build_error_file
@build_error_file ||= tmp_path.join("error.txt")
end
def reset_build_error!
build_error_file.delete if build_error?
end
def build_error?
build_error_file.exist?
end
def raise_build_error!
error = BuildError.new("EmberCLI app #{name.inspect} has failed to build")
error.set_backtrace build_error_file.read.split(?\n)
fail error
end
def prepare
@prepared ||= begin
check_addon!
check_ember_cli_version!
reset_build_error!
FileUtils.touch lockfile
symlink_to_assets_root
add_assets_to_precompile_list
true
end
end
def check_ember_cli_version!
version = dev_dependencies.fetch("ember-cli").split("-").first
unless match_version?(version, EMBER_CLI_VERSION)
fail <<-MSG.strip_heredoc
EmberCLI Rails require ember-cli NPM package version to be
#{EMBER_CLI_VERSION} to work properly. From within your EmberCLI directory
please update your package.json accordingly and run:
$ npm install
MSG
end
end
def check_addon!
unless addon_present?
fail <<-MSG.strip_heredoc
EmberCLI Rails requires your Ember app to have an addon.
From within your EmberCLI directory please run:
$ npm install --save-dev ember-cli-rails-addon@#{ADDON_VERSION}
in you Ember application root: #{app_path}
MSG
end
end
def symlink_to_assets_root
assets_path.join(name).make_symlink dist_path.join("assets")
rescue Errno::EEXIST
# Sometimes happens when starting multiple Unicorn workers.
# Ignoring...
end
def add_assets_to_precompile_list
Rails.configuration.assets.precompile << /\A#{name}\//
end
def command(options={})
watch = options[:watch] ? "--watch" : ""
"#{ember_path} build #{watch} --environment #{environment} --output-path #{dist_path} #{log_pipe}"
end
def log_pipe
"| #{tee_path} -a #{log_path}" if tee_path
end
def ember_app_name
@ember_app_name ||= options.fetch(:name){ package_json.fetch(:name) }
end
def app_path
@app_path ||= begin
path = options.fetch(:path){ Rails.root.join("app", name) }
Pathname.new(path)
end
end
def tmp_path
@tmp_path ||= begin
path = app_path.join("tmp")
path.mkdir unless path.exist?
path
end
end
def log_path
Rails.root.join("log", "ember-#{name}.#{Rails.env}.log")
end
def dist_path
@dist_path ||= EmberCLI.root.join("apps", name).tap(&:mkpath)
end
def assets_path
@assets_path ||= EmberCLI.root.join("assets").tap(&:mkpath)
end
def environment
non_production?? "development" : "production"
end
def package_json
@package_json ||= JSON.parse(app_path.join("package.json").read).with_indifferent_access
end
def dev_dependencies
package_json.fetch("devDependencies", {})
end
def addon_present?
dev_dependencies["ember-cli-rails-addon"] == ADDON_VERSION &&
app_path.join("node_modules", "ember-cli-rails-addon", "package.json").exist?
end
def excluded_ember_deps
Array.wrap(options[:exclude_ember_deps]).join(",")
end
def env_hash
ENV.clone.tap do |vars|
vars.store "DISABLE_FINGERPRINTING", "true"
vars.store "EXCLUDE_EMBER_ASSETS", excluded_ember_deps
vars.store "BUNDLE_GEMFILE", gemfile_path
end
end
def gemfile_path
app_path.join('Gemfile')
end
def exec(cmd, options={})
method_name = options.fetch(:method, :system)
Dir.chdir app_path do
Kernel.public_send(method_name, env_hash, cmd, err: :out)
end
end
def wait_for_build_complete_or_error
loop do
check_for_build_error!
break unless lockfile.exist?
sleep 0.1
end
end
end
end