cdn_fu 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cdn_fu/config.rb +27 -9
- data/lib/cdn_fu/file_info.rb +6 -2
- data/lib/cdn_fu/lister.rb +1 -0
- data/lib/cdn_fu/minifiers/yui_minifier.rb +7 -4
- data/lib/cdn_fu/uploaders/cloudfront_uploader.rb +19 -15
- data/lib/cdn_fu/version.rb +1 -1
- metadata +3 -3
data/lib/cdn_fu/config.rb
CHANGED
@@ -27,6 +27,11 @@ module CdnFu
|
|
27
27
|
@asset_id = args[0]
|
28
28
|
end
|
29
29
|
|
30
|
+
def asset_host(*args)
|
31
|
+
return @asset_host if args.size == 0
|
32
|
+
@asset_host = args[0]
|
33
|
+
end
|
34
|
+
|
30
35
|
def verbose(*args)
|
31
36
|
return @verbose if args.size == 0
|
32
37
|
@verbose = args[0]
|
@@ -52,15 +57,17 @@ module CdnFu
|
|
52
57
|
@tmp_dir ||= "/tmp"
|
53
58
|
FileUtils.mkdir_p(@tmp_dir)
|
54
59
|
|
60
|
+
|
61
|
+
file_list = @lister.list
|
62
|
+
|
63
|
+
puts "Here"
|
55
64
|
case @preprocessor
|
56
65
|
when Proc
|
57
66
|
@preprocessor.call
|
58
|
-
|
59
|
-
@preprocessor.preprocess
|
67
|
+
else
|
68
|
+
@preprocessor.preprocess(file_list)
|
60
69
|
end
|
61
70
|
|
62
|
-
file_list = @lister.list
|
63
|
-
|
64
71
|
case @minifier
|
65
72
|
when Proc
|
66
73
|
@minifier.call(file_list)
|
@@ -76,17 +83,28 @@ module CdnFu
|
|
76
83
|
end
|
77
84
|
end
|
78
85
|
|
79
|
-
def
|
86
|
+
def setup(&block)
|
87
|
+
if block_given?
|
88
|
+
@setup_task = block
|
89
|
+
else
|
90
|
+
raise ConfigError,"No setup block given"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def preprocess(&block)
|
80
95
|
if block_given?
|
81
|
-
@preprocessor =
|
96
|
+
@preprocessor = block
|
82
97
|
else
|
83
98
|
raise ConfigError,"No preprocess block given"
|
84
99
|
end
|
85
100
|
end
|
86
101
|
|
87
|
-
def preprocessor(
|
88
|
-
@preprocessor
|
89
|
-
|
102
|
+
def preprocessor(*args, &block)
|
103
|
+
return @preprocessor if args.size == 0
|
104
|
+
preprocessor_class = args[0]
|
105
|
+
@preprocessor = preprocessor_class.new
|
106
|
+
puts "Setting up preprocessor_class #{@preprocessor}"
|
107
|
+
@preprocessor.instance_eval &block if block_given?
|
90
108
|
end
|
91
109
|
|
92
110
|
def files(&block)
|
data/lib/cdn_fu/file_info.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module CdnFu
|
2
2
|
class FileInfo
|
3
|
-
attr_accessor :local_path,:minified_path,:remote_path
|
4
|
-
attr_writer :gzip,:minify
|
3
|
+
attr_accessor :local_path,:minified_path,:remote_path,:processed_path
|
4
|
+
attr_writer :gzip,:minify,:preprocess
|
5
5
|
|
6
6
|
def gzip?
|
7
7
|
@gzip
|
@@ -10,5 +10,9 @@ module CdnFu
|
|
10
10
|
def minify?
|
11
11
|
@minify
|
12
12
|
end
|
13
|
+
|
14
|
+
def preprocess?
|
15
|
+
@preprocess
|
16
|
+
end
|
13
17
|
end
|
14
18
|
end
|
data/lib/cdn_fu/lister.rb
CHANGED
@@ -24,6 +24,7 @@ module CdnFu
|
|
24
24
|
Dir.glob(File.join(asset_root,glob_str)).each do |file|
|
25
25
|
fi = FileInfo.new
|
26
26
|
fi.local_path = File.expand_path(file)
|
27
|
+
fi.preprocess = glob[:preprocess]
|
27
28
|
fi.gzip = glob[:gzip]
|
28
29
|
fi.minify = glob[:minify]
|
29
30
|
root_sub_path = fi.local_path.gsub(asset_root,'')
|
@@ -27,9 +27,12 @@ class YuiMinifier < CdnFu::Minifier
|
|
27
27
|
private
|
28
28
|
|
29
29
|
def one_minification(file)
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
file.
|
30
|
+
minified_dir = File.join(CdnFu::Config.config.tmp_dir,"cdnfu_minified",File.dirname(file.local_path))
|
31
|
+
FileUtils.mkdir_p(minified_dir)
|
32
|
+
minified_path = File.join(minified_dir,"minified_#{File.basename(file.local_path)}")
|
33
|
+
input_path = file.processed_path ? file.processed_path : file.local_path
|
34
|
+
`java -jar #{@yui_jar_path} #{input_path} > #{minified_path}`
|
35
|
+
puts "[minify] #{input_path} to #{minified_path}" if CdnFu::Config.config.verbose
|
36
|
+
file.minified_path = minified_path
|
34
37
|
end
|
35
38
|
end
|
@@ -18,7 +18,8 @@ class CloudfrontUploader < CdnFu::Uploader
|
|
18
18
|
:secret_access_key => secret_key
|
19
19
|
)
|
20
20
|
|
21
|
-
|
21
|
+
populate_existing_asset_checksums(file_list)
|
22
|
+
|
22
23
|
file_list.each do |file|
|
23
24
|
upload_single_file(file)
|
24
25
|
end
|
@@ -40,22 +41,25 @@ class CloudfrontUploader < CdnFu::Uploader
|
|
40
41
|
end
|
41
42
|
|
42
43
|
private
|
43
|
-
def populate_existing_asset_checksums
|
44
|
+
def populate_existing_asset_checksums(file_list)
|
45
|
+
puts "Populating Existing Checksums...(could take a minute)" if CdnFu::Config.config.verbose
|
44
46
|
@existing_asset_checksums = {}
|
45
47
|
objects = []
|
46
48
|
bucket = Bucket.find(@s3_bucket)
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
49
|
+
|
50
|
+
file_list.each do |cf_file|
|
51
|
+
versioned_filename = CdnFu::Config.config.asset_id.to_s + cf_file.remote_path
|
52
|
+
begin
|
53
|
+
obj = S3Object.about(versioned_filename,@s3_bucket)
|
54
|
+
if obj
|
55
|
+
existing_sha1 = obj.metadata["x-amz-meta-sha1-hash"]
|
56
|
+
if existing_sha1
|
57
|
+
@existing_asset_checksums[versioned_filename] = existing_sha1
|
58
|
+
end
|
59
|
+
end
|
60
|
+
rescue NoSuchKey
|
53
61
|
end
|
54
62
|
end
|
55
|
-
objects.each do |obj|
|
56
|
-
@existing_asset_checksums[obj.path] = obj.metadata['x-amz-meta-sha1-hash']
|
57
|
-
end
|
58
|
-
return objects
|
59
63
|
end
|
60
64
|
|
61
65
|
def upload_single_file(cf_file)
|
@@ -66,7 +70,7 @@ class CloudfrontUploader < CdnFu::Uploader
|
|
66
70
|
path_to_upload ||= cf_file.local_path
|
67
71
|
fstat = File.stat(path_to_upload)
|
68
72
|
sha1sum = Digest::SHA1.hexdigest(File.open(path_to_upload).read)
|
69
|
-
if remote_sha1 = @existing_asset_checksums[
|
73
|
+
if remote_sha1 = @existing_asset_checksums[versioned_filename]
|
70
74
|
if remote_sha1 != sha1sum
|
71
75
|
puts "Your assets are different from the ones in s3 with this asset_id. Please increment your asset_id in cdn_fu.rb"
|
72
76
|
exit
|
@@ -91,8 +95,8 @@ class CloudfrontUploader < CdnFu::Uploader
|
|
91
95
|
|
92
96
|
eight_years = 8 * 60 * 60 * 24 * 365
|
93
97
|
eight_years_from_now = Time.now + eight_years
|
94
|
-
options[
|
95
|
-
options[
|
98
|
+
options["Cache-Control"] = "public, max-age=#{eight_years}"
|
99
|
+
options["Expires"] = eight_years_from_now.httpdate
|
96
100
|
S3Object.store(versioned_filename,file_content,s3_bucket, options)
|
97
101
|
puts "[upload] #{s3_bucket} #{versioned_filename}" if CdnFu::Config.config.verbose
|
98
102
|
end
|
data/lib/cdn_fu/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 6
|
8
|
-
-
|
9
|
-
version: 0.6.
|
8
|
+
- 2
|
9
|
+
version: 0.6.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Curtis Spencer
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-05-
|
17
|
+
date: 2011-05-25 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|