-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply-diffconfig.rb
More file actions
executable file
·55 lines (43 loc) · 1.09 KB
/
apply-diffconfig.rb
File metadata and controls
executable file
·55 lines (43 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
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
#!/usr/bin/env ruby
if ARGV.length < 2
puts "Usage: apply-diffconfig DIFF ORIG_CONFIG"
exit 1
end
puts "Reading diff from #{ARGV[0]}"
puts "Reading config from #{ARGV[1]}"
begin
diff = File.read ARGV[0]
config = File.read ARGV[1]
rescue
puts "Failed to read file"
exit 1
end
diff = diff.split("\n")
config = config.split("\n")
for entry in diff do
if entry.match(/^-/)
cfg_option = entry.gsub("-", "CONFIG_").split(" ").first
config.reject!{|x| x.match(cfg_option)}
next
end
if entry.match(/^\+/)
cfg_option = entry.gsub("+", "CONFIG_").gsub(" ", "=")
config.push cfg_option
end
if entry.match(/^ /)
cfg_array = entry.split(" ")
cfg_option_name = "CONFIG_" + cfg_array.shift
cfg_array = cfg_array.join(" ").split(" -> ")
cfg_old_val = cfg_array.first
cfg_new_val = cfg_array.last
i = config.index config.select{|x|
x.match cfg_option_name + "=" + cfg_old_val
}.first
if i
config[i].gsub!(cfg_old_val, cfg_new_val)
else
config.push cfg_option_name + "=" + cfg_new_val
end
end
end
puts config.join("\n")