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