stimpack 1.0.0.alpha1 → 1.0.0.alpha6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aedf6786a9c8dce60f5d90bf3f9b97bea858d3faeac218c2991a5ed5dd61503e
4
- data.tar.gz: 1b7c218dc706f75f77d4d1e6e099f174520c26ebbf573f985f05e6e28a343af8
3
+ metadata.gz: f7e4f58a5817580fd238b6a209d51565f992d7c39ade6003338d168c2a52235b
4
+ data.tar.gz: 372c732e00ab4fb1c301e8c44ff483954c8fd9864166712e9a42dff124d9bfb8
5
5
  SHA512:
6
- metadata.gz: be4a82f48f0c966edfc9c86c096d4e8cbfe7f23177007ddd91bffe769ac7fab05921f6b7c2855b8fbadc49c54aede9024b1820f76ecbe988b492507adc3fc212
7
- data.tar.gz: 889c48f4b28b1260e6387ba6205f5b586846630c33182f680bbbaf8bfe0a18f89529b16ef87c562991452da74bbd2191168189d1b7f4380e56448eabe586fae1
6
+ metadata.gz: 03fbcedb9c236fe384c124a7ca8f923a1717862d648f134a5954960136b33e2297b2c803e7335af22fe0cc8212e8420cdac1131618c5abfdb353975550e96c3c
7
+ data.tar.gz: 107de7c60b5dc14f02196bc04df5ea933122038a0fae17fac9a144ecfc9b0e22a3ede60cd28699061475771bcc056bf3c26b9df7620b47b7a2d54022e9c28f8d
data/lib/stimpack.rb CHANGED
@@ -5,54 +5,26 @@ module Stimpack
5
5
  extend ActiveSupport::Autoload
6
6
 
7
7
  autoload :Integrations
8
- autoload :Autoloaders
8
+ autoload :Pack
9
9
  autoload :Packs
10
- autoload :Settings
11
- autoload :Stim
12
10
  autoload :Railtie
13
- autoload :Require
14
- autoload :ZeitwerkProxy
11
+ autoload :Stim
15
12
 
16
13
  class Error < StandardError; end
17
14
 
18
15
  class << self
19
- def start!
20
- @started = @started ? return : true
21
-
22
- # Override Rails' autoloader accessors.
23
- # Rails::Autoloaders.singleton_class.prepend(Autoloaders)
24
-
16
+ def load(app)
25
17
  Packs.resolve
26
18
 
27
- install_integrations
28
- end
29
-
30
- def finalize!
31
- @finalized = @finalized ? return : true
32
-
33
- Require.setup
34
- end
35
-
36
- def autoloader(original)
37
- return original if @finalized
38
-
39
- @autoloader_proxies ||= {}
40
- @autoloader_proxies[original] ||= ZeitwerkProxy.new(original)
19
+ Integrations::Rails.install(app)
20
+ Integrations::FactoryBot.install(app)
21
+ Integrations::RSpec.install(app)
41
22
  end
42
23
 
43
24
  def [](name)
44
25
  Packs[name.to_s]
45
26
  end
46
-
47
- private
48
-
49
- def install_integrations
50
- Integrations::FactoryBot.install
51
- Integrations::RSpec.install
52
- end
53
27
  end
54
28
  end
55
29
 
56
30
  require "stimpack/railtie"
57
- # require "stimpack/require"
58
- # require "stimpack/kernel"
@@ -2,9 +2,8 @@ require "active_support"
2
2
 
3
3
  module Stimpack
4
4
  module Integrations
5
- extend ActiveSupport::Autoload
6
-
7
- autoload :RSpec, "stimpack/integrations/rspec"
8
5
  autoload :FactoryBot, "stimpack/integrations/factory_bot"
6
+ autoload :Rails, "stimpack/integrations/rails"
7
+ autoload :RSpec, "stimpack/integrations/rspec"
9
8
  end
10
9
  end
