Skip to content

Commit 8c73c48

Browse files
committed
Introduce EmberCli::Assets::Paths
Decorates `EmberCli::App#{root,dist}_path` for asset-specific directories and files.
1 parent 53ad44e commit 8c73c48

File tree

9 files changed

+101
-36
lines changed

9 files changed

+101
-36
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GIT
22
remote: git://github.com/thoughtbot/ember-cli-rails.git
3-
revision: f1de6c17dab6f40cd8b947351328611f7bca63f8
3+
revision: 2adf53b1544a31a8acd347b67d7bae21ebfcf93f
44
branch: remove-assets
55
specs:
66
ember-cli-rails (0.6.0)

gemfiles/3.2.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ gem "rails", :git => "https://github.com/rails/rails.git", :branch => "3-2-stabl
88

99
group :development, :test do
1010
gem "pry"
11+
gem "ember-cli-rails", :github => "thoughtbot/ember-cli-rails", :branch => "remove-assets"
1112
end
1213

1314
group :test do
14-
gem "ember-cli-rails", :github => "thoughtbot/ember-cli-rails", :branch => "1.0.0-beta"
1515
gem "poltergeist", "~> 1.8.0"
1616
gem "rspec-rails"
1717
end

gemfiles/4.1.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ gem "rails", "~> 4.1.1"
77

88
group :development, :test do
99
gem "pry"
10+
gem "ember-cli-rails", :github => "thoughtbot/ember-cli-rails", :branch => "remove-assets"
1011
end
1112

1213
group :test do
13-
gem "ember-cli-rails", :github => "thoughtbot/ember-cli-rails", :branch => "1.0.0-beta"
1414
gem "poltergeist", "~> 1.8.0"
1515
gem "rspec-rails"
1616
end

gemfiles/4.2.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ gem "rails", "~> 4.2.1"
77

88
group :development, :test do
99
gem "pry"
10+
gem "ember-cli-rails", :github => "thoughtbot/ember-cli-rails", :branch => "remove-assets"
1011
end
1112

1213
group :test do
13-
gem "ember-cli-rails", :github => "thoughtbot/ember-cli-rails", :branch => "1.0.0-beta"
1414
gem "poltergeist", "~> 1.8.0"
1515
gem "rspec-rails"
1616
end

lib/ember_cli/assets/asset_map.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
module EmberCli
44
module Assets
55
class AssetMap
6-
def initialize(ember_app_name:, asset_map:)
7-
@ember_app_name = ember_app_name
6+
def initialize(name:, asset_map:)
7+
@name = name
88
@asset_map = asset_map
99
end
1010

@@ -13,7 +13,7 @@ def javascripts
1313

