Module: Sed

Defined in:
lib/depengine/processor/sed.rb

Class Method Summary collapse

Class Method Details

.copy(source, target, pattern, replacement) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/depengine/processor/sed.rb', line 2

def self.copy(source, target, pattern, replacement)
  unless File.file? source
    $log.writer.error "File #{source} does not exists"
    exit 1
  end

  File.open(source, 'r:UTF-8') do |source_file|
    content = source_file.read
    content.gsub!(pattern, replacement)
    File.open(target, 'w:UTF-8') do |target_file|
      target_file.write(content)
      target_file.close
    end
    source_file.close
  end
end

.patch(filename, pattern, replacement) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/depengine/processor/sed.rb', line 19

def self.patch(filename, pattern, replacement)
  unless File.file? filename
    $log.writer.error "File #{filename} does not exists"
    exit 1
  end

  content = ''
  File.open(filename, 'r:UTF-8') do |source_file|
    content = source_file.read
    content.gsub!(pattern, replacement)
    source_file.close
  end
  File.open(filename, 'w:UTF-8') do |target_file|
    target_file.write(content)
    target_file.close
  end
end