@@ -1,11 +1,11 @@
1
1
  module Stimpack
2
2
  module Integrations
3
3
  class FactoryBot
4
- def self.install
5
- return unless Rails.configuration.respond_to?(:factory_bot)
4
+ def self.install(app)
5
+ return unless app.config.respond_to?(:factory_bot)
6
6
 
7
7
  Packs.each do |pack|
8
- Rails.configuration.factory_bot.definition_file_paths << pack.settings.relative_path.join("spec/factories").to_s
8
+ app.config.factory_bot.definition_file_paths << pack.relative_path.join("spec/factories").to_s
9
9
  end
10
10
  end
11
11
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stimpack
4
+ module Integrations
5
+ class Rails
6
+ PATHS = %w(
7
+ app
8
+ app/controllers
9
+ app/channels
10
+ app/helpers
11
+ app/models
12
+ app/mailers
13
+ app/views
14
+ lib
15
+ lib/tasks
16
+ config
17
+ config/locales
18
+ config/initializers
19
+ ).freeze
20
+
21
+ def self.install(app)
22
+ Packs.each do |pack|
23
+ PATHS.each do |path|
24
+ app.paths[path] << pack.path.join(path)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,11 +1,11 @@
1
1
  module Stimpack
2
2
  module Integrations
3
3
  class RSpec
4
- def self.install
4
+ def self.install(app)
5
5
  return unless defined?(::RSpec)
6
6
 
7
7
  Packs.each do |pack|
8
- ::RSpec.configuration.pattern.concat(",#{pack.settings.relative_path.join("spec/**/*_spec.rb")}")
8
+ ::RSpec.configuration.pattern.concat(",#{pack.relative_path.join("spec/**/*_spec.rb")}")
9
9
  end
10
10
  end
11
11
  end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stimpack
4
+ class Pack
5
+ autoload :Configuration, "stimpack/pack/configuration"
6
+
7
+ attr_reader :name
8
+ attr_reader :path
9
+ attr_reader :engine
10
+
11
+ def self.root
12
+ @root ||= Rails.root.join("packs")
13
+ end
14
+
15
+ def initialize(path)
16
+ @path = path
17
+ @name = path.relative_path_from(self.class.root)
18
+
19
+ if config.engine?
20
+ @engine = create_engine
21
+ end
22
+ end
23
+
24
+ def relative_path
25
+ @relative_path ||= path.relative_path_from(Rails.root)
26
+ end
27
+
28
+ def config
29
+ @config ||= Configuration.new(self)
30
+ end
31
+
32
+ private
33
+
34
+ def create_engine
35
+ namespace = create_namespace(name)
36
+ stim = Stim.new(self, namespace)
37
+ namespace.const_set("Engine", Class.new(Rails::Engine)).include(stim)
38
+ end
39
+
40
+ def create_namespace(name)
41
+ namespace = ActiveSupport::Inflector.camelize(name)
42
+ namespace.split("::").reduce(Object) do |base, mod|
43
+ if base.const_defined?(mod)
44
+ base.const_get(mod)
45
+ else
46
+ base.const_set(mod, Module.new)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stimpack
4
+ class Pack
5
+ class Configuration
6
+ FILE = "package.yml"
7
+ KEY = "metadata"
8
+
9
+ def initialize(pack)
10
+ @pack = pack
11
+ end
12
+
13
+ def engine
14
+ data.fetch("engine", false)
15
+ end
16
+ alias_method :engine?, :engine
17
+
18
+ private
19
+
20
+ def data
21
+ @data ||= YAML.load_file(@pack.path.join(FILE)).fetch(KEY, {}).freeze
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,31 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support"
2
4
  require "pathname"
3
5
  require "rails"
4
6
 
5
7
  module Stimpack
6
8
  module Packs
7
- PATH = Pathname.new("packs").freeze
8
- PACK_CLASS = "Pack".freeze
9
+ PACK_CLASS = "Pack"
9
10
 
10
11
  class << self
