Module: PsmDeadSimpleDeploymentTools::Files
- Defined in:
- lib/psm_dead_simple_deployment_tools/files.rb
Overview
include PsmDeadSimpleDeploymentTools::Files in order to manipulate files
Defined Under Namespace
Classes: FileNotFound, InvalidType
Instance Method Summary collapse
-
#append(path, template_path = nil, new_line: true) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#replace(path, match, template_path = nil) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #write(path, template_path = nil) ⇒ Object
Instance Method Details
#append(path, template_path = nil, new_line: true) ⇒ Object
rubocop:disable Metrics/MethodLength
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/psm_dead_simple_deployment_tools/files.rb', line 27 def append(path, template_path = nil, new_line: true) # rubocop:disable Metrics/MethodLength raise FileNotFound, "file not found: #{path}" unless File.exist?(path) text = if block_given? yield else Utils::ErbUtil.new(template_path, binding).result end content = File.read(path) return :no_change if content.include?(text) File.open(path, "a") do |f| f << "\n" if new_line f << text end :new end |
#replace(path, match, template_path = nil) ⇒ Object
rubocop:disable Metrics/MethodLength
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/psm_dead_simple_deployment_tools/files.rb', line 48 def replace(path, match, template_path = nil) # rubocop:disable Metrics/MethodLength raise FileNotFound, "file not found: #{path}" unless File.exist?(path) text = if block_given? yield else Utils::ErbUtil.new(template_path, binding).result end content = File.read(path) case match when String return :no_change unless content.include?(match) when Regexp return :no_change unless match.match?(content) else raise InvalidType, "match must be String or Regexp" end new_content = content.gsub(match, text) File.open(path, "w") do |f| f.write(new_content) end :new end |
#write(path, template_path = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/psm_dead_simple_deployment_tools/files.rb', line 11 def write(path, template_path = nil) content = if block_given? yield else Utils::ErbUtil.new(template_path, binding).result end return :no_change if File.exist?(path) && File.read(path) == content File.open(path, "w") do |f| f.write(content) end :new end |