middleman-sprockets-cj 3.3.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.simplecov +9 -0
- data/.travis.yml +15 -0
- data/CHANGELOG.md +71 -0
- data/CONTRIBUTING.md +43 -0
- data/Gemfile +32 -0
- data/LICENSE.md +20 -0
- data/README.md +104 -0
- data/Rakefile +45 -0
- data/features/asset_hash-3.0.feature +105 -0
- data/features/asset_hash.feature +107 -0
- data/features/bower.feature +37 -0
- data/features/jst.feature +14 -0
- data/features/long_filenames.feature +17 -0
- data/features/sass_globs.feature +9 -0
- data/features/sass_partials.feature +36 -0
- data/features/sprockets.feature +129 -0
- data/features/sprockets_gems.feature +67 -0
- data/features/step_definitions/server_steps.rb +7 -0
- data/features/support/env.rb +18 -0
- data/fixtures/asset-hash-app/config.rb +7 -0
- data/fixtures/asset-hash-app/source/images/100px.gif +0 -0
- data/fixtures/asset-hash-app/source/images/100px.jpg +0 -0
- data/fixtures/asset-hash-app/source/images/100px.png +0 -0
- data/fixtures/asset-hash-app/source/index.html.erb +308 -0
- data/fixtures/asset-hash-app/source/javascripts/application.js +8 -0
- data/fixtures/asset-hash-app/source/javascripts/sprockets_base.js +5 -0
- data/fixtures/asset-hash-app/source/javascripts/sprockets_sub.js +3 -0
- data/fixtures/asset-hash-app/source/layout.erb +19 -0
- data/fixtures/asset-hash-app/source/other.html.erb +2 -0
- data/fixtures/asset-hash-app/source/partials.html.erb +2 -0
- data/fixtures/asset-hash-app/source/stylesheets/_partial.sass +2 -0
- data/fixtures/asset-hash-app/source/stylesheets/jquery-mobile.css.scss +1 -0
- data/fixtures/asset-hash-app/source/stylesheets/site.css.scss +4 -0
- data/fixtures/asset-hash-app/source/stylesheets/uses_partials.css.sass +4 -0
- data/fixtures/asset-hash-app/source/subdir/index.html.erb +308 -0
- data/fixtures/asset-hash-host-app/config.rb +6 -0
- data/fixtures/asset-hash-host-app/source/images/100px.gif +0 -0
- data/fixtures/asset-hash-host-app/source/images/100px.jpg +0 -0
- data/fixtures/asset-hash-host-app/source/images/100px.png +0 -0
- data/fixtures/asset-hash-host-app/source/index.html.erb +6 -0
- data/fixtures/asset-hash-host-app/source/layout.erb +17 -0
- data/fixtures/asset-hash-host-app/source/other.html.erb +2 -0
- data/fixtures/asset-hash-host-app/source/stylesheets/site.css.scss +4 -0
- data/fixtures/asset-hash-host-app/source/subdir/index.html.erb +6 -0
- data/fixtures/asset-paths-app/config.rb +4 -0
- data/fixtures/asset-paths-app/derp/javascripts/vendored_js.js +1 -0
- data/fixtures/asset-paths-app/source/javascripts/vendored_include.js +1 -0
- data/fixtures/bower-app/bower.json +7 -0
- data/fixtures/bower-app/bower_components/underscore/bower.json +8 -0
- data/fixtures/bower-app/bower_components/underscore/underscore.js +1343 -0
- data/fixtures/bower-app/config.rb +3 -0
- data/fixtures/bower-app/source/javascripts/application.js +1 -0
- data/fixtures/bower-individual-outputdir-app/bower.json +7 -0
- data/fixtures/bower-individual-outputdir-app/bower_components/underscore/bower.json +8 -0
- data/fixtures/bower-individual-outputdir-app/bower_components/underscore/underscore.js +1343 -0
- data/fixtures/bower-individual-outputdir-app/config.rb +6 -0
- data/fixtures/bower-individual-outputdir-app/source/javascripts/application.js +1 -0
- data/fixtures/bower-individual-outputdir-app/vendor/assets/components/lightbox2/.bower.json +34 -0
- data/fixtures/bower-individual-outputdir-app/vendor/assets/components/lightbox2/bower.json +26 -0
- data/fixtures/bower-individual-outputdir-app/vendor/assets/components/lightbox2/img/close.png +0 -0
- data/fixtures/bower-individual-outputdir-app/vendor/assets/components/lightbox2/js/lightbox.js +2 -0
- data/fixtures/bower-individual-outputdir-app/vendor/assets/components/lightbox2/package.json +37 -0
- data/fixtures/bower-json-app/config.rb +1 -0
- data/fixtures/bower-json-app/source/javascripts/application.js.coffee +1 -0
- data/fixtures/bower-json-app/source/javascripts/bower.json +5 -0
- data/fixtures/bower-multiple-assets-app/.bowerrc +4 -0
- data/fixtures/bower-multiple-assets-app/bower.json +7 -0
- data/fixtures/bower-multiple-assets-app/config.rb +7 -0
- data/fixtures/bower-multiple-assets-app/vendor/assets/components/lightbox2/.bower.json +34 -0
- data/fixtures/bower-multiple-assets-app/vendor/assets/components/lightbox2/bower.json +26 -0
- data/fixtures/bower-multiple-assets-app/vendor/assets/components/lightbox2/img/close.png +0 -0
- data/fixtures/bower-multiple-assets-app/vendor/assets/components/lightbox2/js/lightbox.js +2 -0
- data/fixtures/bower-multiple-assets-app/vendor/assets/components/lightbox2/package.json +37 -0
- data/fixtures/glob-app/config.rb +0 -0
- data/fixtures/glob-app/source/stylesheets/main.css.scss +1 -0
- data/fixtures/glob-app/source/stylesheets/module1/_i-am-mod.scss +3 -0
- data/fixtures/glob-app/source/stylesheets/module2/_derp.sass +2 -0
- data/fixtures/glob-app/source/stylesheets/shared/3rd-party/bootstrap.sass +2 -0
- data/fixtures/glob-app/source/stylesheets/shared/shared.scss +3 -0
- data/fixtures/jquery-mobile-app/config.rb +0 -0
- data/fixtures/jquery-mobile-app/source/stylesheets/base.css.scss +2 -0
- data/fixtures/long-filenames-app/config.rb +0 -0
- data/fixtures/long-filenames-app/source/images/00000000-0000-0000-0000-000000.svg +3 -0
- data/fixtures/long-filenames-app/source/images/00000000-0000-0000-0000-0000001.svg +3 -0
- data/fixtures/preview-app/config.rb +0 -0
- data/fixtures/preview-app/source/content.html.erb +1 -0
- data/fixtures/preview-app/source/layout.erb +1 -0
- data/fixtures/preview-app/source/stylesheets/_partial.sass +2 -0
- data/fixtures/preview-app/source/stylesheets/_partial2.css.sass +2 -0
- data/fixtures/preview-app/source/stylesheets/main.css.sass +4 -0
- data/fixtures/preview-app/source/stylesheets/main2.css.sass +4 -0
- data/fixtures/preview-app/source/stylesheets/plain.css.sass +2 -0
- data/fixtures/sprockets-app-debug-assets/config.rb +1 -0
- data/fixtures/sprockets-app-debug-assets/source/index.html.erb +8 -0
- data/fixtures/sprockets-app-debug-assets/source/javascripts/dependency1.js +3 -0
- data/fixtures/sprockets-app-debug-assets/source/javascripts/dependency2.js +1 -0
- data/fixtures/sprockets-app-debug-assets/source/javascripts/main.js +4 -0
- data/fixtures/sprockets-app-debug-assets/source/stylesheets/app.css.scss +4 -0
- data/fixtures/sprockets-app-debug-assets/source/stylesheets/dependency1.css +0 -0
- data/fixtures/sprockets-app-debug-assets/source/stylesheets/dependency2.css.scss +0 -0
- data/fixtures/sprockets-app/config.rb +7 -0
- data/fixtures/sprockets-app/source/index.html.erb +6 -0
- data/fixtures/sprockets-app/source/library/css/bootstrap_include.css.scss +1 -0
- data/fixtures/sprockets-app/source/library/css/plain.css +3 -0
- data/fixtures/sprockets-app/source/library/css/sprockets_base1.css.scss +1 -0
- data/fixtures/sprockets-app/source/library/css/sprockets_base2.css.scss +1 -0
- data/fixtures/sprockets-app/source/library/css/sprockets_sub.css.scss +1 -0
- data/fixtures/sprockets-app/source/library/js/jquery_include.js +1 -0
- data/fixtures/sprockets-app/source/library/js/plain.js +3 -0
- data/fixtures/sprockets-app/source/library/js/sprockets_base.js +5 -0
- data/fixtures/sprockets-app/source/library/js/sprockets_sub.js +3 -0
- data/fixtures/sprockets-app/source/library/js/vendored_include.js +1 -0
- data/fixtures/sprockets-app/vendor/assets/javascripts/coffee.js.coffee +2 -0
- data/fixtures/sprockets-app/vendor/assets/javascripts/vendored_js.js +1 -0
- data/fixtures/sprockets-app/vendor/assets/stylesheets/vendored.css.scss +4 -0
- data/fixtures/sprockets-app2/config.rb +0 -0
- data/fixtures/sprockets-app2/data/test.yml +4 -0
- data/fixtures/sprockets-app2/data/test2.json +4 -0
- data/fixtures/sprockets-app2/source/javascripts/_templates/test.jst.ejs +1 -0
- data/fixtures/sprockets-app2/source/javascripts/_templates/test2.jst.eco +1 -0
- data/fixtures/sprockets-app2/source/javascripts/asset_path.js.erb +3 -0
- data/fixtures/sprockets-app2/source/javascripts/multiple_engines.js.coffee.erb +1 -0
- data/fixtures/sprockets-app2/source/javascripts/sprockets_base.js +5 -0
- data/fixtures/sprockets-app2/source/javascripts/sprockets_sub.js +3 -0
- data/fixtures/sprockets-app2/source/javascripts/templates.js +1 -0
- data/fixtures/sprockets-app2/source/stylesheets/sprockets_base1.css.scss +1 -0
- data/fixtures/sprockets-app2/source/stylesheets/sprockets_base2.css.scss +1 -0
- data/fixtures/sprockets-app2/source/stylesheets/sprockets_sub.css.scss +1 -0
- data/fixtures/sprockets-images-app/config.rb +1 -0
- data/fixtures/sprockets-images-app/source/index.html.erb +10 -0
- data/fixtures/sprockets-images-app/source/library/images/cat.jpg +0 -0
- data/fixtures/sprockets-images-app/vendor/assets/images/cat-2.jpg +0 -0
- data/lib/middleman-sprockets.rb +6 -0
- data/lib/middleman-sprockets/asset.rb +185 -0
- data/lib/middleman-sprockets/asset_list.rb +41 -0
- data/lib/middleman-sprockets/asset_tag_helpers.rb +54 -0
- data/lib/middleman-sprockets/config_only_environment.rb +50 -0
- data/lib/middleman-sprockets/environment.rb +228 -0
- data/lib/middleman-sprockets/extension.rb +145 -0
- data/lib/middleman-sprockets/imported_asset.rb +57 -0
- data/lib/middleman-sprockets/pathname_extensions.rb +10 -0
- data/lib/middleman-sprockets/sass_function_hack.rb +9 -0
- data/lib/middleman-sprockets/version.rb +5 -0
- data/middleman-sprockets.gemspec +22 -0
- data/spec/asset_list_spec.rb +38 -0
- data/spec/asset_spec.rb +116 -0
- data/spec/imported_asset_spec.rb +72 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/support/aruba.rb +18 -0
- data/spec/support/rspec.rb +17 -0
- metadata +257 -0
@@ -0,0 +1,145 @@
|
|
1
|
+
require "sprockets"
|
2
|
+
require "sprockets-sass"
|
3
|
+
require "middleman-sprockets/pathname_extensions"
|
4
|
+
require "middleman-sprockets/asset"
|
5
|
+
require "middleman-sprockets/imported_asset"
|
6
|
+
require "middleman-sprockets/asset_list"
|
7
|
+
require "middleman-sprockets/config_only_environment"
|
8
|
+
require "middleman-sprockets/environment"
|
9
|
+
require "middleman-sprockets/asset_tag_helpers"
|
10
|
+
|
11
|
+
# Sprockets extension
|
12
|
+
module Middleman
|
13
|
+
class SprocketsExtension < Extension
|
14
|
+
option :debug_assets, false, 'Split up each required asset into its own script/style tag instead of combining them (development only)'
|
15
|
+
|
16
|
+
attr_reader :environment
|
17
|
+
|
18
|
+
# This module gets mixed into both the Middleman instance and the Middleman class,
|
19
|
+
# so that it's available in config.rb
|
20
|
+
module SprocketsAccessor
|
21
|
+
# The sprockets environment
|
22
|
+
# @return [Middleman::MiddlemanSprocketsEnvironment]
|
23
|
+
def sprockets
|
24
|
+
extensions[:sprockets].environment
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def initialize(app, options_hash={}, &block)
|
29
|
+
require "middleman-sprockets/sass_function_hack"
|
30
|
+
|
31
|
+
super
|
32
|
+
|
33
|
+
# Start out with a stub environment that can only be configured (paths and such)
|
34
|
+
@environment = ::Middleman::Sprockets::ConfigOnlyEnvironment.new
|
35
|
+
|
36
|
+
app.send :include, SprocketsAccessor
|
37
|
+
end
|
38
|
+
|
39
|
+
helpers do
|
40
|
+
include SprocketsAccessor
|
41
|
+
include ::Middleman::Sprockets::AssetTagHelpers
|
42
|
+
end
|
43
|
+
|
44
|
+
def after_configuration
|
45
|
+
::Tilt.register ::Sprockets::EjsTemplate, 'ejs'
|
46
|
+
::Tilt.register ::Sprockets::EcoTemplate, 'eco'
|
47
|
+
::Tilt.register ::Sprockets::JstProcessor, 'jst'
|
48
|
+
|
49
|
+
app.template_extensions :jst => :js, :eco => :js, :ejs => :js
|
50
|
+
|
51
|
+
if app.config.defines_setting?(:debug_assets) && !options.setting(:debug_assets).value_set?
|
52
|
+
options[:debug_assets] = app.config[:debug_assets]
|
53
|
+
end
|
54
|
+
|
55
|
+
config_environment = @environment
|
56
|
+
debug_assets = !app.build? && options[:debug_assets]
|
57
|
+
@environment = ::Middleman::Sprockets::Environment.new(app, :debug_assets => debug_assets)
|
58
|
+
config_environment.apply_to_environment(@environment)
|
59
|
+
|
60
|
+
append_paths_from_gems
|
61
|
+
|
62
|
+
# Setup Sprockets Sass options
|
63
|
+
if app.config.defines_setting?(:sass)
|
64
|
+
app.config[:sass].each { |k, v| ::Sprockets::Sass.options[k] = v }
|
65
|
+
end
|
66
|
+
|
67
|
+
# Intercept requests to /javascripts and /stylesheets and pass to sprockets
|
68
|
+
our_sprockets = self.environment
|
69
|
+
|
70
|
+
[app.config[:js_dir], app.config[:css_dir], app.config[:images_dir], app.config[:fonts_dir]].each do |dir|
|
71
|
+
app.map("/#{dir}") { run our_sprockets }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Add sitemap resource for every image in the sprockets load path
|
76
|
+
def manipulate_resource_list(resources)
|
77
|
+
imported_assets = Middleman::Sprockets::AssetList.new
|
78
|
+
|
79
|
+
environment.imported_assets.each do |asset|
|
80
|
+
asset.resolve_path_with environment
|
81
|
+
@app.logger.debug "== Importing Sprockets asset #{asset.real_path}"
|
82
|
+
|
83
|
+
imported_assets << asset
|
84
|
+
end
|
85
|
+
|
86
|
+
resources_list = []
|
87
|
+
environment.paths.each do |load_path|
|
88
|
+
environment.each_entry(load_path) do |path|
|
89
|
+
asset = Middleman::Sprockets::Asset.new(path, source_directory: load_path)
|
90
|
+
|
91
|
+
imported_assets.lookup(asset) do |candidate, found_asset|
|
92
|
+
candidate.destination_path = found_asset.output_path if found_asset.output_path
|
93
|
+
candidate.import_it
|
94
|
+
end
|
95
|
+
|
96
|
+
next unless asset.import?
|
97
|
+
|
98
|
+
if asset.has_type? :image
|
99
|
+
asset.destination_directory = @app.config[:images_dir]
|
100
|
+
elsif asset.has_type? :script
|
101
|
+
asset.destination_directory = @app.config[:js_dir]
|
102
|
+
elsif asset.has_type? :font
|
103
|
+
asset.destination_directory = @app.config[:fonts_dir]
|
104
|
+
elsif asset.has_type? :stylesheet
|
105
|
+
asset.destination_directory = @app.config[:css_dir]
|
106
|
+
end
|
107
|
+
|
108
|
+
new_path = @app.sitemap.extensionless_path(asset.destination_path.to_s)
|
109
|
+
|
110
|
+
next if @app.sitemap.find_resource_by_destination_path(new_path.to_s)
|
111
|
+
resources_list << ::Middleman::Sitemap::Resource.new(@app.sitemap, new_path.to_s, path.to_s)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
resources + resources_list
|
115
|
+
end
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
# Add any directories from gems with Rails-like paths to sprockets load path
|
120
|
+
def append_paths_from_gems
|
121
|
+
try_paths = [
|
122
|
+
%w{ assets },
|
123
|
+
%w{ app },
|
124
|
+
%w{ app assets },
|
125
|
+
%w{ vendor },
|
126
|
+
%w{ vendor assets },
|
127
|
+
%w{ lib },
|
128
|
+
%w{ lib assets }
|
129
|
+
].inject([]) do |sum, v|
|
130
|
+
sum + [
|
131
|
+
File.join(v, 'javascripts'),
|
132
|
+
File.join(v, 'stylesheets'),
|
133
|
+
File.join(v, 'images'),
|
134
|
+
File.join(v, 'fonts')
|
135
|
+
]
|
136
|
+
end
|
137
|
+
|
138
|
+
([app.root] + ::Middleman.rubygems_latest_specs.map(&:full_gem_path)).each do |root_path|
|
139
|
+
try_paths.map {|p| File.join(root_path, p) }.
|
140
|
+
select {|p| File.directory?(p) }.
|
141
|
+
each {|path| self.environment.append_path(path) }
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Middleman
|
3
|
+
module Sprockets
|
4
|
+
# ImportedAsset
|
5
|
+
class ImportedAsset
|
6
|
+
attr_reader :logical_path, :output_path, :real_path
|
7
|
+
|
8
|
+
# Create instance
|
9
|
+
#
|
10
|
+
# @param [Pathname] logical_path
|
11
|
+
# The logical path to the asset given in config.rb
|
12
|
+
#
|
13
|
+
# @param [proc] output_dir
|
14
|
+
# An individual output directory for that particular asset
|
15
|
+
def initialize(logical_path, determine_output_path = proc { nil })
|
16
|
+
@logical_path = Pathname.new(logical_path)
|
17
|
+
@output_path = if output_path = determine_output_path.call(@logical_path)
|
18
|
+
Pathname.new(output_path)
|
19
|
+
else
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Resolve logical path to real path
|
25
|
+
#
|
26
|
+
# @param [#resolve] resolver
|
27
|
+
# The objects which is able to resolve a logical path
|
28
|
+
def resolve_path_with(resolver)
|
29
|
+
@real_path = resolver.resolve(logical_path)
|
30
|
+
|
31
|
+
raise ::Sprockets::FileNotFound, "Couldn't find asset '#{logical_path}'" if real_path == nil || real_path == ''
|
32
|
+
end
|
33
|
+
|
34
|
+
# String representation of asset
|
35
|
+
#
|
36
|
+
# @return [String]
|
37
|
+
# The logical path as string
|
38
|
+
def to_s
|
39
|
+
logical_path.to_s
|
40
|
+
end
|
41
|
+
|
42
|
+
# Does the given patch matches asset
|
43
|
+
#
|
44
|
+
# @param [Pathname] path
|
45
|
+
# The path to be checked
|
46
|
+
def match?(path)
|
47
|
+
has_real_path? path
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def has_real_path?(path)
|
53
|
+
real_path == path
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
if defined?(Sass)
|
2
|
+
module Sass::Script::Functions
|
3
|
+
# Override generated_image_url to use Sprockets
|
4
|
+
# a la https://github.com/Compass/compass-rails/blob/master/lib/compass-rails/patches/4_0.rb
|
5
|
+
def generated_image_url(path, only_path = nil)
|
6
|
+
asset_url(path, Sass::Script::String.new("image"))
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "middleman-sprockets/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "middleman-sprockets-cj"
|
7
|
+
s.version = Middleman::Sprockets::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['Thomas Reynolds', 'Ben Hollis', 'Karl Freeman']
|
10
|
+
s.email = ['me@tdreyno.com', 'ben@benhollis.net', 'karlfreeman@gmail.com']
|
11
|
+
s.homepage = "https://github.com/middleman/middleman-sprockets"
|
12
|
+
s.summary = %q{Sprockets support for Middleman}
|
13
|
+
s.description = %q{Sprockets support for Middleman}
|
14
|
+
s.license = "MIT"
|
15
|
+
s.files = `git ls-files -z`.split("\0")
|
16
|
+
s.test_files = `git ls-files -z -- {fixtures,features}/*`.split("\0")
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.add_dependency("middleman-core-cj", ["~> 3.3"])
|
19
|
+
s.add_dependency("sprockets", ["~> 2.12.1"])
|
20
|
+
s.add_dependency("sprockets-sass", ["~> 1.2.0"])
|
21
|
+
s.add_dependency("sprockets-helpers", ["~> 1.1.0"])
|
22
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
RSpec.describe AssetList do
|
4
|
+
context '#add' do
|
5
|
+
it 'adds an asset to the list' do
|
6
|
+
asset = instance_double 'Middleman::Sprockets::Asset'
|
7
|
+
list = AssetList.new
|
8
|
+
|
9
|
+
expect {
|
10
|
+
list << asset
|
11
|
+
}.not_to raise_error
|
12
|
+
end
|
13
|
+
end
|
14
|
+
context '#lookup' do
|
15
|
+
it 'finds an asset in list' do
|
16
|
+
asset = instance_double 'Middleman::Sprockets::Asset'
|
17
|
+
expect(asset).to receive(:source_path).and_return 'path/to/source'
|
18
|
+
expect(asset).to receive(:match?).and_return true
|
19
|
+
|
20
|
+
list = AssetList.new
|
21
|
+
list << asset
|
22
|
+
|
23
|
+
expect(list.lookup(asset)).to be asset
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'supports a block which gets the found asset passed' do
|
27
|
+
asset = instance_double 'Middleman::Sprockets::Asset'
|
28
|
+
allow(asset).to receive(:source_path).and_return 'path/to/source'
|
29
|
+
expect(asset).to receive(:destination_path=).with 'path/to/source'
|
30
|
+
expect(asset).to receive(:match?).and_return true
|
31
|
+
|
32
|
+
list = AssetList.new
|
33
|
+
list << asset
|
34
|
+
|
35
|
+
list.lookup(asset) { |candidate, found_asset| found_asset.destination_path = found_asset.source_path }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/spec/asset_spec.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
RSpec.describe Asset do
|
3
|
+
context '#has_type?' do
|
4
|
+
it 'finds type by extension' do
|
5
|
+
asset = Asset.new('/source/path/to/image.png', source_directory: '/source/path/to')
|
6
|
+
expect(asset).to have_type :image
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'finds type by path' do
|
10
|
+
asset = Asset.new('/source/path/to/images/image.xz', source_directory: '/source/path/to/images')
|
11
|
+
expect(asset).to have_type :image
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context '#import?, #import_it' do
|
16
|
+
it 'fails if file does not exist' do
|
17
|
+
in_current_dir do
|
18
|
+
base_path = File.expand_path('source/path/to')
|
19
|
+
file_path = File.expand_path('source/path/to/image.xz')
|
20
|
+
|
21
|
+
asset = Asset.new(file_path, source_directory: base_path)
|
22
|
+
|
23
|
+
expect(asset).not_to be_import
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'succeeds if import it is set' do
|
28
|
+
in_current_dir do
|
29
|
+
base_path = File.expand_path('source/path/to')
|
30
|
+
file_path = File.expand_path('source/path/to/image.xz')
|
31
|
+
write_file file_path, 'asdf'
|
32
|
+
|
33
|
+
asset = Asset.new(file_path, source_directory: base_path)
|
34
|
+
asset.import_it
|
35
|
+
|
36
|
+
expect(asset).to be_import
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'succeeds if is in trusted directory images' do
|
41
|
+
in_current_dir do
|
42
|
+
base_path = File.expand_path('source/path/to/images')
|
43
|
+
file_path = File.expand_path('source/path/to/images/image.xz')
|
44
|
+
write_file file_path, 'asdf'
|
45
|
+
|
46
|
+
asset = Asset.new(file_path, source_directory: base_path)
|
47
|
+
|
48
|
+
expect(asset).to be_import
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context '#destination_path, #destination_path=, #destination_directory' do
|
54
|
+
it 'returns @destination_path if set' do
|
55
|
+
in_current_dir do
|
56
|
+
base_path = File.expand_path('source/path/to/images')
|
57
|
+
file_path = File.expand_path('source/path/to/images/image.xz')
|
58
|
+
|
59
|
+
asset = Asset.new(file_path, source_directory: base_path)
|
60
|
+
asset.destination_path = 'asdf/image.xz'
|
61
|
+
|
62
|
+
expect(asset.destination_path).to eq Pathname.new('asdf/image.xz')
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'builds path based on destination_directory and relative file path' do
|
67
|
+
in_current_dir do
|
68
|
+
base_path = File.expand_path('source/path/to/images')
|
69
|
+
file_path = File.expand_path('source/path/to/images/image.xz')
|
70
|
+
|
71
|
+
asset = Asset.new(file_path, source_directory: base_path)
|
72
|
+
asset.destination_directory = '/images'
|
73
|
+
|
74
|
+
expect(asset.destination_path.to_s).to eq '/images/image.xz'
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'fails if destination_directory and @destination_path are not set' do
|
79
|
+
in_current_dir do
|
80
|
+
base_path = File.expand_path('source/path/to/images')
|
81
|
+
file_path = File.expand_path('source/path/to/images/image.xz')
|
82
|
+
|
83
|
+
asset = Asset.new(file_path, source_directory: base_path)
|
84
|
+
|
85
|
+
expect {
|
86
|
+
asset.destination_path
|
87
|
+
}.to raise_error ::Sprockets::FileNotFound
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context '#match?' do
|
93
|
+
it 'success if source path is equal' do
|
94
|
+
in_current_dir do
|
95
|
+
base_path = File.expand_path('source/path/to')
|
96
|
+
file_path = File.expand_path('source/path/to/images/image.xz')
|
97
|
+
|
98
|
+
asset1 = Asset.new(file_path, source_directory: base_path)
|
99
|
+
|
100
|
+
expect(asset1).to be_match file_path
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'fails if source path is not equal' do
|
105
|
+
in_current_dir do
|
106
|
+
base_path = File.expand_path('source/path/to')
|
107
|
+
file_path = File.expand_path('source/path/to/images/image.xz')
|
108
|
+
|
109
|
+
asset1 = Asset.new(file_path, source_directory: base_path)
|
110
|
+
|
111
|
+
expect(asset1).not_to be_match 'asdf'
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
RSpec.describe ImportedAsset do
|
3
|
+
context '#output_path' do
|
4
|
+
it 'uses block as second argument on initialize to get path' do
|
5
|
+
asset = ImportedAsset.new 'source/to/asset/image.png', proc { 'hello/world.png' }
|
6
|
+
|
7
|
+
expect(asset.output_path.to_s).to eq 'hello/world.png'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context '#resolve_path_with' do
|
12
|
+
it 'resolves path' do
|
13
|
+
in_current_dir do
|
14
|
+
relative_path = 'source/path/to/image.xz'
|
15
|
+
file_path = File.expand_path(relative_path)
|
16
|
+
|
17
|
+
resolver = double('Environment')
|
18
|
+
expect(resolver).to receive(:resolve).with(Pathname.new(relative_path)).and_return file_path
|
19
|
+
|
20
|
+
asset = ImportedAsset.new relative_path
|
21
|
+
asset.resolve_path_with resolver
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'raises an error if path could not be resolved' do
|
26
|
+
in_current_dir do
|
27
|
+
relative_path = 'source/path/to/image.xz'
|
28
|
+
|
29
|
+
resolver = double('Environment')
|
30
|
+
allow(resolver).to receive(:resolve).with(Pathname.new(relative_path)).and_return nil
|
31
|
+
|
32
|
+
asset = ImportedAsset.new relative_path
|
33
|
+
|
34
|
+
expect {
|
35
|
+
asset.resolve_path_with resolver
|
36
|
+
}.to raise_error ::Sprockets::FileNotFound
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context '#match?' do
|
42
|
+
it 'succeeds if real path matches' do
|
43
|
+
in_current_dir do
|
44
|
+
relative_path = 'source/path/to/image.xz'
|
45
|
+
file_path = File.expand_path(relative_path)
|
46
|
+
|
47
|
+
resolver = double('Environment')
|
48
|
+
allow(resolver).to receive(:resolve).and_return file_path
|
49
|
+
|
50
|
+
asset = ImportedAsset.new relative_path
|
51
|
+
asset.resolve_path_with resolver
|
52
|
+
|
53
|
+
expect(asset).to be_match file_path
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'fails if does not match' do
|
58
|
+
in_current_dir do
|
59
|
+
relative_path = 'source/path/to/image.xz'
|
60
|
+
file_path = File.expand_path(relative_path)
|
61
|
+
|
62
|
+
resolver = double('Environment')
|
63
|
+
allow(resolver).to receive(:resolve).and_return file_path + 'fail'
|
64
|
+
|
65
|
+
asset = ImportedAsset.new relative_path
|
66
|
+
asset.resolve_path_with resolver
|
67
|
+
|
68
|
+
expect(asset).not_to be_match file_path
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|