Skip to content

Commit

Permalink
Merge pull request #533 from Shopify/add-tapioca-compiler
Browse files Browse the repository at this point in the history
Add Tapioca compiler
  • Loading branch information
st0012 authored Jan 21, 2025
2 parents 3a71af5 + 79a7ba3 commit 5f44ee6
Show file tree
Hide file tree
Showing 4 changed files with 504 additions and 3 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ gem "csv" # required for Ruby 3.4+

# for unit testing optional sorbet support
gem "sorbet-runtime"
gem "tapioca"

gem "logger"
41 changes: 38 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ GEM
tzinfo (~> 2.0)
ast (2.4.2)
base64 (0.2.0)
benchmark (0.4.0)
bigdecimal (3.1.9)
coderay (1.1.3)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
csv (3.3.2)
drb (2.2.1)
erubi (1.13.1)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.6)
Expand All @@ -55,17 +57,22 @@ GEM
mono_logger (1.1.2)
multi_json (1.15.0)
mutex_m (0.2.0)
parallel (1.25.1)
netrc (0.11.0)
parallel (1.26.3)
parser (3.3.3.0)
ast (~> 2.4.1)
racc
prism (1.3.0)
pry (0.15.2)
coderay (~> 1.1)
method_source (~> 1.0)
racc (1.8.0)
rack (3.1.8)
rainbow (3.1.1)
rake (13.2.1)
rbi (0.2.3)
prism (~> 1.0)
sorbet-runtime (>= 0.5.9204)
redis (5.3.0)
redis-client (>= 0.22.0)
redis-client (0.23.0)
Expand Down Expand Up @@ -103,15 +110,42 @@ GEM
redis-client (>= 0.19.0)
sinatra (1.0)
rack (>= 1.0)
sorbet (0.5.11460)
sorbet-static (= 0.5.11460)
sorbet-runtime (0.5.11460)
sorbet-static (0.5.11460-universal-darwin)
sorbet-static-and-runtime (0.5.11460)
sorbet (= 0.5.11460)
sorbet-runtime (= 0.5.11460)
spoom (1.5.1)
erubi (>= 1.10.0)
prism (>= 0.28.0)
sorbet-static-and-runtime (>= 0.5.10187)
thor (>= 0.19.2)
tapioca (0.16.8)
benchmark
bundler (>= 2.2.25)
netrc (>= 0.11.0)
parallel (>= 1.21.0)
rbi (~> 0.2)
sorbet-static-and-runtime (>= 0.5.11087)
spoom (>= 1.2.0)
thor (>= 1.2.0)
yard-sorbet
thor (1.3.2)
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
yard (0.9.37)
yard-sorbet (0.9.0)
sorbet-runtime
yard

PLATFORMS
ruby
arm64-darwin
x86_64-darwin
x86_64-linux

DEPENDENCIES
activerecord
Expand All @@ -129,7 +163,8 @@ DEPENDENCIES
rubocop-shopify
sidekiq
sorbet-runtime
tapioca
yard

BUNDLED WITH
2.5.14
2.6.1
96 changes: 96 additions & 0 deletions lib/tapioca/dsl/compilers/job_iteration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# typed: strict
# frozen_string_literal: true

return unless defined?(JobIteration::Iteration)

module Tapioca
module Dsl
module Compilers
class JobIteration < Compiler
extend T::Sig

ConstantType = type_member { { fixed: T.class_of(::JobIteration::Iteration) } }

sig { override.void }
def decorate
return unless constant.instance_methods(false).include?(:build_enumerator)

root.create_path(constant) do |job|
method = constant.instance_method(:build_enumerator)
constant_name = name_of(constant)
signature = signature_of(method)

parameters = compile_method_parameters_to_rbi(method).reject do |typed_param|
typed_param.param.name == "cursor"
end

if signature
fixed_hash_args = signature.arg_types.select { |arg_type| T::Types::FixedHash === arg_type[1] }.to_h
expanded_parameters = parameters.flat_map do |typed_param|
if (hash_type = fixed_hash_args[typed_param.param.name.to_sym])
hash_type.types.map do |key, value|
create_kw_param(key.to_s, type: value.to_s)
end
else
typed_param
end
end
else
expanded_parameters = parameters
end

job.create_method(
"perform_later",
parameters: perform_later_parameters(expanded_parameters, constant_name),
return_type: "T.any(#{constant_name}, FalseClass)",
class_method: true,
)

job.create_method(
"perform_now",
parameters: expanded_parameters,
return_type: "void",
class_method: true,
)

job.create_method(
"perform",
parameters: expanded_parameters,
return_type: "void",
class_method: false,
)
end
end

private

sig do
params(
parameters: T::Array[RBI::TypedParam],
constant_name: T.nilable(String),
).returns(T::Array[RBI::TypedParam])
end
def perform_later_parameters(parameters, constant_name)
if ::Gem::Requirement.new(">= 7.0").satisfied_by?(::ActiveJob.gem_version)
parameters.reject! { |typed_param| RBI::BlockParam === typed_param.param }
parameters + [create_block_param(
"block",
type: "T.nilable(T.proc.params(job: #{constant_name}).void)",
)]
else
parameters
end
end

class << self
extend T::Sig

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
all_classes.select { |c| ::JobIteration::Iteration > c }
end
end
end
end
end
end
Loading

0 comments on commit 5f44ee6

Please sign in to comment.