Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Commit e8aec81

Browse files
author
Alex Evanczuk
authored
Allow package protections to self configure (#28)
* Revert "Use autoload when possible to lazily require internal dependencies (#22)" This reverts commit 0f56f9c. * Load client configuration when initializing config * bump version * use .rb suffix * use guard clause
1 parent 23933f3 commit e8aec81

File tree

9 files changed

+51
-35
lines changed

9 files changed

+51
-35
lines changed

Gemfile.lock

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
PATH
22
remote: .
33
specs:
4-
package_protections (2.1.1)
4+
package_protections (2.2.0)
55
activesupport
66
parse_packwerk
77
rubocop
88
rubocop-sorbet
99
sorbet-runtime
10-
zeitwerk
1110

1211
GEM
1312
remote: https://rubygems.org/
@@ -109,7 +108,6 @@ GEM
109108
yard-sorbet (0.6.1)
110109
sorbet-runtime (>= 0.5)
111110
yard (>= 0.9)
112-
zeitwerk (2.6.0)
113111

114112
PLATFORMS
115113
ruby

lib/package_protections.rb

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
# frozen_string_literal: true
22

33
# typed: strict
4-
54
require 'sorbet-runtime'
6-
75
require 'open3'
86
require 'set'
97
require 'parse_packwerk'
8+
require 'rubocop'
9+
require 'rubocop-sorbet'
1010

11-
require 'zeitwerk'
12-
loader = T.unsafe(Zeitwerk::Loader).for_gem
13-
loader.ignore("#{__dir__}/rubocop")
14-
loader.setup
15-
11+
#
1612
# Welcome to PackageProtections!
1713
# See https://github.com/rubyatscale/package_protections#readme for more info
1814
#
1915
# This file is a reference for the available API to `package_protections`, but all implementation details are private
2016
# (which is why we delegate to `Private` for the actual implementation).
17+
#
2118
module PackageProtections
2219
extend T::Sig
2320

@@ -30,6 +27,18 @@ module PackageProtections
3027
# This is currently the only handled exception that `PackageProtections` will throw.
3128
class IncorrectPublicApiUsageError < StandardError; end
3229

30+
require 'package_protections/offense'
31+
require 'package_protections/violation_behavior'
32+
require 'package_protections/protected_package'
33+
require 'package_protections/per_file_violation'
34+
require 'package_protections/protection_interface'
35+
require 'package_protections/rubocop_protection_interface'
36+
require 'package_protections/private'
37+
38+
# Implementation of rubocop-based protections
39+
require 'rubocop/cop/package_protections/namespaced_under_package_name'
40+
require 'rubocop/cop/package_protections/typed_public_api'
41+
3342
class << self
3443
extend T::Sig
3544

@@ -46,6 +55,7 @@ def self.all
4655

4756
sig { returns(Private::Configuration) }
4857
def self.config
58+
Private.load_client_configuration
4959
@config = T.let(@config, T.nilable(Private::Configuration))
5060
@config ||= Private::Configuration.new
5161
end
@@ -119,7 +129,3 @@ def self.bust_cache!
119129
RubocopProtectionInterface.bust_rubocop_todo_yml_cache
120130
end
121131
end
122-
123-
if defined?(Rubocop)
124-
require 'rubocop/cop/package_protections'
125-
end

lib/package_protections/private.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ def self.bust_cache!
117117
@protected_packages_indexed_by_name = nil
118118
@private_cop_config = nil
119119
PackageProtections.config.bust_cache!
120+
# This comes explicitly after `PackageProtections.config.bust_cache!` because
121+
# otherwise `PackageProtections.config` will attempt to reload the client configuratoin.
122+
@loaded_client_configuration = false
120123
end
121124

122125
sig { params(identifier: Identifier).returns(T::Hash[T.untyped, T.untyped]) }
@@ -161,6 +164,16 @@ def self.exclude_for_rule(rule)
161164

162165
excludes
163166
end
167+
168+
sig { void }
169+
def self.load_client_configuration
170+
@loaded_client_configuration ||= T.let(false, T.nilable(T::Boolean))
171+
return if @loaded_client_configuration
172+
173+
@loaded_client_configuration = true
174+
client_configuration = Pathname.pwd.join('config/package_protections.rb')
175+
require client_configuration.to_s if client_configuration.exist?
176+
end
164177
end
165178

166179
private_constant :Private

lib/package_protections/private/configuration.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ def bust_cache!
3535

3636
sig { returns(T::Array[ProtectionInterface]) }
3737
def default_protections
38-
require 'rubocop/cop/package_protections'
39-
4038
[
4139
Private::OutgoingDependencyProtection.new,
4240
Private::IncomingPrivacyProtection.new,

lib/rubocop/cop/package_protections.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

package_protections.gemspec

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |spec|
22
spec.name = 'package_protections'
3-
spec.version = '2.1.1'
3+
spec.version = '2.2.0'
44
spec.authors = ['Gusto Engineers']
55
spec.email = ['[email protected]']
66
spec.summary = 'Package protections for Rails apps'
@@ -26,7 +26,6 @@ Gem::Specification.new do |spec|
2626
spec.add_dependency 'rubocop'
2727
spec.add_dependency 'rubocop-sorbet'
2828
spec.add_dependency 'sorbet-runtime'
29-
spec.add_dependency 'zeitwerk'
3029

3130
spec.add_development_dependency 'rake'
3231
spec.add_development_dependency 'rspec'

sorbet/rbi/todo.rbi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# srb rbi todo
33

44
# typed: strong
5-
module ::Zeitwerk::Loader; end
5+
module ::RuboCop::RSpec::ExpectOffense; end
66
module RSpec::Matchers; end
7-
module RuboCop::RSpec::ExpectOffense; end
7+
88
class Hash
99
def to_yaml; end
1010
end

spec/package_protections_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,4 +1330,20 @@ def get_new_violations
13301330
})
13311331
end
13321332
end
1333+
1334+
describe 'configuration' do
1335+
context 'app has a user defined configuration' do
1336+
before do
1337+
write_file('config/package_protections.rb', <<~CONFIGURATION)
1338+
PackageProtections.configure do |config|
1339+
config.globally_permitted_namespaces = ['MyNamespace']
1340+
end
1341+
CONFIGURATION
1342+
end
1343+
1344+
it 'properly configures package protections' do
1345+
expect(PackageProtections.config.globally_permitted_namespaces).to eq(['MyNamespace'])
1346+
end
1347+
end
1348+
end
13331349
end

spec/spec_helper.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'pry'
2-
require 'rubocop/cop/package_protections'
3-
require 'rubocop/rspec/support'
42
require 'package_protections'
3+
require 'rubocop/rspec/support'
54
require 'package_protections/rspec/support'
65

76
RSpec.configure do |config|

0 commit comments

Comments
 (0)