Skip to content

Instantly share code, notes, and snippets.

@ksss
Last active Mar 30, 2022
Embed
What would you like to do?
begin
require "rbs_rails/rake_task"
require "rbs"
rescue LoadError
return
end
RbsRails::RakeTask.new
class AppGeneratedWriter < RBS::Writer
def write_comment(*)
# no comment hack
end
def write_decl(decl)
case decl
when RBS::AST::Declarations::Constant
if decl.type.instance_of?(RBS::Types::Bases::Any)
@out.print ' #'
end
end
super
end
def write_member(member)
case member
when RBS::AST::Members::AttrAccessor, RBS::AST::Members::AttrReader, RBS::AST::Members::AttrWriter,
RBS::AST::Members::Alias
# skip generate method signature hack
else
super
end
end
def write_def(member)
skip = member.types.any? do |type|
if type.type.return_type.kind_of?(RBS::Types::Union)
type.type.return_type.types.any? do |in_union_type|
in_union_type.kind_of?(RBS::Types::Bases::Any)
end
else
type.type.return_type.kind_of?(RBS::Types::Bases::Any)
end
end
if skip
@out.print ' #'
end
super
end
end
namespace :rbs do
task setup: ['rbs:collection:install', :auto_completion, 'rbs_rails:all']
namespace :collection do
task :install do
system("bundle exec rbs collection install")
end
task :update do
system("bundle exec rbs collection update")
end
end
task auto_completion: [:environment] do
auto_path = Rails.root / "sig" / "auto_completion"
FileUtils.rm_rf(auto_path)
Dir["app/models/**/*.rb"].each do |src_path|
parser = RBS::Prototype::RB.new
parser.parse File.read(src_path)
dist_dir = auto_path / File.dirname(src_path)
dist_dir.mkpath
dist_path = "#{auto_path / src_path}s"
File.open(dist_path, "w+") do |f|
writer = AppGeneratedWriter.new(out: f)
decls = parser.decls
writer.write decls
end
print "."
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment