Skip to content

Commit 7881705

Browse files
committed
Call ERB.new correctly depending on the RUBY_VERSION
* On Ruby 3.0.x and older, call ERB.new(template_code, nil, "-") * On Ruby 3.1.0 and newer, call ERB.new(template_code, trim_mode: "-") Fixes #1894
1 parent 5d2b485 commit 7881705

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/fpm/package.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,12 @@ def template(path)
331331
template_path = File.join(template_dir, path)
332332
template_code = File.read(template_path)
333333
logger.info("Reading template", :path => template_path)
334-
erb = ERB.new(template_code, nil, "-")
334+
erb = erbnew(template_code)
335335
erb.filename = template_path
336336
return erb
337337
end # def template
338338

339+
339340
#######################################
340341
# The following methods are provided to
341342
# easily override particular substitut-
@@ -518,7 +519,7 @@ def write_scripts
518519
# flag), then apply it as an ERB template.
519520
def script(script_name)
520521
if attributes[:template_scripts?]
521-
erb = ERB.new(scripts[script_name], nil, "-")
522+
erb = erbnew(scripts[script_name])
522523
# TODO(sissel): find the original file name for the file.
523524
erb.filename = "script(#{script_name})"
524525
return erb.result(binding)

lib/fpm/util.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,29 @@ def expand_pessimistic_constraints(constraint)
406406
def logger
407407
@logger ||= Cabin::Channel.get
408408
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
409432
end # module FPM::Util
410433

411434
require 'fpm/util/tar_writer'

0 commit comments

Comments
 (0)