Class: Codeowners::DefinitionsFile
- Inherits:
-
Object
- Object
- Codeowners::DefinitionsFile
- Defined in:
- lib/codeowners/definitions_file.rb
Overview
Parses Cleo Codeowners YAML files
Defined Under Namespace
Classes: Match
Instance Attribute Summary collapse
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
Instance Method Summary collapse
-
#feature_paths(feature:, directory: nil) ⇒ Array, <Pathname>
Finds files defined under a feature, expands all globs, and returns all of them.
- #features_config ⇒ Object
- #file_config ⇒ Object
-
#find_feature_for_file(path:) ⇒ nil, String
Finds a feature the file belongs to.
-
#find_features_for_file(path:) ⇒ Object
Finds a feature the file belongs to.
-
#initialize(definition_glob = '.cleo/codeowners/**/*.y*ml') ⇒ DefinitionsFile
constructor
A new instance of DefinitionsFile.
- #owners_config ⇒ Object
Constructor Details
#initialize(definition_glob = '.cleo/codeowners/**/*.y*ml') ⇒ DefinitionsFile
Returns a new instance of DefinitionsFile.
12 13 14 15 16 |
# File 'lib/codeowners/definitions_file.rb', line 12 def initialize(definition_glob = '.cleo/codeowners/**/*.y*ml') @definition = Dir[definition_glob].inject({}) do |yaml, filepath| yaml.deep_merge(YAML.load_file(filepath)) end end |
Instance Attribute Details
#definition ⇒ Object (readonly)
Returns the value of attribute definition.
18 19 20 |
# File 'lib/codeowners/definitions_file.rb', line 18 def definition @definition end |
Instance Method Details
#feature_paths(feature:, directory: nil) ⇒ Array, <Pathname>
Finds files defined under a feature, expands all globs, and returns all of them
34 35 36 37 |
# File 'lib/codeowners/definitions_file.rb', line 34 def feature_paths(feature:, directory: nil) relative_globs = Array(file_config[feature]).map { |path| "./#{path}" } resolve_local_paths(relative_globs:, directory: directory || Pathname.pwd) end |
#features_config ⇒ Object
20 21 22 |
# File 'lib/codeowners/definitions_file.rb', line 20 def features_config definition['features'] || {} end |
#file_config ⇒ Object
28 29 30 |
# File 'lib/codeowners/definitions_file.rb', line 28 def file_config definition['files'] || {} end |
#find_feature_for_file(path:) ⇒ nil, String
Finds a feature the file belongs to
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/codeowners/definitions_file.rb', line 41 def find_feature_for_file(path:) return nil unless path target_path = File.join('.', path) file_config.each do |feature, paths| next unless paths paths.each do |feature_path| path_pattern = File.join('.', feature_path) path_pattern = "#{path_pattern}/*" if path_pattern.end_with?('/') path_pattern.gsub!('//', '/') return feature if File.fnmatch(path_pattern, target_path) end end nil end |
#find_features_for_file(path:) ⇒ Object
Finds a feature the file belongs to
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/codeowners/definitions_file.rb', line 64 def find_features_for_file(path:) return [] unless path target_path = File.join('.', path) file_config.filter_map do |feature, paths| next unless paths paths.filter_map do |feature_path| path_pattern = File.join('.', feature_path) path_pattern = "#{path_pattern}/*" if path_pattern.end_with?('/') path_pattern.gsub!('//', '/') Match.new(feature, path_pattern) if File.fnmatch(path_pattern, target_path) end end.flatten.uniq end |
#owners_config ⇒ Object
24 25 26 |
# File 'lib/codeowners/definitions_file.rb', line 24 def owners_config definition['owners'] || {} end |