blib/lib/Git/MoreHooks/ScanCtags.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 163 CC 5 SCAR 21.6
CC
5
Cov
0%
update_tags_file
30.0
_setup_config
2.0
BEGIN
2.0
CRAP 8.7
1 package Git::MoreHooks::ScanCtags;
2  
3 use strict;
4 use warnings;
5  
6 # ABSTRACT: Create or update ctags after git commit or merge
7  
8 our $VERSION = '0.021';
9  
10 use utf8;
11 use Carp qw( croak );
12 use Log::Any qw( $log );
13 use Git::Hooks 2.014000; # v2.14.0: oldest release on CPAN
14 use Git::Repository::Log;
15 use File::Find qw(find);
16 use List::Util qw( any );
17 use Module::Load qw{ load };
18 use Path::Tiny qw( path );
19  
20 my $PKG = __PACKAGE__;
21 my $CFG = __PACKAGE__ =~ s/.*::/githooks./rmsx;
22  
23 #############
24 # Grok hook configuration, check it and set defaults.
25  
26 sub _setup_config {
27 my ($git) = @_;
28  
29 my $config = $git->get_config();
30  
31 # Place default values where values are missing
32 $config->{ lc $CFG }->{tags} //= ['tags'];
33 $config->{ lc $CFG }->{dir} //= [qw( lib bin t )];
34 $config->{ lc $CFG }->{suffix} //= [qw( pm pl t )];
35 $config->{ lc $CFG }->{executable} //= [1];
36 $config->{ lc $CFG }->{verbose} //= [1];
37  
38 return;
39 }
40  
41 ##########
42  
43 sub update_tags_file {
44 my ( $git, ) = @_;
45  
46 _setup_config($git);
47  
48 my $errors = 0;
49 eval {
50 require Perl::Tags::Naive::Moose;
51 require Perl::Tags::Naive::Lib;
52 require Perl::Tags::PPI;
53 require Perl::Tags::Hybrid;
54 1;
55 } or do {
56 $log->errorf( $PKG . '::update_tags_file(): Cannot load Perl::Tags modules' );
57 $git->fault( 'Cannot load Perl::Tags module', { prefix => $PKG, } );
58 ++$errors;
59 return $errors;
60 };
61  
62 my %args = (
63 max_level => 1, # Only tag the file, do not read any dependencies.
64 do_variables => 1,
65 exts => 1,
66 );
67  
68 my @taggers = ( Perl::Tags::Naive::Lib->new(%args), Perl::Tags::Naive::Moose->new(%args), Perl::Tags::PPI->new(%args), );
69  
70 my $ptag = Perl::Tags::Hybrid->new( %args, taggers => \@taggers );
71  
72 my $outfile = $git->get_config( $CFG => 'tags' );
73 my @dirs = $git->get_config( $CFG => 'dir' );
74 my @file_suffices = $git->get_config( $CFG => 'suffix' );
75 my $executable_files = $git->get_config( $CFG => 'executable' );
76 $log->debugf( 'cfg: outfile:%s, dirs:%s, suffices:%s, executable:%s', $outfile, \@dirs, \@file_suffices, $executable_files );
77  
78 my @files;
79 find(
80 {
81 follow => 1,
82 no_chdir => 1,
83 wanted => sub {
84 if ( $executable_files && -f "$File::Find::name" ) {
85 if ( -x $File::Find::name ) {
86 push @files, $File::Find::name;
87 $log->debugf( 'Take tags from file: %s', $File::Find::name );
88 }
89 elsif ( $File::Find::name =~ m/[.] ( [[:alnum:]]{1,}) $/msx
90 && any { $_ eq ( $File::Find::name =~ m/[.] ( [[:alnum:]]{1,}) $/msx )[0] } @file_suffices )
91 {
92 push @files, $File::Find::name;
93 $log->debugf( 'Take tags from file: %s', $File::Find::name );
94 }
95 }
96 },
97 },
98 @dirs,
99 );
100 $log->info('Process tags ...');
101 $ptag->process(
102 files => \@files,
103 refresh => 1,
104 );
105  
106 $log->info('Saving tags ...');
107 $log->infof( 'Total %s tags in %s files.', scalar %{ $ptag->{tags} }, scalar %{ $ptag->{seen} } );
108  
109 $ptag->output( outfile => $outfile );
110 if ( $git->get_config( $CFG => 'verbose' ) ) {
111 print {*STDOUT} sprintf '[%s] Discovered total %s tags in %s files. Written to file \'%s\'.' . "\n",
112 __PACKAGE__, scalar %{ $ptag->{tags} }, scalar %{ $ptag->{seen} }, $outfile
113 or croak 'Cannot print';
114 }
115  
116 return;
117 }
118  
119 # Install hooks
120  
121 POST_CHECKOUT( \&update_tags_file );
122 POST_MERGE( \&update_tags_file );
123 POST_APPLYPATCH( \&update_tags_file );
124 POST_COMMIT( \&update_tags_file );
125  
126 1;
127  
128 __END__