lib/reap/task.rb in reap-0.5.0 vs lib/reap/task.rb in reap-0.6.1

- old
+ new

@@ -1,109 +1,169 @@ require 'rbconfig' +require 'yaml' +require 'fileutils' -module Reap +require 'rake' +require 'rake/tasklib' - # Base class for all tasks. - # - # When creating a specifc task, subclass this one and create two class-level - # methods #task and #desc. #task will return the name of the task and - # #desc a description of the task. Then create an instance method called - # #setup to do any initial preping. Usually that means assigning - # some instance vars from the Reapfile configutration. You can use - # section[<name>] for settings from the task's particular section (as - # determined by self.task method above) and/or use master[<name>] - # for top-level settings. Finally write a #run method that preforms the - # actual task. - # - # Here's an oh so simple example: - # - # class MyTask < Task - # def self.task ; 'mytask' ; end - # def self.desc ; 'This is my first custom task.' ; end - # - # def setup - # @message = section['message'] || master['default'] || 'None Found!' - # end - # - # def run - # puts @message - # end - # end - # - # This corresponding settings in the Reapfile will then be: - # - # default: Default text, if any. - # - # mytask: - # message: Message text, if any. - # - # Reap automatically handles multiple task runs if an array is provided - # under the task section instead of a single mapping. For example: - # - # mytask: - # - - # message: First Run. - # - - # message: Second Run. - # - class Task +#require 'mega/filelist' +require 'nano/hash/traverse' - RUBY = Config::CONFIG['ruby_install_name'] +module Reap ; end - DEFAULT_INCLUDE = [ '[A-Z]*', 'setup.rb', 'lib/**/*', 'bin/**/*', 'data/**/*', - 'doc/**/*', 'rdoc/**/*', 'test/**/*', 'bench/**/*', - 'demo/**/*', 'samples/**/*', 'examples/**/*' ] +# Base class for reap tasks. +# +# When creating a specifc task, subclass this one and create two class-level +# methods #task and #desc. #task will return the name of the task and +# #desc a description of the task. Then create an instance method called +# #setup to do any initial preping. Usually that means assigning +# some instance vars from the Reapfile configutration. You can use +# section[<name>] for settings from the task's particular section (as +# determined by self.task method above) and/or use master[<name>] +# for top-level settings. Finally write a #run method that preforms the +# actual task. +# +# Here's an oh so simple example: +# +# class MyTask < Task +# def self.task ; 'mytask' ; end +# def self.desc ; 'This is my first custom task.' ; end +# +# def setup +# @message = section['message'] || master['default'] || 'None Found!' +# end +# +# def run +# puts @message +# end +# end +# +# This corresponding settings in the Reapfile will then be: +# +# default: Default text, if any. +# +# mytask: +# message: Message text, if any. +# +# Reap automatically handles multiple task runs if an array is provided +# under the task section instead of a single mapping. For example: +# +# mytask: +# - +# message: First Run. +# - +# message: Second Run. +# +class Reap::Task < Rake::TaskLib - MUST_EXCLUDE = [ 'InstalledFiles', '**/CVS/**/*', '**/*~' ] + RUBY = Config::CONFIG['ruby_install_name'] - class << self - def inherited( klass ) - (@task_registry ||= []) << klass - end - def registry ; @task_registry ||= [] ; end - def tasks ; @task ||= registry.collect { |t| t.task } ; end + DEFAULT_INCLUDE = [ '[A-Z]*', 'setup.rb', 'lib/**/*', 'bin/**/*', 'data/**/*', + 'doc/**/*', 'rdoc/**/*', 'test/**/*', 'bench/**/*', + 'demo/**/*', 'samples/**/*', 'examples/**/*' ] + + MUST_EXCLUDE = [ 'InstalledFiles', '**/CVS/**/*', '**/*~' ] + + + def initialize #:yield: + load_config + init + yield( self ) if block_given? + define + end + + def init + raise "not implemented" + end + + def define + raise "not implemented" + end + + attr_reader :config + + def load_config( config_file=nil ) + config_file ||= 'Reapfile' + if File.file?( config_file ) + # load config file + @config = ::YAML.load( File.open( config_file ) ).traverse{ |k,v| [k.downcase, v] } + # required main parameters + raise "TITLE is a required configuration field" unless @config['title'] + raise "NAME is a required configuration field" unless @config['name'] + raise "VERSION is a required configuration field" unless @config['version'] + else + @config = {} end + end - attr_reader :master, :section + def master ; @config ; end - def initialize( master_config, section_config ) - @master = master_config - @section = section_config - setup if respond_to?(:setup) + def section + sect = @config[self.class.section_name] || {} + end + + # Class Methods + + class << self + def section_name(n=nil) + @sname = n.to_s if n + @sname end + end - def [](x) ; @section[x] ; end - # Run the system command +cmd+. - # - # Example: - # sh %{ls -ltr} - # - def sh(cmd, options={}) - FileUtils.send( :fu_check_options, options, :noop, :verbose ) - FileUtils.send( :fu_output_message, cmd ) if options[:verbose] - unless options[:noop] - system(cmd) or fail "Command Failed: [#{cmd}]" - end +=begin + class << self + def inherited( klass ) + (@task_registry ||= []) << klass end + def registry ; @task_registry ||= [] ; end + def tasks ; @task ||= registry.collect { |t| t.task_name } ; end - # Run a Ruby interpreter with the given arguments. - # - # Example: - # ruby %{-pe '$_.upcase!' <README} - # - def ruby(*args) - if Hash === args.last - options = args.pop - else - options = {} - end - puts "#{RUBY} #{args.join(' ')}" if $DEBUG - sh "#{RUBY} #{args.join(' ')}", options + def task_name(n=nil) ; @tname = n.to_sym if n ; @tname ; end + def task_desc(d=nil) ; @tdesc = d.to_s if d ; @tdesc ; end + end + + attr_reader :master, :section + + def initialize( master_config, section_config ) + @master = master_config + @section = section_config + setup if respond_to?(:setup) + end + + def [](x) ; @section[x] ; end + + # Run the system command +cmd+. + # + # Example: + # sh %{ls -ltr} + # + def sh(cmd, options={}) + FileUtils.send( :fu_check_options, options, :noop, :verbose ) + FileUtils.send( :fu_output_message, cmd ) if options[:verbose] + unless options[:noop] + system(cmd) or fail "Command Failed: [#{cmd}]" end + end + # Run a Ruby interpreter with the given arguments. + # + # Example: + # ruby %{-pe '$_.upcase!' <README} + # + def ruby(*args) + if Hash === args.last + options = args.pop + else + options = {} + end + puts "#{RUBY} #{args.join(' ')}" if $DEBUG + sh "#{RUBY} #{args.join(' ')}", options + end + # # Attempt to do a normal file link, but fall back # # to a copy if the link fails. # def safe_ln(*args) # if @link_not_supported # FileUtils.cp(*args) @@ -115,38 +175,37 @@ # FileUtils.cp(*args) # end # end # end - # Returns the full package name which combines package name and version. - def package_name - @config['name'] + '-' + ( @config['version'] || '0.0.1' ) - end + # Returns the full package name which combines package name and version. + def package_name + @config['name'] + '-' + ( @config['version'] || '0.0.1' ) + end - # def rakefile - # s = '' - # rf = File.join(locate, 'packmule', 'packmule.rake') - # File.open( rf ) { |f| s << f.gets(nil) } - # s - # end - # - # def yamlform - # s = '' - # yf = File.join(locate, 'packmule', 'packmule.yaml') - # File.open( yf ) { |f| s << f.gets(nil) } - # s - # end + # def rakefile + # s = '' + # rf = File.join(locate, 'packmule', 'packmule.rake') + # File.open( rf ) { |f| s << f.gets(nil) } + # s + # end + # + # def yamlform + # s = '' + # yf = File.join(locate, 'packmule', 'packmule.yaml') + # File.open( yf ) { |f| s << f.gets(nil) } + # s + # end - #def value( key, *alt_masters ) - # return section[key] if section.key?( key ) - # alt_masters.each do |path| - # parr = path.split(':').collect { |e| e.downcase } - # conf = @config - # parr.each { |e| conf = conf[e] } - # return conf if conf - # end - # nil - #end + #def value( key, *alt_masters ) + # return section[key] if section.key?( key ) + # alt_masters.each do |path| + # parr = path.split(':').collect { |e| e.downcase } + # conf = @config + # parr.each { |e| conf = conf[e] } + # return conf if conf + # end + # nil + #end +=end - end - -end #module Reap +end #class Reap::Task