@@ -254,81 +254,88 @@ def initialize(config)
254
254
end
255
255
end
256
256
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 )
295
302
end
296
303
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
304
317
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
+ ]
310
332
end
311
333
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
-
327
334
if __FILE__ == $0
328
335
if ARGV . empty?
329
- TEMPLATES . each { |f | template ( f , locals ) }
336
+ YARP :: TEMPLATES . each { |filepath | YARP . template ( filepath ) }
330
337
else
331
338
name , write_to = ARGV
332
- template ( name , locals , write_to : write_to )
339
+ YARP . template ( name , write_to : write_to )
333
340
end
334
341
end
0 commit comments