asset_sync 2.4.0 → 2.15.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@ module AssetSync
5
5
 
6
6
  # Commandline options can be defined here using Thor-like options:
7
7
  class_option :use_yml, :type => :boolean, :default => false, :desc => "Use YML file instead of Rails Initializer"
8
- class_option :provider, :type => :string, :default => "AWS", :desc => "Generate with support for 'AWS', 'Rackspace', 'Google', or 'AzureRM"
8
+ class_option :provider, :type => :string, :default => "AWS", :desc => "Generate with support for 'AWS', 'Rackspace', 'Google', 'AzureRM', or 'Backblaze'"
9
9
 
10
10
  def self.source_root
11
11
  @source_root ||= File.join(File.dirname(__FILE__), 'templates')
@@ -27,6 +27,10 @@ module AssetSync
27
27
  options[:provider] == 'AzureRM'
28
28
  end
29
29
 
30
+ def backblaze?
31
+ options[:provider] == 'Backblaze'
32
+ end
33
+
30
34
  def aws_access_key_id
31
35
  "<%= ENV['AWS_ACCESS_KEY_ID'] %>"
32
36
  end
@@ -35,6 +39,10 @@ module AssetSync
35
39
  "<%= ENV['AWS_SECRET_ACCESS_KEY'] %>"
36
40
  end
37
41
 
42
+ def aws_session_token
43
+ "<%= ENV['AWS_SESSION_TOKEN'] %>"
44
+ end
45
+
38
46
  def google_storage_access_key_id
39
47
  "<%= ENV['GOOGLE_STORAGE_ACCESS_KEY_ID'] %>"
40
48
  end
@@ -59,6 +67,18 @@ module AssetSync
59
67
  "<%= ENV['AZURE_STORAGE_ACCESS_KEY'] %>"
60
68
  end
61
69
 
70
+ def b2_key_id
71
+ "<%= ENV['B2_KEY_ID'] %>"
72
+ end
73
+
74
+ def b2_key_token
75
+ "<%= ENV['B2_KEY_TOKEN'] %>"
76
+ end
77
+
78
+ def b2_bucket_id
79
+ "<%= ENV['B2_BUCKET_ID'] %>"
80
+ end
81
+
62
82
  def app_name
63
83
  @app_name ||= Rails.application.is_a?(Rails::Application) && Rails.application.class.name.sub(/::Application$/, "").downcase
64
84
  end
@@ -4,15 +4,24 @@ if defined?(AssetSync)
4
4
  config.fog_provider = 'AWS'
5
5
  config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
6
6
  config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
7
+ config.aws_session_token = ENV['AWS_SESSION_TOKEN'] if ENV.key?('AWS_SESSION_TOKEN')
7
8
  # To use AWS reduced redundancy storage.
8
9
  # config.aws_reduced_redundancy = true
9
10
  #
10
11
  # Change AWS signature version. Default is 4
11
12
  # config.aws_signature_version = 4
12
13
  #
14
+ # Change canned ACL of uploaded object. Default is unset. Will override fog_public if set.
15
+ # Choose from: private | public-read | public-read-write | aws-exec-read |
16
+ # authenticated-read | bucket-owner-read | bucket-owner-full-control
17
+ # config.aws_acl = nil
18
+ #
13
19
  # Change host option in fog (only if you need to)
14
20
  # config.fog_host = "s3.amazonaws.com"
15
21
  #
22
+ # Change port option in fog (only if you need to)
23
+ # config.fog_port = "9000"
24
+ #
16
25
  # Use http instead of https. Default should be "https" (at least for fog-aws)
17
26
  # config.fog_scheme = "http"
18
27
  <%- elsif google? -%>
@@ -31,6 +40,12 @@ if defined?(AssetSync)
31
40
  config.azure_storage_account_name = ENV['AZURE_STORAGE_ACCOUNT_NAME']
32
41
  config.azure_storage_access_key = ENV['AZURE_STORAGE_ACCESS_KEY']
33
42
 
43
+ <%- elsif backblaze? -%>
44
+ config.fog_provider = 'Backblaze'
45
+ config.b2_key_id = ENV['B2_KEY_ID']
46
+ config.b2_key_token = ENV['B2_KEY_TOKEN']
47
+ config.b2_bucket_id = ENV['B2_BUCKET_ID']
48
+
34
49
  # config.fog_directory specifies container name of Azure Blob storage
35
50
  <%- end -%>
36
51
  config.fog_directory = ENV['FOG_DIRECTORY']
