Skip to content

Commit

Permalink
Use Packs specification (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Evanczuk authored Dec 28, 2022
1 parent 653637b commit 1176753
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 121 deletions.
7 changes: 4 additions & 3 deletions lib/stimpack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require 'packs'
require "active_support"
require "rails/application"
require 'sorbet-runtime'

module Stimpack
extend ActiveSupport::Autoload

autoload :Integrations
autoload :Pack
autoload :Packs
autoload :Railtie
autoload :Stim

Expand All @@ -21,7 +21,8 @@ def root
end

@config = ActiveSupport::OrderedOptions.new
@config.root = "packs".freeze
# Should this allow a plural version to be set? What are semantics of that?
@config.root = Array("packs".freeze)
@config.paths = %w(
app
app/controllers
Expand Down
2 changes: 2 additions & 0 deletions lib/stimpack/integrations/factory_bot.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# typed: true

module Stimpack
module Integrations
class FactoryBot
Expand Down
15 changes: 10 additions & 5 deletions lib/stimpack/integrations/rails.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true
# typed: true

require "active_support/inflections"

Expand All @@ -14,17 +15,21 @@ def initialize(app)
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.config.engine?
next unless pack.metadata['engine']

pack.engine = create_engine(pack)
create_engine(pack)
end
end

def inject_paths
Packs.all.each do |pack|
Stimpack.config.paths.each do |path|
@app.paths[path] << pack.path.join(path)
@app.paths[path] << pack.relative_path.join(path)
end
end
end
Expand All @@ -43,8 +48,8 @@ def create_namespace(name)
end

def create_engine(pack)
name = pack.path.relative_path_from(Stimpack::Packs.root)
namespace = create_namespace(pack.name)
name = pack.last_name
namespace = create_namespace(name)
stim = Stim.new(pack, namespace)
namespace.const_set("Engine", Class.new(::Rails::Engine)).include(stim)
end
Expand Down
16 changes: 14 additions & 2 deletions lib/stimpack/integrations/rspec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# typed: true

module Stimpack
module Integrations
class RSpec
extend T::Sig

def initialize
# This is the list of directories RSpec was told to run.
to_run = ::RSpec.configuration.instance_variable_get(:@files_or_directories_to_run)
Expand All @@ -10,6 +14,7 @@ def initialize
# 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?
Expand All @@ -26,10 +31,10 @@ def initialize
# If it doesn't match a pack path, we leave it alone.

to_run.map! do |path|
if pack = Packs.all_by_path[path]
if pack = Packs.find(path)
[
pack,
*Packs.all(pack)
*nested_packs_for(pack)
].map do |pack|
spec_path = pack.relative_path.join(default_path)
spec_path.to_s if spec_path.exist?
Expand All @@ -42,6 +47,13 @@ def initialize

::RSpec.configuration.files_or_directories_to_run = to_run.flatten.compact.uniq
end

sig { params(parent_pack: Packs::Pack).returns(T::Array[Packs::Pack]) }
def nested_packs_for(parent_pack)
Packs.all.select do |pack|
pack.name != parent_pack.name && pack.name.include?(parent_pack.name)
end
end
end
end
end
28 changes: 0 additions & 28 deletions lib/stimpack/pack.rb

This file was deleted.

30 changes: 0 additions & 30 deletions lib/stimpack/pack/configuration.rb

This file was deleted.

48 changes: 0 additions & 48 deletions lib/stimpack/packs.rb

This file was deleted.

9 changes: 7 additions & 2 deletions lib/stimpack/stim.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# typed: true

module Stimpack
class Stim < Module
extend T::Sig

sig { params(pack: Packs::Pack, namespace: Module).void }
def initialize(pack, namespace)
@pack = pack
@namespace = namespace
super()
end

def included(engine)
engine.called_from = @pack.path
engine.called_from = @pack.relative_path
engine.extend(ClassMethods)
engine.isolate_namespace(@namespace)

Expand All @@ -28,7 +33,7 @@ def included(engine)

module ClassMethods
def find_root(_from)
called_from
T.unsafe(self).called_from
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/stimpack/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Stimpack
VERSION = "0.8.0".freeze
VERSION = "0.8.1".freeze
end
80 changes: 80 additions & 0 deletions sorbet/rbi/gems/packs@0.0.2.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions spec/fixtures/rails-7.0/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

Stimpack.config.root = ['packs', 'components', 'utilities']

module TestApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Belts
class Brown
end
end
Empty file.
16 changes: 14 additions & 2 deletions spec/stimpack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

it "adds pack paths to the application" do
Stimpack.config.paths.each do |path|
expect(Rails.application.paths[path].paths).to include(rails_dir.join(Stimpack.config.root, "shirts", path))
expect(Rails.application.paths[path].paths).to include(rails_dir.join('packs', "shirts", path))
end
end

Expand All @@ -26,12 +26,24 @@

it "adds pack paths to the application" do
Stimpack.config.paths.each do |path|
expect(Rails.application.paths[path].paths).to include(rails_dir.join(Stimpack.config.root, "pants", "shorts", path))
expect(Rails.application.paths[path].paths).to include(rails_dir.join('packs', "pants", "shorts", path))
end
end

it "creates engines namespace for engine packs" do
expect(defined?(Shorts::Engine)).to eq("constant")
end
end

context 'alternate roots' do
it "autoloads classes in autoload paths" do
expect(defined?(Belts::Brown)).to eq("constant")
end

it "adds pack paths to the application" do
Stimpack.config.paths.each do |path|
expect(Rails.application.paths[path].paths).to include(rails_dir.join('utilities', "belts", path))
end
end
end
end
1 change: 1 addition & 0 deletions stimpack.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "railties"
spec.add_dependency "activesupport"
spec.add_dependency "packs"

spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
Expand Down

0 comments on commit 1176753

Please sign in to comment.