File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -331,11 +331,12 @@ def template(path)
331
331
template_path = File . join ( template_dir , path )
332
332
template_code = File . read ( template_path )
333
333
logger . info ( "Reading template" , :path => template_path )
334
- erb = ERB . new ( template_code , nil , "-" )
334
+ erb = erbnew ( template_code )
335
335
erb . filename = template_path
336
336
return erb
337
337
end # def template
338
338
339
+
339
340
#######################################
340
341
# The following methods are provided to
341
342
# easily override particular substitut-
@@ -518,7 +519,7 @@ def write_scripts
518
519
# flag), then apply it as an ERB template.
519
520
def script ( script_name )
520
521
if attributes [ :template_scripts? ]
521
- erb = ERB . new ( scripts [ script_name ] , nil , "-" )
522
+ erb = erbnew ( scripts [ script_name ] )
522
523
# TODO(sissel): find the original file name for the file.
523
524
erb . filename = "script(#{ script_name } )"
524
525
return erb . result ( binding )
Original file line number Diff line number Diff line change @@ -406,6 +406,29 @@ def expand_pessimistic_constraints(constraint)
406
406
def logger
407
407
@logger ||= Cabin ::Channel . get
408
408
end # def logger
409
+
410
+ def erbnew ( template_code )
411
+ # In Ruby 2.6(?), Ruby changed how ERB::new is invoked.
412
+ # First, it added keyword args like `ERB.new(..., trim_mode: "-")`
413
+ # Later, it deprecated then removed the safe_level feature.
414
+ # As of Ruby 3.1, warnings are printed at runtime when ERB.new is called with the old syntax.
415
+ # Ruby 2.5 and older does not support the ERB.new keyword args.
416
+ #
417
+ # My tests showed:
418
+ # * Ruby 2.3.0 through 3.0 work correctly with the old syntax.
419
+ # * Ruby 3.1.0 and newer (at time of writing, Ruby 3.2) require the new syntax
420
+ # Therefore, in order to support the most versions of ruby, we need to do a version check
421
+ # to invoke ERB.new correctly and without printed warnings.
422
+ # References: https://github.com/jordansissel/fpm/issues/1894
423
+ # Honestly, I'm not sure if Gem::Version is correct to use in this situation, but it works.
424
+ if Gem ::Version . new ( RUBY_VERSION ) < Gem ::Version . new ( "3.1.0" )
425
+ # Ruby 3.0.x and older
426
+ return ERB . new ( template_code , nil , "-" )
427
+ else
428
+ # Ruby 3.1.0 and newer
429
+ return ERB . new ( template_code , trim_mode : "-" )
430
+ end
431
+ end
409
432
end # module FPM::Util
410
433
411
434
require 'fpm/util/tar_writer'
You can’t perform that action at this time.
0 commit comments