mirror of
https://github.com/capistrano/capistrano
synced 2023-03-27 23:21:18 -04:00
reduce dsl, prefer fetch/set
This commit is contained in:
parent
725c47d35b
commit
5e4fe624b5
6 changed files with 93 additions and 37 deletions
|
@ -1,18 +1,6 @@
|
|||
module Capistrano
|
||||
module DSL
|
||||
|
||||
def t(key)
|
||||
I18n.t(key, scope: :capistrano)
|
||||
end
|
||||
|
||||
def stages
|
||||
Dir["config/deploy/*.rb"].map { |f| File.basename(f, ".rb") }
|
||||
end
|
||||
|
||||
def stage_set?
|
||||
!!env.stage
|
||||
end
|
||||
|
||||
def before(task, prerequisite, *args, &block)
|
||||
prerequisite = Rake::Task.define_task(prerequisite, *args, &block) if block_given?
|
||||
Rake::Task[task].enhance [prerequisite]
|
||||
|
@ -25,43 +13,66 @@ module Capistrano
|
|||
end
|
||||
end
|
||||
|
||||
def configure_ssh_kit
|
||||
SSHKit.configure do |sshkit|
|
||||
sshkit.format = env.format
|
||||
sshkit.output_verbosity = env.log_level
|
||||
end
|
||||
end
|
||||
|
||||
def invoke(task)
|
||||
Rake::Task[task].invoke
|
||||
end
|
||||
|
||||
def fetch(key)
|
||||
env.fetch(key)
|
||||
def t(*args)
|
||||
I18n.t(*args, scope: :capistrano)
|
||||
end
|
||||
|
||||
def stages
|
||||
Dir['config/deploy/*.rb'].map { |f| File.basename(f, '.rb') }
|
||||
end
|
||||
|
||||
def stage_set?
|
||||
!!fetch(:stage, false)
|
||||
end
|
||||
|
||||
def configure_ssh_kit
|
||||
SSHKit.configure do |sshkit|
|
||||
sshkit.format = fetch(:format, :pretty)
|
||||
sshkit.output_verbosity = fetch(:log_level, :debug)
|
||||
sshkit.backend.configure do |backend|
|
||||
backend.pty = fetch(:pty, false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def fetch(key, default=nil)
|
||||
config.fetch(key, default)
|
||||
end
|
||||
|
||||
def set(key, value)
|
||||
env.set(key, value)
|
||||
config.set(key, value)
|
||||
end
|
||||
|
||||
def role(name)
|
||||
roles[name]
|
||||
def role(*args)
|
||||
config.roles.values_at(*args).flatten
|
||||
end
|
||||
|
||||
def all
|
||||
roles.values.flatten
|
||||
config.roles.values.flatten
|
||||
end
|
||||
|
||||
def configuration
|
||||
def config
|
||||
Env.configuration
|
||||
end
|
||||
|
||||
def env
|
||||
configuration
|
||||
def deploy_path
|
||||
"#{fetch(:deploy_to)}/current"
|
||||
end
|
||||
|
||||
def roles
|
||||
env.roles
|
||||
def shared_path
|
||||
"#{fetch(:deploy_to)}/shared"
|
||||
end
|
||||
|
||||
def deploy_user
|
||||
fetch(:user)
|
||||
end
|
||||
|
||||
def error(message)
|
||||
#TODO logging
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,11 +8,11 @@ module Capistrano
|
|||
|
||||
class << self
|
||||
def configure(&block)
|
||||
@env ||= new &block
|
||||
@env = new &block
|
||||
end
|
||||
|
||||
def configuration
|
||||
@env
|
||||
@env ||= configure
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -25,8 +25,8 @@ module Capistrano
|
|||
env[key] = value
|
||||
end
|
||||
|
||||
def fetch(value)
|
||||
env[value]
|
||||
def fetch(value, default=nil)
|
||||
env.fetch(value, default)
|
||||
end
|
||||
|
||||
def respond_to?(method)
|
||||
|
|
|
@ -10,6 +10,12 @@ en = {
|
|||
finishing: 'Finishing',
|
||||
finished: 'Finished',
|
||||
stage_not_set: 'Stage not set',
|
||||
written_stage_file: 'create config/deploy/%{stage}.rb'
|
||||
written_stage_file: 'create config/deploy/%{stage}.rb',
|
||||
error: {
|
||||
user: {
|
||||
does_not_exist: 'User %{user} does not exists',
|
||||
cannot_switch: 'Cannot switch to user %{user}'
|
||||
}
|
||||
}
|
||||
}
|
||||
I18n.backend.store_translations(:en, { capistrano: en })
|
||||
|
|
|
@ -7,4 +7,3 @@ stages.each do |stage|
|
|||
configure_ssh_kit
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -4,7 +4,8 @@ Capistrano::Env.configure do |config|
|
|||
config.role :app, %w{example.com}
|
||||
config.role :web, %w{example.com}
|
||||
config.role :db, %w{example.com}
|
||||
config.path '/var/www/my_app/current'
|
||||
config.deploy_to '/var/www/my_app'
|
||||
config.format :pretty # :dot
|
||||
config.log_level :debug # :info
|
||||
config.pty true
|
||||
end
|
||||
|
|
|
@ -36,6 +36,45 @@ module Capistrano
|
|||
|
||||
let(:env) { Env.new }
|
||||
|
||||
describe '#set' do
|
||||
it 'sets the value' do
|
||||
env.set(:host, 'example')
|
||||
expect(env.host).to eq 'example'
|
||||
end
|
||||
end
|
||||
|
||||
describe '#fetch' do
|
||||
context 'without default' do
|
||||
subject { env.fetch(:host) }
|
||||
|
||||
context 'value is set' do
|
||||
before do
|
||||
env.set(:host, 'example')
|
||||
end
|
||||
|
||||
it 'returns the value' do
|
||||
expect(subject).to eq 'example'
|
||||
end
|
||||
end
|
||||
|
||||
context 'value is not set' do
|
||||
it 'returns nil' do
|
||||
expect(subject).to eq nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'with default' do
|
||||
subject { env.fetch(:host, 'default') }
|
||||
|
||||
context 'value is not set' do
|
||||
it 'returns the default value' do
|
||||
expect(subject).to eq 'default'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#role" do
|
||||
before do
|
||||
SSHKit::Host.expects(:new).with('example.com').returns(app)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue