diff --git a/README.md b/README.md index 065c1864..35f5fc3e 100644 --- a/README.md +++ b/README.md @@ -58,18 +58,13 @@ EmberCLI.configure do |c| end ``` -Once you've updated your initializer to taste, you need to tell EmberCLI that -you want the meta tag to be served in your javascript. Open up your -`Brocfile.js` inside your EmberCLI app and add `{storeConfigInMeta: false}`. +Once you've updated your initializer to taste, you need to install the +[ember-cli-rails-addon](https://github.com/rondale-sc/ember-cli-rails-addon). -It should look something like this (if you've left it unchanged): +For each of your EmberCLI applications install the addon with: -```javascript -var EmberApp = require('ember-cli/lib/broccoli/ember-app'); - -var app = new EmberApp({storeConfigInMeta: false}); - -// etc... +```sh +npm install --save-dev ember-cli-rails-addon@0.0.3 ``` And that's it! diff --git a/lib/ember-cli-rails.rb b/lib/ember-cli-rails.rb index b19080b5..a5fcc748 100644 --- a/lib/ember-cli-rails.rb +++ b/lib/ember-cli-rails.rb @@ -7,6 +7,7 @@ module EmberCLI autoload :Configuration, "ember-cli/configuration" autoload :ViewHelpers, "ember-cli/view_helpers" autoload :Helpers, "ember-cli/helpers" + autoload :Middleware, "ember-cli/middleware" def configure yield configuration @@ -26,20 +27,7 @@ def prepare! def enable! prepare! - - Rails.application.singleton_class.class_eval do - alias_method :call_without_ember_cli, :call - - def call(env) - @_ember_cli_enabled ||= begin - EmberCLI.compile! - EmberCLI.run! if Rails.env.development? - true - end - - call_without_ember_cli(env) - end - end + Rails.configuration.middleware.use Middleware end def run! @@ -56,6 +44,10 @@ def stop! each_app &:stop end + def wait! + each_app &:wait + end + def root @root ||= Rails.root.join("tmp", "ember-cli-#{uid}") end diff --git a/lib/ember-cli/app.rb b/lib/ember-cli/app.rb index 2556ca9c..850eb2bf 100644 --- a/lib/ember-cli/app.rb +++ b/lib/ember-cli/app.rb @@ -1,5 +1,9 @@ +require "timeout" + module EmberCLI class App + ADDON_VERSION = "0.0.3" + attr_reader :name, :options, :pid def initialize(name, options={}) @@ -32,20 +36,76 @@ def exposed_css_assets %W[#{name}/vendor #{name}/#{ember_app_name}] end + def wait + Timeout.timeout(build_timeout) do + sleep 0.1 while lockfile.exist? + 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 + private delegate :ember_path, to: :configuration delegate :tee_path, to: :configuration delegate :configuration, to: :EmberCLI + def build_timeout + options.fetch(:build_timeout){ configuration.build_timeout } + end + + def lockfile + app_path.join("tmp", "build.lock") + end + def prepare @prepared ||= begin + check_addon! + FileUtils.touch lockfile symlink_to_assets_root add_assets_to_precompile_list true end end + def check_addon! + dependencies = package_json.fetch("devDependencies", {}) + + unless dependencies["ember-cli-rails-addon"] == ADDON_VERSION + fail <<-MSG.strip_heredoc + EmberCLI Rails requires your Ember app to have an addon. + + 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 symlink_path = dist_path.join("assets") assets_path.join(name).make_symlink symlink_path unless symlink_path.exist? @@ -65,9 +125,7 @@ def log_pipe end def ember_app_name - @ember_app_name ||= options.fetch(:name) do - JSON.parse(app_path.join("package.json").read).fetch("name") - end + @ember_app_name ||= options.fetch(:name){ package_json.fetch(:name) } end def app_path @@ -92,5 +150,9 @@ def assets_path def environment Helpers.non_production?? "development" : "production" end + + def package_json + @package_json ||= JSON.parse(app_path.join("package.json").read).with_indifferent_access + end end end diff --git a/lib/ember-cli/configuration.rb b/lib/ember-cli/configuration.rb index 21302a02..bf79ea11 100644 --- a/lib/ember-cli/configuration.rb +++ b/lib/ember-cli/configuration.rb @@ -23,6 +23,10 @@ def ember_path end end - attr_writer :ember_path + def build_timeout + @build_timeout ||= 5 + end + + attr_writer :ember_path, :build_timeout end end diff --git a/lib/ember-cli/middleware.rb b/lib/ember-cli/middleware.rb new file mode 100644 index 00000000..c7483da6 --- /dev/null +++ b/lib/ember-cli/middleware.rb @@ -0,0 +1,28 @@ +module EmberCLI + class Middleware + def initialize(app) + @app = app + end + + def call(env) + enable_ember_cli + EmberCLI.wait! + + @app.call(env) + end + + private + + def enable_ember_cli + @enabled ||= begin + if Rails.env.development? + EmberCLI.run! + else + EmberCLI.compile! + end + + true + end + end + end +end