Subject: | SVN::Hooks::PerlCritic |
Hi Gustavo,
As I use SVN::Hooks at work, i've started to write a small plugin for
Perl::Critic. Now You can use perlcritic as an hook for post and
pre-commits.
CHECK_PERLCRITIC_PRE_COMMIT(-severity => 'stern' );
CHECK_PERLCRITIC_POST_COMMIT(-severity => 'brutal', -theme => 'bugs');
Would you like to include this hook in the SVN::Hooks distribution?
Is the code clean enough and well documented?
Best Regards,
Subject: | PerlCritic.pm |
package SVN::Hooks::PerlCritic;
use strict;
use warnings;
use Carp;
use SVN::Hooks;
use SVN::Notify;
use Perl::Critic;
use Exporter qw/import/;
our @EXPORT = qw/CHECK_PERLCRITIC_PRE_COMMIT CHECK_PERLCRITIC_POST_COMMIT/;
our $VERSION = $SVN::Hooks::VERSION;
=head1 NAME
SVN::Hooks::PerlCritic - Check files with Perl::Critic in commits.
=head1 SYNOPSIS
This SVN::Hooks plugin allows one to check if the files in a
'svn commit' conforms to Perl::Critic Policy.
It's configured by the following directive.
=head2 CHECK_PERLCRITIC_PRE_COMMIT({OPT => VALUE, ...})
It's active in the C<pre-commit> hook.
=head2 CHECK_PERLCRITIC_POST_COMMIT({OPT => VALUE, ...})
It's active in the C<post-commit> hook.
The available options are the following:
=over
=item B<-profile> is a path to a configuration file.
=item B<-severity> is the minimum severity level.
=item B<-theme> is special expression that determines which Policies to apply based on their respective themes.
=item B<-include> is a reference to a list of string @PATTERNS.
=item B<-exclude> is a reference to a list of string @PATTERNS.
=item B<-single-policy> is a string PATTERN. Only one policy that matches m/$PATTERN/ixms will be used.
=item B<-force> is a boolean value that controls whether Perl::Critic observes the magical "## no critic"
=item B<-verbose> can be a positive integer (from 1 to 11), or a literal format specification.
=back
See L<Perl::Critic> for further explanation.
CHECK_PERLCRITIC_PRE_COMMIT(
-severity => 'stern',
-theme => 'bugs'
);
CHECK_PERLCRITIC_POST_COMMIT(
-severity => 'brutal',
-theme => 'bugs'
);
CHECK_PERLCRITIC_POST_COMMIT(
-severity => 'brutal',
-verbose => 11
);
=cut
sub CHECK_PERLCRITIC_PRE_COMMIT {
my (%opts) = @_;
my $conf = $SVN::Hooks::Confs->{CHECK_PERLCRITIC_PRE_COMMIT};
$conf->{"opts"} = \%opts;
$conf->{'pre-commit'} = \&hook;
return 1;
}
sub CHECK_PERLCRITIC_POST_COMMIT {
my (%opts) = @_;
my $conf = $SVN::Hooks::Confs->{CHECK_PERLCRITIC_POST_COMMIT};
$conf->{"opts"} = \%opts;
$conf->{'post-commit'} = \&hook;
return 1;
}
$SVN::Hooks::Inits{CHECK_PERLCRITIC_PRE_COMMIT} = sub {
return {}
};
$SVN::Hooks::Inits{CHECK_PERLCRITIC_POST_COMMIT} = sub {
return {}
};
sub hook {
my ($self, $svnlook) = @_;
my $critic = Perl::Critic->new(%{$self->{opts}});
my @errors;
foreach my $modified ($svnlook->added(), $svnlook->updated()) {
next if $modified =~ m:/$:; # disregard directories
next if $modified !~ m/\.p(l|m)/; # match only perl source code
my $content = $svnlook->cat($modified);
my @violations = $critic->critique(\$content);
foreach my $violation (@violations)
{
push @errors, "$modified: ".$violation->to_string();
}
}
if (@errors) {
my $message = "PERL CRITIC\n" . join("", @errors);
croak $message;
}
return;
}
=head1 AUTHOR
Christophe Nowicki, C<< <cscm@csquad.org> >>
=head1 BUGS
Please report any bugs or feature requests to
C<bug-svn-hooks-checkproperty at rt.cpan.org>, or through the web
interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SVN-Hooks>. I will
be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc SVN::Hooks
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SVN-Hooks>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/SVN-Hooks>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/SVN-Hooks>
=item * Search CPAN
L<http://search.cpan.org/dist/SVN-Hooks>
=back
=head1 COPYRIGHT & LICENSE
Copyright 2008-2009 CPqD, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1; # End of SVN::Hooks::PerlCritic