ember-cli-rails 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ module EmberCli
2
+ class EmberController < ::ApplicationController
3
+ def index
4
+ @app = params[:ember_app]
5
+
6
+ render layout: false
7
+ end
8
+ end
9
+ end
@@ -2,24 +2,28 @@ require "ember-cli/capture"
2
2
 
3
3
  module EmberRailsHelper
4
4
  def include_ember_index_html(name, &block)
5
+ warn <<-MSG.strip_heredoc
6
+ The `include_ember_index_html` helper has been deprecated.
7
+
8
+ Rename all invocations to `render_ember_app`
9
+ MSG
10
+
11
+ render_ember_app(name, &block)
12
+ end
13
+
14
+ def render_ember_app(name, &block)
5
15
  markup_capturer = EmberCli::Capture.new(sprockets: self, &block)
6
16
 
7
17
  head, body = markup_capturer.capture
8
18
 
9
- html = EmberCli[name].index_html(
10
- sprockets: self,
11
- head: head,
12
- body: body,
13
- )
14
-
15
- render inline: html
19
+ render inline: EmberCli[name].sprockets.index_html(head: head, body: body)
16
20
  end
17
21
 
18
22
  def include_ember_script_tags(name, **options)
19
- javascript_include_tag(*EmberCli[name].exposed_js_assets, options)
23
+ javascript_include_tag(*EmberCli[name].sprockets.assets, options)
20
24
  end
21
25
 
22
26
  def include_ember_stylesheet_tags(name, **options)
23
- stylesheet_link_tag(*EmberCli[name].exposed_css_assets, options)
27
+ stylesheet_link_tag(*EmberCli[name].sprockets.assets, options)
24
28
  end
25
29
  end
@@ -0,0 +1,5 @@
1
+ <%= render_ember_app @app do |head| %>
2
+ <% head.append do %>
3
+ <%= csrf_meta_tags %>
4
+ <% end %>
5
+ <% end %>
@@ -1,4 +1,5 @@
1
1
  require "ember-cli/engine" if defined?(Rails)
2
+ require "ember-cli/errors"
2
3
 
3
4
  module EmberCli
4
5
  extend self
@@ -6,9 +7,7 @@ module EmberCli
6
7
  autoload :App, "ember-cli/app"
7
8
  autoload :Configuration, "ember-cli/configuration"
8
9
  autoload :Helpers, "ember-cli/helpers"
9
- autoload :Middleware, "ember-cli/middleware"
10
10
  autoload :PathSet, "ember-cli/path_set"
11
- autoload :Runner, "ember-cli/runner"
12
11
 
13
12
  def configure
14
13
  yield configuration
@@ -30,36 +29,27 @@ module EmberCli
30
29
  ENV["SKIP_EMBER"].present?
31
30
  end
32
31
 
33
- def prepare!
34
- @prepared ||= begin
32
+ def enable!
33
+ @enabled ||= begin
35
34
  Rails.configuration.assets.paths << root.join("assets").to_s
36
35
  at_exit{ cleanup }
37
36
  true
38
37
  end
39
38
  end
40
39
 
41
- def enable!
42
- prepare!
43
- append_middleware unless env.production?
44
- end
45
-
46
40
  def install_dependencies!
47
- prepare!
48
- each_app &:install_dependencies
41
+ enable!
42
+ each_app(&:install_dependencies)
49
43
  end
50
44
 
51
- def run_tests!
52
- prepare!
53
- each_app &:run_tests
45
+ def test!
46
+ enable!
47
+ each_app(&:test)
54
48
  end
55
49
 
56
50
  def compile!
57
- prepare!
58
- each_app &:compile
59
- end
60
-
61
- def process_path(path)
62
- each_app{ |app| Runner.new(app, path).process }
51
+ enable!
52
+ each_app(&:compile)
63
53
  end
64
54
 
65
55
  def root
@@ -83,11 +73,7 @@ module EmberCli
83
73
  end
84
74
 
85
75
  def each_app
86
- apps.each{ |name, app| yield app }
87
- end
88
-
89
- def append_middleware
90
- Rails.configuration.middleware.use Middleware
76
+ apps.each { |_, app| yield app }
91
77
  end
92
78
  end
93
79
 
data/lib/ember-cli/app.rb CHANGED
@@ -1,334 +1,101 @@
1
- require "timeout"
2
- require "ember-cli/html_page"
3
- require "ember-cli/asset_resolver"
1
+ require "ember-cli/shell"
2
+ require "ember-cli/sprockets"
3
+ require "ember-cli/build_monitor"
4
4
 
5
5
  module EmberCli
6
6
  class App
7
- ADDON_VERSION = "0.0.13"
8
- EMBER_CLI_VERSIONS = [ "~> 0.1.5", "~> 0.2.0", "~> 1.13" ]
9
-
10
- class BuildError < StandardError; end
11
-
12
- attr_reader :name, :options, :paths, :pid
13
-
14
- delegate :root, to: :paths
7
+ attr_reader :name, :options, :paths
15
8
 
16
9
  def initialize(name, **options)
17
- @name, @options = name.to_s, options
18
- @paths = PathSet.new(self)
10
+ @name = name.to_s
11
+ @options = options
12
+ @paths = PathSet.new(
13
+ app: self,
14
+ configuration: EmberCli.configuration,
15
+ environment: Rails.env,
16
+ rails_root: Rails.root,
17
+ ember_cli_root: EmberCli.root,
18
+ )
19
+ @shell = Shell.new(
20
+ paths: @paths,
21
+ env: env_hash,
22
+ options: options,
23
+ )
24
+ @build = BuildMonitor.new(name, @paths)
19
25
  end
20
26
 
21
27
  def compile
22
28
  @compiled ||= begin
23
29
  prepare
24
- silence_build{ exec command }
25
- check_for_build_error!
30
+ @shell.compile
31
+ @build.check!
26
32
  copy_index_html_file
27
33
  true
28
34
  end
29
35
  end
30
36
 
31
- def install_dependencies
32
- if gemfile_path.exist?
33
- exec "#{bundler_path} install"
34
- end
35
-
36
- exec "#{npm_path} prune && #{npm_path} install"
37
-
38
- if bower_path.nil?
39
- fail <<-FAIL
40
- Bower is required by EmberCLI
41
-
42
- Install it with:
43
-
44
- $ npm install -g bower
45
- FAIL
37
+ def build
38
+ if EmberCli.env.development?
39
+ build_and_watch
46
40
  else
47
- exec "#{bower_path} prune && #{bower_path} install"
41
+ compile
48
42
  end
49
- end
50
-
51
- def run
52
- prepare
53
- FileUtils.touch lockfile_path
54
- cmd = command(watch: true)
55
- @pid = exec(cmd, method: :spawn)
56
- Process.detach pid
57
- copy_index_html_file
58
- set_on_exit_callback
59
- end
60
-
61
- def run_tests
62
- prepare
63
-
64
- exec("#{ember_path} test")
65
- end
66
-
67
- def stop
68
- Process.kill :INT, pid if pid
69
- @pid = nil
70
- end
71
-
72
- def index_html(sprockets:, head:, body:)
73
- asset_resolver = AssetResolver.new(
74
- app: self,
75
- sprockets: sprockets,
76
- )
77
- html_page = HtmlPage.new(
78
- asset_resolver: asset_resolver,
79
- content: index_file.read,
80
- head: head,
81
- body: body,
82
- )
83
-
84
- html_page.render
85
- end
86
-
87
- def exposed_js_assets
88
- [vendor_assets, application_assets]
89
- end
90
- alias exposed_css_assets exposed_js_assets
91
43
 
92
- def vendor_assets
93
- "#{name}/vendor"
44
+ @build.wait!
94
45
  end
95
46
 
96
- def application_assets
97
- "#{name}/#{ember_app_name}"
47
+ def install_dependencies
48
+ @shell.install
98
49
  end
99
50
 
100
- def wait
101
- Timeout.timeout(build_timeout) do
102
- wait_for_build_complete_or_error
103
- end
104
- rescue Timeout::Error
105
- suggested_timeout = build_timeout + 5
106
-
107
- warn <<-MSG.strip_heredoc
108
- ============================= WARNING! =============================
109
-
110
- Seems like Ember #{name} application takes more than #{build_timeout}
111
- seconds to compile.
112
-
113
- To prevent race conditions consider adjusting build timeout
114
- configuration in your ember initializer:
115
-
116
- EmberCLI.configure do |config|
117
- config.build_timeout = #{suggested_timeout} # in seconds
118
- end
119
-
120
- Alternatively, you can set build timeout per application like this:
121
-
122
- EmberCLI.configure do |config|
123
- config.app :#{name}, build_timeout: #{suggested_timeout}
124
- end
51
+ def test
52
+ prepare
125
53
 
126
- ============================= WARNING! =============================
127
- MSG
54
+ @shell.test
128
55
  end
129
56
 
130
- def method_missing(method_name, *)
131
- if path_method = supported_path_method(method_name)
132
- paths.public_send(path_method)
133
- else
134
- super
135
- end
57
+ def sprockets
58
+ EmberCli::Sprockets.new(self)
136
59
  end
137
60
 
138
- def respond_to_missing?(method_name, *)
139
- if supported_path_method(method_name)
140
- true
61
+ def index_file
62
+ if EmberCli.env.production?
63
+ paths.applications.join("#{name}.html")
141
64
  else
142
- super
65
+ paths.dist.join("index.html")
143
66
  end
144
67
  end
145
68
 
146
69
  private
147
70
 
148
- def set_on_exit_callback
149
- @on_exit_callback ||= at_exit{ stop }
150
- end
151
-
152
- def supported_path_method(original)
153
- path_method = original.to_s[/\A(.+)_path\z/, 1]
154
- path_method if path_method && paths.respond_to?(path_method)
155
- end
156
-
157
- def silence_build(&block)
158
- if ENV.fetch("EMBER_CLI_RAILS_VERBOSE") { EmberCli.env.production? }
159
- yield
160
- else
161
- silence_stream STDOUT, &block
162
- end
163
- end
164
-
165
- def build_timeout
166
- options.fetch(:build_timeout) { EmberCli.configuration.build_timeout }
167
- end
168
-
169
- def watcher
170
- options.fetch(:watcher) { EmberCli.configuration.watcher }
171
- end
172
-
173
- def check_for_build_error!
174
- raise_build_error! if build_error?
175
- end
176
-
177
- def reset_build_error!
178
- build_error_file_path.delete if build_error?
179
- end
180
-
181
- def build_error?
182
- build_error_file_path.exist? && build_error_file_path.size?
183
- end
184
-
185
- def raise_build_error!
186
- error = BuildError.new("EmberCLI app #{name.inspect} has failed to build")
187
- error.set_backtrace build_error_file_path.readlines
188
- fail error
71
+ def build_and_watch
72
+ prepare
73
+ @shell.build_and_watch
74
+ copy_index_html_file
189
75
  end
190
76
 
191
77
  def prepare
192
78
  @prepared ||= begin
193
- check_dependencies!
194
- check_addon!
195
- check_ember_cli_version!
196
- reset_build_error!
79
+ @build.reset
197
80
  symlink_to_assets_root
198
- add_assets_to_precompile_list
81
+ sprockets.register!
199
82
  true
200
83
  end
201
84
  end
202
85
 
203
- def check_ember_cli_version!
204
- version = dev_dependencies.fetch("ember-cli").split(?-).first
205
-
206
- unless Helpers.match_version?(version, EMBER_CLI_VERSIONS)
207
- fail <<-MSG.strip_heredoc
208
- EmberCLI Rails require ember-cli NPM package version to be
209
- #{EMBER_CLI_VERSIONS.last} to work properly (you have #{version}).
210
- From within your EmberCLI directory please update your package.json
211
- accordingly and run:
212
-
213
- $ npm install
214
-
215
- MSG
216
- end
217
- end
218
-
219
- def check_addon!
220
- unless addon_present?
221
- fail <<-MSG.strip_heredoc
222
- EmberCLI Rails requires your Ember app to have an addon.
223
-
224
- From within your EmberCLI directory please run:
225
-
226
- $ npm install --save-dev ember-cli-rails-addon@#{ADDON_VERSION}
227
-
228
- in your Ember application root: #{root}
229
- MSG
230
- end
231
- end
232
-
233
- def check_dependencies!
234
- unless node_modules_present?
235
- fail <<-MSG.strip_heredoc
236
- EmberCLI app dependencies are not installed. From your Rails application root please run:
237
-
238
- $ bundle exec rake ember:install
239
-
240
- If you do not require Ember at this URL, you can restrict this check using the `enable`
241
- option in the EmberCLI initializer.
242
- MSG
243
- end
244
- end
245
-
246
- def assets_path
247
- paths.assets.join(name)
248
- end
249
-
250
86
  def copy_index_html_file
