Skip to content

Commit

Permalink
[ruby/prism] Add RBI and RBS templates for Prism
Browse files Browse the repository at this point in the history
ruby/prism@b62305ff3e

Co-authored-by: Stan Lo <st0012@users.noreply.github.com>
  • Loading branch information
2 people authored and matzbot committed Oct 27, 2023
1 parent a8af5d3 commit 544f640
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
2 changes: 2 additions & 0 deletions lib/prism/prism.gemspec
Expand Up @@ -107,6 +107,8 @@ Gem::Specification.new do |spec|
"src/util/pm_strpbrk.c",
"src/prism.c",
"prism.gemspec",
"sig/prism.rbs",
"rbi/prism.rbi"
]

spec.extensions = ["ext/prism/extconf.rb"]
Expand Down
72 changes: 44 additions & 28 deletions prism/templates/template.rb
Expand Up @@ -290,33 +290,47 @@ def template(name, write_to: nil)
template = File.expand_path("../#{filepath}", __dir__)

erb = read_template(template)

non_ruby_heading = <<~HEADING
/******************************************************************************/
/* This file is generated by the templates/template.rb script and should not */
/* be modified manually. See */
/* #{filepath + " " * (74 - filepath.size) } */
/* if you are looking to modify the */
/* template */
/******************************************************************************/
HEADING

ruby_heading = <<~HEADING
# frozen_string_literal: true
=begin
This file is generated by the templates/template.rb script and should not be
modified manually. See #{filepath}
if you are looking to modify the template
=end
HEADING

heading =
if File.extname(filepath.gsub(".erb", "")) == ".rb"
ruby_heading
else
non_ruby_heading
end
extension = File.extname(filepath.gsub(".erb", ""))

heading = case extension
when ".rb"
<<~HEADING
# frozen_string_literal: true
=begin
This file is generated by the templates/template.rb script and should not be
modified manually. See #{filepath}
if you are looking to modify the template
=end
HEADING
when ".rbs"
<<~HEADING
# This file is generated by the templates/template.rb script and should not be
# modified manually. See #{filepath}
# if you are looking to modify the template
HEADING
when ".rbi"
<<~HEADING
# typed: true
=begin
This file is generated by the templates/template.rb script and should not be
modified manually. See #{filepath}
if you are looking to modify the template
=end
HEADING
else
<<~HEADING
/******************************************************************************/
/* This file is generated by the templates/template.rb script and should not */
/* be modified manually. See */
/* #{filepath + " " * (74 - filepath.size) } */
/* if you are looking to modify the */
/* template */
/******************************************************************************/
HEADING
end

write_to ||= File.expand_path("../#{name}", __dir__)
contents = heading + erb.result_with_hash(locals)
Expand Down Expand Up @@ -376,7 +390,9 @@ def locals
"src/node.c",
"src/prettyprint.c",
"src/serialize.c",
"src/token_type.c"
"src/token_type.c",
"rbi/prism.rbi",
"sig/prism.rbs",
]
end

Expand Down

0 comments on commit 544f640

Please sign in to comment.