def with_replaced_file(filepath, contents)
      ::Dir.mktmpdir do |temp_dir|
        begin
          temp_path = ::File.join(temp_dir, ::File.basename(filepath))
          ::FileUtils.mkdir_p(::File.dirname(filepath))
          if ::File.file?(filepath)
            ::FileUtils.mv(filepath, temp_path, :force => true)
          end
          ::File.open(filepath, 'w') { |f| f.write(contents) }
          yield
        ensure
          begin
            if ::File.file?(temp_path)
              ::FileUtils.mv(temp_path, filepath, :force => true)
            elsif ::File.file?(filepath)
              ::File.unlink(filepath)
            end
          rescue ::Exception => e
            @logger.note_warning("Failed to restore #{filepath.inspect}: #{e.message}")
          end
        end
      end
    end
def with_replaced_file(filepath, contents)
      ::Dir.mktmpdir do |temp_dir|
        begin
          temp_path = ::File.join(temp_dir, ::File.basename(filepath))
          ::FileUtils.mkdir_p(::File.dirname(filepath))
          if ::File.file?(filepath)
            ::FileUtils.mv(filepath, temp_path, :force => true)
          end
          ::File.open(filepath, 'w') { |f| f.write(contents) }
          yield
        ensure
          begin
            if ::File.file?(temp_path)
              ::FileUtils.mv(temp_path, filepath, :force => true)
            elsif ::File.file?(filepath)
              ::File.unlink(filepath)
            end
          rescue ::Exception => e
            @logger.note_warning("Failed to restore #{filepath.inspect}: #{e.message}")
          end
        end
      end
    end
Utility for replacing a file temporarily within a scope and ensuring it is
 restored afterward.

 @param [String] filepath to replace
 @param [String] contents to substitute

 @yield after replacing file