class Detroit::Syntax

The Syntax tool simply checks all Ruby code for syntax errors. It is a rather trivial tool, and is here mainly for example sake.

NOTE: This method shells out to the command line using ‘ruby -c`.

Attributes

exclude[RW]

Globs to exclude.

extra[RW]

Extra options to append to ‘ruby -c` command.

files[RW]

Files to check.

ignore[RW]

Files to ignore based on file name patterns.

loadpath[RW]

Add these folders to the $LOAD_PATH.

Public Class Methods

man_page() click to toggle source
# File lib/detroit-syntax.rb, line 177
def self.man_page
  File.dirname(__FILE__)+'/../man/detroit-syntax.5'
end

Public Instance Methods

assemble(station, options={}) click to toggle source

Attach check method to test station.

# File lib/detroit-syntax.rb, line 53
def assemble(station, options={})
  case station
  when :test
    check
  end
end
assemble?(station, options={}) click to toggle source

Attaches check method to test station.

# File lib/detroit-syntax.rb, line 45
def assemble?(station, options={})
  case station
  when :test
    true
  end
end
check() click to toggle source

Verify syntax of ruby scripts.

# File lib/detroit-syntax.rb, line 74
def check
  list = run_syntax_check

  if log && (logfile.outofdate?(*files) or force?)
    log_syntax_errors(list)
  end

  abort "Syntax errors found." if list.size > 0

  return true
end
log() click to toggle source

Log syntax errors?

# File lib/detroit-syntax.rb, line 37
def log
  @log
end
log=(file_or_bool) click to toggle source

File name of log file or true to use default ‘log/syntax.rdoc` file.

# File lib/detroit-syntax.rb, line 32
def log=(file_or_bool)
  @log = file_or_bool
end
logfile() click to toggle source

If log is given save results to this log file.

# File lib/detroit-syntax.rb, line 64
def logfile
  case log
  when String
    Pathname.new(log)
  else
    project.log + 'syntax.rdoc'
  end
end

Private Instance Methods

gather_files() click to toggle source

Collect files to be checked.

# File lib/detroit-syntax.rb, line 112
def gather_files
  amass(files.to_list, exclude.to_list, ignore.to_list)
end
initialize_defaults() click to toggle source
# File lib/detroit-syntax.rb, line 170
def initialize_defaults
  @loadpath = metadata.loadpath
  @exclude  = []
end
log_syntax_errors(list) click to toggle source

Create syntax log.

# File lib/detroit-syntax.rb, line 142
def log_syntax_errors(list)
  #logfile = project.log + 'syntax.log'
  mkdir_p(logfile.parent)
  begin
    file = File.open(logfile, 'w+')
    file << "= SYNTAX ERROR LOG\n"
    file << "\n(#{Time.now})\n\n"
    if list.empty?
      file << "No Syntax Errors."
    else
      list.each do |file|
        err = `ruby -c #{opt_I} #{extra} #{file} 2>&1`
        file << "== #{file}\n#{err}\n\n"
      end
    end
    file << "\n\n"
  ensure
    file.close
  end

end
opt_I() click to toggle source
# File lib/detroit-syntax.rb, line 165
def opt_I
  loadpath.map{ |r| "-I#{r}" }.join(' ')
end
run_syntax_check() click to toggle source
# File lib/detroit-syntax.rb, line 89
def run_syntax_check
  files = gather_files
  files = files.select{ |f| File.extname(f) == '.rb' }

  max   = files.collect{ |f| f.size }.max
  list  = []

  puts "Started"

  start = Time.now

  files.each do |file|
    pass = syntax_check_file(file, max)
    list << file if !pass
  end

  puts "\nFinished in %.6f seconds." % [Time.now - start]
  puts "\n#{list.size} Syntax Errors"

  return list
end
syntax_check_file(file, max=nil) click to toggle source

Check a file.

# File lib/detroit-syntax.rb, line 117
def syntax_check_file(file, max=nil)
  return unless File.file?(file)
  max  = max || file.size + 2
  #libs = loadpath.join(';')
  #r = system "ruby -c -Ibin:lib:test #{s} &> /dev/null"
  r = system "ruby -c #{opt_I} #{extra} #{file} > /dev/null 2>&1"
  if r
    if verbose?
      printline("%-#{max}s" % file, "[PASS]")
    else
      print '.'
    end
    true
  else
    if verbose?
      printline("%-#{max}s" % file, "[FAIL]")
      #puts("%-#{max}s  [FAIL]" % [s])
    else
      print 'E'
    end
    false
  end
end