| 1 | package Dist::Zilla::Plugin::DROLSKY::Precious; | ||
| 2 | |||
| 3 | use v5.10; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use autodie; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | our $VERSION = '1.25'; | ||
| 11 | |||
| 12 | use Path::Tiny qw( path ); | ||
| 13 | use Path::Tiny::Rule; | ||
| 14 | use Sort::ByExample qw( sbe ); | ||
| 15 | |||
| 16 | use Moose; | ||
| 17 | |||
| 18 | has stopwords_file => ( | ||
| 19 | is => 'ro', | ||
| 20 | isa => 'Str', | ||
| 21 | predicate => '_has_stopwords_file', | ||
| 22 | ); | ||
| 23 | |||
| 24 | with qw( | ||
| 25 | Dist::Zilla::Role::BeforeBuild | ||
| 26 | Dist::Zilla::Role::TextTemplate | ||
| 27 | ); | ||
| 28 | |||
| 29 | sub before_build { | ||
| 30 | my $self = shift; | ||
| 31 | |||
| 32 | path('precious.toml')->spew_utf8( $self->_precious_toml_content ); | ||
| 33 | |||
| 34 | return; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub _precious_toml_content { | ||
| 38 | my $self = shift; | ||
| 39 | |||
| 40 | return $self->_new_precious_toml | ||
| 41 | unless -e 'precious.toml'; | ||
| 42 | |||
| 43 | return $self->_munged_precious_toml; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _new_precious_toml { | ||
| 47 | my $self = shift; | ||
| 48 | |||
| 49 | my $perl_include = ['**/*.{pl,pm,t,psgi}']; | ||
| 50 | my %precious = ( | ||
| 51 | q{_} => { exclude => $self->_default_perl_exclude }, | ||
| 52 | 'commands.perlcritic' => { | ||
| 53 | type => 'lint', | ||
| 54 | include => $perl_include, | ||
| 55 | cmd => [qw( perlcritic --profile=$PRECIOUS_ROOT/perlcriticrc )], | ||
| 56 | ok_exit_codes => 0, | ||
| 57 | lint_failure_exit_codes => 2, | ||
| 58 | }, | ||
| 59 | 'commands.perltidy' => { | ||
| 60 | type => 'both', | ||
| 61 | include => $perl_include, | ||
| 62 | cmd => [qw( perltidy --profile=$PRECIOUS_ROOT/perltidyrc )], | ||
| 63 | lint_flags => [ | ||
| 64 | '--assert-tidy', | ||
| 65 | '--no-standard-output', | ||
| 66 | '--outfile=/dev/null' | ||
| 67 | ], | ||
| 68 | tidy_flags => [ | ||
| 69 | qw( --backup-and-modify-in-place --backup-file-extension=/ )], | ||
| 70 | ok_exit_codes => 0, | ||
| 71 | lint_failure_exit_codes => 2, | ||
| 72 | |||
| 73 | # I think this is really only true for linting mode, but perltidy | ||
| 74 | # is so complicated it's hard for me to tell. | ||
| 75 | ignore_stderr => 'Begin Error Output Stream', | ||
| 76 | }, | ||
| 77 | 'commands.omegasort-gitignore' => { | ||
| 78 | type => 'both', | ||
| 79 | include => '**/.gitignore', | ||
| 80 | cmd => [qw( omegasort --sort=path )], | ||
| 81 | lint_flags => '--check', | ||
| 82 | tidy_flags => '--in-place', | ||
| 83 | ok_exit_codes => 0, | ||
| 84 | lint_failure_exit_codes => 1, | ||
| 85 | }, | ||
| 86 | 'commands.podchecker' => { | ||
| 87 | type => 'lint', | ||
| 88 | include => ['**/*.{pl,pm,pod}'], | ||
| 89 | cmd => [ 'podchecker', '--warnings', '--warnings' ], | ||
| 90 | ok_exit_codes => [ 0, 2 ], | ||
| 91 | lint_failure_exit_codes => 1, | ||
| 92 | |||
| 93 | # podchecker will print a warning to stderr if a file has no POD at all. | ||
| 94 | ignore_stderr => [ | ||
| 95 | '.+ pod syntax OK', | ||
| 96 | '.+ does not contain any pod commands', | ||
| 97 | ] | ||
| 98 | }, | ||
| 99 | 'commands.podtidy' => { | ||
| 100 | type => 'tidy', | ||
| 101 | include => ['**/*.{pl,pm,pod}'], | ||
| 102 | cmd => | ||
| 103 | [ 'podtidy', '--columns', '80', '--inplace', '--nobackup' ], | ||
| 104 | ok_exit_codes => 0, | ||
| 105 | lint_failure_exit_codes => 1, | ||
| 106 | }, | ||
| 107 | ); | ||
| 108 | |||
| 109 | if ( $self->_has_stopwords_file ) { | ||
| 110 | $precious{'commands.omegasort-stopwords'} = { | ||
| 111 | type => 'both', | ||
| 112 | include => $self->stopwords_file, | ||
| 113 | cmd => [qw( omegasort --sort=text --case-insensitive )], | ||
| 114 | lint_flags => '--check', | ||
| 115 | tidy_flags => '--in-place', | ||
| 116 | ok_exit_codes => 0, | ||
| 117 | lint_failure_exit_codes => 1, | ||
| 118 | }; | ||
| 119 | } | ||
| 120 | |||
| 121 | return $self->_config_to_toml( \%precious ); | ||
| 122 | } | ||
| 123 | |||
| 124 | sub _munged_precious_toml { | ||
| 125 | my $self = shift; | ||
| 126 | |||
| 127 | return path('precious.toml')->slurp_utf8; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub _default_perl_exclude { | ||
| 131 | my $self = shift; | ||
| 132 | |||
| 133 | my @exclude = qw( | ||
| 134 | .build/**/* | ||
| 135 | blib/**/* | ||
| 136 | t/00-* | ||
| 137 | t/author-* | ||
| 138 | t/release-* | ||
| 139 | t/zzz-* | ||
| 140 | xt/**/* | ||
| 141 | ); | ||
| 142 | |||
| 143 | my $dist = $self->zilla->name; | ||
| 144 | push @exclude, "$dist-*/**/*"; | ||
| 145 | |||
| 146 | if ( grep { $_->plugin_name =~ /\bConflicts/ } | ||
| 147 | @{ $self->zilla->plugins } ) { | ||
| 148 | |||
| 149 | my $conflicts_dir = $self->zilla->name =~ s{-}{/}gr; | ||
| 150 | push @exclude, "lib/$conflicts_dir/Conflicts.pm"; | ||
| 151 | } | ||
| 152 | |||
| 153 | return [ sort @exclude ]; | ||
| 154 | } | ||
| 155 | |||
| 156 | my @key_order = qw( | ||
| 157 | type | ||
| 158 | include | ||
| 159 | exclude | ||
| 160 | cmd | ||
| 161 | lint_flags | ||
| 162 | tidy_flags | ||
| 163 | ok_exit_codes | ||
| 164 | lint_failure_exit_codes | ||
| 165 | ); | ||
| 166 | |||
| 167 | my %unquoted_keys = map { $_ => 1 } qw( | ||
| 168 | chdir | ||
| 169 | lint_failure_exit_codes | ||
| 170 | ok_exit_codes | ||
| 171 | ); | ||
| 172 | |||
| 173 | sub _config_to_toml { | ||
| 174 | my $self = shift; | ||
| 175 | my $precious = shift; | ||
| 176 | |||
| 177 | my $sorter = sbe( | ||
| 178 | \@key_order, | ||
| 179 | { | ||
| 180 | fallback => sub { $_[0] cmp $_[1] }, | ||
| 181 | }, | ||
| 182 | ); | ||
| 183 | |||
| 184 | my $toml = q{}; | ||
| 185 | for my $section ( q{_}, sort grep { $_ ne q{_} } keys %{$precious} ) { | ||
| 186 | next unless keys %{ $precious->{$section} }; | ||
| 187 | |||
| 188 | if ( $section ne q{_} ) { | ||
| 189 | $toml .= "[$section]\n"; | ||
| 190 | } | ||
| 191 | |||
| 192 | for my $key ( $sorter->( keys %{ $precious->{$section} } ) ) { | ||
| 193 | my $val = $precious->{$section}{$key}; | ||
| 194 | my $quote = $unquoted_keys{$key} ? sub { $_[0] } : \&_quote; | ||
| 195 | if ( ref $val ) { | ||
| 196 | my @vals = map { $quote->($_) } @{$val}; | ||
| 197 | my $one_line = join ', ', @vals; | ||
| 198 | if ( length $one_line > 70 ) { | ||
| 199 | $toml .= "$key = [\n"; | ||
| 200 | $toml .= " $_,\n" for @vals; | ||
| 201 | $toml .= "]\n"; | ||
| 202 | } | ||
| 203 | else { | ||
| 204 | $toml .= "$key = [ $one_line ]\n"; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | else { | ||
| 208 | $val = $quote->($val); | ||
| 209 | $toml .= "$key = $val\n"; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | $toml .= "\n"; | ||
| 214 | } | ||
| 215 | |||
| 216 | chomp $toml; | ||
| 217 | |||
| 218 | return $toml; | ||
| 219 | } | ||
| 220 | |||
| 221 | sub _quote { | ||
| 222 | return qq{"$_[0]"}; | ||
| 223 | } | ||
| 224 | |||
| 225 | __PACKAGE__->meta->make_immutable; | ||
| 226 | |||
| 227 | 1; | ||
| 228 | |||
| 229 | # ABSTRACT: Creates a default precious.toml file if it doesn't yet exist | ||
| 230 | |||
| 231 | __END__ |