class Packwerk::DeprecatedReferences

Public Class Methods

new(package, filepath) click to toggle source
# File lib/packwerk/deprecated_references.rb, line 11
def initialize(package, filepath)
  @package = package
  @filepath = filepath
  @new_entries = {}
end

Public Instance Methods

add_entries(reference, violation_type) click to toggle source
# File lib/packwerk/deprecated_references.rb, line 32
def add_entries(reference, violation_type)
  package_violations = @new_entries.fetch(reference.constant.package.name, {})
  entries_for_file = package_violations[reference.constant.name] ||= {}

  entries_for_file["violations"] ||= []
  entries_for_file["violations"] << violation_type.serialize

  entries_for_file["files"] ||= []
  entries_for_file["files"] << reference.relative_path.to_s

  @new_entries[reference.constant.package.name] = package_violations
  listed?(reference, violation_type: violation_type)
end
dump() click to toggle source
# File lib/packwerk/deprecated_references.rb, line 65
    def dump
      if @new_entries.empty?
        File.delete(@filepath) if File.exist?(@filepath)
      else
        prepare_entries_for_dump
        message = <<~MESSAGE
          # This file contains a list of dependencies that are not part of the long term plan for #{@package.name}.
          # We should generally work to reduce this list, but not at the expense of actually getting work done.
          #
          # You can regenerate this file using the following command:
          #
          # packwerk update-deprecations #{@package.name}
        MESSAGE
        File.open(@filepath, "w") do |f|
          f.write(message)
          f.write(@new_entries.to_yaml)
        end
      end
    end
listed?(reference, violation_type:) click to toggle source
# File lib/packwerk/deprecated_references.rb, line 21
def listed?(reference, violation_type:)
  violated_constants_found = deprecated_references.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.serialize)
end
stale_violations?() click to toggle source
# File lib/packwerk/deprecated_references.rb, line 47
def stale_violations?
  prepare_entries_for_dump
  deprecated_references.any? do |package, package_violations|
    package_violations.any? do |constant_name, entries_for_file|
      new_entries_violation_types = @new_entries.dig(package, constant_name, "violations")
      return true if new_entries_violation_types.nil?
      if entries_for_file["violations"].all? { |type| new_entries_violation_types.include?(type) }
        stale_violations =
          entries_for_file["files"] - Array(@new_entries.dig(package, constant_name, "files"))
        stale_violations.any?
      else
        return true
      end
    end
  end
end

Private Instance Methods

deprecated_references() click to toggle source
# File lib/packwerk/deprecated_references.rb, line 101
def deprecated_references
  @deprecated_references ||= if File.exist?(@filepath)
    load_yaml(@filepath)
  else
    {}
  end
end
load_yaml(filepath) click to toggle source
# File lib/packwerk/deprecated_references.rb, line 110
def load_yaml(filepath)
  YAML.load_file(filepath) || {}
rescue Psych::Exception
  {}
end
prepare_entries_for_dump() click to toggle source
# File lib/packwerk/deprecated_references.rb, line 88
def prepare_entries_for_dump
  @new_entries.each do |package_name, package_violations|
    package_violations.each do |_, entries_for_file|
      entries_for_file["violations"].sort!.uniq!
      entries_for_file["files"].sort!.uniq!
    end
    @new_entries[package_name] = package_violations.sort.to_h
  end

  @new_entries = @new_entries.sort.to_h
end