@@ -10,6 +10,8 @@ use namespace::clean;
1010use File::pushd;
1111use File::Find;
1212use Path::Tiny;
13+ use YAML::XS;
14+ use Try::Tiny;
1315
1416has git_dir => (
1517 is => ' ro' ,
@@ -180,10 +182,48 @@ sub get_version_desc ($self) {
180182 $output ;
181183}
182184
183- sub check_next {
185+ sub check_next ($self , @) {
186+ my $cf = $self -> hub-> config_file;
187+
188+ # No config file? Huh. Do normal check
189+ return $self -> check_next_file_find unless $cf ;
190+
191+ my $reader = $cf =~ / \. ya?ml\z / ? sub { YAML::XS::LoadFile($_ [0]) }
192+ : $cf =~ / \. json\z / ? \&Synergy::Hub::_slurp_json_file
193+ : $cf =~ / \. toml\z / ? \&Synergy::Hub::_slurp_toml_file
194+ : undef ;
195+
196+ return $self -> check_next_file_find unless $reader ;
197+
198+ my ($config , $err );
199+
200+ try {
201+ $config = $reader -> ($cf );
202+ } catch {
203+ $err = " Failed to parse config ($cf ): $_ " ;
204+ };
205+
206+ return $err if $err ;
207+
208+ my %allowed ;
209+
210+ for my $thing (qw( channels reactors ) ) {
211+ for my $thing_config (values %{ $config -> {$thing } }) {
212+ my $thing_class = delete $thing_config -> {class };
213+
214+ next unless $thing_class ;
215+
216+ $allowed {$thing_class } = 1;
217+ }
218+ }
219+
220+ return $self -> check_next_file_find(\%allowed );
221+ }
222+
223+ sub check_next_file_find ($self , $allowed = {}) {
184224 my $data = " use lib qw(lib);\n " ;
185225
186- find(sub { wanted(\$data ) }, ' lib/' );
226+ find(sub { wanted($allowed , \$data ) }, ' lib/' );
187227
188228 my $f = Path::Tiny-> tempfile;
189229 $f -> spew($data );
@@ -194,9 +234,7 @@ sub check_next {
194234 return ;
195235}
196236
197- sub wanted {
198- my $data = shift ;
199-
237+ sub wanted ($allowed , $data ) {
200238 return unless -f $_ ;
201239 return unless / \. pm$ / ;
202240
@@ -206,6 +244,9 @@ sub wanted {
206244 $name =~ s /\/ / ::/ g ;
207245 $name =~ s /\. pm// ;
208246
247+ # Only load channels/reactors referenced in config
248+ return if %$allowed && $name =~ / Synergy::(Reactor|Channel)/ && ! $allowed -> {$name };
249+
209250 $$data .= " use $name ;\n " ;
210251}
211252
0 commit comments