Skip to content

Commit

Permalink
Merge pull request #145 from seanpdoyle/runner
Browse files Browse the repository at this point in the history
Add Runner class to monitor and activate ember apps
  • Loading branch information
rwz committed Apr 6, 2015
2 parents 4513693 + 7e1d7e7 commit 159e1b6
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 41 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ end
- path - the path, where your EmberCLI applications is located. The default
value is the name of your app in the Rails root.

- enable - a lambda that accepts each requests' path. The default value is a
lambda that returns `true`.

```ruby
EmberCLI.configure do |c|
c.app :adminpanel # path is "<your-rails-root>/adminpanel"
c.app :frontend, path: "/path/to/your/ember-cli-app/on/disk"
c.app :frontend,
path: "/path/to/your/ember-cli-app/on/disk",
enable: -> path { path.starts_with?("/app/") }
end
```

Expand Down Expand Up @@ -240,7 +245,7 @@ found in both `node_modules` and `bower_components`.

ember-cli-rails adds your ember apps' build process to the rails asset compilation process.

Now you should be ready to deploy.
Now you should be ready to deploy.

## Additional Information

Expand Down
19 changes: 3 additions & 16 deletions lib/ember-cli-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module EmberCLI
autoload :Helpers, "ember-cli/helpers"
autoload :Middleware, "ember-cli/middleware"
autoload :PathSet, "ember-cli/path_set"
autoload :Runner, "ember-cli/runner"

def configure
yield configuration
Expand Down Expand Up @@ -47,27 +48,13 @@ def install_dependencies!
each_app &:install_dependencies
end

def run!
prepare!
each_app &:run
end

def run_tests!
prepare!
each_app &:run_tests
end

def compile!
prepare!
each_app &:compile
end

def stop!
each_app &:stop
end

def wait!
each_app &:wait
def process_path(path)
each_app{ |app| Runner.new(app, path).process }
end

def root
Expand Down
20 changes: 14 additions & 6 deletions lib/ember-cli/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ def initialize(name, options={})
end

def compile
prepare
silence_build { exec command }
check_for_build_error!
@compiled ||= begin
prepare
silence_build{ exec command }
check_for_build_error!
true
end
end

def install_dependencies
Expand All @@ -29,9 +32,11 @@ def install_dependencies

def run
prepare
FileUtils.touch lockfile_path
cmd = command(watch: true)
@pid = exec(cmd, method: :spawn)
at_exit{ stop }
Process.detach pid
set_on_exit_callback
end

def run_tests
Expand All @@ -40,7 +45,7 @@ def run_tests
end

def stop
Process.kill "INT", pid if pid
Process.kill :INT, pid if pid
@pid = nil
end

Expand Down Expand Up @@ -100,6 +105,10 @@ def respond_to_missing?(method_name, *)

private

def set_on_exit_callback
@on_exit_callback ||= at_exit{ stop }
end

def supported_path_method(original)
path_method = original.to_s[/\A(.+)_path\z/, 1]
path_method if path_method && paths.respond_to?(path_method)
Expand Down Expand Up @@ -140,7 +149,6 @@ def prepare
check_addon!
check_ember_cli_version!
reset_build_error!
FileUtils.touch lockfile_path
symlink_to_assets_root
add_assets_to_precompile_list
true
Expand Down
20 changes: 3 additions & 17 deletions lib/ember-cli/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,14 @@ def initialize(app)
end

def call(env)
enable_ember_cli
EmberCLI.wait!
path = env["PATH_INFO"].to_s

if env["PATH_INFO"] == "/testem.js"
if path == "/testem.js"
[ 200, { "Content-Type" => "text/javascript" }, [""] ]
else
EmberCLI.process_path path
@app.call(env)
end
end

private

def enable_ember_cli
@enabled ||= begin
if EmberCLI.env.development?
EmberCLI.run!
else
EmberCLI.compile!
end

true
end
end
end
end
53 changes: 53 additions & 0 deletions lib/ember-cli/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module EmberCLI
class Runner
TRUE_PROC = ->(*){ true }

attr_reader :app, :path

def initialize(app, path)
@app, @path = app, path
end

def process
return if skip?

if EmberCLI.env.development?
start_or_restart!
else
compile!
end

wait!
end

private

def skip?
invoker = app.options.fetch(:enable, TRUE_PROC)
!invoker.call(path)
end

def start_or_restart!
run! unless app.pid && still_running?
end

def still_running?
Process.getpgid app.pid
true
rescue Errno::ESRCH # no such process
false
end

def wait!
app.wait
end

def compile!
app.compile
end

def run!
app.run
end
end
end

0 comments on commit 159e1b6

Please sign in to comment.