| 1 | package App::GitHooks::Plugin::RubyCompile; | ||||||||||
| 2 | |||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 8 | ▶ | use base 'App::GitHooks::Plugin'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | # External dependencies. | ||||||||||
| 9 | 8 | ▶ | use System::Command; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | # Internal dependencies. | ||||||||||
| 12 | 8 | ▶ | use App::GitHooks::Constants qw( :PLUGIN_RETURN_CODES ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | |||||||||||
| 15 | =head1 NAME | ||||||||||
| 16 | |||||||||||
| 17 | App::GitHooks::Plugin::RubyCompile - Verify that staged Ruby files compile. | ||||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||
| 21 | |||||||||||
| 22 | Verify that staged Ruby files compile by running ruby -c against them. | ||||||||||
| 23 | |||||||||||
| 24 | Warnings are suppressed with -W0. | ||||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | =head1 VERSION | ||||||||||
| 28 | |||||||||||
| 29 | Version 1.0.0 | ||||||||||
| 30 | |||||||||||
| 31 | =cut | ||||||||||
| 32 | |||||||||||
| 33 | 8 | our $VERSION = '1.0.0'; | |||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | =head1 METHODS | ||||||||||
| 37 | |||||||||||
| 38 | =head2 get_file_pattern() | ||||||||||
| 39 | |||||||||||
| 40 | Return a pattern to filter the files this plugin should analyze. | ||||||||||
| 41 | |||||||||||
| 42 | my $file_pattern = App::GitHooks::Plugin::RubyCompile->get_file_pattern( | ||||||||||
| 43 | app => $app, | ||||||||||
| 44 | ); | ||||||||||
| 45 | |||||||||||
| 46 | =cut | ||||||||||
| 47 | |||||||||||
| 48 | sub get_file_pattern | ||||||||||
| 49 | 6 | ▶ | { | ||||||||
|
Subroutine
get_file_pattern: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 50 | 6 | return qr/\.(?:rb)$/x; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | |||||||||||
| 54 | =head2 get_file_check_description() | ||||||||||
| 55 | |||||||||||
| 56 | Return a description of the check performed on files by the plugin and that | ||||||||||
| 57 | will be displayed to the user, if applicable, along with an indication of the | ||||||||||
| 58 | success or failure of the plugin. | ||||||||||
| 59 | |||||||||||
| 60 | my $description = App::GitHooks::Plugin::RubyCompile->get_file_check_description(); | ||||||||||
| 61 | |||||||||||
| 62 | =cut | ||||||||||
| 63 | |||||||||||
| 64 | sub get_file_check_description | ||||||||||
| 65 | 5 | ▶ | { | ||||||||
|
Subroutine
get_file_check_description: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 66 | 5 | return 'The file passes ruby -c'; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | =head2 run_pre_commit_file() | ||||||||||
| 71 | |||||||||||
| 72 | Code to execute for each file as part of the pre-commit hook. | ||||||||||
| 73 | |||||||||||
| 74 | my $success = App::GitHooks::Plugin::RubyCompile->run_pre_commit_file(); | ||||||||||
| 75 | |||||||||||
| 76 | =cut | ||||||||||
| 77 | |||||||||||
| 78 | sub run_pre_commit_file | ||||||||||
| 79 | 2 | ▶ | { | ||||||||
|
Subroutine
run_pre_commit_file: called CC=4 SCAR=19.3
Pod
documented
| |||||||||||
| 80 | 2 | my ( $class, %args ) = @_; | |||||||||
| 81 | 2 | my $file = delete( $args{'file'} ); | |||||||||
| 82 | 2 | my $git_action = delete( $args{'git_action'} ); | |||||||||
| 83 | 2 | my $app = delete( $args{'app'} ); | |||||||||
| 84 | 2 | my $staged_changes = $app->get_staged_changes(); | |||||||||
| 85 | 2 | my $repository = $app->get_repository(); | |||||||||
| 86 | |||||||||||
| 87 | # Ignore deleted files. | ||||||||||
| 88 | 2 | ▶ | return $PLUGIN_RETURN_SKIPPED | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | if $git_action eq 'D'; | ||||||||||
| 90 | |||||||||||
| 91 | # Execute ruby -c. | ||||||||||
| 92 | 2 | my $path = $repository->work_tree() . '/' . $file; | |||||||||
| 93 | 2 | my ( $pid, $stdin, $stdout, $stderr ) = System::Command->spawn( 'ruby', '-W0', '-c', $path ); | |||||||||
| 94 | |||||||||||
| 95 | # Retrieve the output. | ||||||||||
| 96 | 0 | chomp( my $message_out = do { local $/ = undef; <$stdout> } ); | |||||||||
| 97 | |||||||||||
| 98 | # Raise an exception if we didn't get "syntax OK". | ||||||||||
| 99 | 0 | ▶ | if ( $message_out !~ /^Syntax\ OK$/x ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | my @warnings = <$stderr>; | |||||||||
| 101 | 0 | foreach my $warning ( @warnings ) { | |||||||||
| 102 | 0 | chomp( $warning ); | |||||||||
| 103 | 0 | $warning =~ s/^\Q$path\E:/Line /; | |||||||||
| 104 | } | ||||||||||
| 105 | 0 | die join( "\n", @warnings ) . "\n"; | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 0 | return $PLUGIN_RETURN_PASSED; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | =head1 BUGS | ||||||||||
| 112 | |||||||||||
| 113 | Please report any bugs or feature requests through the web interface at | ||||||||||
| 114 | L<https://github.com/jacobmaurer/App-GitHooks-Plugin-RubyCompile/issues/new>. | ||||||||||
| 115 | I will be notified, and then you'll automatically be notified of progress on | ||||||||||
| 116 | your bug as I make changes. | ||||||||||
| 117 | |||||||||||
| 118 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 119 | |||||||||||
| 120 | Big thanks to Guillaume Aubert! | ||||||||||
| 121 | |||||||||||
| 122 | =head1 SUPPORT | ||||||||||
| 123 | |||||||||||
| 124 | You can find documentation for this module with the perldoc command. | ||||||||||
| 125 | |||||||||||
| 126 | perldoc App::GitHooks::Plugin::RubyCompile | ||||||||||
| 127 | |||||||||||
| 128 | |||||||||||
| 129 | You can also look for information at: | ||||||||||
| 130 | |||||||||||
| 131 | =over | ||||||||||
| 132 | |||||||||||
| 133 | =item * GitHub's request tracker | ||||||||||
| 134 | |||||||||||
| 135 | L<https://github.com/jacobmaurer/App-GitHooks-Plugin-RubyCompile/issues> | ||||||||||
| 136 | |||||||||||
| 137 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 138 | |||||||||||
| 139 | L<http://annocpan.org/dist/app-githooks-plugin-RubyCompile> | ||||||||||
| 140 | |||||||||||
| 141 | =item * CPAN Ratings | ||||||||||
| 142 | |||||||||||
| 143 | L<http://cpanratings.perl.org/d/app-githooks-plugin-RubyCompile> | ||||||||||
| 144 | |||||||||||
| 145 | =item * MetaCPAN | ||||||||||
| 146 | |||||||||||
| 147 | L<https://metacpan.org/release/App-GitHooks-Plugin-RubyCompile> | ||||||||||
| 148 | |||||||||||
| 149 | =back | ||||||||||
| 150 | |||||||||||
| 151 | |||||||||||
| 152 | =head1 AUTHOR | ||||||||||
| 153 | |||||||||||
| 154 | L<Jacob Maurer|https://metacpan.org/author/JMAURER>, | ||||||||||
| 155 | C<< <jmaurer at cpan.org> >>. | ||||||||||
| 156 | |||||||||||
| 157 | |||||||||||
| 158 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 159 | |||||||||||
| 160 | Copyright 2013-2014 Jacob Maurer. | ||||||||||
| 161 | |||||||||||
| 162 | This program is free software: you can redistribute it and/or modify it under | ||||||||||
| 163 | the terms of the GNU General Public License version 3 as published by the Free | ||||||||||
| 164 | Software Foundation. | ||||||||||
| 165 | |||||||||||
| 166 | This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||||||||||
| 167 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||||||||||
| 168 | PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||||||||||
| 169 | |||||||||||
| 170 | You should have received a copy of the GNU General Public License along with | ||||||||||
| 171 | this program. If not, see http://www.gnu.org/licenses/ | ||||||||||
| 172 | |||||||||||
| 173 | =cut | ||||||||||
| 174 | |||||||||||
| 175 | 8 | 1; | |||||||||