From dc11fdb3686d8f9e9a18ecbe87cd431a4eed49e7 Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Thu, 29 Dec 2022 10:45:33 -0500 Subject: [PATCH] Allow packs configuration to be set in one place (#48) --- lib/stimpack.rb | 17 ++++++++++ lib/stimpack/integrations/factory_bot.rb | 2 ++ lib/stimpack/integrations/rails.rb | 6 ++-- lib/stimpack/integrations/rspec.rb | 3 +- lib/stimpack/version.rb | 2 +- .../gems/{packs@0.0.2.rbi => packs@0.0.4.rbi} | 32 +++++++++++-------- 6 files changed, 42 insertions(+), 20 deletions(-) rename sorbet/rbi/gems/{packs@0.0.2.rbi => packs@0.0.4.rbi} (69%) diff --git a/lib/stimpack.rb b/lib/stimpack.rb index 822c220..6808dd9 100644 --- a/lib/stimpack.rb +++ b/lib/stimpack.rb @@ -18,6 +18,23 @@ class << self def root @root ||= Rails::Application.find_root(Dir.pwd) end + + # + # This is temporary. For now, we allow Stimpack roots to be configured via Stimpack.config.root + # Later, if clients configure packs directly, we can deprecate the Stimpack setting and + # remove this function and its invocations. + # + def configure_packs + Packs.configure do |config| + roots = Array(Stimpack.config.root) + pack_paths = roots.flat_map do |root| + # Support nested packs by default. Later, this can be pushed to a client configuration. + ["#{root}/*", "#{root}/*/*"] + end + + config.pack_paths = pack_paths + end + end end @config = ActiveSupport::OrderedOptions.new diff --git a/lib/stimpack/integrations/factory_bot.rb b/lib/stimpack/integrations/factory_bot.rb index 6a5d4c1..9836f61 100644 --- a/lib/stimpack/integrations/factory_bot.rb +++ b/lib/stimpack/integrations/factory_bot.rb @@ -5,8 +5,10 @@ module Integrations class FactoryBot def initialize(app) return unless app.config.respond_to?(:factory_bot) + Stimpack.configure_packs Packs.all.each do |pack| + next if pack.relative_path.glob('*.gemspec').any? app.config.factory_bot.definition_file_paths << pack.relative_path.join("spec/factories").to_s end end diff --git a/lib/stimpack/integrations/rails.rb b/lib/stimpack/integrations/rails.rb index 4eb638a..5de8e9b 100644 --- a/lib/stimpack/integrations/rails.rb +++ b/lib/stimpack/integrations/rails.rb @@ -10,15 +10,13 @@ def initialize(app) @app = app Stimpack.config.paths.freeze + Stimpack.configure_packs + create_engines inject_paths end def create_engines - # Ideally, the user just does `Packs.configure { |config| config.roots = '...' }` - # But that would be a public API change and can come later - Packs.configure { |config| config.roots = Array(Stimpack.config.root) } - Packs.all.each do |pack| next unless pack.metadata['engine'] diff --git a/lib/stimpack/integrations/rspec.rb b/lib/stimpack/integrations/rspec.rb index ff01e00..d7cebe9 100644 --- a/lib/stimpack/integrations/rspec.rb +++ b/lib/stimpack/integrations/rspec.rb @@ -10,11 +10,12 @@ def initialize to_run = ::RSpec.configuration.instance_variable_get(:@files_or_directories_to_run) default_path = ::RSpec.configuration.default_path + Stimpack.configure_packs + if to_run == [default_path] # This is the default case when you run `rspec`. We want to add all the pack's spec paths # to the collection of directories to run. - Packs.configure { |config| config.roots = Array(Stimpack.config.root) } pack_paths = Packs.all.map do |pack| spec_path = pack.relative_path.join(default_path) spec_path.to_s if spec_path.exist? diff --git a/lib/stimpack/version.rb b/lib/stimpack/version.rb index bfc7bcb..33bba13 100644 --- a/lib/stimpack/version.rb +++ b/lib/stimpack/version.rb @@ -1,3 +1,3 @@ module Stimpack - VERSION = "0.8.1".freeze + VERSION = "0.8.2".freeze end diff --git a/sorbet/rbi/gems/packs@0.0.2.rbi b/sorbet/rbi/gems/packs@0.0.4.rbi similarity index 69% rename from sorbet/rbi/gems/packs@0.0.2.rbi rename to sorbet/rbi/gems/packs@0.0.4.rbi index e4a7c42..1fceaca 100644 --- a/sorbet/rbi/gems/packs@0.0.2.rbi +++ b/sorbet/rbi/gems/packs@0.0.4.rbi @@ -12,10 +12,10 @@ module Packs sig { void } def bust_cache!; end - sig { returns(::Packs::Configuration) } + sig { returns(::Packs::Private::Configuration) } def config; end - sig { params(blk: T.proc.params(arg0: ::Packs::Configuration).void).void } + sig { params(blk: T.proc.params(arg0: ::Packs::Private::Configuration).void).void } def configure(&blk); end sig { params(name: ::String).returns(T.nilable(::Packs::Pack)) } @@ -34,17 +34,6 @@ module Packs end end -class Packs::Configuration - sig { void } - def initialize; end - - sig { returns(T::Array[::Pathname]) } - def roots; end - - sig { params(roots: T::Array[::String]).void } - def roots=(roots); end -end - Packs::PACKAGE_FILE = T.let(T.unsafe(nil), String) class Packs::Pack < ::T::Struct @@ -77,4 +66,19 @@ module Packs::Private end end -Packs::ROOTS = T.let(T.unsafe(nil), Array) +class Packs::Private::Configuration < ::T::Struct + prop :pack_paths, T::Array[::String] + + class << self + sig { returns(::Packs::Private::Configuration) } + def fetch; end + + def inherited(s); end + + sig { params(config_hash: T::Hash[T.untyped, T.untyped]).returns(T::Array[::String]) } + def pack_paths(config_hash); end + end +end + +Packs::Private::Configuration::CONFIGURATION_PATHNAME = T.let(T.unsafe(nil), Pathname) +Packs::Private::Configuration::DEFAULT_PACK_PATHS = T.let(T.unsafe(nil), Array)