Skip to content

Commit e6cc779

Browse files
author
Alex Evanczuk
authored
Allow configuration to be specified by packs.yml (#6)
* Change tests to match new desired behavior * Fix broken tests * bump version * allow configuration to be updated via ruby
1 parent b4983b1 commit e6cc779

8 files changed

Lines changed: 73 additions & 53 deletions

File tree

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
packs (0.0.2)
4+
packs (0.0.3)
55
sorbet-runtime
66

77
GEM

lib/packs.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require 'yaml'
44
require 'sorbet-runtime'
55
require 'packs/pack'
6-
require 'packs/configuration'
76
require 'packs/private'
87

98
module Packs
@@ -38,6 +37,17 @@ def bust_cache!
3837
@for_file = nil
3938
end
4039

40+
sig { returns(Private::Configuration) }
41+
def config
42+
@config = T.let(@config, T.nilable(Private::Configuration))
43+
@config ||= Private::Configuration.fetch
44+
end
45+
46+
sig { params(blk: T.proc.params(arg0: Private::Configuration).void).void }
47+
def configure(&blk)
48+
yield(config)
49+
end
50+
4151
private
4252

4353
sig { returns(T::Hash[String, Pack]) }
@@ -56,12 +66,9 @@ def packs_by_name
5666

5767
sig { returns(T::Array[Pathname]) }
5868
def package_glob_patterns
59-
config.roots.flat_map do |root|
60-
absolute_root = Private.root.join(root)
61-
[
62-
*absolute_root.glob("*/#{PACKAGE_FILE}"),
63-
*absolute_root.glob("*/*/#{PACKAGE_FILE}")
64-
]
69+
absolute_root = Private.root
70+
config.pack_paths.flat_map do |pack_path|
71+
Pathname.glob(absolute_root.join(pack_path).join(PACKAGE_FILE))
6572
end
6673
end
6774
end

lib/packs/configuration.rb

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

lib/packs/private.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# typed: strict
22

3+
require 'packs/private/configuration'
4+
35
module Packs
46
module Private
57
extend T::Sig

lib/packs/private/configuration.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# typed: strict
2+
3+
module Packs
4+
module Private
5+
class Configuration < T::Struct
6+
extend T::Sig
7+
DEFAULT_PACK_PATHS = T.let([
8+
'packs/*',
9+
'packs/*/*',
10+
], T::Array[String])
11+
12+
prop :pack_paths, T::Array[String]
13+
14+
sig { returns(Configuration) }
15+
def self.fetch
16+
configuration_path = Pathname.new('packs.yml')
17+
config_hash = configuration_path.exist? ? YAML.load_file('packs.yml') : {}
18+
19+
new(
20+
pack_paths: pack_paths(config_hash),
21+
)
22+
end
23+
24+
sig { params(config_hash: T::Hash[T.untyped, T.untyped]).returns(T::Array[String]) }
25+
def self.pack_paths(config_hash)
26+
specified_pack_paths = config_hash['pack_paths']
27+
if specified_pack_paths.nil?
28+
DEFAULT_PACK_PATHS.dup
29+
else
30+
Array(specified_pack_paths)
31+
end
32+
end
33+
end
34+
end
35+
end

packs.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |spec|
22
spec.name = 'packs'
3-
spec.version = '0.0.2'
3+
spec.version = '0.0.3'
44
spec.authors = ['Gusto Engineers']
55
spec.email = ['dev@gusto.com']
66
spec.summary = 'Packs are the specification for gradual modularization in the `rubyatscale` ecosystem.'

spec/packs_spec.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,25 @@
2020
end
2121

2222
context 'in an app with a differently configured root' do
23+
before do
24+
write_file('packs/my_pack/package.yml')
25+
write_file('components/my_pack/package.yml')
26+
write_file('packs.yml', <<~YML)
27+
pack_paths:
28+
- packs/*
29+
- components/*
30+
YML
31+
end
32+
33+
it { expect(Packs.all.count).to eq 2 }
34+
end
35+
36+
context 'in an app with a differently configured root configured via ruby' do
2337
before do
2438
write_file('packs/my_pack/package.yml')
2539
write_file('components/my_pack/package.yml')
2640
Packs.configure do |config|
27-
config.roots = %w[components packs]
41+
config.pack_paths = ['packs/*', 'components/*']
2842
end
2943
end
3044

@@ -55,9 +69,11 @@
5569
before do
5670
write_file('packs/my_pack/package.yml')
5771
write_file('components/my_pack/package.yml')
58-
Packs.configure do |config|
59-
config.roots = %w[components packs]
60-
end
72+
write_file('packs.yml', <<~YML)
73+
pack_paths:
74+
- packs/*
75+
- components/*
76+
YML
6177
end
6278

6379
it { expect(Packs.find('packs/my_pack').name).to eq 'packs/my_pack' }

spec/spec_helper.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
config.before do
1919
Packs.bust_cache!
20-
Packs.configure do |packs_config|
21-
packs_config.roots = ['packs']
22-
end
2320
end
2421

2522
config.around do |example|

0 commit comments

Comments
 (0)