module RJack::TarPit::LineMatchTaskDefiner

Attributes

history_date_regexp[RW]

Regexp to use for a valid/releasable date on History

history_regexp[RW]

Regexp to use for first line of History file with version, date

history_version_regexp[RW]

Proc returning regexp given gem version as parameter, to use to validate the version of the latest history entry.

init_files[RW]

Array of “init” files to check for gem version references in (default: [ init/<spec.name> ], if exists)

init_line_regexp[RW]

Proc given spec and returning regexp matching gem line to find in init_files. (default: /^gem.+<spec.name>/)

init_version_regexp[RW]

Proc returning regexp given gem version as parameter, to use to validate the gem version of gem line in init_files. (default: /= <spec.version>/)

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/rjack-tarpit/line_match.rb, line 44
def initialize
  super

  @history_regexp         = /^==/
  @history_date_regexp    = /\([0-9\-]+\)$/
  @history_version_regexp = Proc.new { |v| / #{v} / }

  @init_files             = :default
  @init_line_regexp       = Proc.new { |s| /^gem.+#{s.name}/ }
  @init_version_regexp    = Proc.new { |v| /= #{v}/ }

  add_define_hook( :define_line_match_tasks )
end

Public Instance Methods

define_line_match_tasks() click to toggle source
# File lib/rjack-tarpit/line_match.rb, line 58
def define_line_match_tasks

  if spec.history_file && history_regexp
    desc "Check that #{spec.history_file} has latest version"
    task :check_history_version do
      test_line_match( spec.history_file,
                       history_regexp,
                       history_version_regexp.call( spec.version ) )
    end
    [ :gem, :tag, :push ].each { |t| task t => :check_history_version }
  end

  if spec.history_file && history_date_regexp
    desc "Check that #{spec.history_file} has a date for release"
    task :check_history_date do
      test_line_match( spec.history_file,
                       history_regexp,
                       history_date_regexp )
    end
    [ :tag, :push ].each { |t| task t => :check_history_date }
  end

  if init_files == :default
    self.init_files = [ File.join( 'init', spec.name ) ].
      select { |f| File.exist?( f ) }
  end

  init_files.each do |inf|
    desc "Check that #{init_files.join(", ")} has latest version"
    task :check_init_version do
      test_line_match( inf,
                       init_line_regexp.call( spec ),
                       init_version_regexp.call( spec.version ) )
    end
  end

  unless init_files.empty?
    [ :gem, :tag ].each { |t| task t => :check_init_version }
  end

end
test_line_match( files, line_regex, pass_line_regex = // ) click to toggle source

Test that all specified files have at least one line matching line_regex, and that first line additionally matches (optional) pass_line_regex.

Parameters

files<Array(~)>

List of files to test

line_regex<Regexp>

Test first matching line

pass_line_regex

Further test on match line (default: match all)

Raises

RuntimeError

on test failure.

# File lib/rjack-tarpit/line_match.rb, line 109
def test_line_match( files, line_regex, pass_line_regex = // )
  Array( files ).each do |mfile|
    found = false
    open( mfile ) do |mf|
      num = 0
      mf.each do |line|
        num += 1
        if line =~ line_regex
          found = true
          unless line =~ pass_line_regex
            raise( "%s:%d: %s !~ %s" %
                   [ mfile, num, line.strip, pass_line_regex.inspect ] )
          end
          break
        end
      end
    end
    unless found
      raise "#{mfile}: #{line_regex.inspect} not found"
    end
  end
end