lib/rake/funnel/tasks/environments.rb in rake-funnel-0.1.0.pre vs lib/rake/funnel/tasks/environments.rb in rake-funnel-0.2.0.pre

- old
+ new

@@ -1,45 +1,52 @@ +require 'configatron' require 'rake/tasklib' Dir["#{File.dirname(__FILE__)}/environments_support/*.rb"].each do |path| require path end module Rake::Funnel::Tasks class Environments < Rake::TaskLib include Rake::Funnel::Support::Environments - attr_accessor :base_dir, :default_env, :default_config, :local_config, :customizer + attr_accessor :base_dir, :store, :default_env, :default_config, :local_config, :customizer - def initialize + def initialize(*args, &task_block) + setup_ivars(args) + + define(args, &task_block) + end + + private + def setup_ivars(_args) + @store = configatron @default_env = nil @default_config = 'default' @local_config = 'local' @base_dir = 'config' - - yield self if block_given? - define - default_environment_setup - - self end - private - def define + def define(args, &task_block) + task_block.call(*[self].slice(0, task_block.arity)) if task_block + environments.each do |env| desc "Configure for the #{env[:name]} environment" - task env[:name] do - Loader.load_configuration(env, configatron, customizer) + + task(env[:name], *args) do |_, task_args| + Loader.load_configuration(env, store, customizer) end end + + default_environment_setup end def environments - default = File.join(@base_dir, config_ext(default_config)) - local = File.join(@base_dir, config_ext(local_config)) + default = File.join(base_dir, config_ext(default_config)) + local = File.join(base_dir, config_ext(local_config)) - Dir[File.join(@base_dir, config_ext('*'))] + Dir[File.join(base_dir, config_ext('*'))] .reject { |config| config == default || config == local } .map do |config| { name: File.basename(config, '.*'), config_files: [ @@ -54,22 +61,28 @@ def config_ext(name) "#{name}.yaml" end def default_environment_setup - return unless @default_env + return unless default_env - tasks = user_defined_env_tasks - if tasks.empty? - tasks = [] << Rake.application.current_scope.path_with_task_name(@default_env) + top_level_envs = top_level_env_tasks + if top_level_envs.empty? + task = Rake.application.current_scope.path_with_task_name(default_env) + prepend_task(task) + else + top_level_envs.each do |task| + Rake.application.top_level_tasks.delete(task) + prepend_task(task) + end end + end - tasks.each do |task| - Rake::Task[task].invoke - end + def prepend_task(task) + Rake.application.top_level_tasks.unshift(task) end - def user_defined_env_tasks + def top_level_env_tasks expect_user_defined = environments.map { |env| Rake.application.current_scope.path_with_task_name(env[:name]) } Rake.application.top_level_tasks.select { |t| expect_user_defined.include?(t) } end end end