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

Merge #20 and add Assets compile environment #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ And then:
Add this line to `Capfile`, after `require 'capistrano/rails/assets'`

require 'capistrano/faster_assets'

If U want to add some environment variable when compile assets, add `assets_env` in your stage file.

```ruby
set :assets_env, { doit: false }
```

### Warning

Expand Down
31 changes: 21 additions & 10 deletions lib/capistrano/tasks/faster_assets.rake
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ namespace :deploy do
task :precompile do
on roles(fetch(:assets_roles)) do
within release_path do
with rails_env: fetch(:rails_env) do
# assets_env should be a hash, ie: assets_env: { skip_it: false }
_assets_env = fetch(:assets_env, {}).merge(rails_env: fetch(:rails_env))
with _assets_env do
begin
# find the most recent release
# find the most recent release
latest_release = capture(:ls, '-xr', releases_path).split[1]

# precompile if this is the first deploy
Expand All @@ -29,20 +31,29 @@ namespace :deploy do
execute(:ls, latest_release_path.join('assets_manifest_backup')) rescue raise(PrecompileRequired)

fetch(:assets_dependencies).each do |dep|
release = release_path.join(dep)
latest = latest_release_path.join(dep)
# skip if both directories/files do not exist
next if [release, latest].map{|d| test "[ -e #{d} ]"}.uniq == [false]
release = release_path.join(dep)
latest = latest_release_path.join(dep)

# skip if both directories/files do not exist
next if [release, latest].map{|d| test "[ -e #{d} ]"}.uniq == [false]

# execute raises if there is a diff
execute(:diff, '-Nqr', release, latest) rescue raise(PrecompileRequired)
end

info("Skipping asset precompile, no asset diff found")

# copy over all of the assets from the last release
execute(:cp, '-r', latest_release_path.join('public', fetch(:assets_prefix)), release_path.join('public', fetch(:assets_prefix)))
release_asset_path = release_path.join('public', fetch(:assets_prefix))
# skip if assets directory is symlink
begin
execute(:test, '-L', release_asset_path.to_s)
rescue
# copy over all of the assets from the last release
execute(:cp, '-r', latest_release_path.join('public', fetch(:assets_prefix)), release_asset_path.parent)
end

# copy assets if manifest file is not exist (this is first deploy after using symlink)
execute(:ls, release_asset_path.join('manifest*')) rescue raise(PrecompileRequired)
rescue PrecompileRequired
execute(:rake, "assets:precompile")
end
Expand Down