1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
package Perl::Critic::ProfilePrototype;
use 5.010001;
use strict;
use warnings;
use Perl::Critic::Config qw{};
use Perl::Critic::Policy qw{};
use Perl::Critic::Utils qw{ :characters };
use overload ( q{""} => 'to_string' );
our $VERSION = '1.156';
#-----------------------------------------------------------------------------
sub new {
my ($class, %args) = @_;
my $self = bless {}, $class;
my $policies = $args{-policies} || [];
$self->{_policies} = [ sort _by_type @{ $policies } ];
$self->{_comment_out_parameters} = $args{'-comment-out-parameters'} // 1;
my $configuration = $args{'-config'};
if (not $configuration) {
$configuration = Perl::Critic::Config->new(-profile => $EMPTY);
}
$self->{_configuration} = $configuration;
return $self;
}
#-----------------------------------------------------------------------------
sub _get_policies {
my ($self) = @_;
return $self->{_policies};
}
sub _comment_out_parameters {
my ($self) = @_;
return $self->{_comment_out_parameters};
}
sub _configuration {
my ($self) = @_;
return $self->{_configuration};
}
#-----------------------------------------------------------------------------
sub _line_prefix {
my ($self) = @_;
return $self->_comment_out_parameters() ? q{# } : $EMPTY;
}
#-----------------------------------------------------------------------------
sub to_string {
my ($self) = @_;
my $prefix = $self->_line_prefix();
my $configuration = $self->_configuration();
my $prototype = "# Globals\n";
$prototype .= $prefix;
$prototype .= q{severity = };
$prototype .= $configuration->severity();
$prototype .= "\n";
$prototype .= $prefix;
$prototype .= q{force = };
$prototype .= $configuration->force();
$prototype .= "\n";
$prototype .= $prefix;
$prototype .= q{only = };
$prototype .= $configuration->only();
$prototype .= "\n";
$prototype .= $prefix;
$prototype .= q{allow-unsafe = };
$prototype .= $configuration->unsafe_allowed();
$prototype .= "\n";
$prototype .= $prefix;
$prototype .= q{profile-strictness = };
$prototype .= $configuration->profile_strictness();
$prototype .= "\n";
$prototype .= $prefix;
$prototype .= q{color = };
$prototype .= $configuration->color();
$prototype .= "\n";
$prototype .= $prefix;
$prototype .= q{pager = };
$prototype .= $configuration->pager();
$prototype .= "\n";
$prototype .= $prefix;
$prototype .= q{top = };
$prototype .= $configuration->top();
$prototype .= "\n";
$prototype .= $prefix;
$prototype .= q{verbose = };
$prototype .= $configuration->verbose();
$prototype .= "\n";
$prototype .= $prefix;
$prototype .= q{include = };
$prototype .= join $SPACE, $configuration->include();
$prototype .= "\n";
$prototype .= $prefix;
$prototype .= q{exclude = };
$prototype .= join $SPACE, $configuration->exclude();
$prototype .= "\n";
$prototype .= $prefix;
$prototype .= q{single-policy = };
$prototype .= join $SPACE, $configuration->single_policy();
$prototype .= "\n";
$prototype .= $prefix;
$prototype .= q{theme = };
$prototype .= $configuration->theme()->rule();
$prototype .= "\n";
foreach my $item (qw<
color-severity-highest
color-severity-high
color-severity-medium
color-severity-low
color-severity-lowest
>) {
( my $accessor = $item ) =~ s/ - /_/gmsx;
$prototype .= $prefix;
$prototype .= "$item = ";
$prototype .= $configuration->$accessor;
$prototype .= "\n";
}
$prototype .= $prefix;
$prototype .= q{program-extensions = };
$prototype .= join $SPACE, $configuration->program_extensions();
Perl::Critic::Policy::set_format( $self->_proto_format() );
my $policy_prototypes = join qq{\n}, map { "$_" } @{ $self->_get_policies() };
$policy_prototypes =~ s/\s+ \z//xms; # Trim trailing whitespace
return $prototype . "\n\n" . $policy_prototypes . "\n";
}
#-----------------------------------------------------------------------------
# About "%{\\n%\\x7b# \\x7df\n${prefix}%n = %D\\n}O" below:
#
# The %0 format for a policy specifies how to format parameters.
# For a parameter %f specifies the full description.
#
# The problem is that both of these need to take options, but String::Format
# doesn't allow nesting of {}. So, to get the option to the %f, the braces
# are hex encoded. I.e., assuming that comment_out_parameters is in effect,
# the parameter sees:
#
# \n%{# }f\n# %n = %D\n
sub _proto_format {
my ($self) = @_;
my $prefix = $self->_line_prefix();
return <<"END_OF_FORMAT";
# %a
[%p]
${prefix}set_themes = %t
${prefix}add_themes =
${prefix}severity = %s
${prefix}maximum_violations_per_document = %v
%{\\n%\\x7b# \\x7df\\n${prefix}%n = %D\\n}O%{${prefix}Cannot programmatically discover what parameters this policy takes.\\n}U
END_OF_FORMAT
}
#-----------------------------------------------------------------------------
sub _by_type { return ref $a cmp ref $b }
1;
__END__
=pod
=head1 NAME
Perl::Critic::ProfilePrototype - Generate an initial Perl::Critic profile.
=head1 DESCRIPTION
This is a helper class that generates a prototype of a
L<Perl::Critic|Perl::Critic> profile (e.g. a F<.perlcriticrc> file).
There are no user-serviceable parts here.
=head1 INTERFACE SUPPORT
This is considered to be a non-public class. Its interface is subject
to change without notice.
=head1 CONSTRUCTOR
=over
=item C<< new( -policies => \@POLICY_OBJECTS ) >>
Returns a reference to a new C<Perl::Critic::ProfilePrototype> object.
=back
=head1 METHODS
=over
=item to_string()
Returns a string representation of this C<ProfilePrototype>. See
L<"OVERLOADS"> for more information.
=back
=head1 OVERLOADS
When a
L<Perl::Critic::ProfilePrototype|Perl::Critic::ProfilePrototype> is
evaluated in string context, it produces a multi-line summary of the
policy name, default themes, and default severity for each
L<Perl::Critic::Policy|Perl::Critic::Policy> object that was given to
the constructor of this C<ProfilePrototype>. If the Policy supports
an additional parameters, they will also be listed (but
commented-out). The format is suitable for use as a F<.perlcriticrc>
file.
=head1 AUTHOR
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
=head1 COPYRIGHT
Copyright (c) 2005-2023 Imaginative Software Systems
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. The full text of this license
can be found in the LICENSE file included with this module.
=cut
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 78
# indent-tabs-mode: nil
# c-indentation-style: bsd
# End:
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
|