asset_sync 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -94,7 +94,7 @@ The generator will create a Rails initializer at `config/initializers/asset_sync
94
94
 
95
95
  ### YAML (config/asset_sync.yml)
96
96
 
97
- If you used the `--use-yml` flag, the generator will create a YAML file at `config/initializers/asset_sync.rb`.
97
+ If you used the `--use-yml` flag, the generator will create a YAML file at `config/asset_sync.yml`.
98
98
 
99
99
  defaults: &defaults
100
100
  aws_access_key: "<%= ENV['AWS_ACCESS_KEY'] %>"
@@ -136,7 +136,15 @@ Or add to a traditional unix system
136
136
  * **aws\_access\_key**: your Amazon S3 access key
137
137
  * **aws\_access\_secret**: your Amazon S3 access secret
138
138
  * **aws\_region**: the region your S3 bucket is in e.g. *eu-west-1*
139
- * **existing_remote_files**: what to do with previously precompiled files, options are **keep** or **delete**
139
+ * **existing\_remote\_files**: what to do with previously precompiled files, options are **keep** or **delete**
140
+ * **gzip\_compression**: when enabled, will automatically replace files that have a gzip compressed equivalent with the compressed version.
141
+
142
+ ## Automatic gzip compression
143
+
144
+ With the `gzip_compression` option enabled, when uploading your assets. If a file has a gzip compressed equivalent we will replace that asset with the compressed version and sets the correct headers for S3 to serve it. For example, if you have a file **master.css** and it was compressed to **master.css.gz** we will upload the **.gz** file to S3 in place of the uncompressed file.
145
+
146
+ If the compressed file is actually larger than the uncompressed file we will ignore this rule and upload the standard uncompressed version.
147
+
140
148
 
141
149
  ## Amazon S3 Multiple Region Support
142
150
 
@@ -155,10 +163,9 @@ Or via the initializer
155
163
 
156
164
  ## Rake Task
157
165
 
158
- A rake task is installed with the generator to enhance the rails
159
- precompile task by automatically running after it:
166
+ A rake task is included in asset\_sync to enhance the rails precompile task by automatically running after it:
160
167
 
161
- # lib/tasks/asset_sync.rake
168
+ # asset_sync/lib/tasks/asset_sync.rake
162
169
  Rake::Task["assets:precompile"].enhance do
163
170
  AssetSync.sync
164
171
  end
@@ -6,12 +6,12 @@ require "asset_sync/version"
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "asset_sync"
8
8
  s.version = AssetSync::VERSION
9
- s.date = "2011-08-30"
9
+ s.date = "2011-11-05"
10
10
  s.platform = Gem::Platform::RUBY
11
11
  s.authors = ["Simon Hamilton", "David Rice"]
12
12
  s.email = ["shamilton@rumblelabs.com", "me@davidjrice.co.uk"]
13
13
  s.homepage = "https://github.com/rumblelabs/asset_sync"
14
- s.summary = %q{Synchronises Assets between Rails and S3}
14
+ s.summary = %q{Synchronises Assets in a Rails 3 application and S3/Cloudfront}
15
15
  s.description = %q{After you run assets:precompile your assets will be synchronised with your S3 bucket, deleting unused files and only uploading the files it needs to.}
16
16
 
17
17
  s.rubyforge_project = "asset_sync"
@@ -9,6 +9,7 @@ module AssetSync
9
9
  attr_accessor :aws_bucket
10
10
  attr_accessor :aws_region
11
11
  attr_accessor :existing_remote_files
12
+ attr_accessor :gzip_compression
12
13
 
13
14
  validates :aws_access_key, :presence => true
14
15
  validates :aws_access_secret, :presence => true
@@ -19,9 +20,14 @@ module AssetSync
19
20
  self.provider = 'AWS'
20
21
  self.aws_region = nil
21
22
  self.existing_remote_files = 'keep'
23
+ self.gzip_compression = false
22
24
  load_yml! if yml_exists?
23
25
  end
24
26
 
27
+ def gzip?
28
+ self.gzip_compression
29
+ end
30
+
25
31
  def existing_remote_files?
26
32
  (self.existing_remote_files == "keep")
27
33
  end
@@ -45,6 +51,7 @@ module AssetSync
45
51
  self.aws_bucket = yml["aws_bucket"] if yml.has_key?("aws_bucket")
46
52
  self.aws_region = yml["aws_region"] if yml.has_key?("aws_region")
47
53
  self.existing_remote_files = yml["existing_remote_files"] if yml.has_key?("existing_remote_files")
54
+ self.gzip_compression = yml["gzip_compression"] if yml.has_key?("gzip_compression")
48
55
 
49
56
  # TODO deprecate old style config settings
50
57
  self.aws_access_key = yml["access_key_id"] if yml.has_key?("access_key_id")
@@ -54,13 +54,46 @@ module AssetSync
54
54
  end
55
55
 
56
56
  def upload_file(f)
57
- STDERR.puts "Uploading: #{f}"
58
- file = bucket.files.create(
59
- :key => "#{f}",
57
+ # TODO output files in debug logs as asset filename only.
58
+ file = {
59
+ :key => f,
60
60
  :body => File.open("#{path}/#{f}"),
61
61
  :public => true,
62
62
  :cache_control => "max-age=31557600"
63
- )
63
+ }
64
+
65
+ gzipped = "#{path}/#{f}.gz"
66
+ ignore = false
67
+
68
+ if config.gzip? && File.extname(f) == ".gz"
69
+ # Don't bother uploading gzipped assets if we are in gzip_compression mode
70
+ # as we will overwrite file.css with file.css.gz if it exists.
71
+ STDERR.puts "Ignoring: #{f}"
72
+ ignore = true
73
+ elsif config.gzip? && File.exists?(gzipped)
74
+ original_size = File.size("#{path}/#{f}")
75
+ gzipped_size = File.size(gzipped)
76
+
77
+ if gzipped_size < original_size
78
+ percentage = ((gzipped_size.to_f/original_size.to_f)*100).round(2)
79
+ ext = File.extname( f )[1..-1]
80
+ mime = Mime::Type.lookup_by_extension( ext )
81
+ file.merge!({
82
+ :key => f,
83
+ :body => File.open(gzipped),
84
+ :content_type => mime,
85
+ :content_encoding => 'gzip'
86
+ })
87
+ STDERR.puts "Uploading: #{gzipped} in place of #{f} saving #{percentage}%"
88
+ else
89
+ percentage = ((original_size.to_f/gzipped_size.to_f)*100).round(2)
90
+ STDERR.puts "Uploading: #{f} instead of #{gzipped} (compression increases this file by #{percentage}%)"
91
+ end
92
+ else
93
+ STDERR.puts "Uploading: #{f}"
94
+ end
95
+
96
+ file = bucket.files.create( file ) unless ignore
64
97
  end
65
98
 
66
99
  def upload_files
@@ -1,3 +1,3 @@
1
1
  module AssetSync
2
- VERSION = "0.1.8"
2
+ VERSION = "0.1.9"
3
3
  end
@@ -2,6 +2,9 @@ AssetSync.configure do |config|
2
2
  config.aws_access_key = ENV['AWS_ACCESS_KEY']
3
3
  config.aws_access_secret = ENV['AWS_ACCESS_SECRET']
4
4
  config.aws_bucket = ENV['AWS_BUCKET']
5
- # config.aws_region = "eu-west-1"
6
5
  config.existing_remote_files = "keep"
6
+ # Increase upload performance by configuring your region
7
+ # config.aws_region = "eu-west-1"
8
+ # Automatically replace files with their equivalent gzip compressed version
9
+ # config.gzip_compression = true
7
10
  end
@@ -1,27 +1,30 @@
1
1
  defaults: &defaults
2
- access_key_id: "<%= aws_access_key %>"
3
- secret_access_key: "<%= aws_access_secret %>"
2
+ aws_access_key: "<%= aws_access_key %>"
3
+ aws_access_secret: "<%= aws_access_secret %>"
4
4
  # You may need to specify what region your S3 bucket is in
5
- # region: "eu-west-1"
5
+ # aws_region: "eu-west-1"
6
6
  existing_remote_files: keep
7
+ # Automatically replace files with their equivalent gzip compressed version
8
+ # gzip_compression = true
9
+
7
10
 
8
11
  development:
9
12
  <<: *defaults
10
- bucket: "<%= app_name %>_development"
13
+ aws_bucket: "<%= app_name %>_development"
11
14
  # Existing pre-compiled assets on S3 will be kept
12
15
  # existing_remote_files: keep
13
16
 
14
17
  test:
15
18
  <<: *defaults
16
- bucket: "<%= app_name %>_test"
19
+ aws_bucket: "<%= app_name %>_test"
17
20
 
18
21
  staging:
19
22
  <<: *defaults
20
- bucket: "<%= app_name %>_test"
23
+ aws_bucket: "<%= app_name %>_test"
21
24
 
22
25
  production:
23
26
  <<: *defaults
24
- bucket: "<%= app_name %>_production"
27
+ aws_bucket: "<%= app_name %>_production"
25
28
  # Existing pre-compiled assets on S3 will be deleted
26
29
  # existing_remote_files: delete
27
30
 
@@ -39,6 +39,10 @@ describe AssetSync, 'with initializer' do
39
39
  AssetSync.config.existing_remote_files.should == "keep"
40
40
  end
41
41
 
42
+ it "should default gzip_compression to false" do
43
+ AssetSync.config.gzip_compression.should be_false
44
+ end
45
+
42
46
  end
43
47
 
44
48
 
@@ -69,6 +73,10 @@ describe AssetSync, 'from yml' do
69
73
  AssetSync.config.existing_remote_files.should == "keep"
70
74
  end
71
75
 
76
+ it "should default gzip_compression to false" do
77
+ AssetSync.config.gzip_compression.should be_false
78
+ end
79
+
72
80
  end
73
81
 
74
82
  describe AssetSync, 'with no configuration' do
@@ -82,4 +90,18 @@ describe AssetSync, 'with no configuration' do
82
90
  lambda{ AssetSync.sync }.should raise_error(AssetSync::Config::Invalid)
83
91
  end
84
92
 
93
+ end
94
+
95
+ describe AssetSync, 'with gzip_compression enabled' do
96
+
97
+ before(:all) do
98
+ Rails.root = 'without_yml'
99
+ AssetSync.config = AssetSync::Config.new
100
+ AssetSync.config.gzip_compression = true
101
+ end
102
+
103
+ it "config.gzip? should be true" do
104
+ AssetSync.config.gzip?.should be_true
105
+ end
106
+
85
107
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asset_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-08-30 00:00:00.000000000Z
13
+ date: 2011-11-05 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: fog
17
- requirement: &70339227148100 !ruby/object:Gem::Requirement
17
+ requirement: &70129910998440 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70339227148100
25
+ version_requirements: *70129910998440
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: activemodel
28
- requirement: &70339227147580 !ruby/object:Gem::Requirement
28
+ requirement: &70129910980480 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *70339227147580
36
+ version_requirements: *70129910980480
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rspec
39
- requirement: &70339227146940 !ruby/object:Gem::Requirement
39
+ requirement: &70129910979840 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70339227146940
47
+ version_requirements: *70129910979840
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: bundler
50
- requirement: &70339227143360 !ruby/object:Gem::Requirement
50
+ requirement: &70129910979280 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *70339227143360
58
+ version_requirements: *70129910979280
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: jeweler
61
- requirement: &70339227142720 !ruby/object:Gem::Requirement
61
+ requirement: &70129910978860 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,7 +66,7 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *70339227142720
69
+ version_requirements: *70129910978860
70
70
  description: After you run assets:precompile your assets will be synchronised with
71
71
  your S3 bucket, deleting unused files and only uploading the files it needs to.
72
72
  email:
@@ -119,7 +119,7 @@ rubyforge_project: asset_sync
119
119
  rubygems_version: 1.8.10
120
120
  signing_key:
121
121
  specification_version: 3
122
- summary: Synchronises Assets between Rails and S3
122
+ summary: Synchronises Assets in a Rails 3 application and S3/Cloudfront
123
123
  test_files:
124
124
  - spec/asset_sync_spec.rb
125
125
  - spec/spec_helper.rb