11
12
  def resolve
12
- PATH.glob("**/#{Settings::PACKWERK_PACKAGE_CONFIG}").each do |path|
13
- path = path.dirname
14
- create(path.relative_path_from(PATH).to_s, path.expand_path)
15
- end
16
- end
13
+ # Gather all the directories with package config files.
14
+ paths = filter(Pack.root.glob("**/#{Pack::Configuration::FILE}").map(&:dirname).sort)
17
15
 
18
- def create(name, path)
19
- namespace = create_namespace(name)
20
- stim = Stim.new(path)
21
- @packs[name] = namespace.const_set(PACK_CLASS, Class.new(Rails::Engine)).include(stim)
16
+ # Create thes packs.
17
+ paths.each do |path|
18
+ pack = Pack.new(path)
19
+ @packs[pack.name] = pack
20
+ end
22
21
  end
23
22
 
24
23
  def find(path)
25
24
  path = "#{path}/"
26
25
 
27
26
  @packs.values.find do |pack|
28
- path.start_with?("#{pack.root}/")
27
+ path.start_with?("#{pack.path}/")
29
28
  end
30
29
  end
31
30
 
@@ -39,13 +38,14 @@ module Stimpack
39
38
 
40
39
  private
41
40
 
42
- def create_namespace(name)
43
- namespace = ActiveSupport::Inflector.camelize(name)
44
- namespace.split("::").reduce(Object) do |base, mod|
45
- if base.const_defined?(mod)
46
- base.const_get(mod)
47
- else
48
- base.const_set(mod, Module.new)
41
+ def filter(paths)
42
+ # Reject all paths that are nested since they might be just packwerk
43
+ # packages instead of packs.
44
+ paths.reject do |path|
45
+ path = "#{path}/"
46
+ paths.any? do |other_path|
47
+ other_path = "#{other_path}/"
48
+ path != other_path && path.start_with?(other_path)
49
49
  end
50
50
  end
51
51
  end
@@ -2,12 +2,8 @@ require "rails"
2
2
 
3
3
  module Stimpack
4
4
  class Railtie < Rails::Railtie
5
- config.before_configuration do
6
- Stimpack.start!
7
- end
8
-
9
- config.after_initialize do
10
- Stimpack.finalize!
5
+ config.before_configuration do |app|
6
+ Stimpack.load(app)
11
7
  end
12
8
  end
13
9
  end
data/lib/stimpack/stim.rb CHANGED
@@ -1,32 +1,35 @@
1
1
  module Stimpack
2
2
  class Stim < Module
3
- def initialize(path)
4
- @path = path
3
+ def initialize(pack, namespace)
4
+ @pack = pack
5
+ @namespace = namespace
5
6
  super()
6
7
  end
7
8
 
8
9
  def included(engine)
9
- engine.called_from = @path
10
+ engine.called_from = @pack.path
10
11
  engine.extend(ClassMethods)
11
- settings = engine.settings
12
+ engine.isolate_namespace(@namespace)
12
13
 
13
- if settings.isolate_namespace?
14
- engine.isolate_namespace(settings.namespace)
14
+ # Set all of these paths to nil because we want the Rails integration to take
15
+ # care of them. The purpose of this Engine is really just for the namespace
16
+ # isolation.
17
+ (Stimpack::Integrations::Rails::PATHS +
18
+ # In addition to the paths we've delegated to the main app, we don't allow
19
+ # Engine Packs to have various capabilities.
20
+ %w(
21
+ config/environments
22
+ db/migrate
23
+ )
24
+ ).uniq.each do |path|
25
+ engine.paths[path] = nil
15
26
  end
16
-
17
- # if settings.implicit_namespace?
18
- # engine.paths["lib"].skip_load_path!
19
- # end
20
27
  end
21
28
 
22
29
  module ClassMethods
23
30
  def find_root(_from)
24
31
  called_from
25
32
  end
