Skip to content

Commit

Permalink
Refractor into Capistrano::Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Aug 21, 2020
1 parent 9906e69 commit bfeb0a1
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 206 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CHANGELOG
---------

- Unreleased
- Refractor into a `Capistrano::Plugin`

- **0.9.0** - August 19, 2020
- Gem Initial Release
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,25 @@ You can also use the `$PRECOMPILE` environment variable
$ PRECOMPILE_MODE="local" cap production deploy
```

You can also set the capistrano `:precompile` variable for fully automated deploys
Or for fully automated deploys

```ruby
# config/deploy.rb
# Capfile

set :precompile_mode, "local"
ENV['PRECOMPILE_MODE'] = 'local'
require 'capistrano/precompile_chooser'
```

## Configuration Options,

```ruby
### For Local Precompile Only
set :assets_dir, "public/assets"
set :packs_dir, "public/packs"
set :rsync_cmd, "rsync -av --delete"
set :assets_role, "web"
```

## Task Ordering, and Task Details

<a href="https://github.com/westonganger/capistrano-precompile-chooser/blob/master/lib/capistrano/precompile_chooser.rb">See Source</a>

# TODO

- Add Tests
- Optional Precompilation Based on Diff - **PR WANTED**

# Credits

Created & Maintained by [Weston Ganger](https://westonganger.com) - [@westonganger](https://github.com/westonganger)
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ RSpec::Core::RakeTask.new(:spec)

task test: :spec

task default: :spec

task :console do
require 'capistrano'
require 'capistrano/precompile_chooser'
Expand Down
88 changes: 70 additions & 18 deletions lib/capistrano/precompile_chooser.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,74 @@
val = nil
require "capistrano/plugin"
require "capistrano/precompile_chooser/version"

opts = ['local','remote','skip']
module Capistrano
class PrecompileChooser < Capistrano::Plugin

until val && opts.include?(val)
puts
puts "How do you want to compile assets? (#{opts.join('/')})"
val = STDIN.gets.strip.downcase
end
# ### Currently using `namespace :load` in rake file instead
# def set_defaults
# case precompile_mode
# when "local"
# # set_if_empty :foo, :bar
# when "remote"
# # Do nothing
# else
# # Do nothing
# end
# end

def define_tasks
case precompile_mode
when "local"
eval_rakefile File.expand_path("../precompile_chooser/local_precompile.rake", __FILE__)
when "remote"
require 'capistrano/rails/assets' ### Disable for local precompile
else
# Do nothing
end
end

def register_hooks
case precompile_mode
when "local"
after "bundler:install", "deploy:assets:local_precompile:prepare"
after "deploy:assets:local_precompile:prepare", "deploy:assets:local_precompile:rsync"
after "deploy:assets:local_precompile:rsync", "deploy:assets:local_precompile:cleanup"
else
# Do nothing
end
end

def precompile_mode
@precompile_mode ||= begin
if ENV['PRECOMPILE_MODE']
if !valid_mode?(ENV['PRECOMPILE_MODE'])
raise "Invalid Precompile Mode"
end
else
val = nil

until valid_mode?(val)
puts
puts "How do you want to compile assets? (#{PRECOMPILE_MODES.join('/')})"
val = STDIN.gets.strip.downcase
end

puts
end

puts

case val
when "local"
#import 'config/deploy/local_precompile.cap'
#import File.expand_path("../tasks/local_precompile.cap")
load File.expand_path("../tasks/local_precompile.cap")
when "remote"
require 'capistrano/rails/assets' ### Disable for local precompile
else
# Do nothing
val
end
end

private

def valid_mode?(mode)
mode && PRECOMPILE_MODES.include?(mode)
end

PRECOMPILE_MODES = ['local','remote','skip'].freeze

end
end

install_plugin Capistrano::PrecompileChooser
49 changes: 0 additions & 49 deletions lib/capistrano/precompile_chooser/local_precompile.cap

This file was deleted.

53 changes: 53 additions & 0 deletions lib/capistrano/precompile_chooser/local_precompile.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace :load do
task :defaults do
set_if_empty :assets_dir, "public/assets"
set_if_empty :packs_dir, "public/packs"
set_if_empty :rsync_cmd, "rsync -av --delete"
set_if_empty :assets_role, "web"
end
end

namespace :deploy do
namespace :assets do
namespace :local_precompile do

desc "Remove all local precompiled assets"
task :cleanup do
run_locally do
execute "rm", "-rf", fetch(:assets_dir)
execute "rm", "-rf", fetch(:packs_dir)
end
end

desc "Actually precompile the assets locally"
task :prepare do
run_locally do
execute "RAILS_ENV=#{fetch(:rails_env)} bundle exec rake assets:clean"
execute "RAILS_ENV=#{fetch(:rails_env)} bundle exec rake assets:precompile"
end
end

desc "Performs rsync to app servers"
task :rsync do
on roles(fetch(:assets_role)), in: :parallel do |server|
run_locally do
remote_shell = %(-e "ssh -p #{server.port}") if server.port

commands = []
commands << "#{fetch(:rsync_cmd)} #{remote_shell} ./#{fetch(:assets_dir)}/ #{server.user}@#{server.hostname}:#{release_path}/#{fetch(:assets_dir)}/" if Dir.exists?(fetch(:assets_dir))
commands << "#{fetch(:rsync_cmd)} #{remote_shell} ./#{fetch(:packs_dir)}/ #{server.user}@#{server.hostname}:#{release_path}/#{fetch(:packs_dir)}/" if Dir.exists?(fetch(:packs_dir))

commands.each do |command|
if dry_run?
SSHKit.config.output.info command
else
execute command
end
end
end
end
end

end
end
end
4 changes: 3 additions & 1 deletion lib/capistrano/precompile_chooser/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "capistrano/plugin"

module Capistrano
module PrecompileChooser
class PrecompileChooser < Capistrano::Plugin
VERSION = "0.9.0"
end
end
57 changes: 0 additions & 57 deletions spec/configuration_spec.rb

This file was deleted.

68 changes: 0 additions & 68 deletions spec/integration_spec.rb

This file was deleted.

Loading

0 comments on commit bfeb0a1

Please sign in to comment.