lib/capistrano/multiconfig/configurations.rb in capistrano-multiconfig-0.0.4 vs lib/capistrano/multiconfig/configurations.rb in capistrano-multiconfig-3.0.0

- old
+ new

@@ -1,90 +1,49 @@ -Capistrano::Configuration.instance(true).load do - # configurations root directory - config_root = File.expand_path(fetch(:config_root, "config/deploy")) +module Capistrano + module Multiconfig + class Configurations + def self.find_names(root_path) + new(root_path).find_names + end - # list of configurations files - config_files = Dir["#{config_root}/**/*.rb"] + attr_reader :root_path - # remove configuration file if it's part of another configuration - config_files.reject! do |config_file| - config_dir = config_file.gsub(/\.rb$/, '/') - config_files.any? { |file| file[0, config_dir.size] == config_dir } - end + def initialize(root_path) + @root_path = root_path + end - # build configuration names list - config_names = config_files.map do |config_file| - config_file.sub("#{config_root}/", '').sub(/\.rb$/, '').gsub('/', ':') - end - - # ensure that configuration segments don't override any method, task or namespace - config_names.each do |config_name| - config_name.split(':').each do |segment| - if all_methods.any? { |m| m == segment } - raise ArgumentError, - "Config task #{config_name} name overrides #{segment.inspect} (method|task|namespace)" + # find configuration names + def find_names + files = scan_files + files.sort! + remove_shared_files!(files) + build_names(files) end - end - end - # create configuration task for each configuration name - config_names.each do |config_name| - segments = config_name.split(':') - namespace_names = segments[0, segments.size - 1] - task_name = segments.last + private - # create configuration task block. - # NOTE: Capistrano 'namespace' DSL invokes instance_eval that - # that pass evaluable object as argument to block. - block = lambda do |parent| - desc "Load #{config_name} configuration" - task(task_name) do - # set configuration name as :config_name variable - top.set(:config_name, config_name) + # Scan recursively root path + def scan_files + Dir["#{root_path}/**/*.rb"] + end - # recursively load configurations - segments.size.times do |i| - path = ([config_root] + segments[0..i]).join('/') + '.rb' - top.load(:file => path) if File.exists?(path) + # Remove path when there is the same directory with child. + # + # app/staging.rb (is shared configuration for 'alpha' and 'beta') + # app/staging/alpha.rb + # app/staging/beta.rb + def remove_shared_files!(files) + files.reject! do |file| + dir = file.gsub(/\.rb$/, '/') + files.any? { |f| f[0, dir.size] == dir } end end - end - # wrap task block into namespace blocks - # - # namespace_names = [nsN, ..., ns2, ns1] - # - # block = block0 = lambda do |parent| - # desc "DESC" - # task(:task_name) { TASK } - # end - # block = block1 = lambda { |parent| parent.namespace(:ns1, &block0) } - # block = block2 = lambda { |parent| parent.namespace(:ns2, &block1) } - # ... - # block = blockN = lambda { |parent| parent.namespace(:nsN, &blockN-1) } - # - block = namespace_names.reverse.inject(block) do |child, name| - lambda do |parent| - parent.namespace(name, &child) + # Convert "app/blog/production" to "app:blog:production" + def build_names(files) + files.map do |file| + file.sub("#{root_path}/", '').sub(/\.rb$/, '').gsub('/', ':') + end end end - - # create namespaced configuration task - # - # block = lambda do - # namespace :nsN do - # ... - # namespace :ns2 do - # namespace :ns1 do - # desc "DESC" - # task(:task_name) { TASK } - # end - # end - # ... - # end - # end - block.call(top) end - - # set configuration names list - set(:config_names, config_names) end