26
-
27
- def settings
28
- @settings ||= Stimpack::Settings.new(self)
29
- end
30
33
  end
31
34
  end
32
35
  end
@@ -1,3 +1,3 @@
1
1
  module Stimpack
2
- VERSION = "1.0.0.alpha1".freeze
2
+ VERSION = "1.0.0.alpha6".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stimpack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha1
4
+ version: 1.0.0.alpha6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ngan Pham
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-20 00:00:00.000000000 Z
11
+ date: 2021-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -63,28 +63,23 @@ files:
63
63
  - bin/console
64
64
  - bin/setup
65
65
  - lib/stimpack.rb
66
- - lib/stimpack/autoloaders.rb
67
- - lib/stimpack/instance.rb
68
66
  - lib/stimpack/integrations.rb
69
67
  - lib/stimpack/integrations/factory_bot.rb
68
+ - lib/stimpack/integrations/rails.rb
70
69
  - lib/stimpack/integrations/rspec.rb
71
- - lib/stimpack/kernel.rb
72
- - lib/stimpack/package.rb
73
- - lib/stimpack/packages.rb
70
+ - lib/stimpack/pack.rb
71
+ - lib/stimpack/pack/configuration.rb
74
72
  - lib/stimpack/packs.rb
75
73
  - lib/stimpack/railtie.rb
76
- - lib/stimpack/require.rb
77
- - lib/stimpack/settings.rb
78
74
  - lib/stimpack/stim.rb
79
75
  - lib/stimpack/version.rb
80
- - lib/stimpack/zeitwerk_proxy.rb
81
76
  homepage: https://github.com/Gusto/stimpack
82
77
  licenses: []
83
78
  metadata:
84
79
  homepage_uri: https://github.com/Gusto/stimpack
85
80
  source_code_uri: https://github.com/Gusto/stimpack
86
81
  changelog_uri: https://github.com/Gusto/stimpack/blob/master/CHANGELOG.md
87
- post_install_message:
82
+ post_install_message:
88
83
  rdoc_options: []
89
84
  require_paths:
90
85
  - lib
@@ -100,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
95
  version: 1.3.1
101
96
  requirements: []
102
97
  rubygems_version: 3.1.2
103
- signing_key:
98
+ signing_key:
104
99
  specification_version: 4
105
100
  summary: A Rails helper to package your code.
106
101
  test_files: []
