lib/tapioca/commands/dsl.rb in tapioca-0.9.4 vs lib/tapioca/commands/dsl.rb in tapioca-0.10.0

- old
+ new

@@ -12,11 +12,10 @@ requested_constants: T::Array[String], outpath: Pathname, only: T::Array[String], exclude: T::Array[String], file_header: T::Boolean, - compiler_path: String, tapioca_path: String, should_verify: T::Boolean, quiet: T::Boolean, verbose: T::Boolean, number_of_workers: T.nilable(Integer), @@ -29,11 +28,10 @@ requested_constants:, outpath:, only:, exclude:, file_header:, - compiler_path:, tapioca_path:, should_verify: false, quiet: false, verbose: false, number_of_workers: nil, @@ -44,31 +42,54 @@ @requested_constants = requested_constants @outpath = outpath @only = only @exclude = exclude @file_header = file_header - @compiler_path = compiler_path @tapioca_path = tapioca_path @should_verify = should_verify @quiet = quiet @verbose = verbose @number_of_workers = number_of_workers @auto_strictness = auto_strictness @gem_dir = gem_dir @rbi_formatter = rbi_formatter super() + end - @loader = T.let(nil, T.nilable(Runtime::Loader)) + sig { void } + def list_compilers + Loaders::Dsl.load_application( + tapioca_path: @tapioca_path, + eager_load: @requested_constants.empty? + ) + + pipeline = create_pipeline + + say("") + say("Loaded DSL compiler classes:") + say("") + + table = pipeline.compilers.map do |compiler| + status = if pipeline.active_compilers.include?(compiler) + set_color("enabled", :green) + else + set_color("disabled", :red) + end + + [compiler.name, status] + end + + print_table(table, { indent: 2 }) end sig { override.void } def execute - load_dsl_extensions - load_application(eager_load: @requested_constants.empty?) - abort_if_pending_migrations! - load_dsl_compilers + Loaders::Dsl.load_application( + tapioca_path: @tapioca_path, + eager_load: @requested_constants.empty? + ) if @should_verify say("Checking for out-of-date RBIs...") else say("Compiling DSL RBI files...") @@ -76,19 +97,11 @@ say("") outpath = @should_verify ? Pathname.new(Dir.mktmpdir) : @outpath rbi_files_to_purge = existing_rbi_filenames(@requested_constants) - pipeline = Tapioca::Dsl::Pipeline.new( - requested_constants: constantize(@requested_constants), - requested_compilers: constantize_compilers(@only), - excluded_compilers: constantize_compilers(@exclude), - error_handler: ->(error) { - say_error(error, :bold, :red) - }, - number_of_workers: @number_of_workers - ) + pipeline = create_pipeline processed_files = pipeline.run do |constant, contents| constant_name = T.must(Tapioca::Runtime::Reflection.name_of(constant)) if @verbose && !@quiet @@ -118,59 +131,34 @@ validate_rbi_files( command: default_command(:dsl, @requested_constants.join(" ")), gem_dir: @gem_dir, dsl_dir: @outpath.to_s, auto_strictness: @auto_strictness, - compilers: pipeline.compilers + compilers: pipeline.active_compilers ) end say("All operations performed in working directory.", [:green, :bold]) say("Please review changes and commit them.", [:green, :bold]) end end private - sig { params(eager_load: T::Boolean).void } - def load_application(eager_load:) - say("Loading Rails application... ") - - loader.load_rails_application( - environment_load: true, - eager_load: eager_load + sig { returns(Tapioca::Dsl::Pipeline) } + def create_pipeline + Tapioca::Dsl::Pipeline.new( + requested_constants: constantize(@requested_constants), + requested_compilers: constantize_compilers(@only), + excluded_compilers: constantize_compilers(@exclude), + error_handler: ->(error) { + say_error(error, :bold, :red) + }, + number_of_workers: @number_of_workers ) - - say("Done", :green) end - sig { void } - def abort_if_pending_migrations! - return unless File.exist?("config/application.rb") - return unless defined?(::Rake) - - Rails.application.load_tasks - if Rake::Task.task_defined?("db:abort_if_pending_migrations") - Rake::Task["db:abort_if_pending_migrations"].invoke - end - end - - sig { void } - def load_dsl_compilers - say("Loading DSL compiler classes... ") - - Dir.glob([ - "#{@compiler_path}/*.rb", - "#{@tapioca_path}/generators/**/*.rb", # TODO: Here for backcompat, remove later - "#{@tapioca_path}/compilers/**/*.rb", - ]).each do |compiler| - require File.expand_path(compiler) - end - - say("Done", :green) - end - sig { params(requested_constants: T::Array[String], path: Pathname).returns(T::Set[Pathname]) } def existing_rbi_filenames(requested_constants, path: @outpath) filenames = if requested_constants.empty? Pathname.glob(path / "**/*.rbi") else @@ -307,13 +295,13 @@ diff[file] = :removed end common_files = (existing_rbis & new_rbis) - changed_files = common_files.map do |filename| + changed_files = common_files.filter_map do |filename| filename unless FileUtils.identical?(@outpath / filename, tmp_dir / filename) - end.compact + end changed_files.each do |file| diff[file] = :changed end @@ -354,15 +342,10 @@ Pathname.glob(path / "**/*.rbi").map do |file| file.relative_path_from(path) end.sort end - sig { returns(Runtime::Loader) } - def loader - @loader ||= Runtime::Loader.new - end - sig { params(class_name: String).returns(String) } def underscore(class_name) return class_name unless /[A-Z-]|::/.match?(class_name) word = class_name.to_s.gsub("::", "/") @@ -379,14 +362,9 @@ end sig { params(constant: String).returns(String) } def generate_command_for(constant) default_command(:dsl, constant) - end - - sig { void } - def load_dsl_extensions - Dir["#{__dir__}/../dsl/extensions/*.rb"].sort.each { |f| require(f) } end end end end