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

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

allow '-' as config parameter to read from STDIN. via http://d.hatena.ne.jp/drawnboy/20060910/1157818916

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