@@ -42,6 +57,11 @@ if defined?(AssetSync)
42
57
  # Increase upload performance by configuring your region
43
58
  # config.fog_region = 'eu-west-1'
44
59
  #
60
+ # Set `public` option when uploading file depending on value,
61
+ # Setting to "default" makes asset sync skip setting the option
62
+ # Possible values: true, false, "default" (default: true)
63
+ # config.fog_public = true
64
+ #
45
65
  # Don't delete files from the store
46
66
  # config.existing_remote_files = "keep"
47
67
  #
@@ -55,6 +75,12 @@ if defined?(AssetSync)
55
75
  # Upload the manifest file also.
56
76
  # config.include_manifest = false
57
77
  #
78
+ # Upload files concurrently
79
+ # config.concurrent_uploads = false
80
+ #
81
+ # Path to cache file to skip scanning remote
82
+ # config.remote_file_list_cache_file_path = './.asset_sync_remote_file_list_cache.json'
83
+ #
58
84
  # Fail silently. Useful for environments such as Heroku
59
85
  # config.fail_silently = true
60
86
  #
@@ -3,15 +3,24 @@ defaults: &defaults
3
3
  fog_provider: 'AWS'
4
4
  aws_access_key_id: "<%= aws_access_key_id %>"
5
5
  aws_secret_access_key: "<%= aws_secret_access_key %>"
6
+
6
7
  # To use AWS reduced redundancy storage.
7
8
  # aws_reduced_redundancy: true
8
9
  #
9
10
  # Change AWS signature version. Default is 4
10
11
  # aws_signature_version: 4
11
12
  #
13
+ # Change canned ACL of uploaded object. Default is unset. Will override fog_public if set.
14
+ # Choose from: private | public-read | public-read-write | aws-exec-read |
15
+ # authenticated-read | bucket-owner-read | bucket-owner-full-control
16
+ # aws_acl: null
17
+ #
12
18
  # Change host option in fog (only if you need to)
13
19
  # fog_host: "s3.amazonaws.com"
14
20
  #
21
+ # Change port option in fog (only if you need to)
22
+ # fog_port: "9000"
23
+ #
15
24
  # Use http instead of https. Default should be "https" (at least for fog-aws)
16
25
  # fog_scheme: "http"
17
26
  <%- elsif google? -%>
@@ -28,19 +37,34 @@ defaults: &defaults
28
37
  fog_provider: 'AzureRM'
29
38
  azure_storage_account_name: "<%= azure_storage_account_name %>"
30
39
  azure_storage_access_key: "<%= azure_storage_access_key %>"
31
-
32
40
  # fog_directory specifies container name of Azure Blob storage
41
+ <%- elsif backblaze? -%>
42
+ fog_provider: Backblaze
43
+ b2_key_id: "<%= b2_key_id %>"
44
+ b2_key_token: "<%= b2_key_token %>"
45
+ b2_bucket_id: "<%= b2_bucket_id %>"
46
+ # fog_directory specifies container name of Backblaze B2 Bucket
33
47
  <%- end -%>
34
48
  fog_directory: "<%= app_name %>-assets"
49
+
35
50
  # You may need to specify what region your storage bucket is in
36
51
  # fog_region: "eu-west-1"
52
+
53
+ # Set `public` option when uploading file depending on value,
54
+ # Setting to "default" makes asset sync skip setting the option
55
+ # Possible values: true, false, "default" (default: true)
56
+ # fog_public: "true"
57
+
37
58
  existing_remote_files: keep
38
59
  # To delete existing remote files.
39
60
  # existing_remote_files: delete
61
+
40
62
  # Automatically replace files with their equivalent gzip compressed version
41
63
  # gzip_compression: true
64
+
42
65
  # Fail silently. Useful for environments such as Heroku
43
66
  # fail_silently: true
67
+
44
68
  # Allow custom assets to be cacheable. Note: The base filename will be matched
45
69
  # cache_asset_regexps: ['cache_me.js', !ruby/regexp '/cache_some\.\d{8}\.css/']
46
70
 
@@ -19,7 +19,7 @@ if Rake::Task.task_defined?("assets:precompile:nondigest")
19
19
  # will get executed before yaml or Rails initializers.
20
20
  Rake::Task["assets:sync"].invoke if defined?(AssetSync) && AssetSync.config.run_on_precompile
21
21
  end
22
- else
22
+ elsif Rake::Task.task_defined?("assets:precompile")
23
23
  Rake::Task["assets:precompile"].enhance do
24
24
  # rails 3.1.1 will clear out Rails.application.config if the env vars
