class Tool

Abstract base class for tools. Provides basic default settings, allows control over the level of logging to standard out, and installs a sig int handler.

Constants

INVALID_OPTS

Public Class Methods

new(opt,out=STDOUT) click to toggle source
# File lib/shed/tool.rb, line 12
def initialize(opt,out=STDOUT)
  @src      = opt[:src] || '.'
  @output   = opt[:output] || 'tool-shed.txt'
  @verbose  = opt[:verbose] || false
  @silent   = opt[:silent] || false
  @excludes = opt[:excludes] || ['.svn','.git', 'bin', 'bin-debug']
  @out      = out

  add_sigint_handler
end

Public Instance Methods

add_sigint_handler() click to toggle source

Installs a sigint handler.

# File lib/shed/tool.rb, line 66
def add_sigint_handler
  trap 'INT' do
    puts '\nCancelled. Bye Bye!'
    exit!
  end
end
generated_at() click to toggle source

Generate a timestamp to include in reports.

# File lib/shed/tool.rb, line 59
def generated_at
  "Generated at " + Time.now.strftime("[%m/%d/%Y %H:%M:%S]")
end
log(msg) click to toggle source

Puts the message if we are in verbose mode (but not in silent mode).

# File lib/shed/tool.rb, line 33
def log(msg)
  puts msg if @verbose
end
puts(msg) click to toggle source

Puts the message unless we are in silent mode.

# File lib/shed/tool.rb, line 26
def puts(msg)
  @out.puts msg unless @silent
end
to_disk(str) click to toggle source

Write the requested string to the output file.

# File lib/shed/tool.rb, line 47
def to_disk(str)
  file = File.open(@output, "w")
  file.puts str
  file.flush
  file.close

  puts "Saved result to #{File.expand_path(@output)}."
end
valid_opts() click to toggle source

Validates the opts the tool has been invoked with.

# File lib/shed/tool.rb, line 40
def valid_opts
  true
end

Protected Instance Methods

add_desc(heading,list) click to toggle source

Describes a list of collected data.

# File lib/shed/tool.rb, line 78
def add_desc(heading,list)
  description = "\n\n#{heading}: #{list.length.to_s}\n\n\t"
  description << list.join("\n\t") unless list.empty?
  description
end
do_exit(msg='') click to toggle source

Log an error message and raise exit.

# File lib/shed/tool.rb, line 114
def do_exit(msg='')
  @out.puts "#{INVALID_OPTS} #{msg}"
  exit
end
scan_dirs(extension,dir,syntax_regex) click to toggle source

Scans directories for all files that match the regex, and for each match goes on to scan that document for items matching the syntax regex.

# File lib/shed/tool.rb, line 100
def scan_dirs(extension,dir,syntax_regex)
  declarations = []

  Search.find_all(extension,dir,@excludes) do |path|
    declarations << scan_doc(path,syntax_regex)
  end

  declarations.flatten!.sort!.uniq! unless declarations.empty?
  declarations
end
scan_doc(path,regex) click to toggle source

Opens the document specified by path and returns a list of all first capture group matches, after stripping comments.

# File lib/shed/tool.rb, line 88
def scan_doc(path,regex)
  caputres = []
  file = File.open(path,"r").read.strip
  file = Stripper.comments(file)
  file.scan(regex) { caputres << $1 }
  caputres
end