Class: StoryboardLint::Linter

Inherits:
Object
  • Object
show all
Defined in:
lib/storyboardlint.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sb_scanner, source_scanner, matcher) ⇒ Linter



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# 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

Class Method Details

.run!(*args) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# 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.1"
      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

Instance Method Details

#check_custom_classesObject



292
293
294
295
296
297
298
# 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_idsObject



300
301
302
303
304
305
306
307
308
309
310
# 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_namingObject



280
281
282
283
284
285
286
287
288
289
290
# 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

#lintObject



274
275
276
277
278
# File 'lib/storyboardlint.rb', line 274

def lint
 check_naming
 check_custom_classes
 check_ids
end