25
25
  # RAILS_GROUP and RAILS_ENV are not defined. We need to reload the
@@ -27,4 +27,6 @@ else
27
27
  # Rake::Task["assets:environment"].invoke if Rake::Task.task_defined?("assets:environment")
28
28
  Rake::Task["assets:sync"].invoke if defined?(AssetSync) && AssetSync.config.run_on_precompile
29
29
  end
30
+ else
31
+ # Nothing to be enhanced
30
32
  end
@@ -0,0 +1,20 @@
1
+ defaults: &defaults
2
+ fog_provider: "Backblaze"
3
+ b2_key_id: 'xxxx'
4
+ b2_key_token: 'zzzz'
5
+ b2_bucket_id: '1234'
6
+
7
+ development:
8
+ <<: *defaults
9
+ fog_directory: "rails_app_development"
10
+ existing_remote_files: keep
11
+
12
+ test:
13
+ <<: *defaults
14
+ fog_directory: "rails_app_test"
15
+ existing_remote_files: keep
16
+
17
+ production:
18
+ <<: *defaults
19
+ fog_directory: "rails_app_production"
20
+ existing_remote_files: delete
@@ -0,0 +1,19 @@
1
+ defaults: &defaults
2
+ fog_provider: "Google"
3
+ google_json_key_location: 'gcs.json'
4
+ google_project: 'some-project'
5
+
6
+ development:
7
+ <<: *defaults
8
+ fog_directory: "rails_app_development"
9
+ existing_remote_files: keep
10
+
11
+ test:
12
+ <<: *defaults
13
+ fog_directory: "rails_app_test"
14
+ existing_remote_files: keep
15
+
16
+ production:
17
+ <<: *defaults
18
+ fog_directory: "rails_app_production"
19
+ existing_remote_files: delete
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+ require "fog/backblaze"
3
+
4
+ def bucket(name)
5
+ options = {
6
+ :provider => 'Backblaze',
7
+ :b2_key_id => ENV['B2_KEY_ID'],
8
+ :b2_key_token => ENV['B2_KEY_TOKEN'],
9
+ :b2_bucket_id => ENV['B2_BUCKET_ID']
10
+ }
11
+ options.merge!({ :environment => ENV['FOG_REGION'] }) if ENV.has_key?('FOG_REGION')
12
+
13
+ connection = Fog::Storage.new(options)
14
+ connection.directories.get(ENV['FOG_DIRECTORY'])
15
+ end
16
+
17
+ def execute(command)
18
+ app_path = File.expand_path("../../dummy_app", __FILE__)
19
+ Dir.chdir app_path
20
+ `#{command}`
21
+ end
22
+
23
+ describe "AssetSync" do
24
+
25
+ before(:each) do
26
+ @prefix = SecureRandom.hex(6)
27
+ end
28
+
29
+ let(:app_js_regex){
30
+ /#{@prefix}\/application-[a-zA-Z0-9]*.js$/
31
+ }
32
+
33
+ let(:app_js_gz_regex){
34
+ /#{@prefix}\/application-[a-zA-Z0-9]*.js.gz$/
35
+ }
36
+
37
+ let(:files){ bucket(@prefix).files }
38
+
39
+
40
+ after(:each) do
41
+ @directory = bucket(@prefix)
42
+ @directory.files.each do |f|
43
+ f.destroy
44
+ end
45
+ end
46
+
47
+ it "sync" do
48
+ execute "rake ASSET_SYNC_PREFIX=#{@prefix} assets:precompile"
49
+
50
+ files = bucket(@prefix).files
51
+
52
+ app_js = files.select{ |f| f.key =~ app_js_regex }.first
53
+ expect(app_js.content_type).to eq("application/javascript")
54
+
55
+ app_js_gz = files.select{ |f| f.key =~ app_js_gz_regex }.first
56
+ expect(app_js_gz.content_type).to eq("application/javascript")
57
+ expect(app_js_gz.content_encoding).to eq("gzip")
58
+ end
59
+
60
+ it "sync with enabled=false" do
61
+ execute "rake ASSET_SYNC_PREFIX=#{@prefix} ASSET_SYNC_ENABLED=false assets:precompile"
62
+ expect(bucket(@prefix).files.size).to eq(0)
63
+ end
64
+
65
+ it "sync with gzip_compression=true" do
66
+ execute "rake ASSET_SYNC_PREFIX=#{@prefix} ASSET_SYNC_GZIP_COMPRESSION=true assets:precompile"
67
+ # bucket(@prefix).files.size.should == 3
68
+
69
+ app_js_path = files.select{ |f| f.key =~ app_js_regex }.first
70
+ app_js = files.get( app_js_path.key )
71
+ expect(app_js.content_type).to eq("application/javascript")
72
+ end
73
+
74
+ end
data/spec/spec_helper.rb CHANGED
@@ -10,6 +10,12 @@ rescue LoadError
10
10
  # SimpleCov ain't available - continue
