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

Returns a new instance of Pipeline.



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

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 = T.let(
    gather_active_compilers(requested_compilers, excluded_compilers),
    T::Enumerable[T.class_of(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 = T.let([], T::Array[String])
end

Instance Attribute Details

#active_compilersObject (readonly)

Returns the value of attribute active_compilers.



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

def active_compilers
  @active_compilers
end

#error_handlerObject (readonly)

Returns the value of attribute error_handler.



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

def error_handler
  @error_handler
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#requested_constantsObject (readonly)

Returns the value of attribute requested_constants.



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

def requested_constants
  @requested_constants
end

#requested_pathsObject (readonly)

Returns the value of attribute requested_paths.



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

def requested_paths
  @requested_paths
end

#skipped_constantsObject (readonly)

Returns the value of attribute skipped_constants.



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

def skipped_constants
  @skipped_constants
end

Instance Method Details

#add_error(error) ⇒ Object



105
106
107
# File 'lib/tapioca/dsl/pipeline.rb', line 105

def add_error(error)
  @errors << error
end

#compiler_enabled?(compiler_name) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
# File 'lib/tapioca/dsl/pipeline.rb', line 110

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



119
120
121
122
123
124
125
126
# File 'lib/tapioca/dsl/pipeline.rb', line 119

def compilers
  @compilers ||= T.let(
    Runtime::Reflection.descendants_of(Compiler).sort_by do |compiler|
      T.must(compiler.name)
    end,
    T.nilable(T::Array[T.class_of(Compiler)]),
  )
end

#run(&blk) ⇒ Object



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

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
  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

  errors.each do |msg|
    report_error(msg)
  end

  result.compact
end