2019-01-12 21:19:00 +00:00
|
|
|
module Ameba
|
|
|
|
# Helper module that is utilizes helpers for working with globs.
|
|
|
|
module GlobUtils
|
|
|
|
# Returns all files that match specified globs.
|
|
|
|
# Globs can have wildcards or be rejected:
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# find_files_by_globs(["**/*.cr", "!lib"])
|
|
|
|
# ```
|
|
|
|
def find_files_by_globs(globs)
|
|
|
|
rejected = rejected_globs(globs)
|
|
|
|
selected = globs - rejected
|
|
|
|
|
2021-01-25 01:34:33 +00:00
|
|
|
expand(selected) - expand(rejected.map!(&.[1..-1]))
|
2019-01-12 21:19:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Expands globs. Globs can point to files or even directories.
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# expand(["spec/*.cr", "src"]) # => all files in src folder + first level specs
|
|
|
|
# ```
|
|
|
|
def expand(globs)
|
2021-01-11 18:13:58 +00:00
|
|
|
globs.flat_map do |glob|
|
2019-01-12 21:19:00 +00:00
|
|
|
glob += "/**/*.cr" if File.directory?(glob)
|
2023-08-04 20:48:35 +00:00
|
|
|
raise ArgumentError.new("No files found matching #{glob}") if Dir[glob].empty?
|
2019-01-12 21:19:00 +00:00
|
|
|
Dir[glob]
|
2021-01-11 18:13:58 +00:00
|
|
|
end.uniq!
|
2019-01-12 21:19:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private def rejected_globs(globs)
|
|
|
|
globs.select do |glob|
|
|
|
|
glob.starts_with?('!') && !File.exists?(glob)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|