dassets 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +42 -10
- data/lib/dassets.rb +16 -28
- data/lib/dassets/asset_file.rb +36 -33
- data/lib/dassets/default_cache.rb +29 -0
- data/lib/dassets/digest_cmd.rb +36 -0
- data/lib/dassets/engine.rb +0 -2
- data/lib/dassets/file_store.rb +36 -0
- data/lib/dassets/runner.rb +2 -9
- data/lib/dassets/server/request.rb +5 -5
- data/lib/dassets/source_cache.rb +39 -0
- data/lib/dassets/source_file.rb +37 -13
- data/lib/dassets/version.rb +1 -1
- data/test/support/config/assets.rb +1 -0
- data/test/system/digest_cmd_run_tests.rb +37 -39
- data/test/system/rack_tests.rb +3 -7
- data/test/unit/asset_file_tests.rb +72 -40
- data/test/unit/config_tests.rb +3 -17
- data/test/unit/dassets_tests.rb +8 -42
- data/test/unit/default_cache_tests.rb +27 -0
- data/test/unit/{cmds/digest_cmd_tests.rb → digest_cmd_tests.rb} +4 -4
- data/test/unit/file_store_tests.rb +30 -0
- data/test/unit/server/request_tests.rb +7 -11
- data/test/unit/server/response_tests.rb +4 -5
- data/test/unit/source_cache_tests.rb +50 -0
- data/test/unit/source_file_tests.rb +28 -29
- metadata +16 -31
- data/lib/dassets/cmds/cache_cmd.rb +0 -33
- data/lib/dassets/cmds/digest_cmd.rb +0 -53
- data/lib/dassets/digests.rb +0 -61
- data/test/support/app/assets/.digests +0 -5
- data/test/support/app/assets/public/file1.txt +0 -1
- data/test/support/app/assets/public/file2.txt +0 -1
- data/test/support/app/assets/public/grumpy_cat.jpg +0 -0
- data/test/support/app/assets/public/nested/a-thing.txt.no-use +0 -4
- data/test/support/app/assets/public/nested/file3.txt +0 -0
- data/test/support/app_public/.gitkeep +0 -0
- data/test/support/example.digests +0 -3
- data/test/system/cache_cmd_run_tests.rb +0 -27
- data/test/unit/cmds/cache_cmd_tests.rb +0 -33
- data/test/unit/digests_tests.rb +0 -79
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'fileutils'
|
2
|
-
require 'dassets'
|
3
|
-
require 'dassets/source_file'
|
4
|
-
|
5
|
-
module Dassets; end
|
6
|
-
class Dassets::Cmds; end
|
7
|
-
class Dassets::Cmds::DigestCmd
|
8
|
-
|
9
|
-
attr_reader :paths
|
10
|
-
|
11
|
-
def initialize(abs_paths)
|
12
|
-
@paths = abs_paths || []
|
13
|
-
end
|
14
|
-
|
15
|
-
def run(io=nil)
|
16
|
-
files = paths
|
17
|
-
|
18
|
-
if @paths.empty?
|
19
|
-
log io, "clearing `#{Dassets.config.output_path}`"
|
20
|
-
clear_output_path(Dassets.config.output_path)
|
21
|
-
|
22
|
-
# always clear the digests in use
|
23
|
-
log io, "clearing `#{Dassets.digests.file_path}`"
|
24
|
-
clear_digests(Dassets.digests)
|
25
|
-
|
26
|
-
# always get the latest source list
|
27
|
-
files = Dassets::SourceList.new(Dassets.config)
|
28
|
-
end
|
29
|
-
|
30
|
-
log io, "digesting #{files.count} source file(s) ..."
|
31
|
-
digest_the_files(files)
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def clear_output_path(path)
|
37
|
-
Dir.glob(File.join(path, '*')).each{ |p| FileUtils.rm_r(p) } if path
|
38
|
-
end
|
39
|
-
|
40
|
-
def clear_digests(digests)
|
41
|
-
digests.clear.save! if digests
|
42
|
-
end
|
43
|
-
|
44
|
-
def digest_the_files(files)
|
45
|
-
files.map{ |f| Dassets::SourceFile.new(f).digest }
|
46
|
-
end
|
47
|
-
|
48
|
-
def log(io, msg)
|
49
|
-
io.puts msg if io
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|
53
|
-
|
data/lib/dassets/digests.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
require 'dassets/asset_file'
|
2
|
-
|
3
|
-
module Dassets
|
4
|
-
|
5
|
-
class Digests
|
6
|
-
|
7
|
-
attr_reader :file_path
|
8
|
-
|
9
|
-
def initialize(file_path)
|
10
|
-
@file_path, @hash = file_path, decode(file_path)
|
11
|
-
end
|
12
|
-
|
13
|
-
def [](*args); @hash.send('[]', *args); end
|
14
|
-
def []=(*args); @hash.send('[]=', *args); end
|
15
|
-
def delete(*args); @hash.delete(*args); end
|
16
|
-
def clear(*args); @hash.clear(*args); self end
|
17
|
-
|
18
|
-
def paths
|
19
|
-
@hash.keys
|
20
|
-
end
|
21
|
-
|
22
|
-
def asset_files
|
23
|
-
self.paths.map{ |path| self.asset_file(path) }
|
24
|
-
end
|
25
|
-
|
26
|
-
def asset_file(path)
|
27
|
-
Dassets::AssetFile.new(path, @hash[path] || '')
|
28
|
-
end
|
29
|
-
|
30
|
-
def save!
|
31
|
-
encode(@hash, @file_path)
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def decode(file_path)
|
37
|
-
Hash.new.tap do |h|
|
38
|
-
if File.exists?(file_path)
|
39
|
-
File.open(file_path, 'r').each_line do |l|
|
40
|
-
path, md5 = l.split(','); path ||= ''; path.strip!; md5 ||= ''; md5.strip!
|
41
|
-
h[path] = md5 if !path.empty?
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def encode(hash, file_path)
|
48
|
-
File.open(file_path, 'w') do |f|
|
49
|
-
hash.keys.sort.each{ |path| f.write("#{path.strip},#{hash[path].strip}\n") }
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
module NullDigests
|
56
|
-
def self.new
|
57
|
-
Digests.new('/dev/null')
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
end
|
@@ -1 +0,0 @@
|
|
1
|
-
file1.txt
|
@@ -1 +0,0 @@
|
|
1
|
-
file2.txt
|
Binary file
|
File without changes
|
File without changes
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'assert'
|
2
|
-
require 'fileutils'
|
3
|
-
require 'dassets'
|
4
|
-
|
5
|
-
module Dassets
|
6
|
-
|
7
|
-
class CacheCmdRunTests < Assert::Context
|
8
|
-
desc "the CacheCmd"
|
9
|
-
setup do
|
10
|
-
@cache_root_path = File.join(Dassets.config.root_path, 'public')
|
11
|
-
FileUtils.rm_rf(@cache_root_path)
|
12
|
-
end
|
13
|
-
|
14
|
-
should "create the cache root and write the cache files" do
|
15
|
-
assert_not_file_exists @cache_root_path.to_s
|
16
|
-
cmd = Dassets::Cmds::CacheCmd.new(@cache_root_path)
|
17
|
-
cmd.run
|
18
|
-
|
19
|
-
assert_file_exists @cache_root_path.to_s
|
20
|
-
cmd.digests.asset_files.each do |file|
|
21
|
-
assert_file_exists File.join(@cache_root_path, file.url)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'assert'
|
2
|
-
require 'fileutils'
|
3
|
-
require 'dassets/cmds/cache_cmd'
|
4
|
-
|
5
|
-
class Dassets::Cmds::CacheCmd
|
6
|
-
|
7
|
-
class BaseTests < Assert::Context
|
8
|
-
desc "Dassets::Cmds::CacheCmd"
|
9
|
-
setup do
|
10
|
-
@cache_root_path = File.join(Dassets.config.root_path, 'public')
|
11
|
-
FileUtils.mkdir_p @cache_root_path
|
12
|
-
@cmd = Dassets::Cmds::CacheCmd.new(@cache_root_path)
|
13
|
-
end
|
14
|
-
subject{ @cmd }
|
15
|
-
|
16
|
-
should have_readers :cache_root_path, :digests
|
17
|
-
|
18
|
-
should "know its given cache root path" do
|
19
|
-
assert_equal @cache_root_path, subject.cache_root_path.to_s
|
20
|
-
end
|
21
|
-
|
22
|
-
should "know it's digests file" do
|
23
|
-
assert_kind_of Dassets::Digests, subject.digests
|
24
|
-
end
|
25
|
-
|
26
|
-
should "get it's asset files from the digests file" do
|
27
|
-
assert_equal 5, subject.digests.paths.size
|
28
|
-
assert_equal 5, subject.digests.asset_files.size
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
data/test/unit/digests_tests.rb
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
require 'assert'
|
2
|
-
require 'fileutils'
|
3
|
-
require 'dassets/digests'
|
4
|
-
require 'dassets/asset_file'
|
5
|
-
|
6
|
-
class Dassets::Digests
|
7
|
-
|
8
|
-
class BaseTests < Assert::Context
|
9
|
-
desc "Dassets::Digests"
|
10
|
-
setup do
|
11
|
-
@file_path = File.join(Dassets.config.root_path, 'example.digests')
|
12
|
-
@digests = Dassets::Digests.new(@file_path)
|
13
|
-
end
|
14
|
-
subject{ @digests }
|
15
|
-
|
16
|
-
should have_reader :file_path
|
17
|
-
should have_imeths :[], :[]=, :delete, :clear
|
18
|
-
should have_imeths :paths, :asset_files, :asset_file, :save!
|
19
|
-
|
20
|
-
should "know its file path" do
|
21
|
-
assert_equal @file_path, subject.file_path
|
22
|
-
end
|
23
|
-
|
24
|
-
should "know its asset files" do
|
25
|
-
assert_equal subject.paths.size, subject.asset_files.size
|
26
|
-
assert_kind_of Dassets::AssetFile, subject.asset_files.first
|
27
|
-
end
|
28
|
-
|
29
|
-
should "get a specific asset file from its data" do
|
30
|
-
file = subject.asset_file('path/to/file1')
|
31
|
-
|
32
|
-
assert_kind_of Dassets::AssetFile, file
|
33
|
-
assert_equal 'path/to/file1', file.path
|
34
|
-
assert_equal subject['path/to/file1'], file.md5
|
35
|
-
end
|
36
|
-
|
37
|
-
should "read values with the index operator" do
|
38
|
-
assert_equal 'abc123', subject['path/to/file1']
|
39
|
-
end
|
40
|
-
|
41
|
-
should "write values with the index operator" do
|
42
|
-
subject['path/to/test'] = 'testytest'
|
43
|
-
assert_equal 'testytest', subject['path/to/test']
|
44
|
-
end
|
45
|
-
|
46
|
-
should "remove values with the delete method" do
|
47
|
-
assert_includes 'path/to/file1', subject.paths
|
48
|
-
|
49
|
-
subject.delete 'path/to/file1'
|
50
|
-
assert_not_includes 'path/to/file1', subject.paths
|
51
|
-
end
|
52
|
-
|
53
|
-
should "clear values with the clear method" do
|
54
|
-
assert_not_empty subject.paths
|
55
|
-
subject.clear
|
56
|
-
assert_empty subject.paths
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
class SaveTests < BaseTests
|
62
|
-
desc "on save"
|
63
|
-
setup do
|
64
|
-
FileUtils.mv(@file_path, "#{@file_path}.bak")
|
65
|
-
end
|
66
|
-
teardown do
|
67
|
-
FileUtils.mv("#{@file_path}.bak", @file_path)
|
68
|
-
end
|
69
|
-
|
70
|
-
should "write out the digests to the file path" do
|
71
|
-
assert_not_file_exists subject.file_path
|
72
|
-
subject.save!
|
73
|
-
|
74
|
-
assert_file_exists subject.file_path
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
end
|