ShipIt/Conf.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 81 CC 5 SCAR 20.1
CC
5
Cov
0%
parse
12.0
steps
12.0
write_template
2.0
CRAP 7.5
1 package ShipIt::Conf;
2 use strict;
3  
4 =head1 NAME
5  
6 ShipIt::Conf -- holds/parses config info for a project
7  
8 =head1 SYNOPSIS
9  
10 # done for you:
11 my $conf = ShipIt::Conf->parse(CONFFILE);
12  
13 # fetch keys out of it in your Step's init method
14 package ShipIt::Step::Custom;
15 use base 'ShipIt::Step';
16 sub init {
17 my ($self, $conf) = @_;
18 ....
19 $self->{foo} = $conf->value("foo_key");
20 ....
21 }
22  
23 =cut
24  
25 =head1 CLASS METHODS
26  
27 =head2 parse
28  
29 $conf = ShipIt::Conf->parse(".shipit");
30  
31 Returns a ShipIt::Conf object from a file. Dies on parse failure.
32  
33 =cut
34  
35 sub parse {
36 my ($class, $file) = @_;
37 open (my $fh, $file) or die "Error opening config file $file: $!\n";
38 my $self = bless {
39 val => {}, # explicitly configured values
40 asked => {}, # keys asked for by step plugins
41 }, $class;
42 my $line = 0;
43 while (<$fh>) {
44 $line++;
45 s/\#.*//;
46 next unless /\S/;
47 unless (/^\s*(\S+)\s*=\s*(.+?)\s*$/) {
48 die "Bogus syntax on line $line:\n $_";
49 }
50 $self->{val}{$1} = $2;
51 }
52  
53 return $self;
54 }
55  
56 =head2 write_template
57  
58 ShipIt::Conf->write_template($file);
59  
60 Writes out a dummy config file to the provided $file.
61  
62 =cut
63  
64 sub write_template {
65 my ($class, $file) = @_;
66 my $steps = $class->default_steps;
67  
68 open (my $fh, ">$file") or die "Error opening for write config file $file: $!\n";
69 my $c = "# auto-generated shipit config file.
70 steps = $steps
71  
72 # svn.tagpattern = MyProj-%v
73 # svn.tagpattern = http://code.example.com/svn/tags/MyProj-%v
74  
75 # CheckChangeLog.files = ChangeLog, MyProj.CHANGES
76 ";
77 print $fh $c;
78 close($fh) or die;
79 }
80  
81 =head1 INSTANCE METHODS
82  
83 =head2 value
84  
85 $val = $conf->value($key);
86  
87 Fetch a config value. (also marks it as a known key, so any unknown
88 keys in a .shipit config file cause a configuration error)
89  
90 =cut
91  
92 sub value {
93 my ($self, $key) = @_;
94 $self->{asked}{$key} = 1;
95 return $self->{val}{$key};
96 }
97  
98 =head2 die_if_unknown_keys
99  
100 Die if any key exists which has never been asked for.
101  
102 =cut
103  
104 sub die_if_unknown_keys {
105 my $self = shift;
106 my @unknown = grep { ! $self->{asked}{$_} } keys %{ $self->{val} } or
107 return 1;
108 die "Unknown keys in configuration file: " . join(", ", @unknown) . "\n";
109 }
110  
111 sub default_steps {
112 return join(", ",
113 qw(
114 FindVersion
115 ChangeVersion
116 CheckChangeLog
117 DistTest
118 Commit
119 Tag
120 MakeDist
121 ));
122 }
123  
124 =head2 steps
125  
126 Returns array of ShipIt::Step instances, based on the value of
127 B<steps> in your .shipit config file. For instance, in your .shipit file:
128  
129 steps = FindVersion, ChangeVersion, Commit, Tag, MakeDist
130  
131 The makes ShipIt::Step::FindVersion loaded & instantiated (with 'new', which
132 calls by default 'init'), followed by ChangeVersion, etc.
133  
134 =cut
135  
136 # returns ShipIt::Step::Foo instances
137 sub steps {
138 my $self = shift;
139 my $steps = $self->value("steps") || $self->default_steps;
140  
141 my @ret;
142 foreach my $sname (split(/\s*,\s*/, $steps)) {
143 die "Bogus step name: $sname\n" unless $sname =~ /^[\w+:]+$/;
144 my $class = "ShipIt::Step::$sname";
145 my $rv = eval "use $class; 1;";
146 die "Failed to load step module $class: $@\n" unless $rv;
147 push @ret, $class->new($self);
148 }
149  
150 $self->die_if_unknown_keys;
151 return @ret;
152 }
153  
154 1;