Skip to content

Commit 8e45be3

Browse files
committed
Better console helpers
1 parent 97e514d commit 8e45be3

File tree

3 files changed

+153
-26
lines changed

3 files changed

+153
-26
lines changed

lib/query_packwerk.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#
1414
module QueryPackwerk
1515
autoload :Console, 'query_packwerk/console'
16+
autoload :ConsoleHelpers, 'query_packwerk/console_helpers'
1617
autoload :QueryInterface, 'query_packwerk/query_interface'
1718
autoload :Violations, 'query_packwerk/violations'
1819
autoload :Violation, 'query_packwerk/violation'

lib/query_packwerk/console.rb

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# typed: strict
22
# frozen_string_literal: true
33

4+
require 'irb'
5+
require 'irb/completion'
6+
47
module QueryPackwerk
58
# Console for QueryPackwerk
69
class Console
@@ -38,41 +41,20 @@ def self.start(directory = Dir.pwd)
3841

3942
context = Class.new do
4043
include QueryPackwerk
44+
include QueryPackwerk::ConsoleHelpers
4145
extend T::Sig
42-
43-
sig { returns(String) }
44-
def inspect
45-
'query_packwerk console'
46-
end
4746
end
4847

49-
irb = IRB::Irb.new(IRB::WorkSpace.new(context.new))
48+
context_instance = context.new
49+
irb = IRB::Irb.new(IRB::WorkSpace.new(context_instance))
5050
IRB.conf[:MAIN_CONTEXT] = irb.context
5151

52+
context_instance.welcome
53+
5254
trap('SIGINT') do
5355
irb.signal_handle
5456
end
5557

