dassets 0.14.2 → 0.15.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -7
- data/Gemfile +5 -1
- data/README.md +15 -17
- data/dassets.gemspec +14 -9
- data/lib/dassets.rb +51 -13
- data/lib/dassets/asset_file.rb +27 -24
- data/lib/dassets/cache.rb +27 -33
- data/lib/dassets/config.rb +55 -50
- data/lib/dassets/engine.rb +11 -23
- data/lib/dassets/file_store.rb +27 -27
- data/lib/dassets/server.rb +27 -27
- data/lib/dassets/server/request.rb +44 -41
- data/lib/dassets/server/response.rb +101 -80
- data/lib/dassets/source.rb +15 -9
- data/lib/dassets/source_file.rb +103 -82
- data/lib/dassets/source_proxy.rb +36 -20
- data/lib/dassets/version.rb +3 -1
- data/test/helper.rb +31 -25
- data/test/support/app.rb +5 -5
- data/test/support/empty/{.gitkeep → .keep} +0 -0
- data/test/support/factory.rb +3 -2
- data/test/support/{public/nested/file3-d41d8cd98f00b204e9800998ecf8427e.txt → linked_source_files/linked_file.txt} +0 -0
- data/test/support/source_files/linked +1 -0
- data/test/support/source_files/linked_file2.txt +1 -0
- data/test/system/rack_tests.rb +65 -61
- data/test/unit/asset_file_tests.rb +69 -61
- data/test/unit/cache_tests.rb +15 -34
- data/test/unit/config_tests.rb +59 -52
- data/test/unit/dassets_tests.rb +31 -24
- data/test/unit/engine_tests.rb +9 -43
- data/test/unit/file_store_tests.rb +44 -31
- data/test/unit/server/request_tests.rb +57 -59
- data/test/unit/server/response_tests.rb +82 -82
- data/test/unit/server_tests.rb +5 -9
- data/test/unit/source_file_tests.rb +80 -73
- data/test/unit/source_proxy_tests.rb +84 -90
- data/test/unit/source_tests.rb +66 -50
- data/tmp/.gitkeep +0 -0
- metadata +92 -72
- data/.gitignore +0 -19
- data/test/support/public/file2-9bbe1047cffbb590f59e0e5aeff46ae4.txt +0 -1
- data/test/support/public/grumpy_cat-b0d1f399a916f7a25c4c0f693c619013.jpg +0 -0
- data/test/support/public/nested/a-thing.txt-7413d18f2eba9c695a880aff67fde135.no-use +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
---
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
SHA512:
|
6
|
-
|
7
|
-
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8d2158827f5cdbb5f779c9c3647a0f358bec9bffa62f5fefbf8b2b83f654d21a
|
4
|
+
data.tar.gz: 84e0a6194cb0e506431750155dea71e3cc7cc0c62ac68a1ce8155454a5348f84
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b05667686c621b41c5cd0bd80242a58b74f58da3867180b039130e1898b7fcd8f49ee010c3007ad49e19d3cc748aa363c4f90170be6132797ddfb7c522e8a05b
|
7
|
+
data.tar.gz: 1827b4f16b9f9d786306c8aef467a63b040830931b3481affe7af2954e7df11ebe5557cabe016b4d1ed0375923e18b9aa8c7906d8e55dae859f205d9e9bb2c34
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -10,27 +10,25 @@ You have some css, js, images, etc files. You want to update, deploy, and serve
|
|
10
10
|
|
11
11
|
```ruby
|
12
12
|
# in config/dassets.rb
|
13
|
-
require
|
13
|
+
require "dassets"
|
14
14
|
|
15
15
|
Dassets.configure do |c|
|
16
|
-
|
17
16
|
# tell Dassets where to look for source files
|
18
|
-
c.source
|
17
|
+
c.source "/path/to/app/assets"
|
19
18
|
|
20
19
|
# (optional) tell Dassets where to store digested asset files
|
21
20
|
# if none given, Dassets will not write any digested output
|
22
21
|
# use this to "cache" digested assets to the public dir so that
|
23
22
|
# your web server can serve them directly
|
24
|
-
c.file_store
|
25
|
-
|
23
|
+
c.file_store "/path/to/public" # default: `Dassets::NullFileStore.new`
|
26
24
|
end
|
27
25
|
```
|
28
26
|
|
29
27
|
### Link To
|
30
28
|
|
31
|
-
```
|
32
|
-
Dassets[
|
33
|
-
Dassets[
|
29
|
+
```ruby
|
30
|
+
Dassets["css/site.css"].url # => "/css/site-123abc.css"
|
31
|
+
Dassets["img/logos/main.jpg"].url # => "/img/logos/main-a1b2c3.jpg"
|
34
32
|
```
|
35
33
|
|
36
34
|
### Serve
|
@@ -39,7 +37,7 @@ Use the Dassets middleware to serve your digested asset files:
|
|
39
37
|
|
40
38
|
```ruby
|
41
39
|
# `app` is a rack application
|
42
|
-
require
|
40
|
+
require "dassets/server"
|
43
41
|
app.use Dassets::Server
|
44
42
|
```
|
45
43
|
|
@@ -75,16 +73,16 @@ Dassets.configure do |c|
|
|
75
73
|
c.source /path/to/assets do |s|
|
76
74
|
s.filter{ |paths| paths.reject{ |p| File.basename(p) =~ /^_/ } }
|
77
75
|
|
78
|
-
s.engine
|
79
|
-
s.engine
|
80
|
-
|
76
|
+
s.engine "erb", Dassets::Erb::Engine
|
77
|
+
s.engine "scss", Dassets::Sass::Engine, {
|
78
|
+
"syntax" => "scss",
|
81
79
|
# any other engine-specific options here
|
82
80
|
}
|
83
81
|
end
|
84
82
|
end
|
85
83
|
```
|
86
84
|
|
87
|
-
This configuration says that Dassets, for assets in `/path/to/assets`, should 1) ignore any files beginning in `_` 2) process any files ending in `.erb` with the Erb engine and 3) process any files ending in `.scss` with the Sass engine (using
|
85
|
+
This configuration says that Dassets, for assets in `/path/to/assets`, should 1) ignore any files beginning in `_` 2) process any files ending in `.erb` with the Erb engine and 3) process any files ending in `.scss` with the Sass engine (using scss syntax).
|
88
86
|
|
89
87
|
The goal here is to allow you to control how certain asset files are handled based on their location root. This is handy for 3rd-paty gems that provide asset source files (such as [Romo](https://github.com/redding/romo)). See https://github.com/redding/romo/blob/master/lib/romo/dassets.rb for an example of how Romo integrates with Dassets.
|
90
88
|
|
@@ -95,9 +93,9 @@ Combinations are a way to alias many asset files as a single asset. Dassets res
|
|
95
93
|
```ruby
|
96
94
|
Dassets.configure do |c|
|
97
95
|
c.combination "css/special.css", [
|
98
|
-
|
99
|
-
|
100
|
-
|
96
|
+
"css/romo/normalize.css",
|
97
|
+
"css/romo/base.css",
|
98
|
+
"css/romo/component1.css",
|
101
99
|
]
|
102
100
|
end
|
103
101
|
```
|
@@ -110,7 +108,7 @@ Combinations are treated just like regular asset files (think of them as a kind
|
|
110
108
|
|
111
109
|
Add this line to your application's Gemfile:
|
112
110
|
|
113
|
-
gem
|
111
|
+
gem "dassets"
|
114
112
|
|
115
113
|
And then execute:
|
116
114
|
|
data/dassets.gemspec
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
5
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
6
|
require "dassets/version"
|
5
7
|
|
@@ -8,20 +10,23 @@ Gem::Specification.new do |gem|
|
|
8
10
|
gem.version = Dassets::VERSION
|
9
11
|
gem.authors = ["Kelly Redding", "Collin Redding"]
|
10
12
|
gem.email = ["kelly@kellyredding.com", "collin.redding@me.com"]
|
11
|
-
gem.summary =
|
12
|
-
gem.description =
|
13
|
+
gem.summary = "Digested asset files"
|
14
|
+
gem.description = "Digest and serve HTML asset files"
|
13
15
|
gem.homepage = "http://github.com/redding/dassets"
|
14
|
-
gem.license =
|
16
|
+
gem.license = "MIT"
|
17
|
+
|
18
|
+
gem.files = `git ls-files | grep "^[^.]"`.split($INPUT_RECORD_SEPARATOR)
|
15
19
|
|
16
|
-
gem.files = `git ls-files`.split($/)
|
17
20
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
21
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
22
|
gem.require_paths = ["lib"]
|
20
23
|
|
21
|
-
gem.
|
22
|
-
gem.add_development_dependency('assert-rack-test', ["~> 1.0.4"])
|
23
|
-
gem.add_development_dependency("sinatra", ["~> 1.4"])
|
24
|
+
gem.required_ruby_version = "~> 2.5"
|
24
25
|
|
25
|
-
gem.
|
26
|
+
gem.add_development_dependency("much-style-guide", ["~> 0.6.0"])
|
27
|
+
gem.add_development_dependency("assert", ["~> 2.19.3"])
|
28
|
+
gem.add_development_dependency("assert-rack-test", ["~> 1.1.1"])
|
29
|
+
gem.add_development_dependency("sinatra", ["~> 2.1"])
|
26
30
|
|
31
|
+
gem.add_dependency("rack", ["~> 2.1"])
|
27
32
|
end
|
data/lib/dassets.rb
CHANGED
@@ -1,35 +1,75 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dassets/version"
|
4
|
+
require "dassets/asset_file"
|
5
|
+
require "dassets/config"
|
6
|
+
require "dassets/source_file"
|
5
7
|
|
6
8
|
module Dassets
|
9
|
+
AssetFileError = Class.new(RuntimeError)
|
10
|
+
|
11
|
+
def self.config
|
12
|
+
@config ||= Config.new
|
13
|
+
end
|
7
14
|
|
8
|
-
def self.config; @config ||= Config.new; end
|
9
15
|
def self.configure(&block)
|
10
|
-
block.call(
|
16
|
+
block.call(config)
|
11
17
|
end
|
12
18
|
|
13
19
|
def self.init
|
14
|
-
@asset_files
|
15
|
-
@source_files
|
20
|
+
@asset_files ||= {}
|
21
|
+
@source_files = SourceFiles.new(config.sources)
|
16
22
|
end
|
17
23
|
|
18
24
|
def self.reset
|
19
25
|
@asset_files = {}
|
20
|
-
|
26
|
+
config.reset
|
21
27
|
end
|
22
28
|
|
23
|
-
def self.
|
29
|
+
def self.asset_file(digest_path)
|
24
30
|
@asset_files[digest_path] ||= AssetFile.new(digest_path)
|
25
31
|
end
|
26
32
|
|
33
|
+
def self.[](digest_path)
|
34
|
+
asset_file(digest_path).tap do |af|
|
35
|
+
if af.fingerprint.nil?
|
36
|
+
msg =
|
37
|
+
+"error digesting `#{digest_path}`.\n\nMake sure Dassets has " \
|
38
|
+
"either a combination or source file for this digest path. If " \
|
39
|
+
"this path is for a combination, make sure Dassets has either " \
|
40
|
+
"a combination or source file for each digest path of the " \
|
41
|
+
"combination.\n\n"
|
42
|
+
|
43
|
+
msg << "\nCombination digest paths:"
|
44
|
+
msg << (Dassets.combinations.keys.empty? ? " (none)\n\n" : "\n\n")
|
45
|
+
Dassets.combinations.keys.sort.each do |key|
|
46
|
+
bullet = "#{key} => "
|
47
|
+
values = Dassets.combinations[key].sort
|
48
|
+
msg << (
|
49
|
+
["#{bullet}#{values.first}"] +
|
50
|
+
(values[1..-1] || []).map{ |v| "#{" " * bullet.size}#{v}" }
|
51
|
+
).join("\n")
|
52
|
+
msg << "\n\n"
|
53
|
+
end
|
54
|
+
|
55
|
+
msg << "\nSource file digest paths:"
|
56
|
+
msg << (Dassets.source_files.keys.empty? ? " (none)\n\n" : "\n\n")
|
57
|
+
msg << Dassets.source_files.keys.sort.join("\n")
|
58
|
+
|
59
|
+
raise AssetFileError, msg
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
27
64
|
def self.source_files
|
28
65
|
@source_files
|
29
66
|
end
|
30
67
|
|
31
|
-
|
68
|
+
def self.combinations
|
69
|
+
config.combinations
|
70
|
+
end
|
32
71
|
|
72
|
+
module SourceFiles
|
33
73
|
def self.new(sources)
|
34
74
|
# use a hash to store the source files so in the case two source files
|
35
75
|
# have the same digest path, the last one *should* be correct since it
|
@@ -42,9 +82,7 @@ module Dassets
|
|
42
82
|
hash
|
43
83
|
end
|
44
84
|
end
|
45
|
-
|
46
85
|
end
|
47
|
-
|
48
86
|
end
|
49
87
|
|
50
88
|
Dassets.init
|
data/lib/dassets/asset_file.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dassets/source_proxy"
|
4
|
+
require "rack/utils"
|
5
|
+
require "rack/mime"
|
4
6
|
|
5
7
|
module Dassets; end
|
6
|
-
class Dassets::AssetFile
|
7
8
|
|
9
|
+
class Dassets::AssetFile
|
8
10
|
attr_reader :digest_path, :dirname, :extname, :basename, :source_proxy
|
9
11
|
|
10
12
|
def initialize(digest_path)
|
@@ -12,47 +14,49 @@ class Dassets::AssetFile
|
|
12
14
|
@dirname = File.dirname(@digest_path)
|
13
15
|
@extname = File.extname(@digest_path)
|
14
16
|
@basename = File.basename(@digest_path, @extname)
|
15
|
-
@source_proxy =
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
@source_proxy =
|
18
|
+
Dassets::SourceProxy.new(
|
19
|
+
@digest_path,
|
20
|
+
content_cache: Dassets.config.content_cache,
|
21
|
+
fingerprint_cache: Dassets.config.fingerprint_cache,
|
22
|
+
)
|
19
23
|
end
|
20
24
|
|
21
25
|
def digest!
|
22
|
-
return
|
23
|
-
Dassets.config.file_store.save(
|
26
|
+
return unless exists?
|
27
|
+
Dassets.config.file_store.save(url){ content }
|
24
28
|
end
|
25
29
|
|
26
30
|
def url
|
27
|
-
path_basename = "#{@basename}-#{
|
28
|
-
path =
|
31
|
+
path_basename = "#{@basename}-#{fingerprint}#{@extname}"
|
32
|
+
path =
|
33
|
+
File.join(@dirname, path_basename).sub(%r{^\./}, "").sub(%r{^/}, "")
|
29
34
|
"#{dassets_base_url}/#{path}"
|
30
35
|
end
|
31
|
-
|
32
36
|
alias_method :href, :url
|
33
37
|
|
34
38
|
def fingerprint
|
35
|
-
return nil
|
39
|
+
return nil unless exists?
|
36
40
|
@source_proxy.fingerprint
|
37
41
|
end
|
38
42
|
|
39
43
|
def content
|
40
|
-
return nil
|
44
|
+
return nil unless exists?
|
41
45
|
@source_proxy.content
|
42
46
|
end
|
43
47
|
|
44
48
|
def mtime
|
45
|
-
return nil
|
49
|
+
return nil unless exists?
|
46
50
|
@source_proxy.mtime.httpdate
|
47
51
|
end
|
48
52
|
|
49
53
|
def size
|
50
|
-
return nil
|
51
|
-
|
54
|
+
return nil unless exists?
|
55
|
+
content.bytesize
|
52
56
|
end
|
53
57
|
|
54
58
|
def mime_type
|
55
|
-
return nil
|
59
|
+
return nil unless exists?
|
56
60
|
Rack::Mime.mime_type(@extname)
|
57
61
|
end
|
58
62
|
|
@@ -64,10 +68,10 @@ class Dassets::AssetFile
|
|
64
68
|
@source_proxy.exists?
|
65
69
|
end
|
66
70
|
|
67
|
-
def ==(
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
+
def ==(other)
|
72
|
+
other.is_a?(Dassets::AssetFile) &&
|
73
|
+
digest_path == other.digest_path &&
|
74
|
+
fingerprint == other.fingerprint
|
71
75
|
end
|
72
76
|
|
73
77
|
private
|
@@ -75,5 +79,4 @@ class Dassets::AssetFile
|
|
75
79
|
def dassets_base_url
|
76
80
|
Dassets.config.base_url.to_s
|
77
81
|
end
|
78
|
-
|
79
82
|
end
|
data/lib/dassets/cache.rb
CHANGED
@@ -1,45 +1,39 @@
|
|
1
|
-
|
2
|
-
module Dassets::Cache
|
3
|
-
|
4
|
-
class MemCache
|
5
|
-
require 'thread'
|
6
|
-
|
7
|
-
# this is a thread-safe in-memory cache
|
8
|
-
|
9
|
-
def initialize
|
10
|
-
@hash = {}
|
11
|
-
@write_mutex = ::Mutex.new
|
12
|
-
end
|
1
|
+
# frozen_string_literal: true
|
13
2
|
|
14
|
-
|
15
|
-
@hash.keys
|
16
|
-
end
|
3
|
+
require "thread"
|
17
4
|
|
18
|
-
|
19
|
-
@hash[key]
|
20
|
-
end
|
21
|
-
|
22
|
-
def []=(key, value)
|
23
|
-
@write_mutex.synchronize{ @hash[key] = value }
|
24
|
-
end
|
5
|
+
module Dassets; end
|
25
6
|
|
7
|
+
# This is a thread-safe in-memory cache.
|
8
|
+
class Dassets::MemCache
|
9
|
+
def initialize
|
10
|
+
@hash = {}
|
11
|
+
@write_mutex = ::Mutex.new
|
26
12
|
end
|
27
13
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
# off caching.
|
14
|
+
def keys
|
15
|
+
@hash.keys
|
16
|
+
end
|
32
17
|
|
33
|
-
|
34
|
-
|
35
|
-
|
18
|
+
def [](key)
|
19
|
+
@hash[key]
|
20
|
+
end
|
36
21
|
|
37
|
-
|
38
|
-
|
22
|
+
def []=(key, value)
|
23
|
+
@write_mutex.synchronize{ @hash[key] = value }
|
24
|
+
end
|
25
|
+
end
|
39
26
|
|
40
|
-
|
41
|
-
|
27
|
+
# This is a no-op cache object. This is the default cache in use and "turns
|
28
|
+
# off caching.
|
29
|
+
class Dassets::NoCache
|
30
|
+
def keys
|
31
|
+
[]
|
32
|
+
end
|
42
33
|
|
34
|
+
def [](key)
|
43
35
|
end
|
44
36
|
|
37
|
+
def []=(key, value)
|
38
|
+
end
|
45
39
|
end
|
data/lib/dassets/config.rb
CHANGED
@@ -1,67 +1,72 @@
|
|
1
|
-
|
2
|
-
require 'dassets/cache'
|
3
|
-
require 'dassets/file_store'
|
4
|
-
require 'dassets/source'
|
1
|
+
# frozen_string_literal: true
|
5
2
|
|
6
|
-
|
3
|
+
require "pathname"
|
4
|
+
require "dassets/cache"
|
5
|
+
require "dassets/file_store"
|
6
|
+
require "dassets/source"
|
7
7
|
|
8
|
-
|
8
|
+
module Dassets; end
|
9
9
|
|
10
|
-
|
10
|
+
class Dassets::Config
|
11
|
+
attr_reader :sources, :combinations
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
@content_cache = Dassets::Cache::NoCache.new
|
16
|
-
@fingerprint_cache = Dassets::Cache::NoCache.new
|
17
|
-
@file_store = FileStore::NullStore.new
|
18
|
-
end
|
19
|
-
|
20
|
-
def reset
|
21
|
-
@sources = []
|
22
|
-
@combinations = Hash.new{ |h, k| [k] } # digest pass-thru if none defined
|
23
|
-
end
|
13
|
+
def initialize
|
14
|
+
super
|
15
|
+
reset
|
24
16
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
17
|
+
@content_cache = Dassets::NoCache.new
|
18
|
+
@fingerprint_cache = Dassets::NoCache.new
|
19
|
+
@file_store = Dassets::NullFileStore.new
|
20
|
+
end
|
29
21
|
|
30
|
-
|
31
|
-
|
32
|
-
|
22
|
+
def reset
|
23
|
+
@sources = []
|
24
|
+
@combinations = Hash.new{ |_h, k| [k] } # digest pass-thru if none defined
|
25
|
+
@file_store = Dassets::NullFileStore.new
|
26
|
+
end
|
33
27
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
@file_store
|
39
|
-
end
|
28
|
+
def base_url(value = nil)
|
29
|
+
set_base_url(value) unless value.nil?
|
30
|
+
@base_url
|
31
|
+
end
|
40
32
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
33
|
+
def set_base_url(value)
|
34
|
+
@base_url = value
|
35
|
+
end
|
45
36
|
|
46
|
-
|
47
|
-
|
48
|
-
@
|
37
|
+
def file_store(value = nil)
|
38
|
+
unless value.nil?
|
39
|
+
@file_store =
|
40
|
+
if value.is_a?(Dassets::FileStore)
|
41
|
+
value
|
42
|
+
else
|
43
|
+
Dassets::FileStore.new(value)
|
44
|
+
end
|
49
45
|
end
|
46
|
+
@file_store
|
47
|
+
end
|
50
48
|
|
51
|
-
|
52
|
-
|
53
|
-
|
49
|
+
def content_cache(cache = nil)
|
50
|
+
@content_cache = cache unless cache.nil?
|
51
|
+
@content_cache
|
52
|
+
end
|
54
53
|
|
55
|
-
|
56
|
-
|
57
|
-
|
54
|
+
def fingerprint_cache(cache = nil)
|
55
|
+
@fingerprint_cache = cache unless cache.nil?
|
56
|
+
@fingerprint_cache
|
57
|
+
end
|
58
58
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
@combinations[key_digest_path.to_s] != [key_digest_path]
|
63
|
-
end
|
59
|
+
def source(path, &block)
|
60
|
+
@sources << Dassets::Source.new(path).tap{ |s| block.call(s) if block }
|
61
|
+
end
|
64
62
|
|
63
|
+
def combination(key_digest_path, value_digest_paths)
|
64
|
+
@combinations[key_digest_path.to_s] = [*value_digest_paths]
|
65
65
|
end
|
66
66
|
|
67
|
+
def combination?(key_digest_path)
|
68
|
+
# a digest path is only considered a combination is it is not the default
|
69
|
+
# pass-thru above
|
70
|
+
@combinations[key_digest_path.to_s] != [key_digest_path]
|
71
|
+
end
|
67
72
|
end
|