class StoryboardLint::Linter

Public Class Methods

new(sb_scanner, source_scanner, matcher) click to toggle source
# File lib/storyboardlint.rb, line 256
def initialize(sb_scanner, source_scanner, matcher)
  if !sb_scanner
    raise ArgumentError, "The sb_scanner cannot be nil."
  end
  
  if !source_scanner
    raise ArgumentError, "The source_scanner cannot be nil."
  end
  
  if !matcher
    raise ArgumentError, "The matcher cannot be nil."
  end
  
  @matcher = matcher
  @sb_scanner = sb_scanner
  @source_scanner = source_scanner
end
run!(*args) click to toggle source
# File lib/storyboardlint.rb, line 312
def self.run!(*args)
  options = OpenStruct.new
  
  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: storyboardlint <target directory> [options]"
    opts.separator  ""
    opts.separator  "Options"

    opts.on("--storyboard-prefix [PREFIX]", "Storyboard IDs have to begin with PREFIX.") do |prefix|
      options.storyboard_prefix = prefix
    end
    
    opts.on("--storyboard-suffix [SUFFIX]", "Storyboard IDs have to end with SUFFIX") do |suffix|
      options.storyboard_suffix = suffix
    end
    
    opts.on("--segue-prefix [PREFIX]", "Segue IDs have to begin with PREFIX") do |prefix|
      options.segue_prefix = prefix
    end
    
    opts.on("--segue-suffix [SUFFIX]", "Segue IDs have to end with SUFFIX") do |suffix|
      options.segue_suffix = suffix
    end
    
    opts.on("--reuse-prefix [PREFIX]", "Reuse IDs have to begin with PREFIX") do |prefix|
      options.reuse_prefix = prefix
    end
    
    opts.on("--reuse-suffix [SUFFIX]", "Reuse IDs have to end with SUFFIX") do |suffix|
      options.reuse_suffix = suffix
    end
    
    opts.on( '--additional-sources /absolute/path,../relative/to/target_directory', Array, "List of additional directories to scan for source files") do |source_paths|
      options.additional_sources = source_paths
    end
    
    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    # Another typical switch to print the version.
    opts.on_tail("--version", "Show version") do
      puts "StoryboardLint v0.2.2"
      exit
    end
  end
  
  if ARGV.length < 1
    puts opt_parser
    exit 0
  end
  
  opt_parser.parse(args)

  matcher = StoryboardLint::Matcher.new(options)
  sb_scanner = StoryboardLint::StoryboardScanner.new(ARGV[0])
  source_scanner = StoryboardLint::SourceScanner.new(ARGV[0], matcher, options.additional_sources)

  linter = StoryboardLint::Linter.new(sb_scanner, source_scanner, matcher)
  linter.lint
  
  return 0
end

Public Instance Methods

check_custom_classes() click to toggle source
# File lib/storyboardlint.rb, line 292
def check_custom_classes
  @sb_scanner.custom_class_names.each do |custom_class|
    if !@source_scanner.class_names.map {|cn| cn[:class_name]}.include?(custom_class[:class_name])
      puts "error: Custom class '#{custom_class[:class_name]}' used in #{File.basename(custom_class[:file])} could not be found in source code."    
    end
  end
end
check_ids() click to toggle source
# File lib/storyboardlint.rb, line 300
def check_ids
  [{:method_name => :segue_ids, :name => 'Segue ID', :target => 'Storyboard'},
   {:method_name => :storyboard_ids, :name => 'Storyboard ID', :target => 'Storyboard'},
   {:method_name => :reuse_ids, :name => 'Reuse ID', :target => 'Storyboard or XIB'}].each do |data|
    @source_scanner.send(data[:method_name]).each do |source_item|
      if !@sb_scanner.send(data[:method_name]).map {|sb_item| sb_item[:id]}.include?(source_item[:id])
        puts "#{source_item[:file]}:#{source_item[:line]}: warning: #{data[:name]} '#{source_item[:id]}' could not be found in any #{data[:target]}."    
      end
    end
  end
end
check_naming() click to toggle source
# File lib/storyboardlint.rb, line 280
def check_naming
  [{:items => @sb_scanner.segue_ids, :regex => @matcher.segue_id_regex_sb, :name => 'Segue ID'},
   {:items => @sb_scanner.storyboard_ids, :regex => @matcher.storyboard_id_regex_sb, :name => 'Storyboard ID'},
   {:items => @sb_scanner.reuse_ids, :regex => @matcher.reuse_id_regex_sb, :name => 'Reuse ID'}].each do |data|
    data[:items].each do |item|
      if item[:id] !~ data[:regex]
        puts "warning: #{data[:name]} '#{item[:id]}' used in #{File.basename(item[:file])} does not match '#{data[:regex]}."
      end
    end
  end
end
lint() click to toggle source
# File lib/storyboardlint.rb, line 274
def lint
 check_naming
 check_custom_classes
 check_ids
end