56-
puts 'QueryPackwerk Console'
57-
puts '====================='
58-
puts 'Available commands:'
59-
puts ' package(pack_name) - Get a package by name'
60-
puts ' Packages.all - Get all packages'
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'
74-
puts ''
75-
7658
catch(:IRB_EXIT) do
7759
irb.eval_input
7860
end
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# typed: true
2+
3+
module QueryPackwerk::ConsoleHelpers
4+
extend T::Sig
5+
6+
sig { returns(String) }
7+
def inspect
8+
'query_packwerk console'
9+
end
10+
11+
sig { returns(NilClass) } # returning nil is less ugly in the console.
12+
def welcome
13+
help_query_packwerk
14+
puts
15+
help_help
16+
end
17+
18+
sig { params(type: T.nilable(T.any(String, Symbol))).returns(NilClass) }
19+
def help(type = nil)
20+
case type.to_s.downcase
21+
when 'violation'
22+
help_violation
23+
when 'violations'
24+
help_violations
25+
when 'package'
26+
help_package
27+
when 'packages'
28+
help_packages
29+
when '', 'help'
30+
help_help
31+
else
32+
puts "Unknown help topic: #{type}\n"
33+
help_help
34+
end
35+
end
36+
37+
sig { returns(NilClass) }
38+
def help_help
39+
puts <<~HELP
40+
Available help topics:
41+
help # This help
42+
help_violation # Help for QueryPackwerk::Violation
43+
help_violations # Help for QueryPackwerk::Violations
44+
help_package # Help for QueryPackwerk::Package
45+
help_packages # Help for QueryPackwerk::Packages
46+
HELP
47+
end
48+
49+
sig { returns(NilClass) }
50+
def help_violation
51+
puts <<~HELP
52+
QueryPackwerk::Violation (lib/query_packwerk/violation.rb)
53+
------------------------------------------------
54+
.type # Returns the violation type ('privacy' or 'dependency')
55+
.class_name # Returns the violated constant name
56+
.files # Returns array of files containing the violation
57+
.producing_pack # Returns the Package that owns the violated constant
58+
.consuming_pack # Returns the Package that violated the constant
59+
.sources # Returns array of AST nodes representing the violation occurrences
60+
.sources_with_locations # Returns array of [file:line, source] pairs for each violation
61+
.source_counts # Returns hash of how often each violation source occurs
62+
.anonymous_sources # Returns array of violation sources with arguments anonymized
63+
.anonymous_sources_with_locations # Returns array of [file:line, source] pairs for each violation with arguments anonymized
64+
.count # Returns total number of violation occurrences
65+
HELP
66+
end
67+
68+
sig { returns(NilClass) }
69+
def help_violations
70+
puts <<~HELP
71+
QueryPackwerk::Violations (lib/query_packwerk/violations.rb)
72+
--------------------------------------------------
73+
.where(conditions) # Returns new Violations filtered by conditions
74+
.raw_sources # Returns hash of constant => AST nodes for all violations
75+
.sources # Returns hash of constant => source strings for all violations
76+
.sources_with_locations # Returns hash of constant => [file:line, source] pairs
77+
.source_counts # Returns hash of constant => {source => count} with occurrence counts
78+
.sources_with_contexts # Returns hash of constant => [file:line, source] pairs for each violation with context
79+
.anonymous_sources # Returns hash of constant => anonymized sources
80+
.anonymous_sources_with_locations # Returns array of [file:line, source] pairs for each violation with arguments anonymized
81+
.consumers(threshold) # Returns hash of consuming package names and violation counts
82+
.producers(threshold) # Returns hash of producing package names and violation counts
83+
.including_files(globs) # Returns new Violations filtered to include specific files
84+
.excluding_files(globs) # Returns new Violations filtered to exclude specific files
85+
HELP
86+
end
87+
88+
sig { returns(NilClass) }
89+
def help_package
90+
puts <<~HELP
91+
QueryPackwerk::Package (lib/query_packwerk/package.rb)
92+
----------------------------------------------
93+
.name # Returns the package name
94+
.enforce_dependencies # Returns whether package enforces dependencies
95+
.enforce_privacy # Returns whether package enforces privacy
96+
.metadata # Returns the package metadata from package.yml
97+
.config # Returns the package configuration
98+
.dependencies # Returns Packages collection of dependencies
99+
.dependency_names # Returns array of dependency package names
100+
.owner # Returns the package owner or 'Unowned'
101+
.directory # Returns the package directory path
102+
.todos # Returns Violations where this package is the consumer
103+
.violations # Returns Violations where this package is the producer
104+
.consumers # Returns Packages that consume this package
105+
.consumer_names # Returns array of names of consuming packages
106+
.consumer_counts # Returns hash of consumer package names and counts
107+
HELP
108+
end
109+
110+
sig { returns(NilClass) }
111+
def help_packages
112+
puts <<~HELP
113+
QueryPackwerk::Packages (lib/query_packwerk/packages.rb)
114+
------------------------------------------------
115+
.all # Returns all packages in the application
116+
.where(conditions) # Returns new Packages filtered by conditions
117+
.violations # Returns Violations for all packages in collection
118+
HELP
119+
end
120+
121+
sig { returns(NilClass) }
122+
def help_query_packwerk
123+
puts <<~HELP
124+
QueryPackwerk (lib/query_packwerk.rb)
125+
------------------------------
126+
Available commands:
127+
package(pack_name) # Get a package by name
128+
Packages.all # Get all packages
129+
130+
Package Consumption (how others use this package):
131+
violations_for(pack_name) # Get all violations where others access this package
132+
violation_sources_for(pack_name) # Get where others access this package
133+
violation_counts_for(pack_name) # Get how often others access this package
134+
anonymous_violation_sources_for(pack_name) # Get the 'shape' of how others access this package
135+
anonymous_violation_counts_for(pack_name) # Get how often each access pattern occurs
136+
consumers(pack_name) # Get which packages consume this package
137+
138+
Package Dependencies (how this package uses others):
139+
todos_for(pack_name) # Get all todos where this package accesses others
140+
141+
Violations.all # Get all violations
142+
HELP
143+
end
144+
end

0 commit comments

Comments
 (0)