-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow OffensesFormatter to be configured via packwerk.yml and CLI flag #266
Conversation
02e5ce1
to
9dac93b
Compare
9dac93b
to
8203413
Compare
1e61023
to
4a7649f
Compare
@gmcgibbon I think I've addressed/responded to all of your feedback. |
@gmcgibbon I pushed a command line option, as I realized that without it, there is no path for a user to use different formatters in different options without modifying |
lib/packwerk/offenses_formatter.rb
Outdated
private | ||
|
||
sig { void } | ||
def load_default_formatter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There isn't a need to referencer the constant if we require the offense formatter at the top of the file. If the formatter is only used when checking, maybe the best thing to do would be to do a runtime require "packwerk/formatters/offenses_formatter"
in this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I like that! Definitely clearer that we're explicitly loading rather than loading through invoking auto load.
@@ -13,6 +13,11 @@ def setup_application_fixture | |||
@old_working_dir = Dir.pwd | |||
end | |||
|
|||
def remove_extensions | |||
Object.send(:remove_const, :MyLocalExtension) if defined?(MyLocalExtension) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.send(:remove_const, :MyLocalExtension) if defined?(MyLocalExtension) | |
Object.send(:remove_const, :MyLocalExtension) |
If we expect it to be there, it should be there. Otherwise our test is wrong for calling this method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure makes sense! Will change.
239ec66
to
2ce04df
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM after some small changes.
Packwerk::OffensesFormatter.instance_variable_set(:@formatter_by_identifier, nil) | ||
current_formatters = Packwerk::OffensesFormatter.instance_variable_get(:@offenses_formatters) | ||
new_formatters = current_formatters.delete_if { |f| f.new.identifier == "my_offenses_formatter" } | ||
Packwerk::OffensesFormatter.instance_variable_set(:@offenses_formatters, new_formatters) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could just leave it at setting nil
, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't because @offenses_formatters
is initialized when the interface is included when a file is required, so if we reset to nil
in the test suite, then no more offense formatter will be required, so nil.map
will raise an error.
USAGE.md
Outdated
|
||
You can also pass in a formatter on the command line: | ||
``` | ||
bin/packwerk check --format=my_offenses_formatter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure this is using the correct flag before merging.
112e957
to
15135f5
Compare
What are you trying to accomplish?
Today,
packwerk
supports a configurableOffensesFormatter
. However, the only way to configure it is to load the CLI itself. This means that this feature is not usable if a user has runbundle binstubs packwerk
. Instead, the user needs to copyexe/packwerk
into their localbin/packwerk
and pass in a different formatter toPackwerk::Cli.new
.This PR makes this more user-friendly by leveraging the extension system to load in a new formatter. This way, a user can use the vanilla
packwerk
binstub and load in their own offenses formatter more easily.This was originally attempted here: #264
However, I believe both the implementation and the UX of this approach is better and more consistent.
What approach did you choose and why?
I chose to use the
require
directive inpackwerk.yml
to load in a formatter via a setting onPackwerk::Configuration
. The system first prioritizes the local input variable, then the configuration, then the default.I chose to use a similar approach as the
Checker
andValidator
to keep things consistent. That is, by requiring a file that implements the interface, the extension is "loaded." Then to turn "on" the extension, the user needs to set it (viapackwerk.yml
).What should reviewers focus on?
There are some subtle changes that I've pointed out via comments. Looking for feedback on it!
Type of Change
Additional Release Notes
You could definitely argue that this is a breaking change because I change the interface to
OffensesFormatter
. It now requires anidentifier
. I also removed theinitialize
method that passes in a style so that the formatters can be initialized consistently. Therefore those using theOffensesFormatter
directly will experience a breaking API change.Checklist