-
Notifications
You must be signed in to change notification settings - Fork 2
/
project_requirements_from_yaml
executable file
·265 lines (218 loc) · 7.65 KB
/
project_requirements_from_yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env perl
# For support with this file, please file an issue on the GitHub issue tracker
# for this project: https://github.com/spicyjack/perl-scripts/issues
# TODO
# - Create a new View object that just prints the totals from each section;
# make it a Role that the other two View objects inherit
# - Change the output format of Digest mode, print a header with each section,
# then just the issue ID, issue type, and issue title after that, until the
# next section gets printed
=head1 NAME
B<project_requirements_from_yaml.pl> - Display on STDOUT, or create a
database, all of the project requirements given to the script via a YAML file.
=cut
our $copyright = q|Copyright (c)2014 Brian Manning|;
=head1 SYNOPSIS
project_requirements_from_yaml [options]
Script options:
-d|--dump Dump the YAML file after parsing, then exit
-h|--help Displays this help text
-v|--verbose Verbose script output
-y|--yaml YAML file to read from
-o|--output Create a database file with this filename
Default is to write plaintext to STDOUT
-g|--digest Digest mode; print only ID and title of requirements
-x|--overwrite Overwrite an existing database file
You can view the full C<POD> documentation of this file by calling
C<perldoc project_requirements_from_yaml>.
=cut
our @opts = (
# script options
q(dump|d),
q(verbose|v),
q(help|h),
q(yaml|y=s),
q(digest|g),
q(type3|3),
q(output|o=s),
q(overwrite|x),
);
=head1 DESCRIPTION
Parses the contents of the input YAML file, and either outputs formatted
plaintext to C<STDOUT>, or creates a database file and populates it with the
data parsed from the YAML file.
=cut
### Pragmas
use strict;
use warnings;
use 5.010;
### View for printing all information for the project requirements
package App::Beastly::View::AllRequirements;
use Moo;
sub print_item {
my $self = shift;
my %args = @_;
my $item = $args{item};
my $total_items = $args{total_items};
my $actual_time = $args{actual_time};
say sprintf(q(ID: %04u: ), $total_items) . $item->{title};
say " Desc: " . $item->{desc};
say " Created: " . $item->{created};
say " Type: " . $item->{type};
say " Time: " . $item->{half_days} . q| (eѕtimate, in half-days)|;
say " Actual time: " . $args{actual_time};
print qq(\n);
}
### View for printing only the ID and title of each project requirement
package App::Beastly::View::Digest;
use Moo;
sub print_item {
my $self = shift;
my %args = @_;
my $item = $args{item};
my $type = $item->{type};
my $total_items = $args{total_items};
say sprintf(q(ID %04u T%u: ), $total_items, $type) . $item->{title};
}
### View for printing only type 3 project requirements
package App::Beastly::View::Type3;
use Moo;
sub print_item {
my $self = shift;
my %args = @_;
my $item = $args{item};
my $total_items = $args{total_items};
say sprintf(q(ID: %04u: ), $total_items) . $item->{title};
}
### Main package
package main;
### System modules
use File::Basename;
use Getopt::Long;
use Pod::Usage;
use YAML::XS qw(LoadFile);
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Terse = 1;
# for use in help/error output
my $my_name = basename($0);
# arguments parsed by Getopts
my %args;
my $parser = Getopt::Long::Parser->new();
$parser->getoptions(\%args, @opts);
if ( exists $args{help} ) { pod2usage(-exitstatus => 1); }
if ( $args{dump} ) {
say q(Dumping %args;);
say Dumper %args;
}
# create a YAML object using the DATA filehandle
# $struct should end up being a reference to an array of hashes
my $yaml;
if ( ! defined $args{yaml} ) {
warn qq(Error: --yaml file to parse was not specified\n);
die qq(Use '$my_name --help' to see script options\n);
}
if ( -e $args{yaml} ) {
$yaml = LoadFile($args{yaml});
} else {
die q(Error: file ) . $args{yaml} . q( not found/unavailable);
}
if ( $args{dump} ) {
say Dumper $yaml;
exit 0;
}
my $view;
if ( $args{digest} && $args{type3} ) {
die qq(ERROR: use either --digest or --type3, not both\n);
}
if ( $args{digest} ) {
$view = App::Beastly::View::Digest->new();
say q(++++ Listing all issues in "digest" mode ++++);
print qq(\n);
} elsif ( $args{type3} ) {
$view = App::Beastly::View::Type3->new();
say q(++++ Listing all type 3 issues in "digest" mode ++++);
print qq(\n);
} else {
$view = App::Beastly::View::AllRequirements->new();
}
my $total_items = 0;
my $total_sections = 0;
my $item_counter = 0;
my $section_counter = 0;
my $current_section;
my $total_half_days = 0;
my $computed_time = 0;
my $actual_time = q();
foreach my $item (@{$yaml}) {
if ( ref($item) =~ q(HASH) ) {
$item_counter++;
$total_items++;
if ( $item->{type} == 1 ) {
$actual_time
= q|(Type 1 tasks don't receive any time adjustments)|;
$computed_time = $item->{half_days};
}
if ( $item->{type} == 2 ) {
$computed_time = $item->{half_days} * 1.5;
$actual_time = q|(Type 2 task) |
. $item->{half_days}
. q( half-days * 1.5 = )
. $computed_time
. q| half-days total|;
}
if ( $item->{type} == 3 ) {
$computed_time = $item->{half_days} * 3;
$actual_time = q|(Type 3 task) |
. $item->{half_days}
. q( half-days * 3 = )
. $computed_time
. q| half-days total|;
}
# add the computed half-days to the total project time
$total_half_days += $computed_time;
# if we're printing 'type 3' issues only, check here and skip
next if ( $args{type3} && $item->{type} != 3);
$view->print_item(
item => $item,
total_items => $total_items,
actual_time => $actual_time,
);
} else {
# if we've already parsed another section print a section "footer"
if ( defined $current_section ) {
say qq(Total items in section "$current_section": )
. $item_counter;
print qq(\n);
}
# then zero out the counters/section name, and start a new section
$item_counter = 0;
$total_sections++;
$current_section = $item;
say qq(---- Section: $current_section ----);
}
}
# print one last section "footer"
say qq(Total items in section "$current_section": $item_counter);
print qq(\n);
say qq(Total half-days required for project: )
. sprintf(q(%0.2f), $total_half_days);
say qq(Total number of project items: $total_items);
say qq(Total number of project sections: $total_sections);
=head1 AUTHOR
Brian Manning, C<< <cpan at xaoc dot org> >>
=head1 BUGS
Please report any bugs or feature requests to the GitHub issue tracker for
this project:
C<< <https://github.com/spicyjack/perl-scripts/issues> >>.
=head1 SUPPORT
You can view documentation for this script with the C<perldoc> command.
perldoc project_requirements_from_yaml
=head1 COPYRIGHT & LICENSE
Copyright (c) 2014 Brian Manning, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
# конец!
# vim: set shiftwidth=4 tabstop=4: