| 1 | package Dist::Zilla::Plugin::ATOOMIC::TidyAll; | ||
| 2 | |||
| 3 | use v5.10; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use autodie; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | our $VERSION = '1.00'; | ||
| 11 | |||
| 12 | use Code::TidyAll::Config::INI::Reader 0.44; | ||
| 13 | use List::Util 1.45 qw( uniqstr ); | ||
| 14 | use Path::Class qw( file ); | ||
| 15 | use Path::Iterator::Rule; | ||
| 16 | use Perl::Critic::Moose 1.05; | ||
| 17 | use Sort::ByExample qw( sbe ); | ||
| 18 | |||
| 19 | use Moose; | ||
| 20 | |||
| 21 | has sections => ( | ||
| 22 | is => 'ro', | ||
| 23 | isa => 'ArrayRef[Str]', | ||
| 24 | default => sub { [] }, | ||
| 25 | ); | ||
| 26 | |||
| 27 | has stopwords_file => ( | ||
| 28 | is => 'ro', | ||
| 29 | isa => 'Str', | ||
| 30 | predicate => '_has_stopwords_file', | ||
| 31 | ); | ||
| 32 | |||
| 33 | sub mvp_multivalue_args {'sections'} | ||
| 34 | |||
| 35 | with qw( | ||
| 36 | Dist::Zilla::Role::BeforeBuild | ||
| 37 | Dist::Zilla::Role::TextTemplate | ||
| 38 | ); | ||
| 39 | |||
| 40 | sub before_build { | ||
| 41 | my $self = shift; | ||
| 42 | |||
| 43 | file('tidyall.ini')->spew( $self->_tidyall_ini_content ); | ||
| 44 | |||
| 45 | $self->_maybe_write_file( 'perltidyrc', $self->_perltidyrc ); | ||
| 46 | $self->_maybe_write_file( 'perlcriticrc', $self->_perlcriticrc ); | ||
| 47 | |||
| 48 | return; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _tidyall_ini_content { | ||
| 52 | my $self = shift; | ||
| 53 | |||
| 54 | return $self->_new_tidyall_ini | ||
| 55 | unless -e 'tidyall.ini'; | ||
| 56 | |||
| 57 | return $self->_munged_tidyall_ini; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _new_tidyall_ini { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | my $perl_select = ['**/*.{pl,pm,t,psgi}']; | ||
| 64 | my %tidyall = ( | ||
| 65 | q{_} => { ignore => $self->_default_perl_ignore }, | ||
| 66 | 'PerlTidy' => { select => $perl_select }, | ||
| 67 | 'PerlCritic' => { select => $perl_select }, | ||
| 68 | 'Test::Vars' => { select => ['**/*.pm'] }, | ||
| 69 | ); | ||
| 70 | |||
| 71 | if ( $self->_has_stopwords_file ) { | ||
| 72 | $tidyall{'SortLines::Naturally'} | ||
| 73 | = { select => $self->stopwords_file }; | ||
| 74 | } | ||
| 75 | |||
| 76 | for my $line ( @{ $self->sections } ) { | ||
| 77 | my ( $section, $config ) = split /\s*:\s*/, $line, 2; | ||
| 78 | my ( $key, $val ) = split /\s*=\s*/, $config, 2; | ||
| 79 | $tidyall{$section}{$key} //= []; | ||
| 80 | push @{ $tidyall{$section}{$key} }, $val; | ||
| 81 | } | ||
| 82 | |||
| 83 | return $self->_config_to_ini( \%tidyall ); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub _munged_tidyall_ini { | ||
| 87 | my $self = shift; | ||
| 88 | |||
| 89 | my $tidyall | ||
| 90 | = Code::TidyAll::Config::INI::Reader->read_file('tidyall.ini'); | ||
| 91 | |||
| 92 | return $self->_config_to_ini($tidyall); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _default_perl_ignore { | ||
| 96 | my $self = shift; | ||
| 97 | |||
| 98 | my @ignore = qw( | ||
| 99 | .build/**/* | ||
| 100 | blib/**/* | ||
| 101 | t/00-* | ||
| 102 | t/author-* | ||
| 103 | t/release-* | ||
| 104 | t/zzz-* | ||
| 105 | xt/**/* | ||
| 106 | ); | ||
| 107 | |||
| 108 | my $dist = $self->zilla->name; | ||
| 109 | push @ignore, "$dist-*/**/*"; | ||
| 110 | |||
| 111 | if ( grep { $_->plugin_name =~ /\bConflicts/ } | ||
| 112 | @{ $self->zilla->plugins } ) { | ||
| 113 | |||
| 114 | my $conflicts_dir = $self->zilla->name =~ s{-}{/}gr; | ||
| 115 | push @ignore, "lib/$conflicts_dir/Conflicts.pm"; | ||
| 116 | } | ||
| 117 | |||
| 118 | return \@ignore; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub _config_to_ini { | ||
| 122 | my $self = shift; | ||
| 123 | my $tidyall = shift; | ||
| 124 | |||
| 125 | my @xt_files = Path::Iterator::Rule->new->file->name(qr/\.t$/)->all('xt'); | ||
| 126 | |||
| 127 | if (@xt_files) { | ||
| 128 | my $suffix = 'non-auto-generated xt'; | ||
| 129 | for my $plugin (qw( PerlCritic PerlTidy )) { | ||
| 130 | $tidyall->{ $plugin . q{ } . $suffix }{select} = \@xt_files; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | for my $section ( keys %{$tidyall} ) { | ||
| 135 | if ( $section =~ /PerlTidy/ ) { | ||
| 136 | $tidyall->{$section}{argv} = '--profile=$ROOT/perltidyrc'; | ||
| 137 | } | ||
| 138 | elsif ( $section =~ /PerlCritic/ ) { | ||
| 139 | $tidyall->{$section}{argv} = '--profile=$ROOT/perlcriticrc'; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | my $sorter = sbe( | ||
| 144 | [ 'select', 'ignore' ], | ||
| 145 | { | ||
| 146 | fallback => sub { $_[0] cmp $_[1] }, | ||
| 147 | }, | ||
| 148 | ); | ||
| 149 | |||
| 150 | my $ini = q{}; | ||
| 151 | for my $section ( q{_}, sort grep { $_ ne q{_} } keys %{$tidyall} ) { | ||
| 152 | next unless keys %{ $tidyall->{$section} }; | ||
| 153 | |||
| 154 | if ( $section ne q{_} ) { | ||
| 155 | $ini .= "[$section]\n"; | ||
| 156 | } | ||
| 157 | |||
| 158 | for my $key ( $sorter->( keys %{ $tidyall->{$section} } ) ) { | ||
| 159 | for my $val ( | ||
| 160 | sort ref $tidyall->{$section}{$key} | ||
| 161 | ? @{ $tidyall->{$section}{$key} } | ||
| 162 | : $tidyall->{$section}{$key} | ||
| 163 | ) { | ||
| 164 | |||
| 165 | $ini .= "$key = $val\n"; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | $ini .= "\n"; | ||
| 170 | } | ||
| 171 | |||
| 172 | chomp $ini; | ||
| 173 | |||
| 174 | return $ini; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub _maybe_write_file { | ||
| 178 | my $self = shift; | ||
| 179 | my $file = shift; | ||
| 180 | my $content = shift; | ||
| 181 | |||
| 182 | return if -e $file; | ||
| 183 | |||
| 184 | file($file)->spew($content); | ||
| 185 | |||
| 186 | return; | ||
| 187 | } | ||
| 188 | |||
| 189 | my $perltidyrc = <<'EOF'; | ||
| 190 | -l=78 | ||
| 191 | -i=4 | ||
| 192 | -ci=4 | ||
| 193 | -se | ||
| 194 | -b | ||
| 195 | -bar | ||
| 196 | -boc | ||
| 197 | -vt=0 | ||
| 198 | -vtc=0 | ||
| 199 | -cti=0 | ||
| 200 | -pt=1 | ||
| 201 | -bt=1 | ||
| 202 | -sbt=1 | ||
| 203 | -bbt=1 | ||
| 204 | -nolq | ||
| 205 | -npro | ||
| 206 | -nsfs | ||
| 207 | --blank-lines-before-packages=0 | ||
| 208 | --opening-hash-brace-right | ||
| 209 | --no-outdent-long-comments | ||
| 210 | --iterations=2 | ||
| 211 | -wbb="% + - * / x != == >= <= =~ !~ < > | & >= < = **= += *= &= <<= &&= -= /= |= >>= ||= .= %= ^= x=" | ||
| 212 | EOF | ||
| 213 | |||
| 214 | sub _perltidyrc {$perltidyrc} | ||
| 215 | |||
| 216 | my $perlcriticrc = <<'EOF'; | ||
| 217 | severity = 3 | ||
| 218 | verbose = 11 | ||
| 219 | theme = (core && (pbp || bugs || maintenance || cosmetic || complexity || security || tests)) || moose | ||
| 220 | program-extensions = pl psgi t | ||
| 221 | |||
| 222 | exclude = Subroutines::ProhibitCallsToUndeclaredSubs | ||
| 223 | |||
| 224 | [BuiltinFunctions::ProhibitStringySplit] | ||
| 225 | severity = 3 | ||
| 226 | |||
| 227 | [CodeLayout::RequireTrailingCommas] | ||
| 228 | severity = 3 | ||
| 229 | |||
| 230 | [ControlStructures::ProhibitCStyleForLoops] | ||
| 231 | severity = 3 | ||
| 232 | |||
| 233 | [InputOutput::RequireCheckedSyscalls] | ||
| 234 | functions = :builtins | ||
| 235 | exclude_functions = sleep | ||
| 236 | severity = 3 | ||
| 237 | |||
| 238 | [RegularExpressions::ProhibitComplexRegexes] | ||
| 239 | max_characters = 200 | ||
| 240 | |||
| 241 | [RegularExpressions::ProhibitUnusualDelimiters] | ||
| 242 | severity = 3 | ||
| 243 | |||
| 244 | [Subroutines::ProhibitUnusedPrivateSubroutines] | ||
| 245 | private_name_regex = _(?!build)\w+ | ||
| 246 | |||
| 247 | [TestingAndDebugging::ProhibitNoWarnings] | ||
| 248 | allow = redefine | ||
| 249 | |||
| 250 | [ValuesAndExpressions::ProhibitEmptyQuotes] | ||
| 251 | severity = 3 | ||
| 252 | |||
| 253 | [ValuesAndExpressions::ProhibitInterpolationOfLiterals] | ||
| 254 | severity = 3 | ||
| 255 | |||
| 256 | [ValuesAndExpressions::RequireUpperCaseHeredocTerminator] | ||
| 257 | severity = 3 | ||
| 258 | |||
| 259 | [Variables::ProhibitPackageVars] | ||
| 260 | add_packages = Carp Test::Builder | ||
| 261 | |||
| 262 | [-Subroutines::RequireFinalReturn] | ||
| 263 | |||
| 264 | # This incorrectly thinks signatures are prototypes. | ||
| 265 | [-Subroutines::ProhibitSubroutinePrototypes] | ||
| 266 | |||
| 267 | [-ErrorHandling::RequireCarping] | ||
| 268 | |||
| 269 | # No need for /xsm everywhere | ||
| 270 | [-RegularExpressions::RequireDotMatchAnything] | ||
| 271 | [-RegularExpressions::RequireExtendedFormatting] | ||
| 272 | [-RegularExpressions::RequireLineBoundaryMatching] | ||
| 273 | |||
| 274 | # http://stackoverflow.com/questions/2275317/why-does-perlcritic-dislike-using-shift-to-populate-subroutine-variables | ||
| 275 | [-Subroutines::RequireArgUnpacking] | ||
| 276 | |||
| 277 | # "use v5.14" is more readable than "use 5.014" | ||
| 278 | [-ValuesAndExpressions::ProhibitVersionStrings] | ||
| 279 | |||
| 280 | # Explicitly returning undef is a _good_ thing in many cases, since it | ||
| 281 | # prevents very common errors when using a sub in list context to construct a | ||
| 282 | # hash and ending up with a missing value or key. | ||
| 283 | [-Subroutines::ProhibitExplicitReturnUndef] | ||
| 284 | |||
| 285 | # Sometimes I want to write "return unless $x > 4" | ||
| 286 | [-ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions] | ||
| 287 | EOF | ||
| 288 | |||
| 289 | sub _perlcriticrc {$perlcriticrc} | ||
| 290 | |||
| 291 | __PACKAGE__->meta->make_immutable; | ||
| 292 | |||
| 293 | 1; | ||
| 294 | |||
| 295 | # ABSTRACT: Creates default tidyall.ini, perltidyrc, and perlcriticrc files if they don't yet exist | ||
| 296 | |||
| 297 | __END__ |