asset_sync 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # Asset Sync
2
+
3
+ Synchronises Assets between Rails and S3.
4
+
5
+ Asset Sync is built to run with the new Rails Asset Pipeline feature of Rails 3.1. After you run __bundle exec rake assets:precompile__ your assets will be synchronised to your S3
6
+ bucket, optionally deleting unused files and only uploading the files it needs to.
7
+
8
+ This was initially built and is intended to work on [Heroku](http://heroku.com)
9
+
10
+ ## Installation
11
+
12
+ Add the gem to your Gemfile
13
+
14
+ gem "asset_sync"
15
+
16
+ Generate the rake task and config files
17
+
18
+ rails g asset_sync:install
19
+
20
+ ## Configuration
21
+
22
+ Configure __config/environments/production.rb__ to use Amazon
23
+ S3 as the asset host and ensure precompiling is enabled.
24
+
25
+ # config/environments/production.rb
26
+ config.action_controller.asset_host = Proc.new do |source|
27
+ request.ssl? 'https://my_bucket.s3.amazonaws.com' : 'http://my_bucket.s3.amazonaws.com'
28
+ end
29
+
30
+ Add your Amazon S3 configuration details to
31
+
32
+ # config/asset_sync.yml
33
+ development:
34
+ access_key_id: 'MY_ACCESS_KEY'
35
+ secret_access_key: 'MY_ACCESS_SECRET'
36
+ bucket: "my_bucket"
37
+ existing_remote_files: "keep"
38
+
39
+ production:
40
+ access_key_id: 'MY_ACCESS_KEY'
41
+ secret_access_key: 'MY_ACCESS_SECRET'
42
+ bucket: "my_bucket"
43
+ existing_remote_files: "delete"
44
+
45
+ A rake task is installed with the generator to enhance the rails
46
+ precompile task by automatically running after it:
47
+
48
+ # lib/tasks/asset_sync.rake
49
+ Rake::Task["assets:precompile"].enhance do
50
+ AssetSync::Assets.sync
51
+ end
52
+
53
+ ## Todo
54
+
55
+ 1. Write some specs
56
+ 2. Add some before and after filters for deleting and uploading
57
+ 3. Provide more configuration options
58
+
59
+ ## Credits
60
+
61
+ Have borrowed ideas from:
62
+
63
+ - [https://github.com/moocode/asset_id](https://github.com/moocode/asset_id)
64
+ - [https://gist.github.com/1053855](https://gist.github.com/1053855)
65
+
66
+ ## License
67
+
68
+ MIT License. Copyright 2011 Rumble Labs Ltd. [rumblelabs.com](http://rumblelabs.com)
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ require "asset_sync/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "asset_sync"
8
+ s.version = AssetSync::VERSION
9
+ s.date = "2011-07-30"
10
+ s.platform = Gem::Platform::RUBY
11
+ s.authors = ["Simon Hamilton"]
12
+ s.email = ["shamilton@rumblelabs.com"]
13
+ s.homepage = ""
14
+ s.summary = %q{Synchronises Assets between Rails and S3}
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
+
17
+ s.rubyforge_project = "asset_sync"
18
+
19
+ s.add_dependency('fog')
20
+
21
+ s.add_development_dependency "rspec"
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
+ s.require_paths = ["lib"]
27
+ end
data/lib/asset_sync.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'fog'
2
+ require "asset_sync/asset_sync"
@@ -0,0 +1,80 @@
1
+ module AssetSync
2
+ class Assets
3
+
4
+ def self.s3_config
5
+ @config ||= YAML.load_file(File.join(Rails.root, "config/asset_sync.yml"))[Rails.env] rescue nil || {}
6
+ end
7
+
8
+ def self.connection
9
+ Fog::Storage.new(
10
+ :provider => 'AWS',
11
+ :aws_access_key_id => s3_config["access_key_id"],
12
+ :aws_secret_access_key => s3_config["secret_access_key"]
13
+ )
14
+ end
15
+
16
+ def self.bucket
17
+ @bucket ||= connection.directories.get(s3_config["bucket"])
18
+ end
19
+
20
+ def self.keep_existing_remote_files
21
+ (s3_config["existing_remote_files"]) ? (s3_config["existing_remote_files"] == "keep") : true
22
+ end
23
+
24
+ def self.path
25
+ "#{Rails.root.to_s}/public"
26
+ end
27
+
28
+ def self.local_files
29
+ Dir["#{path}/assets/**/**"].map { |f| f[path.length+1,f.length-path.length] }
30
+ end
31
+
32
+ def self.get_remote_files
33
+ return bucket.files.map { |f| f.key }
34
+ end
35
+
36
+ def self.delete_file(f, remote_files_to_delete)
37
+ if remote_files_to_delete.include?(f.key)
38
+ STDERR.puts "Deleting: #{f.key}"
39
+ f.destroy
40
+ end
41
+ end
42
+
43
+ def self.delete_extra_remote_files
44
+ remote_files = get_remote_files
45
+ from_remote_files_to_delete = (local_files | remote_files) - (local_files & remote_files)
46
+
47
+ # Delete unneeded remote files
48
+ bucket.files.each do |f|
49
+ delete_file(f, from_remote_files_to_delete)
50
+ end
51
+ end
52
+
53
+ def self.upload_file(f)
54
+ STDERR.puts "Uploading: #{f}"
55
+ file = bucket.files.create(
56
+ :key => "#{f}",
57
+ :body => File.open("#{path}/#{f}"),
58
+ :public => true
59
+ )
60
+ end
61
+
62
+ def self.upload_files
63
+ # get a fresh list of remote files
64
+ remote_files = get_remote_files
65
+ local_files_to_upload = (remote_files | local_files) - (remote_files & local_files)
66
+
67
+ # Upload new files
68
+ local_files_to_upload.each do |f|
69
+ next unless File.file? "#{path}/#{f}" # Only files.
70
+ upload_file f
71
+ end
72
+ end
73
+
74
+ def self.sync
75
+ delete_extra_remote_files unless keep_existing_remote_files
76
+ upload_files
77
+ STDERR.puts "Done."
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module AssetSync
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ require 'rails/generators'
2
+ module AssetSync
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "Install a config/asset_sync.yml and the asset:precompile rake task enhancer"
5
+
6
+ # Commandline options can be defined here using Thor-like options:
7
+ class_option :my_opt, :type => :boolean, :default => false, :desc => "My Option"
8
+
9
+ # I can later access that option using:
10
+ # options[:my_opt]
11
+
12
+ def self.source_root
13
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
14
+ end
15
+
16
+ def generate_config
17
+ copy_file "asset_sync.yml", "config/asset_sync.yml"
18
+ end
19
+
20
+ def generate_rake_task
21
+ copy_file "asset_sync.rake", "lib/tasks/asset_sync.rake"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ Rake::Task["assets:precompile"].enhance do
2
+ AssetSync::Assets.sync
3
+ end
@@ -0,0 +1,11 @@
1
+ development:
2
+ access_key_id: 'MY_ACCESS_KEY'
3
+ secret_access_key: 'MY_ACCESS_SECRET'
4
+ bucket: "my_bucket"
5
+ existing_remote_files: keep
6
+
7
+ production:
8
+ access_key_id: 'MY_ACCESS_KEY'
9
+ secret_access_key: 'MY_ACCESS_SECRET'
10
+ bucket: "my_bucket"
11
+ existing_remote_files: delete
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asset_sync
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Simon Hamilton
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-30 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: fog
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ version_requirements: *id002
37
+ description: 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.
38
+ email:
39
+ - shamilton@rumblelabs.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - README.md
50
+ - Rakefile
51
+ - asset_sync.gemspec
52
+ - lib/asset_sync.rb
53
+ - lib/asset_sync/asset_sync.rb
54
+ - lib/asset_sync/version.rb
55
+ - lib/generators/asset_sync/install_generator.rb
56
+ - lib/generators/asset_sync/templates/asset_sync.rake
57
+ - lib/generators/asset_sync/templates/asset_sync.yml
58
+ homepage: ""
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project: asset_sync
81
+ rubygems_version: 1.7.2
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Synchronises Assets between Rails and S3
85
+ test_files: []
86
+