Class: Kaya::Support::Update

Inherits:
Object
  • Object
show all
Defined in:
lib/kaya/support/update.rb

Class Method Summary collapse

Class Method Details

.kaya_confObject

Returns the most updated version of kaya_conf file, and updates the file if neded



9
10
11
# File 'lib/kaya/support/update.rb', line 9

def self.kaya_conf
  update_json_file "kaya_conf"
end

.update_json_file(name) ⇒ Object

It also removes all keys not longer present in the template file.



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/kaya/support/update.rb', line 19

def self.update_json_file name
  file = JSON.parse(IO.read(@project_folder + name))
  template = JSON.parse(IO.read(@gem_folder + name + ".tt"))

  modified = false

  deprecated_keys = file.keys - template.keys
  if deprecated_keys.any?
    deprecated_keys.each { |key| file.delete key }
    modified = true
    puts "The following keys: #{deprecated_keys} are deprecated and are going to be removed from your #{name} file.".colorize(:green)
  end

  if file.keys.sort != template.keys.sort
    puts "The following keys: #{template.keys - file.keys} are new and are going to be added to your #{name} file.".colorize(:green)
    modified = true
    file.merge! template
  end

  file.keys.each do |key|
    if file[key].class == Hash
      deprecated_sub_keys = file[key].keys - template[key].keys
      if deprecated_sub_keys.any?
        deprecated_sub_keys.each{ |sub_key| file[key].delete sub_key }
        modified = true
        puts "The following sub keys: #{deprecated_sub_keys} from #{key} key are deprecated and are going to be removed from your #{name} file.".colorize(:green)
      end
      if file[key].keys.sort != template[key].keys.sort
        puts "The following sub keys: #{template[key].keys - file[key].keys} from #{key} key are new and are going to be added to your #{name} file.".colorize(:green)
        modified = true
        file[key].merge! template[key]
      end
    end
  end

  if modified
    File.write(@project_folder + name, JSON.pretty_generate(file))
    puts "Done.".colorize(:green)
  end

  return file

end