-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge_on_merge.rb
executable file
·44 lines (30 loc) · 956 Bytes
/
merge_on_merge.rb
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
#!/usr/bin/env ruby
require 'fileutils'
require 'json'
require "debug"
def run(input_path, base, merge_json_path, output, processed_path)
FileUtils.mkdir_p(processed_path)
FileUtils.mkdir_p(input_path)
puts "Consuming files in #{input_path} path"
files = Dir.open(input_path) do |input_dir|
FileUtils.touch File.join(Dir.pwd, input_dir, '.gitkeep')
Dir.glob(File.join(Dir.pwd, input_dir, '*.json'))
end
puts "Merging files to #{output}, using #{base} as base"
base = JSON.load_file!(base)
base_dest = base.dig(*merge_json_path.split("."))
files.each do |input_path|
json = JSON.load_file!(input_path)
base_dest.append(json)
end
output_json = JSON.pretty_generate(base)
puts "Writing to #{output}:"
puts output_json
puts '<EOF>'
File.open(output, 'w') do |f|
f.write(output_json)
end
# Move files to processed_path folder
FileUtils.mv files, processed_path, verbose: true
end
run(*ARGV)