Skip to content

Commit

Permalink
Merge pull request #2037 from Shopify/ko/add-on-dsl-generation
Browse files Browse the repository at this point in the history
[Tapioca add-on] Generate DSL RBIs in add-on mode
  • Loading branch information
KaanOzkan authored Oct 8, 2024
2 parents a108c1e + 9ba2f1b commit 8a1a2c8
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/ruby_lsp/tapioca/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def workspace_did_change_watched_files(changes)
@rails_runner_client.delegate_notification(
server_addon_name: "Tapioca",
request_name: "dsl",
params: { constants: constants },
constants: constants,
)
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/ruby_lsp/tapioca/server_addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def execute(request, params)
private

def dsl(params)
# TODO
load("tapioca/cli.rb") # Reload the CLI to reset thor defaults between requests
::Tapioca::Cli.start(["dsl", "--lsp_addon", "--workers=1"] + params[:constants])
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/tapioca/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ def todo
type: :hash,
desc: "Options to pass to the DSL compilers",
default: {}
option :lsp_addon,
type: :boolean,
desc: "Generate DSL RBIs from the LSP addon. Internal to tapioca and not intended for end-users",
default: false,
hide: true
def dsl(*constant_or_paths)
set_environment(options)

Expand All @@ -166,6 +171,7 @@ def dsl(*constant_or_paths)
app_root: options[:app_root],
halt_upon_load_error: options[:halt_upon_load_error],
compiler_options: options[:compiler_options],
lsp_addon: options[:lsp_addon],
}

command = if options[:verify]
Expand Down
11 changes: 10 additions & 1 deletion lib/tapioca/commands/abstract_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class AbstractDsl < CommandWithoutTracker
app_root: String,
halt_upon_load_error: T::Boolean,
compiler_options: T::Hash[String, T.untyped],
lsp_addon: T::Boolean,
).void
end
def initialize(
Expand All @@ -47,7 +48,8 @@ def initialize(
rbi_formatter: DEFAULT_RBI_FORMATTER,
app_root: ".",
halt_upon_load_error: true,
compiler_options: {}
compiler_options: {},
lsp_addon: false
)
@requested_constants = requested_constants
@requested_paths = requested_paths
Expand All @@ -66,6 +68,7 @@ def initialize(
@halt_upon_load_error = halt_upon_load_error
@skip_constant = skip_constant
@compiler_options = compiler_options
@lsp_addon = lsp_addon

super()
end
Expand All @@ -74,6 +77,10 @@ def initialize(

sig { params(outpath: Pathname, quiet: T::Boolean).returns(T::Set[Pathname]) }
def generate_dsl_rbi_files(outpath, quiet:)
if @lsp_addon
pipeline.active_compilers.each(&:reset_state)
end

existing_rbi_files = existing_rbi_filenames(all_requested_constants)

generated_files = pipeline.run do |constant, contents|
Expand Down Expand Up @@ -116,6 +123,7 @@ def load_application
eager_load: @requested_constants.empty? && @requested_paths.empty?,
app_root: @app_root,
halt_upon_load_error: @halt_upon_load_error,
lsp_addon: @lsp_addon,
)
end

Expand All @@ -133,6 +141,7 @@ def create_pipeline
skipped_constants: constantize(@skip_constant, ignore_missing: true),
number_of_workers: @number_of_workers,
compiler_options: @compiler_options,
lsp_addon: @lsp_addon,
)
end

Expand Down
7 changes: 7 additions & 0 deletions lib/tapioca/dsl/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ def requested_constants=(constants)
@@requested_constants = constants # rubocop:disable Style/ClassVars
end

sig { void }
def reset_state
@processable_constants = nil
@all_classes = nil
@all_modules = nil
end

private

sig do
Expand Down
7 changes: 5 additions & 2 deletions lib/tapioca/dsl/pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Pipeline
skipped_constants: T::Array[Module],
number_of_workers: T.nilable(Integer),
compiler_options: T::Hash[String, T.untyped],
lsp_addon: T::Boolean,
).void
end
def initialize(
Expand All @@ -44,7 +45,8 @@ def initialize(
error_handler: $stderr.method(:puts).to_proc,
skipped_constants: [],
number_of_workers: nil,
compiler_options: {}
compiler_options: {},
lsp_addon: false
)
@active_compilers = T.let(
gather_active_compilers(requested_compilers, excluded_compilers),
Expand All @@ -57,6 +59,7 @@ def initialize(
@number_of_workers = number_of_workers
@compiler_options = compiler_options
@errors = T.let([], T::Array[String])
@lsp_addon = lsp_addon
end

sig do
Expand Down Expand Up @@ -177,7 +180,7 @@ def filter_anonymous_and_reloaded_constants(constants)
# Find the constants that have been reloaded
reloaded_constants = constants_by_name.select { |_, constants| constants.size > 1 }.keys

unless reloaded_constants.empty?
unless reloaded_constants.empty? || @lsp_addon
reloaded_constant_names = reloaded_constants.map { |name| "`#{name}`" }.join(", ")

$stderr.puts("WARNING: Multiple constants with the same name: #{reloaded_constant_names}")
Expand Down
23 changes: 20 additions & 3 deletions lib/tapioca/loaders/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,27 @@ class << self
extend T::Sig

sig do
params(tapioca_path: String, eager_load: T::Boolean, app_root: String, halt_upon_load_error: T::Boolean).void
params(
tapioca_path: String,
eager_load: T::Boolean,
app_root: String,
halt_upon_load_error: T::Boolean,
lsp_addon: T::Boolean,
).void
end
def load_application(tapioca_path:, eager_load: true, app_root: ".", halt_upon_load_error: true)
def load_application(tapioca_path:, eager_load: true, app_root: ".", halt_upon_load_error: true,
lsp_addon: false)
loader = new(
tapioca_path: tapioca_path,
eager_load: eager_load,
app_root: app_root,
halt_upon_load_error: halt_upon_load_error,
)
loader.load
if lsp_addon
loader.load_dsl_extensions_and_compilers
else
loader.load
end
end
end

Expand All @@ -30,6 +41,12 @@ def load
load_dsl_compilers
end

sig { void }
def load_dsl_extensions_and_compilers
load_dsl_extensions
load_dsl_compilers
end

protected

sig do
Expand Down
2 changes: 1 addition & 1 deletion tasks/readme.rake
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ task :readme do
end

def skip_option?(option)
option.name == "auth"
option.name == "auth" || option.hide
end

def option_value(option)
Expand Down

0 comments on commit 8a1a2c8

Please sign in to comment.