Skip to content

Commit

Permalink
Merge pull request #25 from rwz/lock_steady
Browse files Browse the repository at this point in the history
Add locking feature to prevent stale code
  • Loading branch information
rwz committed Dec 19, 2014
2 parents 8ae7c36 + 0ff1dfe commit fe59997
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 28 deletions.
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
20 changes: 6 additions & 14 deletions lib/ember-cli-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!
Expand All @@ -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
Expand Down
68 changes: 65 additions & 3 deletions lib/ember-cli/app.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require "timeout"

module EmberCLI
class App
ADDON_VERSION = "0.0.3"

attr_reader :name, :options, :pid

def initialize(name, options={})
Expand Down Expand Up @@ -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?
Expand All @@ -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
Expand All @@ -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
6 changes: 5 additions & 1 deletion lib/ember-cli/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 28 additions & 0 deletions lib/ember-cli/middleware.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fe59997

Please sign in to comment.