@@ -1,11 +0,0 @@
1
- module Stimpack
2
- module Autoloaders
3
- def main
4
- Stimpack.autoloader(super)
5
- end
6
-
7
- def once
8
- Stimpack.autoloader(super)
9
- end
10
- end
11
- end
@@ -1,14 +0,0 @@
1
- require "pathname"
2
- require "active_support/inflector"
3
-
4
- module Stimpack
5
- class Instance
6
- def initialize
7
- Pathname.new("packages").glob("*/package.yml").each do |path|
8
- path = path.realpath.dirname
9
- name = ActiveSupport::Inflector.classify(path.basename)
10
- Stimpack::Packages.create(name, path)
11
- end
12
- end
13
- end
14
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Kernel
4
- module_function
5
-
6
- alias_method :stimpack_original_require, :require
7
-
8
- def require(path)
9
- stimpack_original_require(Stimpack::Require.resolve(path) || path)
10
- end
11
- end
@@ -1,14 +0,0 @@
1
- require "rails"
2
- require "active_support/core_ext/module/delegation"
3
-
4
- module Stimpack
5
- class Package < ::Rails::Engine
6
- class << self
7
- delegate :subclasses, to: :superclass
8
-
9
- def find_root(from)
10
- Packages.paths[module_parent.name]
11
- end
12
- end
13
- end
14
- end
@@ -1,35 +0,0 @@
1
- require "rails"
2
-
3
- module Stimpack
4
- module Packages
5
- def self.paths
6
- @paths ||= {}
7
- end
8
-
9
- def self.create(name, path)
10
- paths[name] = path
11
-
12
- class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
13
- module ::#{name}
14
- class Engine < ::Stimpack::Package
15
- end
16
- end
17
- RUBY
18
-
19
- engine = const_get("::#{name}::Engine")
20
- # Isolate the package namespace.
21
- engine.isolate_namespace(engine.module_parent)
22
-
23
- # Disable Railtie initializers for Packages.
24
- engine.paths["config/initializers"] = nil
25
-
26
- engine.paths["lib/tasks"] = "tasks"
27
- engine.paths["db/migrate"] = "migrations"
28
- engine.paths["config/routes.rb"] = "routes.rb"
29
-
30
- if engine.paths["db/migrate"].existent.any?
31
- ActiveRecord::Migrator.migrations_paths << engine.paths["db/migrate"].first
32
- end
33
- end
34
- end
35
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Stimpack
4
- class Require
5
- IGNORED_PATH_PREFIXES = ["/", "./"].freeze
6
- RUBY_EXTENSION = ".rb"
7
-
8
- include Singleton
9
-
10
- attr_accessor :enabled
11
-
12
- def self.resolve(path)
13
- instance.resolve(path)
14
- end
15
-
16
- def self.setup
17
- instance.setup
18
- end
19
-
20
- def initialize
21
- @packs = {}
22
- end
23
-
24
- def setup
25
- @setup = @setup ? return : true
26
-
27
- Packs.each do |pack|
28
- if pack.settings.implicit_namespace?
29
- @packs["#{pack.settings.name}/"] = "#{pack.paths["lib"].first}/"
30
- end
31
- end
32
- end
33
-
34
- def resolve(path)
35
- path = path.to_s
36
- return if path.start_with?(*IGNORED_PATH_PREFIXES)
37
-
38
- @packs.find do |pack_path, full_path|
39
- if path.start_with?(pack_path)
40
- full_pack_path = path.sub(pack_path, full_path)
41
- full_pack_path.concat(RUBY_EXTENSION) unless full_pack_path.end_with?(RUBY_EXTENSION)
42
- return File.exist?(full_pack_path) && full_pack_path
43
- end
44
- end
45
- end
46
- end
47
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Stimpack
4
- class Settings
5
- PACKWERK_PACKAGE_CONFIG = "package.yml"
6
-
7
- def initialize(pack)
8
- @pack = pack
9
-
10
- package_config = YAML.load_file(pack.root.join(PACKWERK_PACKAGE_CONFIG)) || {}
11
- @config = package_config.fetch("metadata", {}).freeze
12
- end
13
-
14
- def path
15
- @pack.called_from
16
- end
17
-
18
- def relative_path
19
- @relative_path ||= path.relative_path_from(Rails.root)
20
- end
21
-
22
- def name
23
- @name ||= ActiveSupport::Inflector.underscore(namespace.name)
24
- end
25
-
26
- def namespace
27
- @pack.module_parent
28
- end
29
-
30
- # def implicit_namespace
31
- # @config.fetch("implicit_namespace", true)
32
- # end
33
- # alias_method :implicit_namespace?, :implicit_namespace
34
-
35
- def isolate_namespace
36
- @config.fetch("isolate_namespace", true)
37
- end
38
- alias_method :isolate_namespace?, :isolate_namespace
39
- end
40
- end
@@ -1,24 +0,0 @@
1
- require "active_support"
2
-
3
- module Stimpack
4
- class ZeitwerkProxy
5
- delegate_missing_to :@loader
6
-
7
- def initialize(loader)
8
- @loader = loader
9
- end
10
-
11
- def push_dir(path, namespace: Object)
12
- @loader.push_dir(path, namespace: _resolve_namespace(path) || namespace)
13
- end
14
-
15
- private
16
-
17
- def _resolve_namespace(path)
18
- pack = Packs.find(path)
19
- return unless pack
20
-
21
- pack.settings.namespace if pack.settings.implicit_namespace?
22
- end
23
- end
24
- end