11
11
  end
12
12
 
13
+ if ENV["TRAVIS"]
14
+ require "coveralls"
15
+ Coveralls.wear!("rails")
16
+ end
17
+
18
+
13
19
  begin
14
20
  Bundler.setup(:default, :development)
15
21
  rescue Bundler::BundlerError => e
@@ -145,7 +145,7 @@ describe AssetSync do
145
145
 
146
146
  it "should default asset_regexps to match regexps" do
147
147
  expect(AssetSync.config.cache_asset_regexps).to eq(['cache_me.js', /cache_some\.\d{8}\.css/])
148
- end
148
+ end
149
149
  end
150
150
 
151
151
  describe 'from yml, exporting to a mobile hybrid development directory' do
@@ -255,12 +255,12 @@ describe AssetSync do
255
255
  expect(AssetSync.config.manifest_path).to match(/public\/custom_assets\/manifest.yml/)
256
256
  end
257
257
  end
258
-
258
+
259
259
  describe 'with cache_asset_regexps' do
260
260
  before(:each) do
261
261
  AssetSync.config = AssetSync::Config.new
262
262
  end
263
-
263
+
264
264
  it "config.cache_asset_regexp should set cache_asset_regexps" do
265
265
  AssetSync.config.cache_asset_regexp = /\.[a-f0-9]{8}/i
266
266
  expect(AssetSync.config.cache_asset_regexps.size).to eq(1)
@@ -283,4 +283,36 @@ describe AssetSync do
283
283
  expect{ AssetSync::Config.new }.to raise_error(Psych::SyntaxError)
284
284
  end
285
285
  end
286
+
287
+ describe 'FogPublicValue' do
288
+ describe "#to_bool" do
289
+ it "true should be converted to true" do
290
+ expect(AssetSync::Config::FogPublicValue.new(true).to_bool).to be_truthy
291
+ end
292
+ it "false should be converted to false" do
293
+ expect(AssetSync::Config::FogPublicValue.new(false).to_bool).to be_falsey
294
+ end
295
+ it "nil should be converted to false" do
296
+ expect(AssetSync::Config::FogPublicValue.new(nil).to_bool).to be_falsey
297
+ end
298
+ it "'default' should be converted to false" do
299
+ expect(AssetSync::Config::FogPublicValue.new("default").to_bool).to be_truthy
300
+ end
301
+ end
302
+
303
+ describe "#use_explicit_value?" do
304
+ it "true should be converted to true" do
305
+ expect(AssetSync::Config::FogPublicValue.new(true).use_explicit_value?).to be_truthy
306
+ end
307
+ it "false should be converted to true" do
308
+ expect(AssetSync::Config::FogPublicValue.new(false).use_explicit_value?).to be_truthy
309
+ end
310
+ it "nil should be converted to true" do
311
+ expect(AssetSync::Config::FogPublicValue.new(nil).use_explicit_value?).to be_truthy
312
+ end
313
+ it "'default' should be converted to false" do
314
+ expect(AssetSync::Config::FogPublicValue.new("default").use_explicit_value?).to be_falsey
315
+ end
316
+ end
317
+ end
286
318
  end
