File Coverage

blib/lib/App/Git/Workflow/Command/Pom.pm
Criterion Covered Total %
statement 50 86 58.1
branch 8 22 36.3
condition 2 3 66.6
subroutine 10 12 83.3
pod 5 5 100.0
total 75 128 58.5


line stmt bran cond sub pod time code
1             package App::Git::Workflow::Command::Pom;
2              
3             # Created on: 2014-03-19 17:18:17
4             # Create by: Ivan Wills
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 2     2   87303 use strict;
  2         13  
  2         58  
10 2     2   10 use warnings;
  2         4  
  2         59  
11 2     2   388 use version;
  2         1584  
  2         10  
12 2     2   576 use English qw/ -no_match_vars /;
  2         3028  
  2         10  
13 2     2   1061 use App::Git::Workflow::Pom;
  2         5  
  2         82  
14 2     2   417 use App::Git::Workflow::Command qw/get_options/;
  2         4  
  2         97  
15 2     2   1563 use Path::Tiny;
  2         21252  
  2         2348  
16              
17             our $VERSION = version->new(1.1.16);
18             our $workflow = App::Git::Workflow::Pom->new;
19             our ($name) = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs;
20             our %option;
21             our %p2u_extra;
22              
23             sub run {
24 3     3 1 6 my ($self) = @_;
25 3         18 %option = (
26             pom => $workflow->config('workflow.pom', 'pom.xml'),
27             fetch => 1,
28             );
29 3 50       19 get_options(
30             \%option,
31             'pom|P=s',
32             'update|u!',
33             'skip|s=s',
34             'match|m=s',
35             'fetch|f!',
36             'tag|t=s',
37             'branch|b=s',
38             'local|l!',
39             ) or return;
40 3 100       9 my $sub_command = @ARGV ? 'do_' . shift @ARGV : "do_uniq";
41 3         7 $sub_command =~ s/-/_/g;
42              
43 3 50       21 if ( !$self->can($sub_command) ) {
44 0         0 warn "Unknown sub command '$sub_command'!\n";
45 0         0 App::Git::Workflow::Command::pod2usage( %p2u_extra, -verbose => 1 );
46 0         0 return 1;
47             }
48              
49 3         8 $workflow->{VERBOSE} = $option{verbose};
50 3         6 $workflow->{TEST } = $option{test};
51              
52             # make sure that git is up-to-date
53 3 50       11 $workflow->git->fetch if $option{fetch};
54              
55 3         14 $self->$sub_command($option{pom}, @ARGV);
56              
57 3         10 return;
58             }
59              
60             sub do_uniq {
61 2     2 1 5 my (undef, $pom) = @_;
62 2         25 my $versions = $workflow->get_pom_versions($pom, $option{match}, $option{skip});
63 2         11 my $numerical = my $version = $workflow->pom_version((scalar path($pom)->slurp), $pom, 1);
64 2         14 $numerical =~ s/-SNAPSHOT$//xms;
65              
66 2 100 66     29 if ( !$versions->{$numerical} || keys %{ $versions->{$numerical} } <= 1 ) {
  2         10  
67 1         39 print "POM Version $version is unique\n";
68             }
69             else {
70 1         31 warn "Following branches are using version $numerical\n";
71 1         6 warn "\t", join "\n\t", (sort keys %{ $versions->{$numerical} }), "\n";
  1         17  
72 1         4 return scalar keys %{ $versions->{$numerical} };
  1         5  
73             }
74              
75 1         7 return;
76             }
77              
78             sub do_next {
79 1     1 1 3 my (undef, $pom) = @_;
80              
81 1         5 my $version = $workflow->next_pom_version($pom);
82 1         27 print "$version\n";
83              
84 1 50       7 if ($option{update}) {
85 0         0 system(qw/mvn versions:set/, "–DnewVersion=$version");
86             }
87              
88 1         3 return;
89             }
90              
91             sub do_whos {
92 0     0 1   my (undef, $pom, $version) = @_;
93              
94 0 0         if (!$version) {
95 0           warn "No version supplied!\n";
96 0           App::Git::Workflow::Command::pod2usage( %p2u_extra, -verbose => 1 );
97 0           return 1;
98             }
99              
100 0           $version =~ s/-SNAPSHOT$//;
101              
102 0           my $versions = $workflow->get_pom_versions($pom, $option{match}, $option{skip});
103              
104 0 0         my $version_re = $version =~ /^\d+[.]\d+[.]\d+/ ? qr/^$version$/ : qr/$version/;
105 0           my %versions = map {%{ $versions->{$_} }} grep {/$version_re/} keys %{ $versions };
  0            
  0            
  0            
  0            
106              
107 0           print map {"$_\t$versions{$_}\n"} sort keys %versions;
  0            
108              
109 0           return;
110             }
111              
112             sub do_bad_branches {
113 0     0 1   my (undef, $pom) = @_;
114              
115 0           my @branches = $workflow->branches('both');
116 0           my $max_age = $workflow->_max_age;
117              
118             BRANCH:
119 0           for my $branch (sort @branches) {
120 0           my $current = $workflow->commit_details($branch);
121              
122             # Skip any branches that are over $MAX_AGE old
123 0 0         if ( $current->{time} < time - $max_age ) {
124 0           next BRANCH;
125             }
126              
127 0           my $xml = $workflow->git->show("$branch:$pom");
128 0           chomp $xml;
129 0 0         next if !$xml;
130              
131 0           $branch =~ s{^origin/}{}xms;
132              
133 0           my $version = eval { $workflow->pom_version($xml) };
  0            
134              
135             # make sure we get a valid version
136 0 0         if ( $@ ) {
137 0           warn "$branch is bad!\n";
138             }
139             }
140              
141 0           return;
142             }
143              
144             1;
145              
146             __DATA__
147              
148             =head1 NAME
149              
150             git-pom - Manage pom.xml (or package.json) file versions
151              
152             =head1 VERSION
153              
154             This documentation refers to git-pom version 1.1.16
155              
156             =head1 SYNOPSIS
157              
158             git-pom [uniq] [option]
159             git-pom next [--update|-u]
160             git-pom whos version [option]
161              
162             SUB-COMMAND:
163             uniq Confirm that the current branch is the only branch using its version
164             next Calculates the next available version number
165             whos Which branch uses the pom version "version"
166              
167             OPTIONS:
168             -P --pom[=]file
169             Specify the pom file location (Default pom.xml)
170             -u --update Update to next version (used with next)
171             -t --tag[=]str
172             Specify a tag that any branch with newer commits must contain
173             -b --branch[=]str
174             Similarly a branch that other branches with newer commits must
175             contain (Default origin/master)
176             -l --local Shorthand for --branch '^master$'
177              
178             -v --verbose Show more detailed option
179             --version Prints the version information
180             --help Prints this help information
181             --man Prints the full documentation for git-pom
182              
183             =head1 DESCRIPTION
184              
185             The C<git-pom> tool helps working with Maven POM files by looking at all branches to see
186             what versions are set. The sub commands allow different kinds of checking to be done.
187              
188             =over 4
189              
190             =item uniq
191              
192             Check that the current branch's POM version is unique across all branches.
193              
194             =item next
195              
196             Finds the next available POM version number buy finding the current nighest
197             POM version and incrementing the second number. If C<--update> is used then
198             the POM version is updated to that number.
199              
200             =item whos
201              
202             Find which branch or branches use a POM version number.
203              
204             =back
205              
206             =head1 SUBROUTINES/METHODS
207              
208             =head2 C<run ()>
209              
210             Executes the git workflow command
211              
212             =head2 C<do_whos ()>
213              
214             =head2 C<do_next ()>
215              
216             =head2 C<do_uniq ()>
217              
218             =head2 C<do_bad_branches ($pom)>
219              
220             Show branches with pom.xml files that don't pass
221              
222             =head1 DIAGNOSTICS
223              
224             =head1 CONFIGURATION AND ENVIRONMENT
225              
226             Defaults for this script can be set through C<git config>
227              
228             workflow.prod Sets how a prod release is determined
229             eg the default equivalent is branch=^origin/master$
230             workflow.pom The default location for the pom.xml file (used by C<--new-pom>
231             when updating pom.xml for the new branch)
232              
233             You can set these values either by editing the repository local C<.git/config>
234             file or C<~/.gitconfig> or use the C<git config> command
235              
236             # eg Setting the global value
237             git config --global workflow.prod 'branch=^origin/master$'
238              
239             # or set a repository's local value
240             git config workflow.prod 'tag=^release_\d{4}_\d{2}\d{2}$'
241              
242             # or set pom.xml location to a sub directory
243             git config workflow.pom 'somedir/pom.xml'
244              
245             =head1 DEPENDENCIES
246              
247             =head1 INCOMPATIBILITIES
248              
249             =head1 BUGS AND LIMITATIONS
250              
251             There are no known bugs in this module.
252              
253             Please report problems to Ivan Wills (ivan.wills@gmail.com).
254              
255             Patches are welcome.
256              
257             =head1 AUTHOR
258              
259             Ivan Wills - (ivan.wills@gmail.com)
260              
261             =head1 LICENSE AND COPYRIGHT
262              
263             Copyright (c) 2014 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
264             All rights reserved.
265              
266             This module is free software; you can redistribute it and/or modify it under
267             the same terms as Perl itself. See L<perlartistic>. This program is
268             distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
269             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
270             PARTICULAR PURPOSE.
271              
272             =cut