251
- if environment == "production"
252
- FileUtils.cp(assets_path.join("index.html"), index_file)
253
- end
254
- end
255
-
256
- def index_file
257
- if environment == "production"
258
- applications_path.join("#{name}.html")
259
- else
260
- dist_path.join("index.html")
87
+ if EmberCli.env.production?
88
+ FileUtils.cp(paths.app_assets.join("index.html"), index_file)
261
89
  end
262
90
  end
263
91
 
264
92
  def symlink_to_assets_root
265
- assets_path.make_symlink dist_path.join("assets")
93
+ paths.app_assets.make_symlink paths.dist
266
94
  rescue Errno::EEXIST
267
95
  # Sometimes happens when starting multiple Unicorn workers.
268
96
  # Ignoring...
269
97
  end
270
98
 
271
- def add_assets_to_precompile_list
272
- Rails.configuration.assets.precompile << /\A#{name}\//
273
- end
274
-
275
- def command(watch: false)
276
- watch_flag = ""
277
-
278
- if watch
279
- watch_flag = "--watch"
280
-
281
- if watcher
282
- watch_flag += " --watcher #{watcher}"
283
- end
284
- end
285
-
286
- "#{ember_path} build #{watch_flag} --environment #{environment} --output-path #{dist_path} #{redirect_errors} #{log_pipe}"
287
- end
288
-
289
- def redirect_errors
290
- "2> #{build_error_file_path}"
291
- end
292
-
293
- def log_pipe
294
- "| #{tee_path} -a #{log_path}" if tee_path
295
- end
296
-
297
- def ember_app_name
298
- @ember_app_name ||= options.fetch(:name){ package_json.fetch(:name) }
299
- end
300
-
301
- def environment
302
- EmberCli.env.production? ? "production" : "development"
303
- end
304
-
305
- def package_json
306
- @package_json ||=
307
- JSON.parse(package_json_file_path.read).with_indifferent_access
308
- end
309
-
310
- def addon_package_json
311
- @addon_package_json ||=
312
- JSON.parse(addon_package_json_file_path.read).with_indifferent_access
313
- end
314
-
315
- def addon_version
316
- addon_package_json.fetch("version")
317
- end
318
-
319
- def dev_dependencies
320
- package_json.fetch("devDependencies", {})
321
- end
322
-
323
- def addon_present?
324
- addon_package_json_file_path.exist? &&
325
- addon_version == ADDON_VERSION
326
- end
327
-
328
- def node_modules_present?
329
- node_modules_path.exist?
330
- end
331
-
332
99
  def excluded_ember_deps
333
100
  Array.wrap(options[:exclude_ember_deps]).join(?,)
334
101
  end
@@ -336,23 +103,8 @@ module EmberCli
336
103
  def env_hash
337
104
  ENV.to_h.tap do |vars|
338
105
  vars["RAILS_ENV"] = Rails.env
339
- vars["DISABLE_FINGERPRINTING"] = "true"
340
106
  vars["EXCLUDE_EMBER_ASSETS"] = excluded_ember_deps
341
- vars["BUNDLE_GEMFILE"] = gemfile_path.to_s if gemfile_path.exist?
342
- end
343
- end
344
-
345
- def exec(cmd, method: :system)
346
- Dir.chdir root do
347
- Kernel.public_send(method, env_hash, cmd, err: :out) || exit(1)
348
- end
349
- end
350
-
351
- def wait_for_build_complete_or_error
352
- loop do
353
- check_for_build_error!
354
- break unless lockfile_path.exist?
355
- sleep 0.1
107
+ vars["BUNDLE_GEMFILE"] = paths.gemfile.to_s if paths.gemfile.exist?
356
108
  end
357
109
  end
358
110
  end