root/trunk/plagger/lib/Plagger/ConfigLoader.pm

Revision 1391 (checked in by miyagawa, 14 years ago)

merge from hackathon-mt

Line 
1 package Plagger::ConfigLoader;
2 use strict;
3 use Carp;
4
5 sub new {
6     my $class = shift;
7     bless {}, $class;
8 }
9
10 sub load {
11     my($self, $stuff) = @_;
12
13     my $config;
14     if (-e $stuff && -r _) {
15         $config = YAML::LoadFile($stuff);
16         $self->{config_path} = $stuff;
17     } elsif (ref($stuff) && ref($stuff) eq 'SCALAR') {
18         $config = YAML::Load(${$stuff});
19     } elsif (ref($stuff) && ref($stuff) eq 'HASH') {
20         $config = Storable::dclone($stuff);
21     } else {
22         croak "Plagger::ConfigLoader->load: $stuff: $!";
23     }
24
25     return $config;
26 }
27
28 sub load_include {
29     my($self, $config) = @_;
30
31     my $includes = $config->{include} or return;
32     $includes = [ $includes ] unless ref $includes;
33
34     for my $file (@$includes) {
35         my $include = YAML::LoadFile($file);
36
37         for my $key (keys %{ $include }) {
38             my $add = $include->{$key};
39             unless ($config->{$key}) {
40                 $config->{$key} = $add;
41                 next;
42             }
43             if (ref($config->{$key}) eq 'HASH') {
44                 next unless ref($add) eq 'HASH';
45                 for (keys %{ $include->{$key} }) {
46                     $config->{$key}->{$_} = $include->{$key}->{$_};
47                 }
48             } elsif (ref($include->{$key}) eq 'ARRAY') {
49                 $add = [ $add ] unless ref($add) eq 'ARRAY';
50                 push(@{ $config->{$key} }, @{ $include->{$key} });
51             } elsif ($add) {
52                 $config->{$key} = $add;
53             }
54         }
55     }
56 }
57
58 sub load_recipes {
59     my($self, $config) = @_;
60
61     for (@{ $config->{recipes} }) {
62         $self->error("no such recipe to $_") unless $config->{define_recipes}->{$_};
63         my $plugin = $config->{define_recipes}->{$_};
64         $plugin = [ $plugin ] unless ref($plugin) eq 'ARRAY';
65         push(@{ $config->{plugins} }, @{ $plugin });
66     }
67 }
68
69 1;
Note: See TracBrowser for help on using the browser.