module Processor class Properties attr_accessor :properties_hash attr_accessor :assigner def initialize() properties_hash = {} assigner = "=" end ### patch properties (key/value) 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 data = File.open(source, 'rb').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?('') entry = entry.sub(//, properties_hash['db_endpoint_1']) end if not entry.nil? and entry.include?('') entry = entry.sub(//, properties_hash['db_endpoint_2']) end end end end target_file.puts entry } target_file.close end ### patch strings def substitute(source, target) if not 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 { |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?('') entry = entry.sub(//, properties_hash['db_endpoint_1']) end if not entry.nil? and entry.include?('') entry = entry.sub(//, properties_hash['db_endpoint_2']) end } target_file.puts entry } target_file.close end ### patch all recursive directory 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 ### append strings 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 end end