Skip to content

Commit 8b7430d

Browse files
committed
Move templating logic until YARP
1 parent ceecaa9 commit 8b7430d

File tree

2 files changed

+78
-71
lines changed

2 files changed

+78
-71
lines changed

Rakefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ task default: [:check_manifest, :compile, :test]
1212
require_relative "templates/template"
1313

1414
desc "Generate all ERB template based files"
15-
task templates: TEMPLATES
15+
task templates: YARP::TEMPLATES
1616

1717
def windows?
1818
RUBY_PLATFORM.include?("mingw")
@@ -59,13 +59,13 @@ elsif RUBY_ENGINE == "jruby"
5959
end
6060

6161
# So `rake clobber` will delete generated files
62-
CLOBBER.concat(TEMPLATES)
62+
CLOBBER.concat(YARP::TEMPLATES)
6363
CLOBBER.concat(["build"])
6464
CLOBBER << "lib/yarp/yarp.#{RbConfig::CONFIG["DLEXT"]}"
6565

66-
TEMPLATES.each do |filepath|
66+
YARP::TEMPLATES.each do |filepath|
6767
desc "Generate #{filepath}"
6868
file filepath => ["templates/#{filepath}.erb", "templates/template.rb", "config.yml"] do |t|
69-
template(t.name, locals)
69+
YARP.template(t.name)
7070
end
7171
end

templates/template.rb

Lines changed: 74 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -254,81 +254,88 @@ def initialize(config)
254254
end
255255
end
256256

257-
# This templates out a file using ERB with the given locals. The locals are
258-
# derived from the config.yml file.
259-
def template(name, locals, write_to: nil)
260-
filepath = "templates/#{name}.erb"
261-
template = File.expand_path("../#{filepath}", __dir__)
262-
write_to ||= File.expand_path("../#{name}", __dir__)
263-
264-
if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
265-
erb = ERB.new(File.read(template), trim_mode: "-")
266-
else
267-
erb = ERB.new(File.read(template), nil, "-")
268-
end
269-
erb.filename = template
270-
271-
non_ruby_heading = <<~HEADING
272-
/******************************************************************************/
273-
/* This file is generated by the templates/template.rb script and should not */
274-
/* be modified manually. See */
275-
/* #{filepath + " " * (74 - filepath.size) } */
276-
/* if you are looking to modify the */
277-
/* template */
278-
/******************************************************************************/
279-
HEADING
280-
281-
ruby_heading = <<~HEADING
282-
# frozen_string_literal: true
283-
=begin
284-
This file is generated by the templates/template.rb script and should not be
285-
modified manually. See #{filepath}
286-
if you are looking to modify the template
287-
=end
288-
289-
HEADING
290-
291-
heading = if File.extname(filepath.gsub(".erb", "")) == ".rb"
292-
ruby_heading
293-
else
294-
non_ruby_heading
257+
module YARP
258+
class << self
259+
# This templates out a file using ERB with the given locals. The locals are
260+
# derived from the config.yml file.
261+
def template(name, write_to: nil)
262+
filepath = "templates/#{name}.erb"
263+
template = File.expand_path("../#{filepath}", __dir__)
264+
write_to ||= File.expand_path("../#{name}", __dir__)
265+
266+
if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
267+
erb = ERB.new(File.read(template), trim_mode: "-")
268+
else
269+
erb = ERB.new(File.read(template), nil, "-")
270+
end
271+
erb.filename = template
272+
273+
non_ruby_heading = <<~HEADING
274+
/******************************************************************************/
275+
/* This file is generated by the templates/template.rb script and should not */
276+
/* be modified manually. See */
277+
/* #{filepath + " " * (74 - filepath.size) } */
278+
/* if you are looking to modify the */
279+
/* template */
280+
/******************************************************************************/
281+
HEADING
282+
283+
ruby_heading = <<~HEADING
284+
# frozen_string_literal: true
285+
=begin
286+
This file is generated by the templates/template.rb script and should not be
287+
modified manually. See #{filepath}
288+
if you are looking to modify the template
289+
=end
290+
291+
HEADING
292+
293+
heading = if File.extname(filepath.gsub(".erb", "")) == ".rb"
294+
ruby_heading
295+
else
296+
non_ruby_heading
297+
end
298+
299+
contents = heading + erb.result_with_hash(locals)
300+
FileUtils.mkdir_p(File.dirname(write_to))
301+
File.write(write_to, contents)
295302
end
296303

297-
contents = heading + erb.result_with_hash(locals)
298-
FileUtils.mkdir_p(File.dirname(write_to))
299-
File.write(write_to, contents)
300-
end
301-
302-
def locals
303-
config = YAML.load_file(File.expand_path("../config.yml", __dir__))
304+
private
305+
306+
def locals
307+
@locals ||=
308+
YAML.load_file(File.expand_path("../config.yml", __dir__)).then do |config|
309+
{
310+
nodes: config.fetch("nodes").map { |node| NodeType.new(node) }.sort_by(&:name),
311+
tokens: config.fetch("tokens").map { |token| Token.new(token) },
312+
flags: config.fetch("flags").map { |flags| Flags.new(flags) }
313+
}
314+
end
315+
end
316+
end
304317

305-
{
306-
nodes: config.fetch("nodes").map { |node| NodeType.new(node) }.sort_by(&:name),
307-
tokens: config.fetch("tokens").map { |token| Token.new(token) },
308-
flags: config.fetch("flags").map { |flags| Flags.new(flags) }
309-
}
318+
TEMPLATES = [
319+
"ext/yarp/api_node.c",
320+
"include/yarp/ast.h",
321+
"java/org/yarp/Loader.java",
322+
"java/org/yarp/Nodes.java",
323+
"java/org/yarp/AbstractNodeVisitor.java",
324+
"lib/yarp/mutation_visitor.rb",
325+
"lib/yarp/node.rb",
326+
"lib/yarp/serialize.rb",
327+
"src/node.c",
328+
"src/prettyprint.c",
329+
"src/serialize.c",
330+
"src/token_type.c"
331+
]
310332
end
311333

312-
TEMPLATES = [
313-
"ext/yarp/api_node.c",
314-
"include/yarp/ast.h",
315-
"java/org/yarp/Loader.java",
316-
"java/org/yarp/Nodes.java",
317-
"java/org/yarp/AbstractNodeVisitor.java",
318-
"lib/yarp/mutation_visitor.rb",
319-
"lib/yarp/node.rb",
320-
"lib/yarp/serialize.rb",
321-
"src/node.c",
322-
"src/prettyprint.c",
323-
"src/serialize.c",
324-
"src/token_type.c"
325-
]
326-
327334
if __FILE__ == $0
328335
if ARGV.empty?
329-
TEMPLATES.each { |f| template(f, locals) }
336+
YARP::TEMPLATES.each { |filepath| YARP.template(filepath) }
330337
else
331338
name, write_to = ARGV
332-
template(name, locals, write_to: write_to)
339+
YARP.template(name, write_to: write_to)
333340
end
334341
end

0 commit comments

Comments
 (0)