Class: Packwerk::PackageTodo
- Inherits:
-
Object
- Object
- Packwerk::PackageTodo
- Defined in:
- lib/packwerk/package_todo.rb
Instance Method Summary collapse
-
#add_entries(reference, violation_type) ⇒ Object
: (Packwerk::Reference reference, String violation_type) -> bool.
-
#delete_if_exists ⇒ Object
: -> void.
-
#dump ⇒ Object
: -> void.
-
#initialize(package, path) ⇒ PackageTodo
constructor
: (Packwerk::Package package, String path) -> void.
-
#listed?(reference, violation_type:) ⇒ Boolean
: (Packwerk::Reference reference, violation_type: String) -> bool.
-
#stale_violations?(for_files) ⇒ Boolean
: (Set for_files) -> bool.
Constructor Details
#initialize(package, path) ⇒ PackageTodo
: (Packwerk::Package package, String path) -> void
15 16 17 18 19 20 |
# File 'lib/packwerk/package_todo.rb', line 15 def initialize(package, path) @package = package @path = path @new_entries = {} #: entries @old_entries = nil #: entries? end |
Instance Method Details
#add_entries(reference, violation_type) ⇒ Object
: (Packwerk::Reference reference, String violation_type) -> bool
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/packwerk/package_todo.rb', line 34 def add_entries(reference, violation_type) package_violations = new_entries.fetch(reference.constant.package.name, {}) entries_for_constant = package_violations[reference.constant.name] ||= {} entries_for_constant["violations"] ||= [] entries_for_constant.fetch("violations") << violation_type entries_for_constant["files"] ||= [] entries_for_constant.fetch("files") << reference.relative_path.to_s new_entries[reference.constant.package.name] = package_violations listed?(reference, violation_type: violation_type) end |
#delete_if_exists ⇒ Object
: -> void
88 89 90 |
# File 'lib/packwerk/package_todo.rb', line 88 def delete_if_exists File.delete(@path) if File.exist?(@path) end |
#dump ⇒ Object
: -> void
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/packwerk/package_todo.rb', line 66 def dump if new_entries.empty? delete_if_exists else prepare_entries_for_dump = <<~MESSAGE # This file contains a list of dependencies that are not part of the long term plan for the # '#{@package.name}' package. # We should generally work to reduce this list over time. # # You can regenerate this file using the following command: # # bin/packwerk update-todo MESSAGE File.open(@path, "w") do |f| f.write() f.write(new_entries.to_yaml) end end end |
#listed?(reference, violation_type:) ⇒ Boolean
: (Packwerk::Reference reference, violation_type: String) -> bool
23 24 25 26 27 28 29 30 31 |
# File 'lib/packwerk/package_todo.rb', line 23 def listed?(reference, violation_type:) violated_constants_found = old_entries.dig(reference.constant.package.name, reference.constant.name) return false unless violated_constants_found violated_constant_in_file = violated_constants_found.fetch("files", []).include?(reference.relative_path) return false unless violated_constant_in_file violated_constants_found.fetch("violations", []).include?(violation_type) end |
#stale_violations?(for_files) ⇒ Boolean
: (Set for_files) -> bool
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/packwerk/package_todo.rb', line 49 def stale_violations?(for_files) prepare_entries_for_dump old_entries.any? do |package, violations| files = for_files + deleted_files_for(package) violations_for_files = package_violations_for(violations, files: files) # We `next false` because if we cannot find existing violations for `for_files` within # the `package_todo.yml` file, then there are no violations that # can be considered stale. next false if violations_for_files.empty? stale_violation_for_package?(package, violations: violations_for_files) end end |