Skip to content

Commit

Permalink
Handle lists vs array of refs
Browse files Browse the repository at this point in the history
  • Loading branch information
swelljoe committed Apr 21, 2017
1 parent fe5dbc2 commit 2353cfd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
1 change: 1 addition & 0 deletions bin/init-system.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env perl
use strict;
use warnings;
use 5.10.1;
use Getopt::Long;
use Pod::Usage;
use Virtualmin::Config;
Expand Down
21 changes: 11 additions & 10 deletions lib/Virtualmin/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use strict;
use warnings;
no warnings qw(once); # We've got some globals that effect Webmin behavior
use 5.010_001; # Version shipped with CentOS 6. Nothing older.
use Getopt::Long qw( GetOptionsFromArray );
use Pod::Usage;
use Module::Load;
use Term::ANSIColor qw(:constants);
use Term::Spinner::Color;
Expand Down Expand Up @@ -66,14 +64,14 @@ sub _gather_plugins {
}

# Check with the command arguments
if ( defined $self->{include} ) {
if ( ref($self->{include}) eq 'ARRAY' || ref($self->{include}) eq 'STRING' ) {
for my $include ($self->{'include'}) {
push (@plugins, $include) unless grep( /^$include$/, @plugins );
push (@plugins, $include) unless ( map { grep( /^$include$/, @{$_} ) } @plugins);
}
}

# Check for excluded plugins
if ( defined $self->{exclude} ) {
if ( ref($self->{exclude}) eq 'ARRAY' || ref($self->{exclude}) eq 'STRING' ) {
for my $exclude ($self->{'exclude'}) {
my @dix = reverse(grep { $plugins[$_] eq $exclude } 0..$#plugins);
for (@dix) {
Expand All @@ -88,23 +86,26 @@ sub _gather_plugins {
# Take the gathered list of plugins and sort them to resolve deps
sub _order_plugins {
my ($self, @plugins) = @_;
my $plugin_details = {}; # Will hold an array of hashes containing name/depends
my %plugin_details; # Will hold an array of hashes containing name/depends
# Load up @plugin_details with name and dependency list
if (ref($plugins[0]) eq 'ARRAY') { # XXX Why is this so stupid?
@plugins = map{@$_} @plugins; # Flatten the array of refs into list.
}
for my $plugin_name (@plugins) {
my $pkg = "Virtualmin::Config::Plugin::$plugin_name";
load $pkg;
my $plugin = $pkg->new();
$plugin_details->{$plugin->{'name'}} = $plugin->{'depends'} // [];
$plugin_details{$plugin->{'name'}} = $plugin->{'depends'} // [];
}
return _topo_sort($plugin_details);
return _topo_sort(%plugin_details);
}

# Topological sort on dependencies
sub _topo_sort {
my ($deps) = @_;
my (%deps) = @_;

my %ba;
while ( my ( $before, $afters_aref ) = each %{$deps} ) {
while ( my ( $before, $afters_aref ) = each %deps ) {
unless ( @{$afters_aref} ) {
$ba{$before} = {};
}
Expand Down
6 changes: 3 additions & 3 deletions t/01-plugins.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ my @plugins = $bundle->_gather_plugins();
ok(grep {/Test/} @plugins);
ok(grep {/Test2/} @plugins);

my $include = Virtualmin::Config->new(include => ('Test'));
my $include = Virtualmin::Config->new(include => ['Test']);
my @plugins2 = $include->_gather_plugins();
ok(grep {/^Test$/} @plugins2);
ok(map { grep {/^Test$/} @{$_}} @plugins2);
ok(scalar @plugins2 == 1);

my $depends = Virtualmin::Config->new(include => ('Test2'));
my $depends = Virtualmin::Config->new(include => ['Test2']);
my @plugins3 = $depends->_gather_plugins();
my @resolved = $depends->_order_plugins(@plugins3);
ok(grep {/^Test2$/} @resolved);
Expand Down

0 comments on commit 2353cfd

Please sign in to comment.