Class: Processor::Properties

Inherits:
Object
  • Object
show all
Defined in:
lib/depengine/processor/properties.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProperties

Returns a new instance of Properties.



6
7
8
9
# File 'lib/depengine/processor/properties.rb', line 6

def initialize
  @properties_hash = {}
  @assigner        = '='
end

Instance Attribute Details

#assignerObject

Returns the value of attribute assigner.



4
5
6
# File 'lib/depengine/processor/properties.rb', line 4

def assigner
  @assigner
end

#properties_hashObject

Returns the value of attribute properties_hash.



3
4
5
# File 'lib/depengine/processor/properties.rb', line 3

def properties_hash
  @properties_hash
end

Instance Method Details

#add(source, target) ⇒ Object

append strings



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/depengine/processor/properties.rb', line 119

def add(source, target)
  unless File.file? source
    $log.writer.error "File #{source} does not exists"
    fail "File #{source} does not exists"
  end

  FileUtils.copy_file(source, target) if source.strip != target.strip

  target_file = File.open(target, 'a')

  properties_hash.each_value do |value|
    target_file.puts value
  end

  target_file.close
end

#patch(source, target) ⇒ Object

patch properties (key/value)



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/depengine/processor/properties.rb', line 12

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

  if target.nil?
    $log.writer.error 'Parameter target not set'
    exit 1
  end

  if target.length == 0
    $log.writer.error 'Parameter target not set'
    exit 1
  end

  data = File.open(source, 'rb').readlines
  target_file = File.open(target, 'w')

  data.each do |entry|
    if entry.include? assigner
      keyvalue = entry.split(assigner, 2)

      # convert all inputs to String
      keyvalue[0] = keyvalue[0].to_s if keyvalue[0].class != String
      keyvalue[1] = keyvalue[1].to_s if keyvalue[1].class != String
      properties_hash[keyvalue[0].strip] = properties_hash[keyvalue[0].strip].to_s if properties_hash[keyvalue[0].strip].class != String

      if not properties_hash[keyvalue[0].strip].nil? and
         not properties_hash[keyvalue[0].strip].empty?
        if keyvalue[1].strip.empty?
          entry = entry.chomp + properties_hash[keyvalue[0].strip]
        else
          entry = keyvalue[0] + assigner + keyvalue[1].sub(keyvalue[1].strip, properties_hash[keyvalue[0].strip])

          ### patch global variable
          if not entry.nil? and entry.include?('<t:db_endpoint_1/>')
            entry = entry.sub(/<t\:db_endpoint_1\/>/, properties_hash['db_endpoint_1'])
          end
          if not entry.nil? and entry.include?('<t:db_endpoint_2/>')
            entry = entry.sub(/<t\:db_endpoint_2\/>/, properties_hash['db_endpoint_2'])
          end
        end
      end
    end
    target_file.puts entry
  end

  target_file.close
end

#substitute(source, target) ⇒ Object

patch strings



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/depengine/processor/properties.rb', line 64

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

  data        = File.open(source, 'rb').readlines
  target_file = File.open(target, 'w')

  data.each do |entry|
    properties_hash.keys.each do |hashkey|
      hashkey = hashkey.to_s
      entry = entry.sub(hashkey, properties_hash[hashkey].to_s)
      ### patch global variable
      next if entry.nil?
      entry = entry.sub(/<t\:db_endpoint_1\/>/, properties_hash['db_endpoint_1']) if entry.include?('<t:db_endpoint_1/>')
      entry = entry.sub(/<t\:db_endpoint_2\/>/, properties_hash['db_endpoint_2']) if entry.include?('<t:db_endpoint_2/>')

    end
    target_file.puts entry
  end

  target_file.close
end

#substitute_r(source, target) ⇒ Object

patch all recursive directory



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/depengine/processor/properties.rb', line 90

def substitute_r(source, target)
  unless File.directory? source
    $log.writer.error "Directory #{source} does not exists"
    exit 1
  end

  basepath_array = source.split('/')

  Dir.glob(source + '/**/*') do |file|
    keep_path = File.join(file.split('/') - basepath_array)
    if File.file? file
      target_path = File.join(target, keep_path)
      target_basepath = File.join(File.split(target_path).first)

      unless File.directory?(target_basepath)
        begin
          FileUtils.mkdir_p target_basepath
        rescue => e
          $log.writer.error "Can not create directory #{target_basepath}"
          $log.writer.error e.message
          exit 1
        end
      end
      substitute(file, target_path)
    end
  end
end