Class: Tapioca::Dsl::Pipeline

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tapioca/dsl/pipeline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(requested_constants:, requested_paths: [], requested_compilers: [], excluded_compilers: [], error_handler: $stderr.method(:puts).to_proc, skipped_constants: [], number_of_workers: nil, compiler_options: {}, lsp_addon: false) ⇒ Pipeline

: ( | requested_constants: Array[T::Module], | ?requested_paths: Array, | ?requested_compilers: Array, | ?excluded_compilers: Array, | ?error_handler: ^(String error) -> void, | ?skipped_constants: Array[T::Module], | ?number_of_workers: Integer?, | ?compiler_options: Hash[String, untyped], | ?lsp_addon: bool | ) -> void



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tapioca/dsl/pipeline.rb', line 38

def initialize(
  requested_constants:,
  requested_paths: [],
  requested_compilers: [],
  excluded_compilers: [],
  error_handler: $stderr.method(:puts).to_proc,
  skipped_constants: [],
  number_of_workers: nil,
  compiler_options: {},
  lsp_addon: false
)
  @active_compilers =
    gather_active_compilers(requested_compilers, excluded_compilers) #: Enumerable[singleton(Compiler)]
  @requested_constants = requested_constants
  @requested_paths = requested_paths
  @error_handler = error_handler
  @skipped_constants = skipped_constants
  @number_of_workers = number_of_workers
  @compiler_options = compiler_options
  @lsp_addon = lsp_addon
  @errors = [] #: Array[String]
end

Instance Attribute Details

#active_compilersObject (readonly)



10
11
12
# File 'lib/tapioca/dsl/pipeline.rb', line 10

def active_compilers
  @active_compilers
end

#error_handlerObject (readonly)

: ^(String error) -> void



22
23
24
# File 'lib/tapioca/dsl/pipeline.rb', line 22

def error_handler
  @error_handler
end

#errorsObject (readonly)

: Array



25
26
27
# File 'lib/tapioca/dsl/pipeline.rb', line 25

def errors
  @errors
end

#requested_constantsObject (readonly)



13
14
15
# File 'lib/tapioca/dsl/pipeline.rb', line 13

def requested_constants
  @requested_constants
end

#requested_pathsObject (readonly)

: Array



16
17
18
# File 'lib/tapioca/dsl/pipeline.rb', line 16

def requested_paths
  @requested_paths
end

#skipped_constantsObject (readonly)



19
20
21
# File 'lib/tapioca/dsl/pipeline.rb', line 19

def skipped_constants
  @skipped_constants
end

Instance Method Details

#add_error(error) ⇒ Object

: (String error) -> void



102
103
104
# File 'lib/tapioca/dsl/pipeline.rb', line 102

def add_error(error)
  @errors << error
end

#compiler_enabled?(compiler_name) ⇒ Boolean

: (String compiler_name) -> bool

Returns:

  • (Boolean)


107
108
109
110
111
112
113
# File 'lib/tapioca/dsl/pipeline.rb', line 107

def compiler_enabled?(compiler_name)
  potential_names = Compilers::NAMESPACES.map { |namespace| namespace + compiler_name }

  active_compilers.any? do |compiler|
    potential_names.any?(compiler.name)
  end
end

#compilersObject

: -> Array



116
117
118
119
120
# File 'lib/tapioca/dsl/pipeline.rb', line 116

def compilers
  @compilers ||= Runtime::Reflection.descendants_of(Compiler).sort_by do |compiler|
    T.must(compiler.name)
  end #: Array[singleton(Compiler)]?
end

#run(&blk) ⇒ Object

: [R] { (T::Module constant, RBI::File rbi) -> R } -> Array



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
# File 'lib/tapioca/dsl/pipeline.rb', line 62

def run(&blk)
  constants_to_process = gather_constants(requested_constants, requested_paths, skipped_constants)
    .select { |c| Module === c } # Filter value constants out
    .sort_by! { |c| T.must(Runtime::Reflection.name_of(c)) }

  # It's OK if there are no constants to process if we received a valid file/path.
  if constants_to_process.empty? && requested_paths.none? { |p| File.exist?(p) }
    report_error(<<~ERROR)
      No classes/modules can be matched for RBI generation.
      Please check that the requested classes/modules include processable DSL methods.
    ERROR
    raise Tapioca::Error, ""
  end

  if defined?(::ActiveRecord::Base) && constants_to_process.any? { |c| ::ActiveRecord::Base > c }
    abort_if_pending_migrations!
  end

  result = Executor.new(
    constants_to_process,
    number_of_workers: @number_of_workers,
  ).run_in_parallel do |constant|
    rbi = rbi_for_constant(constant)
    next if rbi.nil?

    blk.call(constant, rbi)
  end

  if errors.any?
    errors.each do |msg|
      report_error(msg)
    end

    raise Tapioca::Error, ""
  end

  result.compact
end