44require 'pathname'
55require 'sorbet-runtime'
66require 'packs/pack'
7- require 'packs/private '
7+ require 'packs/specification '
88
9+ # We let `packs-specification` define some API methods such as all, find, and for_file,
10+ # because this allows a production environment to require `packs-specification` only and get some simple functionality, without
11+ # needing to load all of `packs`.
912module Packs
1013 PACKAGE_FILE = T . let ( 'package.yml' . freeze , String )
1114
@@ -14,67 +17,17 @@ class << self
1417
1518 sig { returns ( T ::Array [ Pack ] ) }
1619 def all
17- packs_by_name . values
20+ Specification . all
1821 end
1922
2023 sig { params ( name : String ) . returns ( T . nilable ( Pack ) ) }
2124 def find ( name )
22- packs_by_name [ name ]
25+ Specification . find ( name )
2326 end
2427
2528 sig { params ( file_path : T . any ( Pathname , String ) ) . returns ( T . nilable ( Pack ) ) }
2629 def for_file ( file_path )
27- path_string = file_path . to_s
28- @for_file = T . let ( @for_file , T . nilable ( T ::Hash [ String , T . nilable ( Pack ) ] ) )
29- @for_file ||= { }
30- @for_file [ path_string ] ||= all . find { |package | path_string . start_with? ( "#{ package . name } /" ) || path_string == package . name }
31- end
32-
33- sig { void }
34- def bust_cache!
35- @packs_by_name = nil
36- @config = nil
37- @for_file = nil
38- end
39-
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- # If packs.yml is being used, then ignore direct configuration.
49- # This is only a stop-gap to permit Stimpack users to more easily migrate
50- # to packs.yml
51- return if Private ::Configuration ::CONFIGURATION_PATHNAME . exist?
52-
53- yield ( config )
54- end
55-
56- private
57-
58- sig { returns ( T ::Hash [ String , Pack ] ) }
59- def packs_by_name
60- @packs_by_name = T . let ( @packs_by_name , T . nilable ( T ::Hash [ String , Pack ] ) )
61- @packs_by_name ||= begin
62- all_packs = package_glob_patterns . map do |path |
63- Pack . from ( path )
64- end
65-
66- # We want to match more specific paths first so for_file works correctly.
67- sorted_packages = all_packs . sort_by { |package | -package . name . length }
68- sorted_packages . to_h { |p | [ p . name , p ] }
69- end
70- end
71-
72- sig { returns ( T ::Array [ Pathname ] ) }
73- def package_glob_patterns
74- absolute_root = Private . root
75- config . pack_paths . flat_map do |pack_path |
76- Pathname . glob ( absolute_root . join ( pack_path ) . join ( PACKAGE_FILE ) )
77- end
30+ Specification . for_file ( file_path )
7831 end
7932 end
8033end
0 commit comments