Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Rails application type generator #99

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/sorbet-rails/application_rbi_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# typed: true
class ApplicationRbiFormatter
def initialize(rails_application_class_name)
@rails_application_class_name = rails_application_class_name
end

def generate_rbi
puts "-- Generate sigs for Rails::Application --"

<<~"RBI"
# This is an autogenerated file for your Rails::Application subclass

# typed: strong
class #{@rails_application_class_name} < Rails::Application
end

module Rails
extend T::Sig

sig { returns(#{@rails_application_class_name}) }
def self.application; end
end
RBI
end
end
10 changes: 10 additions & 0 deletions lib/sorbet-rails/tasks/rails_rbi.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require("sorbet-rails/application_rbi_formatter")
require("sorbet-rails/model_rbi_formatter")
require("sorbet-rails/routes_rbi_formatter")
require("sorbet-rails/helper_rbi_formatter")
Expand All @@ -9,6 +10,15 @@ require("sorbet-rails/utils")

namespace :rails_rbi do

desc "Generate rbis for rails application"
task application: :environment do |t, args|
rails_application_class_name = Rails.application.class.name
formatter = ApplicationRbiFormatter.new(rails_application_class_name)
file_path = Rails.root.join("sorbet", "rails-rbi", "application.rbi")
FileUtils.mkdir_p(File.dirname(file_path))
File.write(file_path, formatter.generate_rbi)
end

desc "Generate rbis for rails routes"
task :routes, [:root_dir] => :environment do |t, args|
all_routes = Rails.application.routes.routes
Expand Down
12 changes: 12 additions & 0 deletions spec/application_rbi_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'rails_helper'
require 'sorbet-rails/application_rbi_formatter'

RSpec.describe ApplicationRbiFormatter do
it 'returns the expected rbi for an application' do
formatter = ApplicationRbiFormatter.new(Rails.application.class.name)
expect_match_file(
formatter.generate_rbi,
'expected_application.rbi'
)
end
end
15 changes: 15 additions & 0 deletions spec/rake_rails_rbi_application_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

RSpec.describe 'rake rails_rbi:application', type: :task do

it "preloads the Rails environment" do
expect(task.prerequisites).to include("environment")
end

it "generates application.rbi correctly" do
task.invoke
expected_path = Rails.root.join("sorbet", "rails-rbi", "application.rbi")
generated = File.read(expected_path)
expect_match_file(generated, 'expected_application.rbi')
end
end
2 changes: 1 addition & 1 deletion spec/support/v4.2/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module V42
module Hogwarts_4_2
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
Expand Down
2 changes: 1 addition & 1 deletion spec/support/v5.0/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module V50
module Hogwarts_5_0
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
Expand Down
2 changes: 1 addition & 1 deletion spec/support/v5.1/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module RailsApp517
module Hogwarts_5_17
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
Expand Down
2 changes: 1 addition & 1 deletion spec/support/v5.2-no-sorbet/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module RailsApp
module Hogwarts_5_2_NoSorbet
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
Expand Down
2 changes: 1 addition & 1 deletion spec/support/v5.2/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module RailsApp
module Hogwarts_5_2
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
Expand Down
2 changes: 1 addition & 1 deletion spec/support/v6.0/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module V60
module Hogwarts_6_0
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.0
Expand Down
12 changes: 12 additions & 0 deletions spec/test_data/v4.2/expected_application.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This is an autogenerated file for your Rails::Application subclass

# typed: strong
class Hogwarts_4_2::Application < Rails::Application
end

module Rails
extend T::Sig

sig { returns(Hogwarts_4_2::Application) }
def self.application; end
end
12 changes: 12 additions & 0 deletions spec/test_data/v5.0/expected_application.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This is an autogenerated file for your Rails::Application subclass

# typed: strong
class Hogwarts_5_0::Application < Rails::Application
end

module Rails
extend T::Sig

sig { returns(Hogwarts_5_0::Application) }
def self.application; end
end
12 changes: 12 additions & 0 deletions spec/test_data/v5.1/expected_application.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This is an autogenerated file for your Rails::Application subclass

# typed: strong
class Hogwarts_5_17::Application < Rails::Application
end

module Rails
extend T::Sig

sig { returns(Hogwarts_5_17::Application) }
def self.application; end
end
12 changes: 12 additions & 0 deletions spec/test_data/v5.2-no-sorbet/expected_application.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This is an autogenerated file for your Rails::Application subclass

# typed: strong
class Hogwarts_5_2_NoSorbet::Application < Rails::Application
end

module Rails
extend T::Sig

sig { returns(Hogwarts_5_2_NoSorbet::Application) }
def self.application; end
end
12 changes: 12 additions & 0 deletions spec/test_data/v5.2/expected_application.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This is an autogenerated file for your Rails::Application subclass

# typed: strong
class Hogwarts_5_2::Application < Rails::Application
end

module Rails
extend T::Sig

sig { returns(Hogwarts_5_2::Application) }
def self.application; end
end
12 changes: 12 additions & 0 deletions spec/test_data/v6.0/expected_application.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This is an autogenerated file for your Rails::Application subclass

# typed: strong
class Hogwarts_6_0::Application < Rails::Application
end

module Rails
extend T::Sig

sig { returns(Hogwarts_6_0::Application) }
def self.application; end
end