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, number_of_workers: nil) ⇒ Pipeline

Returns a new instance of Pipeline.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tapioca/dsl/pipeline.rb', line 34

def initialize(
  requested_constants:,
  requested_paths: [],
  requested_compilers: [],
  excluded_compilers: [],
  error_handler: $stderr.method(:puts).to_proc,
  number_of_workers: nil
)
  @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
  @number_of_workers = number_of_workers
  @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.



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

def error_handler
  @error_handler
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

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

Instance Method Details

#add_error(error) ⇒ Object



93
94
95
# File 'lib/tapioca/dsl/pipeline.rb', line 93

def add_error(error)
  @errors << error
end

#compiler_enabled?(compiler_name) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
# File 'lib/tapioca/dsl/pipeline.rb', line 98

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



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

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



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

def run(&blk)
  constants_to_process = gather_constants(requested_constants, requested_paths)
    .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