Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add locking feature to prevent stale code #25

Merged
merged 5 commits into from
Dec 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to lock to the specific version in the README (just requires work to keep updated when otherwise this project doesn't necessarily need to change).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the concern is that if we make changes to ember-cli-rails-addon it shouldn't break existing installations of ember-cli-rails (the gem).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addon is not paired with a specific version of ember-cli-rails, so I am not sure how you could break things. Either way is totally good with me, was just an observation.

```

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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to check for EmberCLI version. It must be greater than 1.3.0 in order to have the preBuild hook that the addon needs.

ember-cli/ember-cli#2411

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unsure that you should care too much about this here. You can make the addon check the required version var version = this.project.emberCLIVersion(); and it can be kept with a nice message. Then here (in ember-cli-rails) you just don't have to care about it.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These heredocs are great!

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