summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Newton <kddnewton@gmail.com>2023-08-30 15:35:35 -0400
committergit <svn-admin@ruby-lang.org>2023-08-30 20:41:39 +0000
commitf83070816dbe281e8cd2993494704c4a2af49a47 (patch)
treee8dbef81d7bc37317dd8d849fbede402d32c95f1
parentae609a995e344877a990f4c16eca88b02dab5eba (diff)
[ruby/yarp] Move templating logic until YARP
https://github.com/ruby/yarp/commit/8b7430dbc7
-rwxr-xr-xyarp/templates/template.rb141
1 files changed, 74 insertions, 67 deletions
diff --git a/yarp/templates/template.rb b/yarp/templates/template.rb
index e34d5b1a7b..a5e25b61b1 100755
--- a/yarp/templates/template.rb
+++ b/yarp/templates/template.rb
@@ -254,81 +254,88 @@ class Flag
end
end
-# This templates out a file using ERB with the given locals. The locals are
-# derived from the config.yml file.
-def template(name, locals, write_to: nil)
- filepath = "templates/#{name}.erb"
- template = File.expand_path("../#{filepath}", __dir__)
- write_to ||= File.expand_path("../#{name}", __dir__)
-
- if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
- erb = ERB.new(File.read(template), trim_mode: "-")
- else
- erb = ERB.new(File.read(template), nil, "-")
- end
- erb.filename = 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
+module YARP
+ class << self
+ # This templates out a file using ERB with the given locals. The locals are
+ # derived from the config.yml file.
+ def template(name, write_to: nil)
+ filepath = "templates/#{name}.erb"
+ template = File.expand_path("../#{filepath}", __dir__)
+ write_to ||= File.expand_path("../#{name}", __dir__)
+
+ if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
+ erb = ERB.new(File.read(template), trim_mode: "-")
+ else
+ erb = ERB.new(File.read(template), nil, "-")
+ end
+ erb.filename = 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
+
+ contents = heading + erb.result_with_hash(locals)
+ FileUtils.mkdir_p(File.dirname(write_to))
+ File.write(write_to, contents)
end
- contents = heading + erb.result_with_hash(locals)
- FileUtils.mkdir_p(File.dirname(write_to))
- File.write(write_to, contents)
-end
-
-def locals
- config = YAML.load_file(File.expand_path("../config.yml", __dir__))
+ private
+
+ def locals
+ @locals ||=
+ YAML.load_file(File.expand_path("../config.yml", __dir__)).then do |config|
+ {
+ nodes: config.fetch("nodes").map { |node| NodeType.new(node) }.sort_by(&:name),
+ tokens: config.fetch("tokens").map { |token| Token.new(token) },
+ flags: config.fetch("flags").map { |flags| Flags.new(flags) }
+ }
+ end
+ end
+ end
- {
- nodes: config.fetch("nodes").map { |node| NodeType.new(node) }.sort_by(&:name),
- tokens: config.fetch("tokens").map { |token| Token.new(token) },
- flags: config.fetch("flags").map { |flags| Flags.new(flags) }
- }
+ TEMPLATES = [
+ "ext/yarp/api_node.c",
+ "include/yarp/ast.h",
+ "java/org/yarp/Loader.java",
+ "java/org/yarp/Nodes.java",
+ "java/org/yarp/AbstractNodeVisitor.java",
+ "lib/yarp/mutation_visitor.rb",
+ "lib/yarp/node.rb",
+ "lib/yarp/serialize.rb",
+ "src/node.c",
+ "src/prettyprint.c",
+ "src/serialize.c",
+ "src/token_type.c"
+ ]
end
-TEMPLATES = [
- "ext/yarp/api_node.c",
- "include/yarp/ast.h",
- "java/org/yarp/Loader.java",
- "java/org/yarp/Nodes.java",
- "java/org/yarp/AbstractNodeVisitor.java",
- "lib/yarp/mutation_visitor.rb",
- "lib/yarp/node.rb",
- "lib/yarp/serialize.rb",
- "src/node.c",
- "src/prettyprint.c",
- "src/serialize.c",
- "src/token_type.c"
-]
-
if __FILE__ == $0
if ARGV.empty?
- TEMPLATES.each { |f| template(f, locals) }
+ YARP::TEMPLATES.each { |filepath| YARP.template(filepath) }
else
name, write_to = ARGV
- template(name, locals, write_to: write_to)
+ YARP.template(name, write_to: write_to)
end
end