Sorbet

Set up sorbet, sorbet-rails and rubocop-sorbet
Icons/chart bar
Used 59 times
Created by
T TobiasBales

Usage

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/VqqseW"
Template Source

Review the code before running this template on your machine.

gem_group :development, :test do
  gem 'sorbet'
end

gem 'sorbet-runtime'
gem 'sorbet-rails'

def file_contains?(filename, string)
  File.foreach(filename).detect { |line| line.include?(string) }
end

if file_contains? 'Gemfile', 'activeadmin'
  # pundit and cancancan are required by active admin and if not present makes srb init fail
  gem 'cancancan'
  gem 'pundit'
end

if File.exist? 'Guardfile'
  gem 'guard-sorbet'

  prepend_to_file 'Guardfile' do <<~RB

  guard :sorbet do
    watch(/^.*.rb$/)
    watch(/^.*.rbi$/)
  end
  RB
  end
end

if File.exist? '.rubocop.yml'
  gem_group :development, :test do
    gem 'rubocop-sorbet', require: false
  end
  unless file_contains? '.rubocop.yml', 'require:'
    prepend_to_file '.rubocop.yml', 'require:'
  end
  inject_into_file '.rubocop.yml', after: 'require:' do <<-YAML

  - rubocop-sorbet
    YAML
  end

  inject_into_file '.rubocop.yml' do <<~YAML
      Sorbet/ForbidSuperclassConstLiteral:
        Enabled: false
      YAML
  end
end

Bundler.with_unbundled_env { run 'bundle install' }

create_file 'bin/sorbet_generate' do <<~SH
    #!/usr/bin/env bash
    set -euo pipefail

    bundle exec rake rails_rbi:all
    bundle exec srb rbi hidden-definitions
    bundle exec srb rbi suggest-typed
  SH
end
`chmod +x bin/sorbet_generate`

inject_into_file '.gitignore' do <<~GITIGNORE

    sorbet/rbi/hidden-definitions/errors.txt
  GITIGNORE
end

if File.exist? 'bin/linters_ci'
  inject_into_file 'bin/linters_ci' do <<~SH

    bundle exec srb tc
  SH
  end
end

if File.exist? 'bin/strong_versions_fix'
  system('bin/strong_versions_fix')
end

system({'SRB_YES' => "1"}, 'srb init')
system('bin/sorbet_generate')

if File.exist? 'bin/rubocop_fix'
  system('bin/rubocop_fix')
end
Comments

Sign up or Login to leave a comment.