Class: Tapioca::Commands::CheckShims

Inherits:
Command
  • Object
show all
Extended by:
T::Sig
Includes:
RBIFilesHelper, SorbetHelper
Defined in:
lib/tapioca/commands/check_shims.rb

Constant Summary

Constants included from SorbetHelper

SorbetHelper::FEATURE_REQUIREMENTS, SorbetHelper::SORBET_BIN, SorbetHelper::SORBET_EXE_PATH_ENV_VAR, SorbetHelper::SORBET_GEM_SPEC, SorbetHelper::SORBET_PAYLOAD_URL

Instance Method Summary collapse

Methods included from RBIFilesHelper

#duplicated_nodes_from_index, #index_rbi, #index_rbis, #location_to_payload_url, #validate_rbi_files

Methods included from SorbetHelper

#sorbet, #sorbet_path, #sorbet_supports?

Methods included from Tapioca::CliHelper

#netrc_file, #rbi_formatter, #say_error

Constructor Details

#initialize(gem_rbi_dir:, dsl_rbi_dir:, annotations_rbi_dir:, shim_rbi_dir:, todo_rbi_file:, payload:, number_of_workers:) ⇒ CheckShims

Returns a new instance of CheckShims.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tapioca/commands/check_shims.rb', line 22

def initialize(
  gem_rbi_dir:,
  dsl_rbi_dir:,
  annotations_rbi_dir:,
  shim_rbi_dir:,
  todo_rbi_file:,
  payload:,
  number_of_workers:
)
  super()
  @gem_rbi_dir = gem_rbi_dir
  @dsl_rbi_dir = dsl_rbi_dir
  @annotations_rbi_dir = annotations_rbi_dir
  @shim_rbi_dir = shim_rbi_dir
  @todo_rbi_file = todo_rbi_file
  @payload = payload
  @number_of_workers = number_of_workers
end

Instance Method Details

#execute ⇒ Object



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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/tapioca/commands/check_shims.rb', line 42

def execute
  index = RBI::Index.new

  if (!Dir.exist?(@shim_rbi_dir) || Dir.empty?(@shim_rbi_dir)) && !File.exist?(@todo_rbi_file)
    say("No shim RBIs to check", :green)

    return
  end

  payload_path = T.let(nil, T.nilable(String))

  if @payload
    if sorbet_supports?(:print_payload_sources)
      Dir.mktmpdir do |dir|
        payload_path = dir
        result = sorbet("--no-config --print=payload-sources:#{payload_path}")

        unless result.status
          raise Thor::Error, <<~ERROR
            "Sorbet failed to dump payload"
            #{result.err}
          ERROR
        end

        index_rbis(index, "payload", payload_path, number_of_workers: @number_of_workers)
      end
    else
      raise Thor::Error, <<~ERROR
        The version of Sorbet used in your Gemfile.lock does not support `--print=payload-sources`
        Current: v#{SORBET_GEM_SPEC.version}
        Required: #{FEATURE_REQUIREMENTS[:print_payload_sources]}
      ERROR
    end
  end

  index_rbi(index, "todo", @todo_rbi_file)
  index_rbis(index, "shim", @shim_rbi_dir, number_of_workers: @number_of_workers)
  index_rbis(index, "gem", @gem_rbi_dir, number_of_workers: @number_of_workers)
  index_rbis(index, "dsl", @dsl_rbi_dir, number_of_workers: @number_of_workers)
  index_rbis(index, "annotation", @annotations_rbi_dir, number_of_workers: @number_of_workers)

  duplicates = duplicated_nodes_from_index(index, shim_rbi_dir: @shim_rbi_dir, todo_rbi_file: @todo_rbi_file)

  unless duplicates.empty?
    messages = []

    duplicates.each do |key, nodes|
      messages << set_color("\nDuplicated RBI for #{key}:", :red)

      nodes.each do |node|
        node_loc = node.loc

        next unless node_loc

        loc_string = location_to_payload_url(node_loc, path_prefix: payload_path)
        messages << set_color(" * #{loc_string}", :red)
      end
    end

    messages << set_color(
      "\nPlease remove the duplicated definitions from #{@shim_rbi_dir} and #{@todo_rbi_file}", :red
    )

    raise Thor::Error, messages.join("\n")
  end

  say("\nNo duplicates found in shim RBIs", :green)
end