generated-assets 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +9 -0
- data/Rakefile +30 -0
- data/generated-assets.gemspec +28 -0
- data/lib/generated-assets.rb +23 -0
- data/lib/generated-assets/entry.rb +27 -0
- data/lib/generated-assets/manifest.rb +64 -0
- data/lib/generated-assets/rails_manifest.rb +88 -0
- data/lib/generated-assets/railtie.rb +25 -0
- data/lib/generated-assets/version.rb +5 -0
- data/spec/app/assets/javascripts/application.js +0 -0
- data/spec/config/application.rb +10 -0
- data/spec/config/i18n-js.yml +2 -0
- data/spec/config/locales/en.yml +3 -0
- data/spec/config/locales/es.yml +3 -0
- data/spec/entry_spec.rb +48 -0
- data/spec/log/development.log +0 -0
- data/spec/log/test.log +71 -0
- data/spec/manifest_spec.rb +50 -0
- data/spec/precomp_spec.rb +40 -0
- data/spec/rails_manifest_spec.rb +88 -0
- data/spec/railtie_spec.rb +19 -0
- data/spec/spec_helper.rb +59 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a05c43b996cd0851316737175dc7fcee25867fe7
|
4
|
+
data.tar.gz: 17e4115951bf3864c79aef73bbdd781e02acc80e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 56e082770b8894698932e9aa3d79d66d191d7bb5e90cfcdec6404217cf0a4238d2c34d338eb8902e4f847fa94f5694d8291ee07a163c910024d580c1f8266db2
|
7
|
+
data.tar.gz: b8271ea4b6dc287a82677eb96397a11660b2bf159ac734c96466248a14da9ff96b021b095b76c770553df6b5997be50c0a520a7cebc5d92dad90f4f8121f25c4
|
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
$:.unshift File.join(File.dirname(__FILE__), 'lib')
|
4
|
+
|
5
|
+
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
6
|
+
|
7
|
+
require 'bundler'
|
8
|
+
require 'pry-nav'
|
9
|
+
require 'rspec/core/rake_task'
|
10
|
+
require 'rubygems/package_task'
|
11
|
+
|
12
|
+
require 'rails'
|
13
|
+
require 'sprockets/railtie'
|
14
|
+
require 'generated-assets'
|
15
|
+
|
16
|
+
Bundler::GemHelper.install_tasks
|
17
|
+
|
18
|
+
task :default => :spec
|
19
|
+
|
20
|
+
desc 'Run specs'
|
21
|
+
RSpec::Core::RakeTask.new do |t|
|
22
|
+
t.pattern = './spec/**/*_spec.rb'
|
23
|
+
end
|
24
|
+
|
25
|
+
# for dummy app
|
26
|
+
Dir.chdir('spec') do
|
27
|
+
require File.expand_path('../spec/config/application', __FILE__)
|
28
|
+
GeneratedAssets::DummyApplication.initialize!
|
29
|
+
GeneratedAssets::DummyApplication.load_tasks
|
30
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), 'lib')
|
2
|
+
require 'generated-assets/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "generated-assets"
|
6
|
+
s.version = ::GeneratedAssets::VERSION
|
7
|
+
s.authors = ["Cameron Dutro"]
|
8
|
+
s.email = ["camertron@gmail.com"]
|
9
|
+
s.homepage = "http://github.com/camertron"
|
10
|
+
|
11
|
+
s.description = s.summary = 'Programmatically generate assets for the Rails asset pipeline.'
|
12
|
+
|
13
|
+
s.platform = Gem::Platform::RUBY
|
14
|
+
s.has_rdoc = true
|
15
|
+
|
16
|
+
if ENV['RAILS_VERSION']
|
17
|
+
s.add_dependency 'railties', "~> #{ENV['RAILS_VERSION']}"
|
18
|
+
else
|
19
|
+
s.add_dependency 'railties', '>= 3.2.0', '< 5'
|
20
|
+
end
|
21
|
+
|
22
|
+
s.add_dependency 'sprockets-rails'
|
23
|
+
s.add_dependency 'tilt'
|
24
|
+
s.add_dependency 'tzinfo'
|
25
|
+
|
26
|
+
s.require_path = 'lib'
|
27
|
+
s.files = Dir["{lib,spec}/**/*", "Gemfile", "README.md", "Rakefile", "generated-assets.gemspec"]
|
28
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'fileutils'
|
4
|
+
require 'generated-assets/railtie'
|
5
|
+
require 'securerandom'
|
6
|
+
require 'tmpdir'
|
7
|
+
|
8
|
+
module GeneratedAssets
|
9
|
+
autoload :Entry, 'generated-assets/entry'
|
10
|
+
autoload :Manifest, 'generated-assets/manifest'
|
11
|
+
autoload :Processor, 'generated-assets/processor'
|
12
|
+
autoload :RailsManifest, 'generated-assets/rails_manifest'
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def asset_dir
|
16
|
+
@asset_dir ||= begin
|
17
|
+
dir = File.join(Dir.tmpdir, SecureRandom.hex)
|
18
|
+
FileUtils.mkdir_p(dir)
|
19
|
+
dir
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
module GeneratedAssets
|
6
|
+
class Entry
|
7
|
+
DEFAULT_PROC = Proc.new { '' }
|
8
|
+
|
9
|
+
attr_reader :logical_path, :callback, :options
|
10
|
+
|
11
|
+
def initialize(logical_path, callback, options = {})
|
12
|
+
@logical_path = logical_path
|
13
|
+
@callback = callback || DEFAULT_PROC
|
14
|
+
@options = options
|
15
|
+
end
|
16
|
+
|
17
|
+
def precompile?
|
18
|
+
@options.fetch(:precompile, false)
|
19
|
+
end
|
20
|
+
|
21
|
+
def write_to(prefix)
|
22
|
+
path = File.join(prefix, logical_path)
|
23
|
+
FileUtils.mkdir_p(File.dirname(path))
|
24
|
+
File.write(path, callback.call)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module GeneratedAssets
|
4
|
+
class Manifest
|
5
|
+
attr_reader :app, :prefix, :entries
|
6
|
+
attr_reader :before_hooks, :after_hooks
|
7
|
+
|
8
|
+
def initialize(app, prefix)
|
9
|
+
@app = app
|
10
|
+
@prefix = prefix
|
11
|
+
@entries = []
|
12
|
+
@before_hooks = []
|
13
|
+
@after_hooks = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def add(logical_path, options = {}, &block)
|
17
|
+
entries << Entry.new(logical_path, block, options)
|
18
|
+
end
|
19
|
+
|
20
|
+
def apply!
|
21
|
+
before_hooks.each(&:call)
|
22
|
+
|
23
|
+
write_files
|
24
|
+
add_precompile_paths
|
25
|
+
|
26
|
+
after_hooks.each(&:call)
|
27
|
+
end
|
28
|
+
|
29
|
+
def before_apply(&block)
|
30
|
+
before_hooks << block
|
31
|
+
end
|
32
|
+
|
33
|
+
def after_apply(&block)
|
34
|
+
after_hooks << block
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def write_files
|
40
|
+
entries.each do |entry|
|
41
|
+
entry.write_to(prefix)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def add_precompile_paths
|
46
|
+
entries.each do |entry|
|
47
|
+
if entry.precompile?
|
48
|
+
app.config.assets.precompile << remove_extra_extensions(
|
49
|
+
entry.logical_path
|
50
|
+
)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def remove_extra_extensions(path)
|
56
|
+
until File.extname(path).empty?
|
57
|
+
ext = File.extname(path)
|
58
|
+
path = path.chomp(ext)
|
59
|
+
end
|
60
|
+
|
61
|
+
"#{path}#{ext}"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module GeneratedAssets
|
7
|
+
class RailsManifest
|
8
|
+
class << self
|
9
|
+
def load_for(app)
|
10
|
+
asset_path = asset_path_for(app)
|
11
|
+
|
12
|
+
[YamlManifest, JsonManifest].each do |klass|
|
13
|
+
if manifest_file = klass.find_in(asset_path)
|
14
|
+
return klass.load_file(manifest_file)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def asset_path_for(app)
|
22
|
+
Pathname(
|
23
|
+
File.join(Rails.public_path, app.config.assets.prefix)
|
24
|
+
)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
attr_reader :data
|
29
|
+
|
30
|
+
def initialize(data)
|
31
|
+
@data = data
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class YamlManifest < RailsManifest
|
36
|
+
class << self
|
37
|
+
def find_in(path)
|
38
|
+
Dir.glob(File.join(path, 'manifest.yml')).first
|
39
|
+
end
|
40
|
+
|
41
|
+
def load_file(file)
|
42
|
+
load(File.read(file))
|
43
|
+
end
|
44
|
+
|
45
|
+
def load(raw)
|
46
|
+
new(YAML.load(raw))
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def files
|
51
|
+
data
|
52
|
+
end
|
53
|
+
|
54
|
+
def find_by_logical(logical_path)
|
55
|
+
files[logical_path]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class JsonManifest < RailsManifest
|
60
|
+
class << self
|
61
|
+
def find_in(path)
|
62
|
+
Dir.glob(File.join(path, '.*sprockets-manifest*.json')).first
|
63
|
+
end
|
64
|
+
|
65
|
+
def load_file(file)
|
66
|
+
load(File.read(file))
|
67
|
+
end
|
68
|
+
|
69
|
+
def load(raw)
|
70
|
+
new(JSON.parse(raw))
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def files
|
75
|
+
data['files']
|
76
|
+
end
|
77
|
+
|
78
|
+
def find_by_logical(logical_path)
|
79
|
+
files.each_pair do |digest_file, attributes|
|
80
|
+
if attributes['logical_path'] == logical_path
|
81
|
+
return digest_file
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
nil
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module GeneratedAssets
|
4
|
+
class Railtie < ::Rails::Railtie
|
5
|
+
config.before_configuration do |app|
|
6
|
+
app.config.assets.generated = GeneratedAssets::Manifest.new(
|
7
|
+
app, File.join(GeneratedAssets.asset_dir, app.class.parent_name)
|
8
|
+
)
|
9
|
+
end
|
10
|
+
|
11
|
+
config.after_initialize do |app|
|
12
|
+
if app.config.assets.compile
|
13
|
+
app.config.assets.generated.apply!
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
initializer :i18n_js_assets, group: :all do |app|
|
18
|
+
if app.config.assets.compile
|
19
|
+
app.config.assets.paths << File.join(
|
20
|
+
GeneratedAssets.asset_dir, app.class.parent_name
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
File without changes
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module GeneratedAssets
|
2
|
+
class DummyApplication < ::Rails::Application
|
3
|
+
config.eager_load = false
|
4
|
+
config.assets.enabled = true
|
5
|
+
config.assets.compile = true
|
6
|
+
config.assets.allow_debugging = true
|
7
|
+
config.assets.digest = true
|
8
|
+
config.active_support.deprecation = :stderr
|
9
|
+
end
|
10
|
+
end
|
data/spec/entry_spec.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
include GeneratedAssets
|
6
|
+
|
7
|
+
describe Entry do
|
8
|
+
let(:entry) do
|
9
|
+
Entry.new(logical_path, callback)
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:callback) do
|
13
|
+
-> { 'Callback contents' }
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:logical_path) do
|
17
|
+
'foo/bar.txt'
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#precompile?' do
|
21
|
+
it 'indicates no precompilation' do
|
22
|
+
expect(entry.precompile?).to eq(false)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#write_to' do
|
27
|
+
it 'writes the file to the given path' do
|
28
|
+
entry.write_to(tmpdir)
|
29
|
+
absolute_path = tmpdir.join(logical_path)
|
30
|
+
files = Dir.glob(tmpdir.join('**', '**'))
|
31
|
+
expect(files).to include(absolute_path.to_s)
|
32
|
+
contents = File.read(absolute_path)
|
33
|
+
expect(contents).to eq(callback.call)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'with an entry that should be precompiled' do
|
38
|
+
let(:entry) do
|
39
|
+
Entry.new('foo/bar.txt', nil, precompile: true)
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '#precompile?' do
|
43
|
+
it 'indicates precompilation' do
|
44
|
+
expect(entry.precompile?).to eq(true)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
File without changes
|
data/spec/log/test.log
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
Compiled application.js (0ms) (pid 95884)
|
2
|
+
Compiled test/generated.js (0ms) (pid 95884)
|
3
|
+
Compiled application.js (0ms) (pid 96212)
|
4
|
+
Compiled test/generated.js (0ms) (pid 96212)
|
5
|
+
Compiled application.js (0ms) (pid 96898)
|
6
|
+
Compiled test/generated.js (0ms) (pid 96898)
|
7
|
+
Compiled application.js (0ms) (pid 1953)
|
8
|
+
Compiled test/generated.js (0ms) (pid 1953)
|
9
|
+
Compiled application.js (0ms) (pid 2286)
|
10
|
+
Compiled test/generated.js (0ms) (pid 2286)
|
11
|
+
Compiled application.js (0ms) (pid 2624)
|
12
|
+
Compiled test/generated.js (0ms) (pid 2624)
|
13
|
+
Compiled application.js (0ms) (pid 2961)
|
14
|
+
Compiled test/generated.js (0ms) (pid 2961)
|
15
|
+
Compiled application.js (0ms) (pid 3629)
|
16
|
+
Compiled test/generated.js (0ms) (pid 3629)
|
17
|
+
Compiled application.js (0ms) (pid 3964)
|
18
|
+
Compiled test/generated.js (0ms) (pid 3964)
|
19
|
+
Compiled application.js (0ms) (pid 4297)
|
20
|
+
Compiled test/generated.js (0ms) (pid 4297)
|
21
|
+
Compiled application.js (0ms) (pid 4632)
|
22
|
+
Compiled test/generated.js (0ms) (pid 4632)
|
23
|
+
Compiled application.js (0ms) (pid 4964)
|
24
|
+
Compiled test/generated.js (0ms) (pid 4964)
|
25
|
+
Compiled application.js (0ms) (pid 6636)
|
26
|
+
Compiled test/generated.js (0ms) (pid 6636)
|
27
|
+
Compiled application.js (0ms) (pid 7347)
|
28
|
+
Compiled test/generated.js (0ms) (pid 7347)
|
29
|
+
Compiled application.js (0ms) (pid 8340)
|
30
|
+
Compiled test/generated.js (0ms) (pid 8340)
|
31
|
+
Compiled application.js (0ms) (pid 8672)
|
32
|
+
Compiled test/generated.js (0ms) (pid 8672)
|
33
|
+
Compiled application.js (0ms) (pid 9007)
|
34
|
+
Compiled test/generated.js (0ms) (pid 9007)
|
35
|
+
Compiled application.js (0ms) (pid 9344)
|
36
|
+
Compiled test/generated.js (0ms) (pid 9344)
|
37
|
+
Compiled application.js (0ms) (pid 9678)
|
38
|
+
Compiled test/generated.js (0ms) (pid 9678)
|
39
|
+
Compiled application.js (0ms) (pid 10012)
|
40
|
+
Compiled test/generated.js (0ms) (pid 10012)
|
41
|
+
Compiled application.js (0ms) (pid 10348)
|
42
|
+
Compiled test/generated.js (0ms) (pid 10348)
|
43
|
+
Compiled application.js (3ms) (pid 10683)
|
44
|
+
Compiled test/generated.js (3ms) (pid 10683)
|
45
|
+
Compiled application.js (3ms) (pid 11019)
|
46
|
+
Compiled application.js (3ms) (pid 11689)
|
47
|
+
Compiled application.js (3ms) (pid 13034)
|
48
|
+
Compiled application.js (0ms) (pid 13395)
|
49
|
+
Compiled application.js (0ms) (pid 13728)
|
50
|
+
Compiled application.js (0ms) (pid 14061)
|
51
|
+
Compiled application.js (0ms) (pid 14396)
|
52
|
+
Compiled application.js (0ms) (pid 14734)
|
53
|
+
Compiled application.js (0ms) (pid 15753)
|
54
|
+
Compiled application.js (0ms) (pid 16114)
|
55
|
+
Compiled application.js (0ms) (pid 16446)
|
56
|
+
Compiled application.js (0ms) (pid 16793)
|
57
|
+
Compiled application.js (0ms) (pid 17124)
|
58
|
+
Compiled application.js (0ms) (pid 18123)
|
59
|
+
Compiled application.js (0ms) (pid 18458)
|
60
|
+
Compiled application.js (0ms) (pid 18804)
|
61
|
+
Compiled application.js (0ms) (pid 24527)
|
62
|
+
Compiled application.js (0ms) (pid 24532)
|
63
|
+
Compiled application.js (0ms) (pid 25284)
|
64
|
+
Compiled application.js (0ms) (pid 25289)
|
65
|
+
Compiled application.js (0ms) (pid 25293)
|
66
|
+
Compiled application.js (0ms) (pid 25665)
|
67
|
+
Compiled application.js (0ms) (pid 25670)
|
68
|
+
Compiled application.js (0ms) (pid 25674)
|
69
|
+
Compiled application.js (0ms) (pid 47444)
|
70
|
+
Compiled application.js (0ms) (pid 47450)
|
71
|
+
Compiled application.js (0ms) (pid 47455)
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
include GeneratedAssets
|
6
|
+
|
7
|
+
describe Manifest do
|
8
|
+
let(:manifest) do
|
9
|
+
Manifest.new(app, tmpdir)
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:assets_config) do
|
13
|
+
app.config.assets
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:logical_path) do
|
17
|
+
'foo/bar.txt'
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#add' do
|
21
|
+
it 'adds a new entry to the manifest' do
|
22
|
+
expect { manifest.add(logical_path) }.to(
|
23
|
+
change { manifest.entries.size }.by(1)
|
24
|
+
)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#apply!' do
|
29
|
+
it 'writes all files' do
|
30
|
+
manifest.add('foo/bar.txt') { "Bar's text" }
|
31
|
+
manifest.add('foo/baz.txt') { "Baz's text" }
|
32
|
+
manifest.apply!
|
33
|
+
|
34
|
+
expect(tmpdir.join('foo/bar.txt').read).to eq("Bar's text")
|
35
|
+
expect(tmpdir.join('foo/baz.txt').read).to eq("Baz's text")
|
36
|
+
end
|
37
|
+
|
38
|
+
it "writes all files and adds those marked to the app's precompile list" do
|
39
|
+
manifest.add('foo/bar.txt') { "Bar's text" }
|
40
|
+
manifest.add('foo/baz.txt', precompile: true) { "Baz's text" }
|
41
|
+
manifest.apply!
|
42
|
+
|
43
|
+
expect(tmpdir.join('foo/bar.txt').read).to eq("Bar's text")
|
44
|
+
expect(tmpdir.join('foo/baz.txt').read).to eq("Baz's text")
|
45
|
+
|
46
|
+
expect(assets_config.precompile).to include('foo/baz.txt')
|
47
|
+
expect(assets_config.precompile).to_not include('foo/bar.txt')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'pathname'
|
5
|
+
|
6
|
+
include GeneratedAssets
|
7
|
+
|
8
|
+
describe 'precompilation' do
|
9
|
+
let(:assets_config) do
|
10
|
+
app.config.assets
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:asset_path) do
|
14
|
+
Pathname(
|
15
|
+
File.join(Rails.public_path, assets_config.prefix)
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'precompiles generated assets correctly' do
|
20
|
+
assets_config.generated.add('foo/bar.txt', precompile: true) do
|
21
|
+
'bar text'
|
22
|
+
end
|
23
|
+
|
24
|
+
assets_config.generated.apply!
|
25
|
+
|
26
|
+
begin
|
27
|
+
Rake::Task['assets:precompile:primary'].invoke
|
28
|
+
Rake::Task['assets:precompile:nondigest'].invoke
|
29
|
+
rescue RuntimeError
|
30
|
+
Rake::Task['assets:precompile'].invoke
|
31
|
+
end
|
32
|
+
|
33
|
+
rails_manifest = RailsManifest.load_for(app)
|
34
|
+
digest_file = rails_manifest.find_by_logical('foo/bar.txt')
|
35
|
+
|
36
|
+
expect(digest_file).to_not be_nil
|
37
|
+
contents = asset_path.join(digest_file).read
|
38
|
+
expect(contents).to eq('bar text')
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'json'
|
5
|
+
require 'securerandom'
|
6
|
+
require 'yaml'
|
7
|
+
|
8
|
+
include GeneratedAssets
|
9
|
+
|
10
|
+
describe RailsManifest do
|
11
|
+
shared_examples 'a compliant manifest' do
|
12
|
+
describe '#find_by_logical' do
|
13
|
+
it 'finds the digested asset for the logical one' do
|
14
|
+
expect(manifest.find_by_logical('myfiles/file1.js')).to(
|
15
|
+
eq('myfiles/file1-abc123.js')
|
16
|
+
)
|
17
|
+
|
18
|
+
expect(manifest.find_by_logical('myfiles/file2.css')).to(
|
19
|
+
eq('myfiles/file2-def456.css')
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'with a json manifest (i.e. Rails 4)' do
|
26
|
+
let(:manifest_hash) do
|
27
|
+
{
|
28
|
+
'files' => {
|
29
|
+
'myfiles/file1-abc123.js' => {
|
30
|
+
'logical_path' => 'myfiles/file1.js'
|
31
|
+
},
|
32
|
+
'myfiles/file2-def456.css' => {
|
33
|
+
'logical_path' => 'myfiles/file2.css'
|
34
|
+
}
|
35
|
+
}
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
let(:manifest_file) do
|
40
|
+
app.root
|
41
|
+
.join('public/assets')
|
42
|
+
.join(".sprockets-manifest-#{SecureRandom.hex}.json")
|
43
|
+
end
|
44
|
+
|
45
|
+
before(:each) do
|
46
|
+
FileUtils.mkdir_p(manifest_file.dirname)
|
47
|
+
File.write(manifest_file, manifest_hash.to_json)
|
48
|
+
end
|
49
|
+
|
50
|
+
let(:manifest) { RailsManifest.load_for(app) }
|
51
|
+
|
52
|
+
describe '.load_for' do
|
53
|
+
it 'finds the json manifest and loads it' do
|
54
|
+
expect(manifest).to be_a(JsonManifest)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it_behaves_like 'a compliant manifest'
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'with a yaml manifest (i.e. rails 3)' do
|
62
|
+
let(:manifest_hash) do
|
63
|
+
{
|
64
|
+
'myfiles/file1.js' => 'myfiles/file1-abc123.js',
|
65
|
+
'myfiles/file2.css' => 'myfiles/file2-def456.css'
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
let(:manifest_file) do
|
70
|
+
app.root.join('public/assets/manifest.yml')
|
71
|
+
end
|
72
|
+
|
73
|
+
before(:each) do
|
74
|
+
FileUtils.mkdir_p(manifest_file.dirname)
|
75
|
+
File.write(manifest_file, manifest_hash.to_json)
|
76
|
+
end
|
77
|
+
|
78
|
+
let(:manifest) { RailsManifest.load_for(app) }
|
79
|
+
|
80
|
+
describe '.load_for' do
|
81
|
+
it 'finds the yaml manifest and loads it' do
|
82
|
+
expect(manifest).to be_a(YamlManifest)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
it_behaves_like 'a compliant manifest'
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
include GeneratedAssets
|
6
|
+
|
7
|
+
describe Railtie do
|
8
|
+
let(:assets_config) do
|
9
|
+
app.config.assets
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'sets up the manifest' do
|
13
|
+
expect(assets_config.generated).to be_a(Manifest)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'adds the generated asset path to the list of asset paths' do
|
17
|
+
expect(assets_config.paths).to include(assets_config.generated.prefix)
|
18
|
+
end
|
19
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
$:.push(File.dirname(__FILE__))
|
4
|
+
|
5
|
+
require 'rspec'
|
6
|
+
require 'fileutils'
|
7
|
+
require 'pathname'
|
8
|
+
require 'tmpdir'
|
9
|
+
require 'pry-nav'
|
10
|
+
|
11
|
+
require 'rails'
|
12
|
+
require 'rake'
|
13
|
+
require 'sprockets/railtie'
|
14
|
+
|
15
|
+
ENV['RAILS_ENV'] ||= 'test'
|
16
|
+
|
17
|
+
require 'generated-assets'
|
18
|
+
|
19
|
+
Dir.chdir('spec') do
|
20
|
+
require File.expand_path('../config/application', __FILE__)
|
21
|
+
GeneratedAssets::DummyApplication.initialize!
|
22
|
+
GeneratedAssets::DummyApplication.load_tasks # used by precompilation specs
|
23
|
+
end
|
24
|
+
|
25
|
+
module LetDeclarations
|
26
|
+
extend RSpec::SharedContext
|
27
|
+
|
28
|
+
let(:tmpdir) do
|
29
|
+
Pathname(Dir.mktmpdir)
|
30
|
+
end
|
31
|
+
|
32
|
+
let(:app) do
|
33
|
+
Rails.application
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
RSpec.configure do |config|
|
38
|
+
config.include(LetDeclarations)
|
39
|
+
|
40
|
+
config.before(:each) do
|
41
|
+
FileUtils.rm_rf(
|
42
|
+
GeneratedAssets::DummyApplication.root.join('tmp').to_s
|
43
|
+
)
|
44
|
+
|
45
|
+
FileUtils.rm_rf(
|
46
|
+
GeneratedAssets::DummyApplication.root.join('public').to_s
|
47
|
+
)
|
48
|
+
|
49
|
+
FileUtils.rm_rf(
|
50
|
+
app.config.assets.generated.prefix
|
51
|
+
)
|
52
|
+
|
53
|
+
app.config.assets.generated.entries.clear
|
54
|
+
end
|
55
|
+
|
56
|
+
config.after(:each) do
|
57
|
+
FileUtils.rm_rf(tmpdir)
|
58
|
+
end
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: generated-assets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cameron Dutro
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.0
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.0
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: sprockets-rails
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: tilt
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: tzinfo
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :runtime
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
description: Programmatically generate assets for the Rails asset pipeline.
|
76
|
+
email:
|
77
|
+
- camertron@gmail.com
|
78
|
+
executables: []
|
79
|
+
extensions: []
|
80
|
+
extra_rdoc_files: []
|
81
|
+
files:
|
82
|
+
- Gemfile
|
83
|
+
- Rakefile
|
84
|
+
- generated-assets.gemspec
|
85
|
+
- lib/generated-assets.rb
|
86
|
+
- lib/generated-assets/entry.rb
|
87
|
+
- lib/generated-assets/manifest.rb
|
88
|
+
- lib/generated-assets/rails_manifest.rb
|
89
|
+
- lib/generated-assets/railtie.rb
|
90
|
+
- lib/generated-assets/version.rb
|
91
|
+
- spec/app/assets/javascripts/application.js
|
92
|
+
- spec/config/application.rb
|
93
|
+
- spec/config/i18n-js.yml
|
94
|
+
- spec/config/locales/en.yml
|
95
|
+
- spec/config/locales/es.yml
|
96
|
+
- spec/entry_spec.rb
|
97
|
+
- spec/log/development.log
|
98
|
+
- spec/log/test.log
|
99
|
+
- spec/manifest_spec.rb
|
100
|
+
- spec/precomp_spec.rb
|
101
|
+
- spec/rails_manifest_spec.rb
|
102
|
+
- spec/railtie_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
homepage: http://github.com/camertron
|
105
|
+
licenses: []
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.2.3
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: Programmatically generate assets for the Rails asset pipeline.
|
127
|
+
test_files: []
|