Skip to content

Commit ed42983

Browse files
committed
More clarification and documentation
1 parent 9fd9df1 commit ed42983

File tree

5 files changed

+48
-22
lines changed

5 files changed

+48
-22
lines changed

lib/query_packwerk.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,45 @@ def package(name)
3131
Packages.where(name: name).first
3232
end
3333

34-
# All violations for a pack
34+
# Get all violations where other packages access code from this package
35+
# (i.e., this package is the producer, others are consumers)
3536
sig { params(pack_name: String).returns(QueryPackwerk::Violations) }
3637
def violations_for(pack_name)
3738
QueryPackwerk::Violations.where(producing_pack: full_name(pack_name))
3839
end
3940

41+
# Get all todos where this package accesses code from other packages
42+
# (i.e., this package is the consumer, others are producers)
43+
sig { params(pack_name: String).returns(QueryPackwerk::Violations) }
4044
def todos_for(pack_name)
41-
package(pack_name).todos
45+
QueryPackwerk::Violations.where(consuming_pack: full_name(pack_name))
4246
end
4347

44-
# Where the violations occurred
48+
# Get where the violations occurred (where other packages access this package)
4549
sig { params(pack_name: String).returns(T::Hash[String, T::Array[String]]) }
4650
def violation_sources_for(pack_name)
4751
violations_for(pack_name).sources_with_locations
4852
end
4953

50-
# How often the violations occurred
54+
# Get how often the violations occurred
5155
sig { params(pack_name: String, threshold: Integer).returns(T::Hash[String, T::Hash[String, Integer]]) }
5256
def violation_counts_for(pack_name, threshold: 0)
5357
violations_for(pack_name).source_counts(threshold: threshold)
5458
end
5559

56-
# The "shape" of all of the occurring violations
60+
# Get the 'shape' of all violations (how other packages access this package)
5761
sig { params(pack_name: String).returns(T::Hash[String, T::Array[String]]) }
5862
def anonymous_violation_sources_for(pack_name)
5963
violations_for(pack_name).anonymous_sources
6064
end
6165

62-
# How often each of those shapes occurs
66+
# Get how often each 'shape' of violation occurs (counts of how other packages access this package)
6367
sig { params(pack_name: String, threshold: Integer).returns(T::Hash[String, T::Hash[String, Integer]]) }
6468
def anonymous_violation_counts_for(pack_name, threshold: 0)
6569
violations_for(pack_name).anonymous_source_counts(threshold: threshold)
6670
end
6771

68-
# Who consumes this pack?
72+
# Get which packages consume code from this package (who depends on this package)
6973
sig { params(pack_name: String, threshold: Integer).returns(T::Hash[String, Integer]) }
7074
def consumers(pack_name, threshold: 0)
7175
violations_for(pack_name).consumers(threshold: threshold)

lib/query_packwerk/console.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,21 @@ def inspect
5656
puts 'QueryPackwerk Console'
5757
puts '====================='
5858
puts 'Available commands:'
59-
puts ' violations_for(pack_name) - Get all violations for a pack'
60-
puts ' violation_sources_for(pack_name) - Get where violations occurred'
61-
puts ' violation_counts_for(pack_name) - Get how often violations occurred'
62-
puts " anonymous_violation_sources_for(pack_name) - Get the 'shape' of violations"
63-
puts ' anonymous_violation_counts_for(pack_name) - Get how often each shape occurs'
64-
puts ' consumers(pack_name) - Get who consumes this pack'
6559
puts ' package(pack_name) - Get a package by name'
6660
puts ' Packages.all - Get all packages'
67-
puts ' Violations.all - Get all violations'
61+
puts ''
62+
puts 'Package Consumption (how others use this package):'
63+
puts ' violations_for(pack_name) - Get all violations where others access this package'
64+
puts ' violation_sources_for(pack_name) - Get where others access this package'
65+
puts ' violation_counts_for(pack_name) - Get how often others access this package'
66+
puts " anonymous_violation_sources_for(pack_name) - Get the 'shape' of how others access this package"
67+
puts ' anonymous_violation_counts_for(pack_name) - Get how often each access pattern occurs'
68+
puts ' consumers(pack_name) - Get which packages consume this package'
69+
puts ''
70+
puts 'Package Dependencies (how this package uses others):'
71+
puts ' todos_for(pack_name) - Get all todos where this package accesses others'
72+
puts ''
73+
puts 'Violations.all - Get all violations'
6874
puts ''
6975