1414
[
1515
asset_matching(/vendor(.*)\.js\z/),
16-
asset_matching(/#{ember_app_name}(.*)\.js\z/),
16+
asset_matching(/#{name}(.*)\.js\z/),
1717
]
1818
end
1919

@@ -22,13 +22,13 @@ def stylesheets
2222

2323
[
2424
asset_matching(/vendor(.*)\.css\z/),
25-
asset_matching(/#{ember_app_name}(.*)\.css\z/),
25+
asset_matching(/#{name}(.*)\.css\z/),
2626
]
2727
end
2828

2929
private
3030

31-
attr_reader :app_name, :ember_app_name, :asset_map
31+
attr_reader :name, :asset_map
3232

3333
def asset_matching(regex)
3434
matching_asset = files.detect { |asset| asset =~ regex }
@@ -59,7 +59,7 @@ def raise_missing_asset(regex)
5959
def assert_asset_map!
6060
if assets.empty?
6161
raise BuildError.new <<-MSG
62-
Missing `#{ember_app_name}/assets/assetMap.json`
62+
Missing `#{name}/assets/assetMap.json`
6363
MSG
6464
end
6565
end

lib/ember_cli/assets/lookup.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require "ember_cli/assets/paths"
12
require "ember_cli/assets/asset_map"
23
require "ember_cli/assets/directory_asset_map"
34

@@ -6,6 +7,7 @@ module Assets
67
class Lookup
78
def initialize(app)
89
@app = app
10+
@paths = Paths.new(app)
911
end
1012

1113
def javascript_assets
@@ -18,36 +20,33 @@ def stylesheet_assets
1820

1921
private
2022

21-
attr_reader :app
23+
attr_reader :app, :paths
2224

2325
def asset_map
2426
AssetMap.new(
25-
ember_app_name: ember_app_name,
27+
name: name_from_package_json,
2628
asset_map: asset_map_hash.to_h,
2729
)
2830
end
2931

3032
def asset_map_file
31-
app.paths.asset_map
33+
paths.asset_map
3234
end
3335

3436
def asset_map_hash
3537
if asset_map_file.present? && asset_map_file.exist?
3638
JSON.parse(asset_map_file.read)
3739
else
38-
DirectoryAssetMap.new(app.paths.assets)
40+
DirectoryAssetMap.new(paths.assets)
3941
end
4042
end
4143

42-
def ember_app_name
43-
@ember_app_name ||= app.options.fetch(:name) do
44-
package_json.fetch(:name)
45-
end
44+
def name_from_package_json
45+
package_json.fetch("name")
4646
end
4747

4848
def package_json
49-
@package_json ||=
50-
JSON.parse(app.paths.package_json_file.read).with_indifferent_access
49+
@package_json ||= JSON.parse(paths.package_json.read)
5150
end
5251
end
5352
end

lib/ember_cli/assets/paths.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module EmberCli
2+
module Assets
3+
class Paths
4+
def initialize(app)
5+
@app = app
6+
end
7+
8+
def assets
9+
app.dist_path.join("assets")
10+
end
11+
12+
def asset_map
13+
Pathname.glob(assets.join("assetMap*.json")).first
14+
end
15+
16+
def package_json
17+
app.root_path.join("package.json")
18+
end
19+
20+
protected
21+
22+
attr_reader :app
23+
end
24+
end
25+
end

spec/lib/ember_cli/assets/asset_map_spec.rb

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
},
1212
"prepend" => "foo/",
1313
}
14-
assets = build_assets(
15-
ember_app_name: "bar",
16-
asset_map: asset_map,
17-
)
14+
assets = build_assets(name: "bar", asset_map: asset_map)
1815

1916
javascripts = assets.javascripts
2017

@@ -26,10 +23,7 @@
2623

2724
context "when the asset_map is empty" do
2825
it "raises a BuildError" do
29-
assets = build_assets(
30-
asset_map: {},
31-
ember_app_name: "bar",
32-
)
26+
assets = build_assets(asset_map: {}, name: "bar")
3327

3428
expect { assets.javascripts }.to raise_build_error
3529
end
@@ -46,10 +40,7 @@
4640
},
4741
"prepend" => "foo/",
4842
}
49-
assets = build_assets(
50-
ember_app_name: "bar",
51-
asset_map: asset_map,
52-
)
43+
assets = build_assets(name: "bar", asset_map: asset_map)
5344

5445
stylesheets = assets.stylesheets
5546

@@ -61,10 +52,7 @@
6152

6253
context "when the asset_map is empty" do
6354
it "raises a BuildError" do
64-
assets = build_assets(
65-
asset_map: {},
66-
ember_app_name: "bar",
67-
)
55+
assets = build_assets(asset_map: {}, name: "bar")
6856

6957
expect { assets.stylesheets }.to raise_build_error
7058
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require "ember_cli/assets/paths"
2+
require "fileutils"
3+
require "pathname"
4+
require "tmpdir"
5+
6+
describe EmberCli::Assets::Paths do
7+
describe "#package_json" do
8+
it "is a child of EmberCli::App#root_path" do
9+
app = build_app
10+
paths = EmberCli::Assets::Paths.new(app)
11+
12+
expect(paths.package_json).to eq app.root_path.join("package.json")
13+
end
14+
end
15+
16+
describe "#assets" do
17+
it "is a child of EmberCli::App#dist_path" do
18+
app = build_app
19+
paths = EmberCli::Assets::Paths.new(app)
20+
21+
expect(paths.assets).to eq app.dist_path.join("assets")
22+
end
23+
end
24+
25+
describe "#asset_map" do
26+
it "globs the EmberCli::App#dist_path directory for an `asset_map.json`" do
27+
app = build_app
28+
paths = EmberCli::Assets::Paths.new(app)
29+
asset_map = app.dist_path.join("assets", "assetMap-abc123.json")
30+
create_file(asset_map)
31+
32+
expect(paths.asset_map).to exist
33+
expect(paths.asset_map).to eq(asset_map)
34+
end
35+
end
36+
37+
def build_app
38+
root_path = Pathname.new(Dir.mktmpdir)
39+
40+
double(
41+
root_path: root_path,
42+
dist_path: root_path.join("dist").tap(&:mkpath),
43+
)
44+
end
45+
46+
def create_file(path)
47+
path.parent.mkpath
48+
49+
FileUtils.touch(path)
50+
51+
path
52+
end
53+
end

0 commit comments

Comments
 (0)