| 1 | package FreePAN::Config; | ||
| 2 | use Spoon::Config -Base; | ||
| 3 | use mixin 'FreePAN::Installer'; | ||
| 4 | |||
| 5 | const class_id => 'config'; | ||
| 6 | const class_title => 'Configuration Module'; | ||
| 7 | const config_file => "config.yaml"; | ||
| 8 | field path => []; | ||
| 9 | field plugins_file => ''; | ||
| 10 | field base => -init => '$self->base_init'; | ||
| 11 | field default_path => -init => '[ $self->base . "/config" ]'; | ||
| 12 | |||
| 13 | sub base_init { | ||
| 14 | $ENV{FREEPAN_BASE} || $ENV{HOME} | ||
| 15 | ? "$ENV{HOME}/.freepan" : die "Can't determine FreePAN base directory"; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub init { | ||
| 19 | $self->add_path(@{$self->default_path}); | ||
| 20 | $self->add_file($self->config_file); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub paired_arguments { qw(-plugins) } | ||
| 24 | sub new { | ||
| 25 | my ($args, @configs) = $self->parse_arguments(@_); | ||
| 26 | $self = super(@configs); | ||
| 27 | if (my $plugins_file = $args->{-plugins}) { | ||
| 28 | $self->add_plugins_file($plugins_file); | ||
| 29 | $self->plugins_file($plugins_file); | ||
| 30 | } | ||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub add_plugins_file { | ||
| 35 | my $plugins_file = shift; | ||
| 36 | return unless -f $plugins_file; | ||
| 37 | $self->add_config( | ||
| 38 | { | ||
| 39 | plugin_classes => [ $self->read_plugins($plugins_file) ], | ||
| 40 | } | ||
| 41 | ); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub read_plugins { | ||
| 45 | my $plugins_file = io(shift); | ||
| 46 | my @plugins = grep { | ||
| 47 | s/^([\+\-]?[\w\:]+)\s*$/$1/; | ||
| 48 | } $plugins_file->slurp; | ||
| 49 | return @plugins unless grep /^[\+\-]/, @plugins or not @plugins; | ||
| 50 | my $filename = $plugins_file->filename; | ||
| 51 | die "Can't create plugins list" | ||
| 52 | unless -e "../$filename"; | ||
| 53 | my $updir = io->updir->chdir; | ||
| 54 | my @parent_plugins = $self->read_plugins($filename); | ||
| 55 | for (@plugins) { | ||
| 56 | my $remove = $_; | ||
| 57 | $remove =~ s/^\-// or next; | ||
| 58 | @parent_plugins = grep {$_ ne $remove} @parent_plugins; | ||
| 59 | } | ||
| 60 | my %have; | ||
| 61 | @have{@parent_plugins} = ('1') x @parent_plugins; | ||
| 62 | return @parent_plugins, grep { | ||
| 63 | not /^\-/ and do { | ||
| 64 | s/^\+//; | ||
| 65 | not $have{$_}; | ||
| 66 | } | ||
| 67 | } @plugins; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub default_classes { | ||
| 71 | ( | ||
| 72 | command_class => 'FreePAN::Command', | ||
| 73 | config_class => 'FreePAN::Config', | ||
| 74 | hub_class => 'Spoon::Hub', | ||
| 75 | registry_class => 'FreePAN::Registry', | ||
| 76 | template_class => 'FreePAN::Template', | ||
| 77 | ) | ||
| 78 | } | ||
| 79 | |||
| 80 | sub add_file { | ||
| 81 | my $file = shift | ||
| 82 | or return; | ||
| 83 | my $file_path = ''; | ||
| 84 | for (@{$self->path}) { | ||
| 85 | $file_path = "$_/$file", last | ||
| 86 | if -f "$_/$file"; | ||
| 87 | } | ||
| 88 | return unless $file_path; | ||
| 89 | my $hash = $self->hash_from_file($file_path); | ||
| 90 | for my $key (keys %$hash) { | ||
| 91 | next if defined $self->{$key}; | ||
| 92 | field $key; | ||
| 93 | $self->{$key} = $hash->{$key}; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | sub add_path { | ||
| 98 | splice @{$self->path}, 0, 0, @_; | ||
| 99 | } | ||
| 100 | |||
| 101 | __DATA__ |