lib/reap/task.rb in reap-03.01 vs lib/reap/task.rb in reap-4.0

- old
+ new

@@ -1,20 +1,16 @@ -require 'rake' -require 'rake/tasklib' - require 'rbconfig' require 'fileutils' -#require 'mega/filelist' -require 'reap/projectinfo' +require 'facet/fileutils/safe_ln' +require 'facet/string/tabto' +require 'facet/module/basename' -module Reap ; VERSION = 3 ; end +require 'carat/filelist' -module ReapTask - extend self -end +require 'reap/projectinfo' # Base class for reap tasks. # # Here's an oh so simple example: # @@ -43,96 +39,149 @@ # TASK: mytask # message: Message text, if any. # module Reap - #def self.register( name, klass ) - # @registry ||= {} - # @registry[name.to_s.downcase] = klass - #end + def self.registry ; @registry ; end - #def self.registry ; @registry ; end + def self.register + @registry ||= {} + ObjectSpace.each_object(Class) { |klass| + if klass < ::Reap::Task + if klass.verify + @registry[klass.basename.downcase] = klass + end + end + } + end - class Task < ::Rake::TaskLib + def self.tasks ; @tasks ; end - RUBY = Config::CONFIG['ruby_install_name'] - - def self.register( name ) - klass = self.class - ReapTask.class_eval { - define_method( "#{name}_task" ) { |*args| klass.new(*args) } - } + def self.initialize + @tasks ||= {} + registry.each do |name, klass| + @tasks[name] = klass.new end + end - def master - ::ProjectInfo.info - end - #attr_reader :name, :description, :subtask + class Task - def initialize( task_name=nil, subtask=nil, &yld ) #:yield: - o = nil - o = yld.call(self) if yld - if String === o - begin - section( o ).each { |k,v| send("#{k}=", v ) } - end + include ::Config + include ::FileUtils + + RUBY = CONFIG['ruby_install_name'] + + # class methods + + def self.task_desc ; '(no desciption given)' ; end + def self.task_name ; basename.downcase ; end + + def self.section_required( val ) ; @section_required = val ;end + def self.section_required? ; @section_required ; end + + def self.verify + assume = true + if section_required? + assume = ::ProjectInfo.info.key?(task_name) end + assume + end - init + # instance methods - task_name ||= default_name - task_desc = @desc || default_desc + def task_name ; self.class.task_name ; end + def task_desc ; self.class.task_desc ; end - if subtask - desc task_desc - task task_name => subtask do - run + def master ; ::ProjectInfo.info ; end + + def section ; @section ; end + + def section_required? ; self.class.section_required? ; end + + def initialize + sect = master[task_name] + if sect.is_a?(Array) + sect.each do |s| + section_parameters_set( s ) + init end else - desc task_desc - task task_name do - run - end + section_parameters_set( sect || {} ) + init end end - def default_desc ; '(no desciption given)' ; end - #def default_name ; self.class.basename.downcase ; end + def section_parameters_set( s ) + @section = s + @section.each { |k,v| send("#{k}=", v) } + end def init - raise "not implemented" + raise "not implemented for '#{task_name}' task" end - def section( ymlstr ) - iy = YAML::load(ProjectInfo.info_stream + "\ntask:\n" + ymlstr.tabto(2)) - iy['task'] || {} + def run( *args ) + raise "no action defined for task #{task_name}" end + # task support methods + + def sh( arg ) + puts arg + system arg unless $PRETEND + end + +# o = nil +# o = yld.call(self) if yld +# if String === o +# begin +# section( o ).each { |k,v| send("#{k}=", v ) } +# end +# end +# +# init +# +# task_name ||= default_name +# task_desc = @desc || default_desc +# +# if subtask +# desc task_desc +# task task_name => subtask do +# run +# end +# else +# desc task_desc +# task task_name do +# run +# end +# end +# end + end #class Task end #module Reap ## # Convenience methods for built-in rake tasks ## -module RakeTask - extend self - - def gem_package_task(*args,&blk) - ::Rake::GemPackageTask.new(*args,&blk) - end - - def package_task(*args,&blk) - ::Rake::PackageTask.new(*args,&blk) - end - - def test_task(*args,&blk) - ::Rake::TestTask.new(*args,&blk) - end - - def rdoc_task(*args,&blk) - ::Rake::RDocTask.new(*args,&blk) - end - -end +# module RakeTask +# extend self +# +# def gem_package_task(*args,&blk) +# ::Rake::GemPackageTask.new(*args,&blk) +# end +# +# def package_task(*args,&blk) +# ::Rake::PackageTask.new(*args,&blk) +# end +# +# def test_task(*args,&blk) +# ::Rake::TestTask.new(*args,&blk) +# end +# +# def rdoc_task(*args,&blk) +# ::Rake::RDocTask.new(*args,&blk) +# end +# +# end