Skip to content

Commit d5b8aca

Browse files
committed
Initial Implementation
Extracted from a PR against [ember-cli-rails]. `EmberCLI::Deploy` takes a `namespace` (the name of your app declared in your initializer) and handles all interaction with the Redis instance. This is great for `staging` and `production` deploys, but introduces an extra step in the feedback loop during development. Luckily, `EmberCLI::Deploy` also accepts an `index_html` override, which will replace the call to the Redis instance. This allows integration with the normal `ember-cli-rails` workflow: [ember-cli-rails]: tricknotes/ember-cli-rails#172
1 parent 315b0fe commit d5b8aca

File tree

13 files changed

+322
-64
lines changed

13 files changed

+322
-64
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ language: ruby
22
rvm:
33
- 2.2.2
44
before_install: gem install bundler -v 1.10.5
5+
notifications:
6+
email: false

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
source 'https://rubygems.org'
1+
source "https://rubygems.org"
22

33
# Specify your gem's dependencies in ember-cli-rails-deploy-redis.gemspec
44
gemspec

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# EmberCLI Rails - Deploy Redis
1+
# EmberCLI::Rails - Deploy Redis
22

33
[EmberCLI Rails] is an integration story between (surprise suprise) EmberCLI and
44
Rails 3.1 and up.
@@ -36,36 +36,37 @@ This project attempts to streamline the process of pushing and serving
3636
EmberCLI-built static assets.
3737

3838
To integrate with `ember-cli-deploy`'s ["Lightning Fast Deploys"][lightning]
39-
(using the Redis adapter), instantiate an `EmberCLI::Deploy` in your controller:
39+
(using the Redis adapter), instantiate an `EmberCLI::Deploy::Redis`
40+
in your controller:
4041

4142
```ruby
42-
require "ember-cli/deploy"
43+
require "ember-cli/deploy/redis"
4344

4445
class ApplicationController < ActionController::Base
4546
def index
46-
@deploy = EmberCLI::Deploy.new(namespace: "frontend")
47+
@deploy = EmberCLI::Deploy::Redis.new(namespace: "frontend")
4748

4849
render text: @deploy.html, layout: false
4950
end
5051
end
5152
```
5253

53-
`EmberCLI::Deploy` takes a `namespace` (the name of your app declared in your
54-
initializer) and handles all interaction with the Redis instance.
54+
`EmberCLI::Deploy::Redis` takes a `namespace` (the name of your app declared in
55+
your initializer) and handles all interaction with the Redis instance.
5556

5657
This is great for `staging` and `production` deploys, but introduces an extra
5758
step in the feedback loop during development.
5859

59-
Luckily, `EmberCLI::Deploy` also accepts an `index_html` override, which will
60-
replace the call to the Redis instance. This allows integration with the normal
61-
`ember-cli-rails` workflow:
60+
Luckily, `EmberCLI::Deploy::Redis` also accepts an `index_html` override, which
61+
will replace the call to the Redis instance. This allows integration with the
62+
normal `ember-cli-rails` workflow:
6263

