pocke / rbs_rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consider to use Rails generator

pocke opened this issue · comments

Perhaps it will be useful.

If i understood correctly, you meant Rails generator for creating rbs files for the project that is currently done by the tasks defined in the Readme?

Assuming yes, as a starting point the rake tasks provided in the Readme can be added to the source code and the users can use that to generate files rather defining rake tasks themselves and later generators can be added.

Does that sounds good?

# lib/rbs_rails.rb
require_relative 'railtie' if defined?(Rails)
# lib/railtie.rb
require 'rails'

module RbsRails
  class Railtie < Rails::Railtie
    railtie_name :rbs_rails

    rake_tasks do
      path = File.expand_path(__dir__)
      Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
    end
  end
end
# lib/tasks/sig.rake

namespace :rbs_rails do
  task copy_signature_files: :environment do
    require 'rbs_rails'

    to = Rails.root.join('sig/rbs_rails/')
    to.mkpath unless to.exist?
    RbsRails.copy_signatures(to: to)
  end

  task generate_rbs_for_model: :environment do
    require 'rbs_rails'

    out_dir = Rails.root / 'sig'
    out_dir.mkdir unless out_dir.exist?

    Rails.application.eager_load!

    ActiveRecord::Base.descendants.each do |klass|
      next if klass.abstract_class?

      path = out_dir / "app/models/#{klass.name.underscore}.rbs"
      FileUtils.mkdir_p(path.dirname)

      sig = RbsRails::ActiveRecord.class_to_rbs(klass)
      path.write sig
    end
  end

  task generate_rbs_for_path_helpers: :environment do
    require 'rbs_rails'
    out_path = Rails.root.join 'sig/path_helpers.rbs'
    rbs = RbsRails::PathHelpers.generate
    out_path.write rbs
  end
end