# encoding: utf-8 require 'fedux_org_stdlib/require_files' require_library %w{ json rake rake/tasklib shellwords active_support/core_ext/object/blank active_support/core_ext/string/inflections} require 'fedux_org_stdlib/rake/exceptions' require 'fedux_org_stdlib/logging/logger' module FeduxOrgStdlib module Rake # Rake Task # # This class is mainly used as basis for more specific class like # {Shelltask} or {Projecttask}. # # @see Rakefile class Task < ::Rake::TaskLib include ::Rake::DSL if defined?(::Rake::DSL) # @!attribute [r] name # Name of task. attr_reader :name # @!attribute [r] description # A description for the task attr_reader :description # @!attribute [r] verbose (true) # Use verbose output. If this is set to true, the task will print the # executed spec command to stdout. attr_reader :verbose private attr_reader :task_arguments, :task_block, :logger, :working_directory # Create task instance # # @param [String] description # A description for the task # # @param [String] name # The name for the task (including namespace), e.g. namespace1:task1 # # @param [Array] arguments # Arguments for the task. Look # [here](http://viget.com/extend/protip-passing-parameters-to-your-rake-tasks) # for a better description on how to use arguments in rake tasks # # @param [true,false] define_in_ci_mode # Should the task be defined and therefor available when in Continous # Integration (CI)-mode + all needed dependencies (gems etc.)? It is # best not to include some rubygems (debugger etc.) when running in # CI-mode. CI-mode is detected by looking for a defined environment variable # 'CI' (ENV['CI']). # # @param [true,false] activate_ci_mode # Activate CI-mode when task is called. # # @param [Array] dependencies # An array of other tasks this task depends on. # # @yield # A block which is called before the "run_task"-method is called. The # parameters it taskes depends on the number of parameters the block # can take. If the block is defined which two parameters, it takes two # parameters from the paramter 'arguments'. def initialize( description:, name: self.class.to_s.split(/::/).slice(-2..-1).join(':').gsub(/Task$/, '').underscore, arguments: [], define_in_ci_mode: true, activate_ci_mode: false, dependencies: [], &task_block ) before_initialize @description = description @task_arguments = Array(arguments) @task_block = task_block @logger = FeduxOrgStdlib::Logging::Logger.new @working_directory = Dir.getwd @activate_ci_mode = activate_ci_mode if dependencies.blank? @name = name else @name = { name: Array(dependencies).map(&:to_sym) } end after_initialize define_task unless running_in_ci_mode? define_task if define_in_ci_mode? and running_in_ci_mode? end private # Activate ci mode def activate_ci_mode ENV['CI'] = 'true' end # Run code after initialize def after_initialize; end # Run code before initialize def before_initialize; end # Define task def define_task desc description unless ::Rake.application.last_comment task name, *task_arguments do |_, task_args| activate_ci_mode if activate_ci_mode? RakeFileUtils.__send__(:verbose, verbose) do instance_exec(*[self, task_args].slice(0, task_block.arity), &task_block) if task_block.respond_to? :call run_task verbose end end end # Run code if task is executed def run_task(verbose); end # Should this task be defined in continous integration mode def define_in_ci_mode? @define_in_ci_mode == true end # Check if running in continous integration mode def running_in_ci_mode? ENV.key? 'CI' end # Check if continous integration mode is activated def activate_ci_mode? @activate_ci_mode == true end public # Binding to instance def get_binding binding end # Include module in instance def include(modules) modules = Array(modules) modules.each { |m| self.class.include m } end end end end