6364
```ruby
64-
require "ember-cli/deploy"
65+
require "ember-cli/deploy/redis"
6566

6667
class ApplicationController < ActionController::Base
6768
def index
68-
@deploy = EmberCLI::Deploy.new(
69+
@deploy = EmberCLI::Deploy::Redis.new(
6970
namespace: "frontend",
7071
index_html: index_html,
7172
)
@@ -97,7 +98,7 @@ require "ember-cli/deploy"
9798

9899
class ApplicationController < ActionController::Base
99100
def index
100-
@deploy = EmberCLI::Deploy.new(
101+
@deploy = EmberCLI::Deploy::Redis.new(
101102
namespace: "frontend",
102103
index_html: index_html,
103104
)
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
# coding: utf-8
2-
lib = File.expand_path('../lib', __FILE__)
2+
lib = File.expand_path("../lib", __FILE__)
33
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4-
require 'ember/cli/rails/deploy/redis/version'
4+
require "ember-cli/deploy/version"
55

66
Gem::Specification.new do |spec|
77
spec.name = "ember-cli-rails-deploy-redis"
8-
spec.version = Ember::Cli::Rails::Deploy::Redis::VERSION
8+
spec.version = EmberCLI::Rails::Deploy::VERSION
99
spec.authors = ["Sean Doyle"]
1010
spec.email = ["sean.p.doyle24@gmail.com"]
1111

12-
spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
13-
spec.description = %q{TODO: Write a longer description or delete this line.}
14-
spec.homepage = "TODO: Put your gem's website or public repo URL here."
12+
spec.summary = %q{Lightning Fast deploys with ember-cli-rails}
13+
spec.description = %q{Lightning Fast deploys with ember-cli-rails}
14+
spec.homepage = "https://github.com/seanpdoyle/ember-cli-rails-deploy-redis"
1515
spec.license = "MIT"
1616

17-
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18-
# delete this section to allow pushing this gem to any host.
19-
if spec.respond_to?(:metadata)
20-
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21-
else
22-
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23-
end
24-
2517
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
2618
spec.bindir = "exe"
2719
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
2820
spec.require_paths = ["lib"]
2921

22+
spec.add_dependency "railties", ">= 3.1", "< 5"
23+
spec.add_dependency "redis", ">= 3.0"
24+
3025
spec.add_development_dependency "bundler", "~> 1.10"
3126
spec.add_development_dependency "rake", "~> 10.0"
3227
spec.add_development_dependency "rspec"
28+
spec.add_development_dependency "climate_control"
29+
spec.add_development_dependency "fakeredis"
30+
spec.add_development_dependency "pry"
3331
end

lib/ember-cli/deploy/page.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module EmberCLI
2+
module Deploy
3+
class Page
4+
def initialize(html:)
5+
@html = html.clone
6+
@body_markup = []
7+
@head_markup = []
8+
end
9+
10+
def build
11+
if has_head?
12+
head_markup.each do |markup|
13+
html.insert(head_position, markup)
14+
end
15+
end
16+
17+
if has_body?
18+
body_markup.each do |markup|
19+
html.insert(body_position, markup)
20+
end
21+
end
22+
23+
html
24+
end
25+
26+
def append_to_body(markup)
27+
body_markup << markup
28+
end
29+
30+
def append_to_head(markup)
31+
head_markup << markup
32+
end
33+
34+
private
35+
36+
attr_reader :body_markup, :head_markup, :html
37+
38+
def has_head?
39+
html.include?("<head")
40+
end
41+
42+
def has_body?
43+
html.include?("<body")
44+
end
45+
46+
def head_position
47+
html.index("</head")
48+
end
49+
50+
def body_position
51+
html.index("</body")
52+
end
53+
end
54+
end
55+
end

lib/ember-cli/deploy/redis.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
require "active_support/core_ext/object/blank"
2+
require "ember-cli/deploy/page"
3+
require "redis"
4+
5+
module EmberCLI
6+
module Deploy
7+
class Redis
8+
def initialize(namespace:, index_html: nil, redis_client: build_client)
9+
@redis_client = redis_client
10+
@namespace = namespace
11+
@index_html = index_html
12+
@body_markup = []
13+
@head_markup = []
14+
end
15+
16+
def append_to_body(markup)
17+
body_markup << markup
18+
end
19+
20+
def append_to_head(markup)
21+
head_markup << markup
22+
end
23+
24+
def html
25+
if index_html.present?
26+
page = Page.new(html: index_html)
27+
28+
body_markup.each do |markup|
29+
page.append_to_body(markup)
30+
end
31+
32+
head_markup.each do |markup|
33+
page.append_to_head(markup)
34+
end
35+
36+
page.build
37+
else
38+
index_html_missing!
39+
end
40+
end
41+
42+
private
43+
44+
attr_reader :body_markup, :head_markup, :namespace, :redis_client
45+
46+
def index_html
47+
@index_html ||= redis_client.get(deploy_key).presence
48+
end
49+
50+
def current_key
51+
"#{namespace}:current"
52+
end
53+
54+
def deploy_key
55+
redis_client.get(current_key).presence || deployment_not_activated!
56+
end
57+
58+
def build_client
59+
::Redis.new(url: ENV.fetch("REDIS_URL"))
60+
end
61+
62+
def index_html_missing!
63+
message = <<-FAIL
64+
HTML for #{deploy_key} is missing.
65+
66+
Did you forget to call `ember deploy`?
67+
FAIL
68+
69+
raise KeyError, message
70+
end
71+
72+
def deployment_not_activated!
73+
message = <<-FAIL
74+
#{current_key} is empty.
75+
76+
Did you forget to call `ember deploy:activate`?
77+
FAIL
78+
79+
raise KeyError, message
80+
end
81+
end
82+
end
83+
end

lib/ember-cli/deploy/version.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module EmberCLI
2+
module Rails
3+
module Deploy
4+
VERSION = "0.0.1"
5+
end
6+
end
7+
end

lib/ember/cli/rails/deploy/redis.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/ember/cli/rails/deploy/redis/version.rb

Lines changed: 0 additions & 11 deletions
This file was deleted.

spec/ember/cli/rails/deploy/redis_spec.rb

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)