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) ⇒ Scanner

Returns a new instance of Scanner.



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

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

Instance Attribute Details

#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



52
53
54
55
56
# File 'lib/keela/scanner.rb', line 52

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

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



19
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
# File 'lib/keela/scanner.rb', line 19

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

  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)

  if report_mode
    elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
    reporter.print_full_report(unused_collection, elapsed)
    write_baseline_file if update_baseline
    return true
  end

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

  new_unused.empty? && removed.empty?
end