From 7f6415938eb888893b49cc968a5c01082743b7d7 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sat, 23 Nov 2013 11:48:42 +0000 Subject: [PATCH 1/1] Defer evaluation of an edited file's contents We want to make sure we don't build the contents until the resource is asctually being run, as it may rely on template files installed by a previous resource. --- cookbooks/chef/libraries/edit_file.rb | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/cookbooks/chef/libraries/edit_file.rb b/cookbooks/chef/libraries/edit_file.rb index 1c8e263e4..400e1967e 100644 --- a/cookbooks/chef/libraries/edit_file.rb +++ b/cookbooks/chef/libraries/edit_file.rb @@ -1,9 +1,22 @@ class Chef + class Util + class EditedFile + def initialize(file, block) + @file = file + @block = block + end + + def to_s + ::File.new(@file).collect do |line| + line = @block.call(line) + end.join("") + end + end + end + class Recipe def edit_file(file, &block) - ::File.new(file).collect do |line| - line = yield line - end.join("") + Chef::Util::EditedFile.new(file, block) end end end -- 2.43.2