module Bones::Helpers

Constants

DEV_NULL
GEM
HAVE
HAVE_SVN
RCOV
RDOC
SUDO

Public Instance Methods

find_file( *args ) click to toggle source

Given a list of filenames, return the first one that resolves to an existing file on the filesystem. This allows us to specify a list of valid files for the README and HISTORY files then pick the one that actually exists on the user's filesystem.

# File lib/bones/helpers.rb, line 99
def find_file( *args )
  args.each {|fn| return fn if test(?f, fn)}
  args.first
end
have?( key, &block ) click to toggle source
# File lib/bones/helpers.rb, line 17
def have?( key, &block )
  return HAVE[key] if block.nil?
  HAVE[key] = block.call
end
paragraphs_of( path, *args ) click to toggle source

Reads the file located at the given path and returns an array of the desired paragraphs. The paragraphs can be given as a single section title or any number of paragraph numbers or ranges.

For example:

changes = paragraphs_of('History.txt', 0..1).join("\n\n")
summary, *description = paragraphs_of('README.md', 3, 3..8)
features = paragraphs_of('README.md', 'features').join("\n\n")
examples = paragraphs_of('README.md', 'examples').join("\n\n")
# File lib/bones/helpers.rb, line 44
def paragraphs_of( path, *args )
  return [] unless test(?f, path)

  title = String === args.first ? args.shift : nil
  paragraphs = File.read(path).delete("\r").split(/\n\n+/)

  if title.nil? then
    title = '.+'
    single_section = false
  else
    title = Regexp.escape(title)
    single_section = true
  end

  start_rgxp = [/\A[=#]+\s*#{title}/i, /\A#{title}\n[=-]+\s*\Z/i]
  end_rgxp   = [/\A[=#]+/i, /\A.+\n[=-]+\s*\Z/i]

  result = []
  matching = false
  rgxp = start_rgxp

  paragraphs.each do |p|
    if rgxp.any? { |r| p =~ r }
      if matching && single_section
        break
      end
      matching = true
      rgxp = end_rgxp
      next
    end

    if matching
      result << p
    end
  end

  args.empty? ? result : result.values_at(*args)
end
quiet( &block ) click to toggle source
# File lib/bones/helpers.rb, line 22
def quiet( &block )
  io = [STDOUT.dup, STDERR.dup]
  STDOUT.reopen DEV_NULL
  STDERR.reopen DEV_NULL
  block.call
ensure
  STDOUT.reopen io.first
  STDERR.reopen io.last
  io.each {|x| x.close}
end
remove_desc_for_task( names ) click to toggle source

Find a rake task using the task name and remove any description text. This will prevent the task from being displayed in the list of available tasks.

# File lib/bones/helpers.rb, line 86
def remove_desc_for_task( names )
  Array(names).each do |task_name|
    task = Rake.application.tasks.find {|t| t.name == task_name}
    next if task.nil?
    task.instance_variable_set :@comment, nil
  end
end