Module: MonkeyDo

Defined in:
lib/monkey_do.rb

Class Method Summary collapse

Class Method Details

.monkey_patchObject



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
# File 'lib/monkey_do.rb', line 32

def self.monkey_patch
  patch_cfg = YAML::load_file(@conf)

  patch_cfg.each { |monkey_item|
    if monkey_item.has_key?(:patch)
      if Kernel.const_defined?('DEBUG')
        puts "copying #{monkey_item[:patch][:source]} to #{monkey_item[:patch][:destination]}" if DEBUG
      end

       FileUtils.cp monkey_item[:patch][:source], monkey_item[:patch][:destination]
    elsif monkey_item.has_key?(:update_text)
      unless File.exists?(monkey_item[:update_text][:file])
        raise "no file found for #{monkey_item[:update_text][:file]}"
      end
      if monkey_item[:update_text].has_key?(:search_eval)
        search = eval(monkey_item[:update_text][:search_eval])
      elsif monkey_item[:update_text].has_key?(:search)
        search = monkey_item[:update_text][:search]
      end
      if monkey_item[:update_text].has_key?(:replace_eval)
        replace = eval(monkey_item[:update_text][:replace_eval])
        if monkey_item[:update_text].has_key?(:string_wrap)
          replace = "\"#{replace}\""  
        end

      elsif monkey_item[:update_text].has_key?(:replace)
        replace = monkey_item[:update_text][:replace]
      end    

      file = monkey_item[:update_text][:file]
      backup_file = "#{file}.#{Time.now.usec}"
      updated_file = File.readlines(file).to_s.gsub("#{search}", "#{replace}")
      begin
        FileUtils.mv(file, backup_file)
        fd=File.open(file, 'w+')
        fd.puts updated_file
        FileUtils.rm_f backup_file
      rescue => e
        puts e.inspect
        FileUtils.mv(backup_file, file)
      end
    end  

  }
end