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.



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

def initialize()
  properties_hash = {}
  assigner        = "="
end

Instance Attribute Details

#assignerObject

Returns the value of attribute assigner.



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

def assigner
  @assigner
end

#properties_hashObject

Returns the value of attribute properties_hash.



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

def properties_hash
  @properties_hash
end

Instance Method Details

#add(source, target) ⇒ Object

append strings



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/depengine/processor/properties.rb', line 151

def add(source, target)

  if not File.file? source
    $log.writer.error "File #{source} does not exists"
    exit 1
  end   
   
  if source.strip != target.strip
     FileUtils.copy_file(source, target)
  end
   
  target_file = File.open(target, "a")
 
  properties_hash.each_value { |value|
    target_file.puts value
  }

  target_file.close

end

#patch(source, target) ⇒ Object

patch properties (key/value)



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/depengine/processor/properties.rb', line 13

def patch(source, target)

  if not 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
  
   
  ### read source file in array
  if RUBY_VERSION < '1.9'
    read_mode = 'r'
  else
    read_mode = 'rb'
  end

  data = File.open(source, read_mode).readlines

  target_file = File.open(target, "w")
  
  data.each { |entry|
    if entry.include? assigner
      keyvalue = entry.split(assigner,2)

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

      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 = entry.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
  }

  target_file.close

end

#substitute(source, target) ⇒ Object

patch strings



82
83
84
85
86
87
88
89
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
117
# File 'lib/depengine/processor/properties.rb', line 82

def substitute(source, target)

  if not File.file? source
    $log.writer.error "File #{source} does not exists"
    exit 1
  end   
   
  ### read source file in array
  if RUBY_VERSION < '1.9'
    read_mode = 'r'
  else
    read_mode = 'rb'
  end

  data        = File.open(source, read_mode).readlines
  target_file = File.open(target, "w")

  data.each { |entry|
    properties_hash.keys.each { |hashkey|
      hashkey = hashkey.to_s
      entry = entry.sub(hashkey, properties_hash[hashkey].to_s)
      ### 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
      
    }
    target_file.puts entry
  }

  target_file.close

end

#substitute_r(source, target) ⇒ Object

patch all recursive directory



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/depengine/processor/properties.rb', line 120

def substitute_r(source, target)

  if not 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 )

      if not File.directory?(target_basepath)
        begin
          FileUtils.mkdir_p target_basepath
        rescue Exception => 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