Class: FeatureMap::Private::AssignmentsFile

Inherits:
Object
  • Object
show all
Defined in:
lib/feature_map/private/assignments_file.rb

Overview

This class is responsible for turning FeatureMap directives (e.g. annotations, directory assignments, etc) into a assignments.yml file, that can be used as an input to a variety of engineering team utilities (e.g. PR/release announcements, documentation generation, etc).

Defined Under Namespace

Classes: FileContentError

Constant Summary collapse

FILES_KEY =
'files'
FILE_FEATURE_KEY =
'feature'
FILE_MAPPER_KEY =
'mapper'
FEATURES_KEY =
'features'
FEATURE_FILES_KEY =
'files'

Class Method Summary collapse

Class Method Details

.actual_contents_linesObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/feature_map/private/assignments_file.rb', line 22

def self.actual_contents_lines
  if path.exist?
    content = path.read
    lines = path.read.split("\n")
    if content.end_with?("\n")
      lines << ''
    end
    lines
  else
    ['']
  end
end

.expected_contents_linesObject



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
# File 'lib/feature_map/private/assignments_file.rb', line 35

def self.expected_contents_lines
  cache = Private.glob_cache.raw_cache_contents

  header = <<~HEADER
    # STOP! - DO NOT EDIT THIS FILE MANUALLY
    # This file was automatically generated by "bin/featuremap validate". The next time this file
    # is generated any changes will be lost. For more details:
    # https://github.com/Beyond-Finance/feature_map
    #
    # It is recommended to commit this file into your source control. It will only change when the
    # set of files assigned to a feature change, which should be explicitly tracked.
  HEADER

  files_content = {}
  files_by_feature = {}
  features_content = {}

  cache.each do |mapper_description, assignment_map_cache|
    assignment_map_cache = assignment_map_cache.sort_by do |glob, _feature|
      glob
    end

    assignment_map_cache.to_h.each do |path, feature|
      files_content[path] = { FILE_FEATURE_KEY => feature.name, FILE_MAPPER_KEY => mapper_description }

      files_by_feature[feature.name] ||= []
      files_by_feature[feature.name] << path
    end
  end

  # Ordering of features in the resulting YAML content is determined by the order in which keys are added to
  # each hash.
  CodeFeatures.all.sort_by(&:name).each do |feature|
    files = files_by_feature[feature.name] || []
    expanded_files = files.flat_map { |file| Dir.glob(file) }.reject { |path| File.directory?(path) }

    # Exclude features that have no releated files. These features are presumably irrelevant to the current
    # repo/application.
    next if expanded_files.empty?

    features_content[feature.name] = { 'files' => expanded_files.sort }

    if !Private.configuration.skip_code_ownership
      features_content[feature.name]['teams'] = expanded_files.map { |file| CodeOwnership.for_file(file)&.name }.compact.uniq.sort
    end
  end

  [
    *header.split("\n"),
    '', # For line between header and file assignments lines
    *{ FILES_KEY => files_content, FEATURES_KEY => features_content }.to_yaml.split("\n"),
    '' # For end-of-file newline
  ]
end

.load_features!Object



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/feature_map/private/assignments_file.rb', line 134

def self.load_features!
  assignments_content = YAML.load_file(path)

  return assignments_content[FEATURES_KEY] if assignments_content.is_a?(Hash) && assignments_content[FEATURES_KEY]

  raise FileContentError, "Unexpected content found in #{path}. Use `bin/featuremap validate` to regenerate it and try again."
rescue Psych::SyntaxError => e
  raise FileContentError, "Invalid YAML content found at #{path}. Error: #{e.message} Use `bin/featuremap validate` to generate it and try again."
rescue Errno::ENOENT
  raise FileContentError, "No feature assignments file found at #{path}. Use `bin/featuremap validate` to generate it and try again."
end

.pathObject



95
96
97
# File 'lib/feature_map/private/assignments_file.rb', line 95

def self.path
  Pathname.pwd.join('.feature_map/assignments.yml')
end

.to_glob_cacheObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/feature_map/private/assignments_file.rb', line 114

def self.to_glob_cache
  raw_cache_contents = {}
  features_by_name = CodeFeatures.all.each_with_object({}) do |feature, map|
    map[feature.name] = feature
  end
  mapper_descriptions = Set.new(Mapper.all.map(&:description))

  features_file_content = YAML.load_file(path)
  features_file_content[FILES_KEY]&.each do |file_path, file_assignment|
    next if file_assignment.nil?
    next if file_assignment[FILE_FEATURE_KEY].nil? || features_by_name[file_assignment[FILE_FEATURE_KEY]].nil?
    next if file_assignment[FILE_MAPPER_KEY].nil? || !mapper_descriptions.include?(file_assignment[FILE_MAPPER_KEY])

    raw_cache_contents[file_assignment[FILE_MAPPER_KEY]] ||= {}
    raw_cache_contents.fetch(file_assignment[FILE_MAPPER_KEY])[file_path] = features_by_name[file_assignment[FILE_FEATURE_KEY]]
  end

  GlobCache.new(raw_cache_contents)
end

.update_cache!(files) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/feature_map/private/assignments_file.rb', line 99

def self.update_cache!(files)
  cache = Private.glob_cache
  # Each mapper returns a new copy of the cache subset related to that mapper,
  # which is then stored back into the cache.
  Mapper.all.each do |mapper|
    existing_cache = cache.raw_cache_contents.fetch(mapper.description, {})
    updated_cache = mapper.update_cache(existing_cache, files)
    cache.raw_cache_contents[mapper.description] = updated_cache
  end
end

.use_features_cache?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/feature_map/private/assignments_file.rb', line 110

def self.use_features_cache?
  AssignmentsFile.path.exist? && !Private.configuration.skip_features_validation
end

.write!Object



90
91
92
93
# File 'lib/feature_map/private/assignments_file.rb', line 90

def self.write!
  FileUtils.mkdir_p(path.dirname) if !path.dirname.exist?
  path.write(expected_contents_lines.join("\n"))
end