Class: Keela::Scanner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strategy:, configuration: Keela.configuration, baseline: nil) ⇒ Scanner

Returns a new instance of Scanner.



10
11
12
13
14
15
16
17
18
# File 'lib/keela/scanner.rb', line 10

def initialize(strategy:, configuration: Keela.configuration, baseline: nil)
  @strategy = strategy
  @configuration = configuration
  @baseline = baseline || Baseline.new(configuration.baseline_path)
  @source_files = {}
  @unused_collection = Hash.new { |hash, key| hash[key] = [] }
  @new_unused = []
  @removed = []
end

Instance Attribute Details

#baselineObject (readonly)

Returns the value of attribute baseline.



8
9
10
# File 'lib/keela/scanner.rb', line 8

def baseline
  @baseline
end

#configurationObject (readonly)

Returns the value of attribute configuration.



8
9
10
# File 'lib/keela/scanner.rb', line 8

def configuration
  @configuration
end

#new_unusedObject (readonly)

Returns the value of attribute new_unused.



8
9
10
# File 'lib/keela/scanner.rb', line 8

def new_unused
  @new_unused
end

#removedObject (readonly)

Returns the value of attribute removed.



8
9
10
# File 'lib/keela/scanner.rb', line 8

def removed
  @removed
end

#source_filesObject (readonly)

Returns the value of attribute source_files.



8
9
10
# File 'lib/keela/scanner.rb', line 8

def source_files
  @source_files
end

#strategyObject (readonly)

Returns the value of attribute strategy.



8
9
10
# File 'lib/keela/scanner.rb', line 8

def strategy
  @strategy
end

#unused_collectionObject (readonly)

Returns the value of attribute unused_collection.



8
9
10
# File 'lib/keela/scanner.rb', line 8

def unused_collection
  @unused_collection
end

Instance Method Details

#file_globsObject



60
61
62
63
64
65
66
# File 'lib/keela/scanner.rb', line 60

def file_globs
  all_patterns = configuration.directory_patterns + configuration.include_patterns

  configuration.extensions.flat_map do |ext|
    all_patterns.map { |pattern| format(pattern, ext: ext) }
  end
end

#run(force_report: false, update_baseline: false, silent: false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/keela/scanner.rb', line 20

def run(force_report: false, update_baseline: false, silent: false)
  return true unless should_run?

  validate_configuration!

  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  load_source_files
  definitions = find_definitions
  definitions = filter_excluded(definitions)

  # Determine mode: report if forced, updating baseline, or no baseline exists
  report_mode = force_report || update_baseline || !baseline.exists?

  find_unused(definitions, show_progress: report_mode && !silent)

  if report_mode
    elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
    reporter.print_full_report(unused_collection, elapsed) unless silent
    if update_baseline
      baseline.set(strategy.name, unused_collection)
      # Note: caller is responsible for calling baseline.save after all strategies run
    end
    return true
  end

  # Baseline mode: compare against known unused code
  compare_with_baseline
  unless silent
    reporter.print_diff_report(
      new_unused,
      removed,
      excluded_path: configuration.excluded_path || "excluded.yml",
      baseline_path: baseline.path
    )
  end

  new_unused.empty? && removed.empty?
end