-
Notifications
You must be signed in to change notification settings - Fork 9
/
generate_require_files.rb
49 lines (42 loc) · 1.79 KB
/
generate_require_files.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
45
46
47
48
49
# typed: true
# frozen_string_literal: true
BASE_FOLDER_PATH = "rspec"
def directories_in(root)
Dir.entries(root).sort.select do |entry|
fully_qualified_entry = File.join(root, entry)
File.directory?(fully_qualified_entry) && ![".", ".."].include?(entry.to_s)
end
end
def files_in(root)
Dir.entries(root).sort.reject do |entry|
fully_qualified_entry = File.join(root, entry)
File.directory?(fully_qualified_entry) || entry.to_s == "all.rb" || entry[-3..-1] != ".rb"
end
end
def recursive_files_in(root)
full_paths = Dir.glob(File.join(root, "**", "*")).sort.reject do |entry|
fully_qualified_entry = File.join(root, entry)
File.directory?(fully_qualified_entry) || entry[-7..-1] == "/all.rb" || entry[-3..-1] != ".rb"
end
remove_path = root.split("/")[0..-2].join("/") + "/"
full_paths.map { |full_path| full_path.gsub(remove_path, "") }
end
def write_require_file(root, require_directories, require_files)
require_file_path = File.join(root, "all.rb")
File.open(require_file_path, "w") do |file|
file.write("# frozen_string_literal: true\n\n")
file.write("# THIS FILE IS AUTOGENERATED AND SHOULD NOT BE MANUALLY MODIFIED\n\n")
require_directories.each do |require_directory|
file.write("require '#{BASE_FOLDER_PATH}/#{require_directory}/all'\n")
end
file.write("\n") unless require_directories.empty?
require_files.each { |require_file| file.write("require '#{BASE_FOLDER_PATH}/#{require_file[0..-4]}'\n") }
end
end
root = File.join(__dir__, "lib", BASE_FOLDER_PATH)
directories = directories_in(root)
write_require_file(root, directories, files_in(root))
directories.each do |directory|
fully_qualified_directory = File.join(root, directory)
write_require_file(fully_qualified_directory, [], recursive_files_in(fully_qualified_directory))
end