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

Revision 2052 (checked in by miyagawa, 3 years ago)

Added no dclone option for Remedie

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 = $stuff->{global}->{no_config_clone}
23             ? $stuff : Storable::dclone($stuff);
24     } else {
25         croak "Plagger::ConfigLoader->load: $stuff: $!";
26     }
27
28     unless ($config->{global} && $config->{global}->{no_decode_utf8}) {
29         Plagger::Walker->decode_utf8($config);
30     }
31
32     return $config;
33 }
34
35 sub load_include {
36     my($self, $config) = @_;
37
38     my $includes = $config->{include} or return;
39     $includes = [ $includes ] unless ref $includes;
40
41     for my $file (@$includes) {
42         my $include = YAML::LoadFile($file);
43
44         for my $key (keys %{ $include }) {
45             my $add = $include->{$key};
46             unless ($config->{$key}) {
47                 $config->{$key} = $add;
48                 next;
49             }
50             if (ref($config->{$key}) eq 'HASH') {
51                 next unless ref($add) eq 'HASH';
52                 for (keys %{ $include->{$key} }) {
53                     $config->{$key}->{$_} = $include->{$key}->{$_};
54                 }
55             } elsif (ref($include->{$key}) eq 'ARRAY') {
56                 $add = [ $add ] unless ref($add) eq 'ARRAY';
57                 push(@{ $config->{$key} }, @{ $include->{$key} });
58             } elsif ($add) {
59                 $config->{$key} = $add;
60             }
61         }
62     }
63 }
64
65 sub load_recipes {
66     my($self, $config) = @_;
67
68     for (@{ $config->{recipes} }) {
69         Plagger->context->error("no such recipe to $_") unless $config->{define_recipes}->{$_};
70         my $plugin = $config->{define_recipes}->{$_};
71         $plugin = [ $plugin ] unless ref($plugin) eq 'ARRAY';
72         push(@{ $config->{plugins} }, @{ $plugin });
73     }
74 }
75
76 1;
Note: See TracBrowser for help on using the browser.