7076
catch(:IRB_EXIT) do

lib/query_packwerk/package.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,40 @@ def directory
6161
Pathname.new(name).cleanpath
6262
end
6363

64+
# Returns violations where this package is the consumer (i.e., this package
65+
# has dependencies on other packages). These are the "todos" in the package_todo.yml
66+
# file for this package.
6467
sig { returns(QueryPackwerk::Violations) }
6568
def todos
6669
QueryPackwerk::Violations.where(consuming_pack: name)
6770
end
68-
alias dependency_violations todos
71+
alias outgoing_violations todos
6972

73+
# Returns violations where this package is the producer (i.e., other packages
74+
# depend on this package). These are violations where other packages are
75+
# accessing code from this package.
7076
sig { returns(QueryPackwerk::Violations) }
7177
def violations
7278
QueryPackwerk::Violations.where(producing_pack: name)
7379
end
74-
alias consumer_violations violations
80+
alias incoming_violations violations
7581

82+
# Returns all packages that consume (depend on) this package
7683
sig { returns(QueryPackwerk::Packages) }
7784
def consumers
7885
Packages.where(name: consumer_names)
7986
end
8087

88+
# Returns the names of all packages that consume (depend on) this package
8189
sig { returns(T::Array[String]) }
8290
def consumer_names
83-
consumer_violations.consumers.keys
91+
violations.consumers.keys
8492
end
8593

94+
# Returns a count of how often each consumer package accesses this package
8695
sig { returns(T::Hash[String, Integer]) }
8796
def consumer_counts
88-
consumer_violations.consumers
97+
violations.consumers
8998
end
9099

91100
sig { returns(String) }

lib/query_packwerk/violations.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ def consumers(threshold: 0)
219219
threshold_filter_sort(tallies, threshold:)
220220
end
221221

222+
# Find which packs produce these violations
223+
sig { params(threshold: Integer).returns(T::Hash[String, Integer]) }
224+
def producers(threshold: 0)
225+
tallies = @original_collection.map { |v| v.producing_pack.name }.tally
226+
threshold_filter_sort(tallies, threshold:)
227+
end
228+
222229
# Filter for violations which include one of the provided file globs
223230
sig { params(file_globs: T.any(String, Regexp)).returns(QueryPackwerk::Violations) }
224231
def including_files(*file_globs)

spec/query_packwerk/package_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@
6363
end
6464
end
6565

66-
describe '#dependency_violations' do
66+
describe '#outgoing_violations' do
6767
it 'is an alias for #todos' do
6868
# Both methods should return equivalent objects but might not be equal with `==`
69-
expect(package.todos.to_a).to match_array(package.dependency_violations.to_a)
69+
expect(package.todos.to_a).to match_array(package.outgoing_violations.to_a)
7070
end
7171
end
7272

@@ -77,10 +77,10 @@
7777
end
7878
end
7979

80-
describe '#consumer_violations' do
80+
describe '#incoming_violations' do
8181
it 'is an alias for #violations' do
8282
# Both methods should return equivalent objects but might not be equal with `==`
83-
expect(package.consumer_violations.to_a).to match_array(package.violations.to_a)
83+
expect(package.incoming_violations.to_a).to match_array(package.violations.to_a)
8484
end
8585
end
8686

0 commit comments

Comments
 (0)