@@ -0,0 +1,150 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe AssetSync do
4
+ include_context "mock Rails without_yml"
5
+
6
+ describe 'with initializer' do
7
+ before(:each) do
8
+ AssetSync.config = AssetSync::Config.new
9
+ AssetSync.configure do |config|
10
+ config.fog_provider = 'Backblaze'
11
+ config.b2_key_id = 'aaaa'
12
+ config.b2_key_token = 'bbbb'
13
+ config.b2_bucket_id = '4567'
14
+ config.fog_directory = 'mybucket'
15
+ config.existing_remote_files = "keep"
16
+ end
17
+ end
18
+
19
+ it "should configure provider as Backblaze" do
20
+ expect(AssetSync.config.fog_provider).to eq('Backblaze')
21
+ end
22
+
23
+ it "should should keep existing remote files" do
24
+ expect(AssetSync.config.existing_remote_files?).to eq(true)
25
+ end
26
+
27
+ it "should configure b2_key_id" do
28
+ expect(AssetSync.config.b2_key_id).to eq("aaaa")
29
+ end
30
+
31
+ it "should configure b2_key_token" do
32
+ expect(AssetSync.config.b2_key_token).to eq("bbbb")
33
+ end
34
+
35
+ it "should configure b2_bucket_id" do
36
+ expect(AssetSync.config.b2_bucket_id).to eq("4567")
37
+ end
38
+
39
+ it "should configure fog_directory" do
40
+ expect(AssetSync.config.fog_directory).to eq("mybucket")
41
+ end
42
+
43
+ it "should configure existing_remote_files" do
44
+ expect(AssetSync.config.existing_remote_files).to eq("keep")
45
+ end
46
+
47
+ it "should default gzip_compression to false" do
48
+ expect(AssetSync.config.gzip_compression).to be_falsey
49
+ end
50
+
51
+ it "should default manifest to false" do
52
+ expect(AssetSync.config.manifest).to be_falsey
53
+ end
54
+ end
55
+
56
+ describe 'from yml' do
57
+ before(:each) do
58
+ set_rails_root('backblaze_with_yml')
59
+ AssetSync.config = AssetSync::Config.new
60
+ end
61
+
62
+ it "should configure b2_key_id" do
63
+ expect(AssetSync.config.b2_key_id).to eq("xxxx")
64
+ end
65
+
66
+ it "should configure b2_key_token" do
67
+ expect(AssetSync.config.b2_key_token).to eq("zzzz")
68
+ end
69
+
70
+ it "should configure b2_bucket_id" do
71
+ expect(AssetSync.config.b2_bucket_id).to eq("1234")
72
+ end
73
+
74
+ it "should configure fog_directory" do
75
+ expect(AssetSync.config.fog_directory).to eq("rails_app_test")
76
+ end
77
+
78
+ it "should configure existing_remote_files" do
79
+ expect(AssetSync.config.existing_remote_files).to eq("keep")
80
+ end
81
+
82
+ it "should default gzip_compression to false" do
83
+ expect(AssetSync.config.gzip_compression).to be_falsey
84
+ end
85
+
86
+ it "should default manifest to false" do
87
+ expect(AssetSync.config.manifest).to be_falsey
88
+ end
89
+ end
90
+
91
+ describe 'with no configuration' do
92
+ before(:each) do
93
+ AssetSync.config = AssetSync::Config.new
94
+ end
95
+
96
+ it "should be invalid" do
97
+ expect{ AssetSync.sync }.to raise_error(::AssetSync::Config::Invalid)
98
+ end
99
+ end
100
+
101
+ describe 'with fail_silent configuration' do
102
+ before(:each) do
103
+ allow(AssetSync).to receive(:stderr).and_return(StringIO.new)
104
+ AssetSync.config = AssetSync::Config.new
105
+ AssetSync.configure do |config|
106
+ config.fail_silently = true
107
+ end
108
+ end
109
+
110
+ it "should not raise an invalid exception" do
111
+ expect{ AssetSync.sync }.not_to raise_error
112
+ end
113
+ end
114
+
115
+ describe 'with gzip_compression enabled' do
116
+ before(:each) do
117
+ AssetSync.config = AssetSync::Config.new
118
+ AssetSync.config.gzip_compression = true
119
+ end
120
+
121
+ it "config.gzip? should be true" do
122
+ expect(AssetSync.config.gzip?).to be_truthy
123
+ end
124
+ end
125
+
126
+ describe 'with manifest enabled' do
127
+ before(:each) do
128
+ AssetSync.config = AssetSync::Config.new
129
+ AssetSync.config.manifest = true
130
+ end
131
+
132
+ it "config.manifest should be true" do
133
+ expect(AssetSync.config.manifest).to be_truthy
134
+ end
135
+
136
+ it "config.manifest_path should default to public/assets.." do
137
+ expect(AssetSync.config.manifest_path).to match(/public\/assets\/manifest.yml/)
138
+ end
139
+
140
+ it "config.manifest_path should default to public/assets.." do
141
+ Rails.application.config.assets.manifest = "/var/assets"
142
+ expect(AssetSync.config.manifest_path).to eq("/var/assets/manifest.yml")
143
+ end
144
+
145
+ it "config.manifest_path should default to public/custom_assets.." do
146
+ Rails.application.config.assets.prefix = 'custom_assets'
147
+ expect(AssetSync.config.manifest_path).to match(/public\/custom_assets\/manifest.yml/)
148
+ end
149
+ end
150
+ end