Class: EmergeCLI::Commands::Reaper

Inherits:
GlobalOptions
  • Object
show all
Defined in:
lib/commands/reaper/reaper.rb

Defined Under Namespace

Classes: DeadCodeResult

Instance Method Summary collapse

Methods inherited from GlobalOptions

#before

Constructor Details

#initialize(network: nil) ⇒ Reaper

Returns a new instance of Reaper.



22
23
24
# File 'lib/commands/reaper/reaper.rb', line 22

def initialize(network: nil)
  @network = network
end

Instance Method Details

#call(**options) ⇒ Object



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/commands/reaper/reaper.rb', line 26

def call(**options)
  @options = options
  @profiler = EmergeCLI::Profiler.new(enabled: options[:profile])
  @prompt = TTY::Prompt.new
  before(options)
  success = false

  begin
    api_token = @options[:api_token] || ENV.fetch('EMERGE_API_TOKEN', nil)
    raise 'API token is required' unless api_token

    @network ||= EmergeCLI::Network.new(api_token:)
    project_root = @options[:project_root] || Dir.pwd

    Sync do
      all_data = @profiler.measure('fetch_dead_code') { fetch_all_dead_code(@options[:upload_id]) }
      result = @profiler.measure('parse_dead_code') { DeadCodeResult.new(all_data) }

      Logger.info result.to_s

      selected_types = prompt_class_selection(result.filtered_unseen_classes, result.['platform'])
      Logger.info 'Selected classes:'
      selected_types.each do |selected_class|
        Logger.info " - #{selected_class['class_name']}"
      end

      confirmed = confirm_deletion(selected_types.length)
      if !confirmed
        Logger.info 'Operation cancelled'
        return false
      end

      Logger.info 'Proceeding with deletion...'
      platform = result.['platform']
      deleter = EmergeCLI::Reaper::CodeDeleter.new(
        project_root: project_root,
        platform: platform,
        profiler: @profiler,
        skip_delete_usages: options[:skip_delete_usages]
      )
      @profiler.measure('delete_types') { deleter.delete_types(selected_types) }
    end

    @profiler.report if @options[:profile]
    success = true
  rescue StandardError => e
    Logger.error "Failed to analyze dead code: #{e.message}"
    raise e
  ensure
    @network&.close
  end

  success
end