-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
97 lines (82 loc) · 2.35 KB
/
Rakefile
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# The 'deep_merge()' from 'activesupport' does not merge arrays.
# We need to be able to merge arrays as there are e.g. arrays of rubocop
# plugins from different input guides.
require "deep_merge"
require "yaml"
# Define input and output directories
def dirs(key)
{
rubocop: "src/rubocop",
ci_out: "ci",
}[key]
end
# Define input file paths
def input_guides(key)
{
ribose: "#{dirs(:rubocop)}/rubocop.ribose.yml",
thoughtbot: "#{dirs(:rubocop)}/rubocop.tb.yml",
rails: "#{dirs(:rubocop)}/rubocop.rails.yml",
}[key]
end
# Define output file paths
def output_guides(key)
{
default: "#{dirs(:ci_out)}/rubocop.yml",
rails: "#{dirs(:ci_out)}/rubocop.rails.yml",
}[key]
end
# Define input guides for each rubocop flavour
def default_guide_inputs
%i[thoughtbot ribose]
end
def rails_guide_inputs
default_guide_inputs + %i[rails]
end
namespace :build do
task default: :all
task all: [:rubocop]
task rubocop: %i[rubocop:default rubocop:rails]
task :"rubocop:default" do
merge_yaml_i(*default_guide_inputs, to: output_guides(:default))
end
task :"rubocop:rails" do
merge_yaml_i(*rails_guide_inputs, to: output_guides(:rails))
end
def merge_yaml_i(*src, to:)
merge_yaml(*src.map(&method(:input_guides)), to: to)
end
def merge_yaml(*src, to:)
aggregation = src.reduce({}) do |acc, file|
full_path = path_in_project(file)
y = YAML.safe_load(File.read(full_path))
acc.deep_merge!(
y,
sort_merged_arrays: true,
extend_existing_arrays: true,
)
end
target_full_path = path_in_project(to)
FileUtils.mkdir_p(File.dirname(target_full_path))
File.write(to, [yaml_header(*src), YAML.dump(aggregation)].join("\n"))
aggregation
end
def path_in_project(path)
File.expand_path(path, __dir__)
end
def yaml_header(*src)
(<<~YAML).chomp
############################################
# This file is auto-generated. #
# DO NOT EDIT DIRECTLY #
# OR YOUR CHANGES MAY BE OVERWRITTEN #
# Edit source files instead. #
# See README for details. #
############################################
#
# Source files:
#{src.map { |f| "# - #{f}" }.join("\n")}
#
############################################
YAML
end
end