Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional execution on precompile #122

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions README.md
Expand Up @@ -306,21 +306,33 @@ With the new **user_env_compile** feature of Heroku (see above), this is no long

## Rake Task

A rake task is included within the **asset_sync** gem to enhance the rails precompile task by automatically running after it.
A rake task is included within the **asset_sync** gem to perform the sync:

``` ruby
namespace :assets do
desc "Synchronize assets to S3"
task :sync => :environment do
AssetSync.sync
end
end
```

If `AssetSync.config.run_on_precompile` is `true` (default), then assets will be uploaded to S3 automatically after the `assets:precompile` rake task is invoked:

``` ruby
# asset_sync/lib/tasks/asset_sync.rake
if Rake::Task.task_defined?("assets:precompile:nondigest")
Rake::Task["assets:precompile:nondigest"].enhance do
AssetSync.sync
Rake::Task["assets:sync"].invoke if defined?(AssetSync) && AssetSync.config.run_on_precompile
end
else
Rake::Task["assets:precompile"].enhance do
AssetSync.sync
Rake::Task["assets:sync"].invoke if defined?(AssetSync) && AssetSync.config.run_on_precompile
end
end
```

You can disable this behavior by setting `AssetSync.config.run_on_precompile = false`.

## Sinatra/Rack Support

You can use the gem with any Rack application, but you must specify two
Expand Down
3 changes: 3 additions & 0 deletions lib/asset_sync/config.rb
Expand Up @@ -14,6 +14,7 @@ class Invalid < StandardError; end
attr_accessor :prefix
attr_accessor :public_path
attr_accessor :enabled
attr_accessor :run_on_precompile

# FOG configuration
attr_accessor :fog_provider # Currently Supported ['AWS', 'Rackspace']
Expand Down Expand Up @@ -50,6 +51,7 @@ def initialize
self.always_upload = []
self.ignored_files = []
self.enabled = true
self.run_on_precompile = true
load_yml! if defined?(Rails) && yml_exists?
end

Expand Down Expand Up @@ -126,6 +128,7 @@ def load_yml!
self.fail_silently = yml["fail_silently"] if yml.has_key?("fail_silently")
self.always_upload = yml["always_upload"] if yml.has_key?("always_upload")
self.ignored_files = yml["ignored_files"] if yml.has_key?("ignored_files")
self.run_on_precompile = yml["run_on_precompile"] if yml.has_key?("run_on_precompile")

# TODO deprecate the other old style config settings. FML.
self.aws_access_key_id = yml["aws_access_key"] if yml.has_key?("aws_access_key")
Expand Down
13 changes: 11 additions & 2 deletions lib/tasks/asset_sync.rake
@@ -1,13 +1,22 @@
namespace :assets do
desc "Synchronize assets to S3"
task :sync => :environment do
AssetSync.sync
end
end

if Rake::Task.task_defined?("assets:precompile:nondigest")
Rake::Task["assets:precompile:nondigest"].enhance do
AssetSync.sync
# Conditional execution needs to be inside the enhance block because the enhance block
# will get executed before yaml or Rails initializers.
Rake::Task["assets:sync"].invoke if defined?(AssetSync) && AssetSync.config.run_on_precompile
end
else
Rake::Task["assets:precompile"].enhance do
# rails 3.1.1 will clear out Rails.application.config if the env vars
# RAILS_GROUP and RAILS_ENV are not defined. We need to reload the
# assets environment in this case.
# Rake::Task["assets:environment"].invoke if Rake::Task.task_defined?("assets:environment")
AssetSync.sync
Rake::Task["assets:sync"].invoke if defined?(AssetSync) && AssetSync.config.run_on_precompile
end
end
8 changes: 8 additions & 0 deletions spec/asset_sync_spec.rb
Expand Up @@ -16,6 +16,10 @@
end
end

it "should default to running on precompile" do
AssetSync.config.run_on_precompile.should be_true
end

it "should default AssetSync to enabled" do
AssetSync.config.enabled?.should be_true
AssetSync.enabled?.should be_true
Expand Down Expand Up @@ -70,6 +74,10 @@
AssetSync.enabled?.should be_true
end

it "should configure run_on_precompile" do
AssetSync.config.run_on_precompile.should be_false
end

it "should configure aws_access_key_id" do
AssetSync.config.aws_access_key_id.should == "xxxx"
end
Expand Down
1 change: 1 addition & 0 deletions spec/aws_with_yml/config/asset_sync.yml
Expand Up @@ -3,6 +3,7 @@ defaults: &defaults
aws_access_key_id: "xxxx"
aws_secret_access_key: "zzzz"
region: "eu-west-1"
run_on_precompile: false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This option makes sense, but shouldn't it default to running on precompile, as existing users expect?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@turadg It does default to running on precompile; if that run_on_precompile: false line is not present, it will run. I added it to this spec yaml file to test that it does not run when disabled. See lib/asset_sync/config.rb, line 54 for the default setting.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, sorry I didn't notice this asset_sync.yml was part of a test.


development:
<<: *defaults
Expand Down